-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_project_test.cpp
464 lines (337 loc) · 16.3 KB
/
my_project_test.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <iostream>
#include <seqan/sequence.h>
#include <seqan/index.h>
#include <seqan/align.h>
#include <seqan/seq_io.h>
#include <vector>
#include <algorithm>
#include <string>
//#define __SEGFAULT__
#define __UNIT_LEN__ 12
#define __TOGGLE_THRESHOLD__ 3
#define __HALT_THRESHOLD__ 1
using namespace seqan;
typedef FastFMIndexConfig<void, uint64_t> TConfig;
typedef Index<StringSet<String<Dna5> >, BidirectionalIndex<FMIndex<void, TConfig> > > BWTIndex;
typedef Index<StringSet<String<Dna5> >, IndexQGram<UngappedShape<__UNIT_LEN__> > > KIndex;
struct LocStruct {
long unsigned seq_no;
long unsigned seq_offset;
unsigned support_units;
};
struct LocPosCmp {
inline bool operator() (const LocStruct & struct1, const LocStruct & struct2) {
if (struct1.seq_no < struct2.seq_no)
return true;
else if (struct1.seq_no > struct2.seq_no)
return false;
else
return (struct1.seq_offset < struct2.seq_offset);
}
};
BWTIndex fmIndex;
Finder<BWTIndex> fmFinder;
KIndex kIndex;
std::vector<long unsigned> streamlinedDir;
std::vector<long unsigned> streamlinedPosDir;
std::vector<LocStruct> streamlinedPos;
std::vector<LocStruct> unitPos;
void storeStreamline (std::string filename) {
long unsigned last_dir = streamlinedDir[length(indexDir(kIndex) ) - 1];
long unsigned last_pos = streamlinedPosDir[last_dir];
long unsigned dir_size = last_dir + 1;
long unsigned pos_size = last_pos + 1;
std::ofstream output_file;
output_file.open(filename.c_str(), std::ios::out | std::ios::binary);
output_file.write((char*) &(dir_size), sizeof(long unsigned) );
output_file.write((char*) &(pos_size), sizeof(long unsigned) );
for (long unsigned hash = 0; hash < length(indexDir(kIndex) ); ++hash)
output_file.write((char*) &(streamlinedDir[hash]), sizeof(long unsigned) );
for (long unsigned dir_idx = 0; dir_idx <= last_dir; ++dir_idx)
output_file.write((char*) &(streamlinedPosDir[dir_idx]), sizeof(long unsigned) );
for (long unsigned pos_idx = 0; pos_idx <= last_pos; ++pos_idx) {
output_file.write((char*) &(streamlinedPos[pos_idx].seq_no), sizeof(long unsigned) );
output_file.write((char*) &(streamlinedPos[pos_idx].seq_offset), sizeof(long unsigned) );
output_file.write((char*) &(streamlinedPos[pos_idx].support_units), sizeof(unsigned) );
}
output_file.close();
}
void loadStreamline (std::string filename) {
std::ifstream input_file;
input_file.open(filename.c_str(), std::ios::in | std::ios::binary);
long unsigned dir_size;
long unsigned pos_size;
input_file.read((char*) &dir_size, sizeof(unsigned long) );
input_file.read((char*) &pos_size, sizeof(unsigned long) );
streamlinedDir.resize(length(indexDir(kIndex) ) );
streamlinedPosDir.resize(dir_size);
streamlinedPos.resize(pos_size);
for (long unsigned hash = 0; hash < length(indexDir(kIndex) ); ++hash)
input_file.read((char*) &(streamlinedDir[hash]), sizeof(long unsigned) );
for (long unsigned dir_idx = 0; dir_idx < dir_size; ++dir_idx)
input_file.read((char*) &(streamlinedPosDir[dir_idx]), sizeof(long unsigned) );
for (long unsigned pos_idx = 0; pos_idx < pos_size; ++pos_idx) {
input_file.read((char*) &(streamlinedPos[pos_idx].seq_no), sizeof(long unsigned) );
input_file.read((char*) &(streamlinedPos[pos_idx].seq_offset), sizeof(long unsigned) );
input_file.read((char*) &(streamlinedPos[pos_idx].support_units), sizeof(unsigned) );
}
input_file.close();
}
unsigned streamlineUnit(Iter<BWTIndex, VSTree<TopDown<ParentLinks<> > > > bwt_iter, unsigned left_extend, unsigned right_extend, unsigned streamlined_loc_num, unsigned unit_num, unsigned ori_left_extend) {
auto locs = getOccurrences(bwt_iter, Fwd());
unsigned count_freq = length(locs);
unsigned next_loc_num;
unsigned left_offset = ori_left_extend - left_extend;
// std::cout << representative(bwt_iter) << std::endl;
if (right_extend == 0 && left_extend == 0) {
next_loc_num = streamlined_loc_num + 1;
if (unitPos.size() < next_loc_num)
unitPos.resize(1.5 * unitPos.size() );
unitPos[streamlined_loc_num].seq_no = getSeqNo(locs[0]);
unitPos[streamlined_loc_num].seq_offset = getSeqOffset(locs[0]) + left_offset;
unitPos[streamlined_loc_num].support_units = unit_num;
return next_loc_num;
}
if (count_freq <= __HALT_THRESHOLD__) {
next_loc_num = streamlined_loc_num + count_freq;
if (unitPos.size() < next_loc_num)
unitPos.resize(1.5 * unitPos.size() );
for (unsigned i = 0; i < count_freq; ++i) {
unitPos[streamlined_loc_num + i].seq_no = getSeqNo(locs[i]);
unitPos[streamlined_loc_num + i].seq_offset = getSeqOffset(locs[i]) + left_offset;
unitPos[streamlined_loc_num + i].support_units = 0;
}
return next_loc_num;
}
if (right_extend != 0) {
goDown(bwt_iter, Rev() );
streamlined_loc_num = streamlineUnit(bwt_iter, left_extend, right_extend - 1, streamlined_loc_num, unit_num, ori_left_extend);
while (goRight(bwt_iter, Rev() ) )
streamlined_loc_num = streamlineUnit(bwt_iter, left_extend, right_extend - 1, streamlined_loc_num, unit_num, ori_left_extend);
}
else {
goDown(bwt_iter, Fwd() );
streamlined_loc_num = streamlineUnit(bwt_iter, left_extend - 1, right_extend, streamlined_loc_num, unit_num, ori_left_extend);
while (goRight(bwt_iter, Fwd() ) )
streamlined_loc_num = streamlineUnit(bwt_iter, left_extend - 1, right_extend, streamlined_loc_num, unit_num, ori_left_extend);
}
return streamlined_loc_num;
}
void streamlineKIndex(StringSet<String<Dna5> > & ref, unsigned unit_num) {
streamlinedDir.resize(length(indexDir(kIndex) ) );
streamlinedPosDir.resize((long unsigned) (100 + 0.001 * length(ref) ) );
streamlinedPos.resize((long unsigned) (1000 + 0.1 * length(ref) ) );
unitPos.resize(1000);
long unsigned pos_dir_idx = 0;
long unsigned pos_idx = 0;
for (long unsigned hash = 0; hash < length(indexDir(kIndex) ) - 1; ++hash) {
streamlinedDir[hash] = pos_dir_idx;
if (indexDir(kIndex)[hash + 1] - indexDir(kIndex)[hash] >= __TOGGLE_THRESHOLD__) {
String<Dna5> unit_string;
unhash(unit_string, hash, __UNIT_LEN__);
Iter<BWTIndex, VSTree<TopDown<ParentLinks<> > > > bwt_iter(fmIndex);
goDown(bwt_iter, unit_string, Rev() );
//std::cout << unit_string << std::endl;
for (int unit_iter = 0; unit_iter < unit_num; ++unit_iter) {
streamlinedPosDir[pos_dir_idx] = pos_idx;
unsigned streamlined_loc_num = 0;
streamlined_loc_num = streamlineUnit(bwt_iter, unit_num + unit_iter * __UNIT_LEN__, unit_num + (unit_num - 1 - unit_iter) * __UNIT_LEN__, streamlined_loc_num, unit_num, unit_num + unit_iter * __UNIT_LEN__);
//std::cout << streamlined_loc_num << " ";
sort(unitPos.begin(), unitPos.begin() + streamlined_loc_num, LocPosCmp() );
unsigned long next_pos_dir_idx = pos_dir_idx + 1;
unsigned long next_pos_idx = pos_idx + streamlined_loc_num;
if (streamlinedPosDir.size() <= next_pos_idx + 1)
streamlinedPosDir.resize(1.5 * streamlinedPosDir.size() );
if (streamlinedPos.size() <= next_pos_idx + 1)
streamlinedPos.resize(1.5 * streamlinedPos.size() );
std::copy(unitPos.begin(), unitPos.begin() + streamlined_loc_num, streamlinedPos.begin() + pos_idx);
pos_dir_idx = next_pos_dir_idx;
pos_idx = next_pos_idx;
}
//std::cout << std::endl;
}
}
streamlinedDir[length(indexDir(kIndex) ) - 1] = pos_dir_idx;
streamlinedPosDir[pos_dir_idx] = pos_idx;
}
int main()
{
StringSet<String<Dna5> > ref;
appendValue (ref, "TCGTGCTCATCTCCTTGGCTGTGATACGTGGCCGGCCCTCGCTCCAGCAGCTGGACCCCTAC");
appendValue (ref, "ATGTGCTCATCTCCTTGGCTGTGATACGTGGCCGGCCCTCGCTCCAGCAGCTGGACCCCTAC");
appendValue (ref, "CAGTGCTCATCTCCTTGGCTGTGATACGTGGCCGGCCCTCGCTCCAGCAGCTGGACCCCTACAAAAAAAAAA");
appendValue (ref, "AGGTGCTCCTCTCCTTGGCTGTGATACGTGGCCGGCCCTCGCTCCAGCAGCTGGACCCCTACTTTTTTTTTT");
/* // UNCOMMENT ME!!!!
FaiIndex faiIndex;
build(faiIndex, "chr1.fasta");
int chromoNum = numSeqs(faiIndex);
for (int seqIter = 0; seqIter < chromoNum; ++seqIter) {
String<Dna5> chromosome;
readSequence(chromosome, faiIndex, seqIter);
appendValue(ref, chromosome);
}
*/ // UNCOMMENT ME!!!!
Infix<Dna5String>::Type test = infix(ref[0], 3, 19);
if (!open(fmIndex, "test.bi_dir") )
std::cout << "can't load" << std::endl;
#ifndef __SEGFAULT__
BWTIndex temp(ref);
fmIndex = temp;
#else
//fmIndex = BWTIndex(ref);
#endif
/*
setHaystack(fmFinder, fmIndex);
std::cout << test << std::endl;
goBegin(fmFinder);
clear(fmFinder);
while(find(fmFinder, test)) {
//std::cout << beginPosition(fmFinder) << " " << endPosition(fmFinder) << std::endl;
std::cout << position(fmFinder) << std::endl;
long unsigned seq_no = getSeqNo(position(fmFinder));
long unsigned seq_offset = getSeqOffset(position(fmFinder));
std::cout << infix(ref[seq_no], seq_offset - 2, 23) << std::endl;
}
goBegin(fmFinder);
clear(fmFinder);
*/
String<Dna5> testRev = test;
reverse(testRev);
std::cout << "rev: " << testRev << std::endl;
Iter<BWTIndex, VSTree<TopDown<ParentLinks<> > > > bwt_iter(fmIndex);
goDown(bwt_iter, test, Rev() );
Iter<BWTIndex, VSTree<TopDown<ParentLinks<> > > > bwt_iter_reserve(bwt_iter);
std::cout << representative(bwt_iter, Fwd()) << std::endl;
goDown(bwt_iter, Rev() );
goDown(bwt_iter, Rev() );
//goDown(bwt_iter, Dna5String("TG"), Rev() );
//goDown(bwt_iter, Dna5String("GA"), Fwd() );
std::cout << representative(bwt_iter, Fwd()) << std::endl;
while(goRight(bwt_iter, Rev() ) ) {
std::cout << "going right" << std::endl;
}
std::cout << "!!stop going right" << std::endl;
std::cout << representative(bwt_iter, Fwd()) << std::endl;
for (unsigned i = 0; i < length(getOccurrences(bwt_iter, Rev())); ++i)
std::cout << getOccurrences(bwt_iter, Fwd())[i] << std::endl;
//goRight(bwt_iter);
//goUp(bwt_iter, Rev() );
//representative(bwt_iter, Rev());
for (unsigned i = 0; i < length(getOccurrences(bwt_iter, Rev())); ++i)
std::cout << getOccurrences(bwt_iter, Fwd())[i] << std::endl;
std::cout << "reserve" << std::endl;
for (unsigned i = 0; i < length(getOccurrences(bwt_iter_reserve, Rev())); ++i)
std::cout << getOccurrences(bwt_iter_reserve, Rev())[i] << std::endl;
std::cout << representative(bwt_iter, Fwd()) << std::endl;
std::cout << representative(bwt_iter, Rev()) << std::endl;
std::cout << "count bidirectional iterator: " << countOccurrences(bwt_iter) << std::endl;
/*
do {
std::cout << representative(bwt_iter, Rev()) << std::endl;
if (!goDown(bwt_iter, Rev()) && !goRight(bwt_iter, Rev()))
while (goUp(bwt_iter, Rev()) && !goRight(bwt_iter, Rev()))
;
}
while (true);
//if (!save(fmIndex, "test.bi_dir") )
//std::cout << "can't save" << std::endl;
std::cout << length(getOccurrences(bwt_iter)) << std::endl;
for (int i = 0; i < length(getOccurrences(bwt_iter)); ++i) {
long unsigned seq_no = getSeqNo(getOccurrences(bwt_iter)[i]);
long unsigned seq_off = getSeqOffset(getOccurrences(bwt_iter)[i]);
std::cout << getOccurrences(bwt_iter)[i] << std::endl;
std::cout << infix(ref[seq_no], seq_off - 2, seq_off + length(test) + 4 ) << std::endl;
}
*/
kIndex = KIndex(ref);
Infix<String<Dna5> >::Type seq = infix(ref[0], __UNIT_LEN__, 2 * __UNIT_LEN__);
std::cout << length(ref) << std::endl;
hash(indexShape(kIndex), begin(seq) );
//hash(indexShape(kIndex), seq);
Infix<const String<Pair<long unsigned, long unsigned, Pack> > >::Type locs = getOccurrences(kIndex, indexShape(kIndex) );
//std::cout << locs << std::endl;
for (unsigned i = 0; i < length(locs); ++i)
std::cout << getSeqNo(locs[i]) << " " << getSeqOffset(locs[i]) << std::endl;
std::cout << "counts" << std::endl;
std::cout << indexCounts(kIndex) << std::endl;
std::cout << "shape" << std::endl;
auto hash_val = value(indexShape(kIndex));
std::cout << hash_val << std::endl;
std::cout << getBucket(indexBucketMap(kIndex), hash_val) << std::endl;
std::cout << end(indexDir(kIndex), Standard() ) - begin(indexDir(kIndex), Standard() ) << std::endl;
std::cout << length(indexDir(kIndex) ) << std::endl;
String<Dna5> temp_str;
unhash(temp_str, value(indexShape(kIndex) ), __UNIT_LEN__);
std::cout << temp_str << std::endl;
unhash(temp_str, 0, __UNIT_LEN__);
std::cout << temp_str << std::endl;
unhash(temp_str, 15, __UNIT_LEN__);
std::cout << temp_str << std::endl;
unhash(temp_str, length(indexDir(kIndex) ) - 1, __UNIT_LEN__);
std::cout << temp_str << std::endl;
unsigned unit_num = 3;
// streamlineKIndex(ref, unit_num);
// storeStreamline("wtf");
loadStreamline("wtf");
for (long unsigned hash = 0; hash < length(indexDir(kIndex) ) - 1; ++hash) {
if (indexDir(kIndex)[hash + 1] - indexDir(kIndex)[hash] >= __TOGGLE_THRESHOLD__) {
String<Dna5> unit_string;
unhash(unit_string, hash, __UNIT_LEN__);
std::cout << unit_string << std::endl;
std::cout << indexDir(kIndex)[hash + 1] - indexDir(kIndex)[hash] << std::endl;
long unsigned dir_idx = streamlinedDir[hash];
for (int i = 0; i < unit_num; ++i)
std::cout << streamlinedPosDir[dir_idx + i + 1] - streamlinedPosDir[dir_idx + i] << " ";
std::cout << std::endl;
long unsigned pos_idx = streamlinedPosDir[dir_idx];
for (int i = 0; i < streamlinedPosDir[dir_idx + 1] - streamlinedPosDir[dir_idx]; i++)
std::cout << "<" << streamlinedPos[pos_idx + i].seq_no << ", " << streamlinedPos[pos_idx + i].seq_offset << ">:" << streamlinedPos[pos_idx + i].support_units << " ";
std::cout << std::endl;
}
}
/*
goBegin(fmFinder);
clear(fmFinder);
find(fmFinder, DnaString('A'));
TIterator initial_fm_pos = fmFinder.range.i1;
std::cout << fmFinder.range.i2 - fmFinder.range.i1 << std::endl;
std::cout << fmFinder.range.i1 - initial_fm_pos << std::endl;
std::cout << fmFinder.range.i2 - initial_fm_pos << std::endl;
goBegin(fmFinder);
clear(fmFinder);
find(fmFinder, DnaString('C'));
std::cout << fmFinder.range.i2 - fmFinder.range.i1 << std::endl;
std::cout << fmFinder.range.i1 - initial_fm_pos << std::endl;
std::cout << fmFinder.range.i2 - initial_fm_pos << std::endl;
goBegin(fmFinder);
clear(fmFinder);
find(fmFinder, DnaString('G'));
std::cout << fmFinder.range.i2 - fmFinder.range.i1 << std::endl;
std::cout << fmFinder.range.i1 - initial_fm_pos << std::endl;
std::cout << fmFinder.range.i2 - initial_fm_pos << std::endl;
goBegin(fmFinder);
clear(fmFinder);
find(fmFinder, DnaString('T'));
std::cout << fmFinder.range.i2 - fmFinder.range.i1 << std::endl;
std::cout << fmFinder.range.i1 - initial_fm_pos << std::endl;
std::cout << fmFinder.range.i2 - initial_fm_pos << std::endl;
goBegin(fmFinder);
clear(fmFinder);
find(fmFinder, DnaString('N'));
std::cout << fmFinder.range.i2 - fmFinder.range.i1 << std::endl;
std::cout << fmFinder.range.i1 - initial_fm_pos << std::endl;
std::cout << fmFinder.range.i2 - initial_fm_pos << std::endl;
typedef String<Dna5> TSequence;
typedef Align<TSequence, ArrayGaps> TAlign;
TAlign align;
TSequence str1("GTGGCCGGCCCTCGCTCCAGCAGCTGGACC");
TSequence str2("ACGTGGCCGGCCCTCGCTCCAAGCAGCTGGACCCC");
resize(rows(align), 2);
assignSource(row(align, 0), str1);
assignSource(row(align, 1), str2);
int score = globalAlignmentScore(str1, str2, Score<int, Simple>(0, -1, -1), AlignConfig<false, true, true, false>(), -3, 2);//, MyersBitVector() );// Hirschberg());
std::cout << "Score: " << score << std::endl;
//std::cout << align << std::endl;
*/
return 0;
}