-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFilterLowComplexity.cpp
310 lines (281 loc) · 7.25 KB
/
FilterLowComplexity.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iomanip> // std::setw
#include "BitNucVector.h"
#include "FASTAReader.h"
#include "FASTASequence.h"
#include <math.h>
using namespace std;
class WordCount {
public:
int count;
Word w;
int operator<(const WordCount &rhs) const {
return count > rhs.count;
}
};
void ResetWordQueries(vector<map<Word, int> > &queries) {
int i;
for (i = 0; i < queries.size(); i++) {
map<Word, int>::iterator mapIt, mapEnd;
mapEnd = queries[i].end();
mapIt = queries[i].begin();
while (mapIt != mapEnd) {
(*mapIt).second = 0;
++mapIt;
}
}
}
int main(int argc, char* argv[]) {
if (argc < 4) {
cout << "Usage: filterlc fasta k tableOut [options]" << endl;
cout << "Options:" << endl;
cout << " -c \tPrint word counts" << endl;
cout << " -f FLOAT\tminimal enrichment [0,1]" << endl;
cout << " -n INT\tminimum number of enriched [5]" << endl;
cout << " -w fasta \tPrint sequences enriched for words in this fasta file." << endl;
cout << " -o fasta \tPrint matching sequences to this fasta file." << endl;
exit(1);
}
string fastaFileName = argv[1];
int k = atoi(argv[2]);
string outTableFileName = argv[3];
int argi = 4;
float fraction = 0.05;
int minEnriched = 5;
bool printWordCounts = false;
bool printWordRates = false;
bool printMeanRate = false;
string wordQueryFile = "";
string outFastaFileName = "";
while (argi < argc) {
if (strcmp(argv[argi], "-f") == 0) {
++argi;
fraction = atof(argv[argi]);
}
if (strcmp(argv[argi], "-n") == 0) {
++argi;
minEnriched = atoi(argv[argi]);
}
if (strcmp(argv[argi], "-c") == 0) {
printWordCounts = true;
}
if (strcmp(argv[argi], "-r") == 0) {
printWordRates = true;
}
if (strcmp(argv[argi], "-m") == 0) {
printMeanRate = true;
}
if (strcmp(argv[argi], "-w") == 0) {
++argi;
wordQueryFile = argv[argi];
}
if (strcmp(argv[argi], "-o") == 0) {
++argi;
outFastaFileName = argv[argi];
}
++argi;
}
ifstream in;
ofstream out, outFasta;
in.open(fastaFileName.c_str());
out.open(outTableFileName.c_str());
BitNucVector::k = k;
int numWords = 1 << (2*k);
if (outFastaFileName != "") {
outFasta.open(outFastaFileName.c_str());
}
vector <map<Word, int > > wordQueries;
vector <string> wordQueriesTitles;
if (wordQueryFile != "") {
FASTAReader reader;
reader.Initialize(wordQueryFile);
FASTASequence seq;
int seqIndex = 0;
while (reader.GetNext(seq)) {
int i;
wordQueries.push_back(map<Word, int>());
for (i = 0; i < seq.length - k + 1; i++) {
BitNucVector v, key;
if (v.InitializeFromString((unsigned char*) &seq.seq[i], k)) {
v.SetKey(key);
wordQueries[seqIndex][key.data] = 0;
}
}
wordQueriesTitles.push_back(seq.GetName());
++seqIndex;
}
seq.Free();
}
FASTAReader seqReader;
int readIndex = 0;
while (in) {
++readIndex;
if (readIndex % 10000 == 0) {
cerr << "filerlc " << readIndex << endl;
}
string title;
getline(in, title);
string seq;
while (in.peek() != '>' and in) {
char p = in.peek();
string line;
(in) >> line;
if (line == "") {
break;
}
assert(line[0] != '>');
seq += line;
if (in.peek() == '\n') {
in.get();
}
}
BitNucVector tuple;
tuple.k = k;
int i;
map<Word, int> wordCount;
//
// Count all words in this file.
//
if (seq.size() < k) {
continue;
}
for (i = 0; i < seq.size() - k + 1; i++) {
if (tuple.InitializeFromString((unsigned char*) &seq.c_str()[i], tuple.k)) {
BitNucVector key;
tuple.SetKey(key);
if (wordCount.find(key.data) == wordCount.end()) {
wordCount[key.data] = 1;
}
else {
wordCount[key.data]++;
}
}
else {
i++;
}
}
float pWord = 1.0/numWords;
float logPWord = -log(pWord);
int maxNumEnriched = 0;
map<Word, int>::iterator mapIt, mapEnd;
if (wordQueryFile != "") {
//
// First compute the expected number of matches.
//
int s;
float maxEnrichment = 0;
int maxEnrichmentI = 0;
int maxNQueryWords = 0;
int maxCountQueryWords = 0;
int numEnriched = 0;
for (s = 0; s < wordQueries.size(); s++ ) {
int nQueryWords = wordQueries[s].size();
int expNQueryWords = nQueryWords * pWord * seq.size();
int countQueryWords = 0;
float totalEnrichment = 0;
mapIt = wordQueries[s].begin();
mapEnd = wordQueries[s].end();
while (mapIt != mapEnd) {
if ( wordCount.find((*mapIt).first) != wordCount.end()) {
countQueryWords += wordCount[mapIt->first];
totalEnrichment += (wordCount[mapIt->first]) / (pWord * seq.size());
}
++mapIt;
}
// totalEnrichment = countQueryWords / float(wordQueries[s].size());
// totalEnrichment /= seq.size();
totalEnrichment = countQueryWords / float(seq.size());
if (totalEnrichment > maxEnrichment) {
maxEnrichmentI = s;
maxEnrichment = totalEnrichment;
maxNQueryWords = expNQueryWords;
maxCountQueryWords = countQueryWords;
}
}
if (maxEnrichment >= fraction) {
out << title
<< "\t"
<< wordQueriesTitles[maxEnrichmentI]
<< "\t" << seq.size()
<< "\t" << maxNQueryWords
<< "\t" << maxCountQueryWords
<< "\t" << maxEnrichment
<< "\t" << numEnriched << endl;
if (outFastaFileName != "") {
outFasta << title << endl;
outFasta << seq << endl;
}
}
continue;
}
vector<WordCount> counts;
mapEnd = wordCount.end();
counts.resize(wordCount.size());
i = 0;
for (mapIt = wordCount.begin(); mapIt != wordCount.end(); mapIt++) {
counts[i].w = (*mapIt).first;
counts[i].count = (*mapIt).second;
i++;
}
sort(counts.begin(), counts.end());
int nEnriched = 0;
for (i = 0; i < counts.size(); i++) {
if (counts[i].count > fraction * seq.size()) {
nEnriched ++;
}
}
if (printWordCounts) {
int i;
out << title << " ";
vector<float> freqs(counts.size());
for (i = 0; i < counts.size(); i++) {
freqs[i] = counts[i].count / ((float)seq.size());
}
std::sort(freqs.begin(), freqs.end());
std::reverse(freqs.begin(), freqs.end());
float total = 0;
for (i = 0; i < counts.size() and i < 10; i++) {
//out << std::setprecision(2) << freqs[i] << " " ;
total += freqs[i];
}
out << std::setprecision(2) << total << endl;
// out << endl;
}
else if (printWordRates) {
int i;
out << title << " ";
for (i = 0; i < counts.size(); i++) {
out << counts[i].count / float(counts.size()) << " ";
}
out << endl;
}
else if (printMeanRate){
float sum = 0;
for (i = 0; i < minEnriched; i++) {
sum += counts[i].count;
}
out << title << "\t" << sum / (counts.size()*minEnriched) << endl;
}
else {
out << nEnriched << "\t" << counts.size() << "\t" << float(nEnriched)/counts.size() << endl;
}
/* std::setw(4);
out << title << "\t" << nEnriched << "\t" << counts.size() << "\t" << float(nEnriched)/counts.size() << "\t";
for (i = 0; i < 5 and i < counts.size(); i++) {
tuple.data = counts[i].w;
// std::setwidth(4);
out << "\t" << tuple.ToString() << "\t" << counts[i].count / float(seq.size());
}
out << "\t" << seq.size() << "\t" << seq;
out << endl;
// }
*/
}
if (outFastaFileName != "") {
outFasta.close();
}
}