-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenomeMatcher.cpp
163 lines (147 loc) · 5.91 KB
/
GenomeMatcher.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "provided.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include "Trie.h"
#include <utility>
#include <algorithm>
using namespace std;
class GenomeMatcherImpl
{
public:
GenomeMatcherImpl(int minSearchLength);
~GenomeMatcherImpl();
void addGenome(const Genome& genome);
int minimumSearchLength() const;
bool findGenomesWithThisDNA(const string& fragment, int minimumLength, bool exactMatchOnly, vector<DNAMatch>& matches) const;
bool findRelatedGenomes(const Genome& query, int fragmentMatchLength, bool exactMatchOnly, double matchPercentThreshold, vector<GenomeMatch>& results) const;
private:
int m_minSearchLength;
Trie<DNAMatch> lib;
unordered_map<string, Genome*> genomes;
};
GenomeMatcherImpl::GenomeMatcherImpl(int minSearchLength)
{
m_minSearchLength = minSearchLength;
}
GenomeMatcherImpl::~GenomeMatcherImpl()
{
for (unordered_map<string, Genome*>::iterator it = genomes.begin(); it != genomes.end(); it++) {
delete it->second;
}
}
void GenomeMatcherImpl::addGenome(const Genome& genome)
{
string seq;
genome.extract(0, genome.length(), seq);
genomes.insert(make_pair(genome.name(), new Genome(genome.name(), seq)));
for (int i = 0; i + m_minSearchLength <= genome.length(); i++) {
genome.extract(i, m_minSearchLength, seq);
DNAMatch dm;
dm.genomeName = genome.name();
dm.length = m_minSearchLength;
dm.position = i;
lib.insert(seq, dm);
}
}
int GenomeMatcherImpl::minimumSearchLength() const
{
return m_minSearchLength; // This compiles, but may not be correct
}
bool GenomeMatcherImpl::findGenomesWithThisDNA(const string& fragment, int minimumLength, bool exactMatchOnly, vector<DNAMatch>& matches) const
{
matches.clear();
if (fragment.length() < minimumLength || minimumLength < m_minSearchLength) return false;
// find the matches
vector<DNAMatch> allMatches;
allMatches = lib.find(fragment.substr(0, m_minSearchLength), exactMatchOnly); // O(F^2)
int diff;
DNAMatch temp;
unordered_map<string, DNAMatch> bestMatches;
for (int i = 0; i < allMatches.size(); i++) { // O(H)
diff = 0;
std::string str;
genomes.at(allMatches[i].genomeName)->extract(allMatches[i].position, m_minSearchLength, str); // O(F)
if (str == fragment.substr(0, m_minSearchLength) && !exactMatchOnly) diff = 1; // O(F)
temp = allMatches[i];
int j = temp.position + m_minSearchLength;
genomes.at(temp.genomeName)->extract(0, genomes.at(temp.genomeName)->length(), str);
while (j < temp.position + fragment.length() && j < genomes.at(temp.genomeName)->length()) {
if (str[j] != fragment[j - temp.position]) { // O(F)
if (diff) diff--;
else break;
}
j++;
}
if (j - temp.position >= minimumLength) {
temp.length = j - temp.position;
if (!bestMatches.count(temp.genomeName) || bestMatches[temp.genomeName].length < temp.length) {
bestMatches[temp.genomeName] = temp;
}
}
}
for (unordered_map<string, DNAMatch>::iterator it = bestMatches.begin(); it != bestMatches.end(); it++) {
matches.push_back((*it).second);
}
if (matches.size() == 0) return false; // This compiles, but may not be correct
return true;
}
bool GenomeMatcherImpl::findRelatedGenomes(const Genome& query, int fragmentMatchLength, bool exactMatchOnly, double matchPercentThreshold, vector<GenomeMatch>& results) const
{
if (fragmentMatchLength < m_minSearchLength) return false;
string seq;
results.clear();
vector<DNAMatch> matches;
unordered_map<string, int> counts;
for (int i = fragmentMatchLength; i <= query.length(); i+= fragmentMatchLength) { // O(Q)
query.extract(i - fragmentMatchLength, fragmentMatchLength, seq); // O(F)
findGenomesWithThisDNA(seq, fragmentMatchLength, exactMatchOnly, matches); // O(HF)
for (int j = 0; j < matches.size(); j++) { // O(F) worst case
if (!counts.count(matches[j].genomeName)) counts.insert(make_pair(matches[j].genomeName, 1)); // O(1)
else counts[matches[j].genomeName]++;
}
}
GenomeMatch temp;
vector<pair<double, string>> temp2;
for (unordered_map<string, int>::iterator it = counts.begin(); it != counts.end(); it++) {
temp2.push_back(make_pair((double) it->second / (query.length() / fragmentMatchLength), it->first));
}
sort(temp2.rbegin(), temp2.rend()); // O(HlogH) , H ~= F
for (int i = 0; i < temp2.size(); i++) {
temp.genomeName = temp2[i].second;
temp.percentMatch = 100 * temp2[i].first;
if (temp.percentMatch < matchPercentThreshold) continue;
results.push_back(temp);
}
if (results.size() == 0) return false;
return true;
}
//******************** GenomeMatcher functions ********************************
// These functions simply delegate to GenomeMatcherImpl's functions.
// You probably don't want to change any of this code.
GenomeMatcher::GenomeMatcher(int minSearchLength)
{
m_impl = new GenomeMatcherImpl(minSearchLength);
}
GenomeMatcher::~GenomeMatcher()
{
delete m_impl;
}
void GenomeMatcher::addGenome(const Genome& genome)
{
m_impl->addGenome(genome);
}
int GenomeMatcher::minimumSearchLength() const
{
return m_impl->minimumSearchLength();
}
bool GenomeMatcher::findGenomesWithThisDNA(const string& fragment, int minimumLength, bool exactMatchOnly, vector<DNAMatch>& matches) const
{
return m_impl->findGenomesWithThisDNA(fragment, minimumLength, exactMatchOnly, matches);
}
bool GenomeMatcher::findRelatedGenomes(const Genome& query, int fragmentMatchLength, bool exactMatchOnly, double matchPercentThreshold, vector<GenomeMatch>& results) const
{
return m_impl->findRelatedGenomes(query, fragmentMatchLength, exactMatchOnly, matchPercentThreshold, results);
}