-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMinCount.h
More file actions
339 lines (319 loc) · 9.15 KB
/
MinCount.h
File metadata and controls
339 lines (319 loc) · 9.15 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef MIN_COUNT_H_
#define MIN_COUNT_H_
#include "TupleOps.h"
#include "SeqUtils.h"
#include "htslib/kseq.h"
template <typename TupPos, typename Tup>
void StoreMinimizers(char *seq, GenomePos seqLen, int k, int w, vector<TupPos> &minimizers, bool Global, bool canonical = true) {
//
// Initialize first.
//
if (seqLen < k) {
return;
}
TupPos cur, curRC, minTuple, can;
GenomePos minPos;
int windowSpan=w+k-1;
GenomePos p = 0;
TupPos m;
//
// Skip N's as a start
InitMask(m, k);
int nextValidWindowEnd=0;
int nextValidWindowStart=0;
bool valid=false;
if (seqLen < windowSpan) return;
while (nextValidWindowStart < seqLen - windowSpan and !valid) {
valid=true;
for (int n=nextValidWindowStart; valid and n < nextValidWindowStart+windowSpan; n++ ) {
if (seqLen < n) return;
if (seqMapN[seq[n]] > 3) {
nextValidWindowStart = n+1;
valid=false;
}
}
}
// all n
if (valid == false) {
return;
}
nextValidWindowEnd = nextValidWindowStart + windowSpan;
StoreTuple(seq,p,k,cur);
TupleRC(cur, curRC, k);
//
// Initialize the first minimzer.
// Store canonical information in the rightest bit of can.t; The last bit = 1 ==> reverse strand
// If canonical == True, for_mask = 0111...11 --> minimizer & for_mask = 0minimizer; rev_mask = 1000...00 --> minimizer | rev_mask = 1minimizer
// Else for_mask = 111...11 --> minimizer & for_mask = minimizer, rev_mask = 000...00 --> minimizer | rev_mask = minimizer
//
Tup for_mask = TupPos::for_mask_s;
Tup rev_mask = TupPos::rev_mask_s;
Tup mask = 0;
if (!canonical and Global) {
rev_mask = (rev_mask & mask); // 0000...00 64 bits
for_mask = ~rev_mask; // 111...11 64 bits
}
if (canonical) {
if ((cur.t & for_mask) < (curRC.t & for_mask)) can.t = (cur.t & for_mask); //can.t = min(cur.t, curRC.t);
else can.t = (curRC.t | rev_mask);
}
else { can.t = cur.t; }
minPos = 0;
TupPos activeMinimizer, curMinimizer;
activeMinimizer.t = can.t;
activeMinimizer.pos = 0;
vector<TupPos> curTuples(w);
curTuples[0] = activeMinimizer;
//
// Find the active minimizer in this window
//
int nMinimizers=1;
int nSkipped=0;
for (p = 1; p < w && p < seqLen-k+1 ; p++) {
ShiftOne(seq, p+k-1, m, cur);
ShiftOneRC(seq, p+k-1, k, curRC);
/*
Tuple test, testrc;
StoreTuple(seq->seq.s, p, k, test);
TupleRC(test, testrc, k);
assert(test == cur);
assert(testrc == curRC);
*/
curMinimizer.pos = p;
if ((cur.t & for_mask) < (curRC.t & for_mask)) curMinimizer.t = (cur.t & for_mask);
else curMinimizer.t = (curRC.t | rev_mask);
if (curMinimizer.t < activeMinimizer.t) {
activeMinimizer.t = curMinimizer.t;
activeMinimizer.pos = p;
}
curTuples[p%w] = curMinimizer;
}
//
// Only store the first minimizer if the first window starts at the beginning of the sequence.
//
if (nextValidWindowEnd == windowSpan ) {
minimizers.push_back(activeMinimizer);
}
// Now scan the chromosome
minTuple.t=m.t;
for (p = w; p < seqLen-k+1; p++) {
// If the next valid window ends at the next nucleotide, check to see if
// it is a valid window (no N's). If so, bump by one.
// Otherwise, search for the next valid window end.
if ( nextValidWindowEnd == p+k-1) {
if ( seqMapN[seq[p+k-1]] <= 3 ) {
nextValidWindowEnd++;
}
else {
nextValidWindowStart = p+k;
valid=false;
if (seqLen < windowSpan) return;
while (nextValidWindowStart < seqLen - windowSpan and not valid) {
valid=true;
for (int n=nextValidWindowStart; valid and n < nextValidWindowStart+windowSpan; n++ ) {
if (seqMapN[seq[n]] > 3) {
nextValidWindowStart = n+1;
valid=false;
}
}
}
// all n
if (valid == false) {
return;
}
nextValidWindowEnd = nextValidWindowStart + windowSpan;
}
}
ShiftOne(seq, p+k-1, m, cur);
ShiftOneRC(seq, p+k-1, k, curRC);
#ifdef _TESTING_
TupPos test, testrc;
StoreTuple(seq, p, k, test);
TupleRC(test, testrc, k);
assert(test.t == cur.t);
assert(testrc.t == curRC.t);
#endif
if ((cur.t & for_mask) < (curRC.t & for_mask)) curMinimizer.t = (cur.t & for_mask);
else curMinimizer.t = (curRC.t | rev_mask);
curMinimizer.pos = p;
curTuples[p%w] = curMinimizer;
if (p - w >= activeMinimizer.pos) {
activeMinimizer = curTuples[0];
for (int j =1; j < w; j++) {
if ((curTuples[j].t & for_mask) < (activeMinimizer.t & for_mask)) {
activeMinimizer = curTuples[j];
}
}
if (nextValidWindowEnd == p+k) {
minimizers.push_back(activeMinimizer);
nMinimizers+=1;
}
else {
nSkipped++;
}
}
else {
if ((curMinimizer.t & for_mask) < (activeMinimizer.t & for_mask)) { //TODO(Jingwen)
activeMinimizer = curMinimizer;
if (nextValidWindowEnd == p+k) {
minimizers.push_back(activeMinimizer);
nMinimizers++;
}
else {
nSkipped++;
}
}
}
if (p + 1 % 10000 == 0) {
cerr << p +1 << "\t" << nMinimizers << "\t" << nSkipped << endl;
}
}
}
template <typename TupPos, typename Tup>
void StoreMinimizers_noncanonical(char *seq, GenomePos seqLen, int k, int w, vector<TupPos> &minimizers, bool Global) {
//
// Initialize first.
//
if (seqLen < k) {
return;
}
TupPos cur, curRC, minTuple, can;
GenomePos minPos;
GenomePos p = 0;
TupPos m;
InitMask(m, k);
int nextValidWindowEnd=0;
int nextValidWindowStart=0;
int windowSpan=w+k-1;
bool valid=false;
if (seqLen < windowSpan) return;
while (nextValidWindowStart < seqLen - windowSpan and !valid) {
valid=true;
for (int n=nextValidWindowStart; valid and n < nextValidWindowStart+windowSpan; n++ ) {
if (seqLen < n) return;
if (seqMapN[seq[n]] > 3) {
nextValidWindowStart = n+1;
valid=false;
}
}
}
// all n
if (valid == false) {
return;
}
nextValidWindowEnd = nextValidWindowStart + windowSpan;
StoreTuple(seq, p, k, cur);
// TupleRC(cur, curRC, k);
//
// Initialize the first minimzer.
// Store canonical information in the rightest bit of can.t; The last bit = 1 ==> reverse strand
// If canonical == True, for_mask = 0111...11 --> minimizer & for_mask = 0minimizer; rev_mask = 1000...00 --> minimizer | rev_mask = 1minimizer
// Else for_mask = 111...11 --> minimizer & for_mask = minimizer, rev_mask = 000...00 --> minimizer | rev_mask = minimizer
//
Tup for_mask = TupPos::for_mask_s;
Tup rev_mask = TupPos::rev_mask_s;
Tup mask = 0;
if (Global) {
rev_mask = (rev_mask & mask); // 0000...00 64 bits
for_mask = ~rev_mask; // 111...11 64 bits
}
// cerr << "Global: " << Global << endl;
// cerr << "for_mask: " << for_mask << endl;
// cerr << "rev_mask: " << rev_mask << endl;
can.t = cur.t;
minPos = 0;
TupPos activeMinimizer, curMinimizer;
activeMinimizer.t = can.t;
activeMinimizer.pos = 0;
vector<TupPos> curTuples(w);
curTuples[0] = activeMinimizer;
//
// Find the active minimizer in this window
//
int nMinimizers=1;
for (p = 1; p < w && p < seqLen-k+1 ; p++) {
ShiftOne(seq, p+k-1, m, cur);
// ShiftOneRC(seq, p+k-1, k, curRC);
/*
Tuple test, testrc;
StoreTuple(seq->seq.s, p, k, test);
TupleRC(test, testrc, k);
assert(test == cur);
assert(testrc == curRC);
*/
curMinimizer.pos = p;
curMinimizer.t = (cur.t & for_mask);
// if ((cur.t & for_mask) < (curRC.t & for_mask)) curMinimizer.t = (cur.t & for_mask);
// else curMinimizer.t = (curRC.t | rev_mask);
if (curMinimizer.t < activeMinimizer.t) {
activeMinimizer.t = curMinimizer.t;
activeMinimizer.pos = p;
}
curTuples[p%w] = curMinimizer;
}
if (nextValidWindowEnd == windowSpan ) {
minimizers.push_back(activeMinimizer);
}
// Now scan the chromosome
minTuple.t=m.t;
for (p = w; p < seqLen-k+1; p++) {
// Check if past current active minimzier
ShiftOne(seq, p+k-1, m, cur);
// ShiftOneRC(seq, p+k-1, k, curRC);
curMinimizer.t = (cur.t & for_mask);
// if ((cur.t & for_mask) < (curRC.t & for_mask)) curMinimizer.t = (cur.t & for_mask);
// else curMinimizer.t = (curRC.t | rev_mask);
if ( nextValidWindowEnd == p+k-1) {
if ( seqMapN[seq[p+k-1]] <= 3 ) {
nextValidWindowEnd++;
}
else {
nextValidWindowStart = p+k;
valid=false;
if (seqLen < windowSpan) return;
while (nextValidWindowStart < seqLen - windowSpan and not valid) {
valid=true;
for (int n=nextValidWindowStart; valid and n < nextValidWindowStart+windowSpan; n++ ) {
if (seqLen < n) return;
if (seqMapN[seq[n]] > 3) {
nextValidWindowStart = n+1;
valid=false;
}
}
}
// all n
if (valid == false) {
return;
}
nextValidWindowEnd = nextValidWindowStart + windowSpan;
}
}
curMinimizer.pos = p;
curTuples[p%w] = curMinimizer;
if (p - w >= activeMinimizer.pos) {
activeMinimizer = curTuples[0];
for (int j =1; j < w; j++) {
if ((curTuples[j].t & for_mask) < (activeMinimizer.t & for_mask)) {
activeMinimizer = curTuples[j];
}
}
if (nextValidWindowEnd == p+k) {
minimizers.push_back(activeMinimizer);
nMinimizers+=1;
}
}
else {
if ((curMinimizer.t & for_mask) < (activeMinimizer.t & for_mask)) { //TODO(Jingwen)
activeMinimizer = curMinimizer;
if (nextValidWindowEnd == p+k) {
minimizers.push_back(activeMinimizer);
nMinimizers++;
}
}
}
if (p + 1 % 10000 == 0) {
cerr << p +1 << endl;
}
}
}
#endif