-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApproxPattern.cpp
More file actions
84 lines (61 loc) · 2 KB
/
ApproxPattern.cpp
File metadata and controls
84 lines (61 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
int hammingDist(const string &DnaPattern, const string &pattern){
int count = 0;
for(size_t j = 0; j < pattern.length(); j++){
if(DnaPattern[j] != pattern[j]){
count++;
}
}
return count;
}
vector<int> findStartingPositions(const string& pattern, const string& text, const int d) {
size_t textLen = text.length();
size_t patternLen = pattern.length();
vector<int> positions;
for (size_t i = 0; i <= textLen - patternLen; i++){
string DnaPattern = text.substr(i, patternLen);
int count = hammingDist(DnaPattern, pattern);
if (count <= d){
positions.push_back(i);
}
}
return positions;
}
int main(){
string pattern;
std::cout << "Enter the pattern: ";
getline(cin, pattern);
int d;
std::cout << "Enter the number of mismatches allowed : ";
std::cin >> d;
std::string text;
std::string fileName;
std::cout << "Enter the filename containing the DNA sequence: ";
std::cin >> fileName;
// Open the file
std::ifstream inputFile(fileName);
if (!inputFile) {
std::cerr << "Failed to open the file: " << fileName << std::endl;
return 1;
}
// Read the DNA sequence from the file
text.assign((std::istreambuf_iterator<char>(inputFile)),
std::istreambuf_iterator<char>());
inputFile.close();
vector<int> positions = findStartingPositions(pattern, text, d);
cout << "Starting positions of the pattern with at most " << d << " mismatches: ";
for (int pos : positions) {
cout << pos << " ";
}
cout << endl;
// Output the number of positions
cout << "Number of positions: " << positions.size() << endl;
return 0;
}