-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReadQueue.cpp
4776 lines (4243 loc) · 147 KB
/
ReadQueue.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Metal - A fast methylation alignment and calling tool for WGBS data.
// Copyright (C) 2017 Jonas Fischer
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Jonas Fischer [email protected]
#include <iostream>
#include <chrono>
#include <algorithm>
#include "ReadQueue.h"
ReadQueue::ReadQueue(const char* filePath, RefGenome& reference, const bool isGZ, const bool bsFlag) :
ref(reference)
, readBuffer(MyConst::CHUNKSIZE)
, isPaired(false)
, isSC(false)
, methLevels(ref.cpgTable.size())
, methLevelsStart(ref.cpgStartTable.size())
, bothStrandsFlag(bsFlag)
, r1FwdMatches(0)
, r1RevMatches(0)
, matchR1Fwd(true)
//TODO
, of("errOut.txt")
{
if (isGZ)
{
igz.open(filePath);
if(!igz)
{
std::cerr << "Opening read file " << std::string(filePath) << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
} else {
file.open(filePath);
if(!file)
{
std::cerr << "Opening read file " << std::string(filePath) << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
}
// fill counting structure for parallelization
for (unsigned int i = 0; i < CORENUM; ++i)
{
// fwdMetaIDs[i] = google::dense_hash_map<uint32_t, uint16_t, MetaHash>();
// revMetaIDs[i] = google::dense_hash_map<uint32_t, uint16_t, MetaHash>();
// fwdMetaIDs[i].set_deleted_key(ref.metaCpGs.size() + 10);
// revMetaIDs[i].set_deleted_key(ref.metaCpGs.size() + 10);
// fwdMetaIDs[i].set_empty_key(ref.metaWindows.size() + 11);
// revMetaIDs[i].set_empty_key(ref.metaWindows.size() + 11);
fwdMetaIDs[i] = tsl::hopscotch_map<uint32_t, uint16_t, MetaHash>();
revMetaIDs[i] = tsl::hopscotch_map<uint32_t, uint16_t, MetaHash>();
}
// fill array mapping - locale specific filling
lmap['A'%16] = 0;
lmap['C'%16] = 1;
lmap['G'%16] = 2;
lmap['T'%16] = 3;
}
ReadQueue::ReadQueue(const char* filePath, const char* filePath2, RefGenome& reference, const bool isGZ, const bool bsFlag) :
ref(reference)
, readBuffer(MyConst::CHUNKSIZE)
, readBuffer2(MyConst::CHUNKSIZE)
, isPaired(true)
, isSC(false)
, methLevels(ref.cpgTable.size())
, methLevelsStart(ref.cpgStartTable.size())
, bothStrandsFlag(bsFlag)
, r1FwdMatches(0)
, r1RevMatches(0)
, matchR1Fwd(true)
// TODO
, of("errOut.txt")
{
// TODO: remove this
//
// std::ofstream cCountFile ("cCounts.tsv");
// cCountFile << "Chrom\tPos\tStrand\tCContent\n";
// for (size_t i = 0; i < ref.cpgTable.size(); ++i)
// {
// const auto& cpg = ref.cpgTable[i];
// uint32_t cCount = 0;
// for (std::vector<char>::iterator it = ref.fullSeq[cpg.chrom].begin() + cpg.pos - 100; it <= ref.fullSeq[cpg.chrom].begin() + cpg.pos + 100; ++it)
// {
// if (*it == 'C')
// {
// ++cCount;
// }
// }
// cCountFile << ref.chrMap[cpg.chrom] << "\t" << cpg.pos + MyConst::READLEN - 2 << "\t+\t" << cCount << "\n";
// }
// // reverse strand
// for (size_t i = 0; i < ref.cpgTable.size(); ++i)
// {
// const auto& cpg = ref.cpgTable[i];
// uint32_t cCount = 0;
// for (std::vector<char>::iterator it = ref.fullSeq[cpg.chrom].begin() + cpg.pos - 100; it <= ref.fullSeq[cpg.chrom].begin() + cpg.pos + 100; ++it)
// {
// if (*it == 'G')
// {
// ++cCount;
// }
// }
// cCountFile << ref.chrMap[cpg.chrom] << "\t" << cpg.pos + MyConst::READLEN - 2 << "\t-\t" << cCount << "\n";
// }
// cCountFile.close();
if (isGZ)
{
igz.open(filePath);
if(!igz)
{
std::cerr << "Opening read file " << std::string(filePath) << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
igz2.open(filePath2);
if(!igz2)
{
std::cerr << "Opening read file " << std::string(filePath2) << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
} else {
file.open(filePath);
if(!file)
{
std::cerr << "Opening read file " << std::string(filePath) << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
file2.open(filePath2);
if(!file2)
{
std::cerr << "Opening read file " << std::string(filePath2) << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
}
// fill counting structure for parallelization
for (unsigned int i = 0; i < CORENUM; ++i)
{
// paired_fwdMetaIDs[i] = google::dense_hash_map<uint32_t, std::tuple<uint8_t, uint8_t, bool, bool>, MetaHash>();
// paired_revMetaIDs[i] = google::dense_hash_map<uint32_t, std::tuple<uint8_t, uint8_t, bool, bool>, MetaHash>();
// paired_fwdMetaIDs[i].set_deleted_key(ref.metaCpGs.size() + 10);
// paired_revMetaIDs[i].set_deleted_key(ref.metaCpGs.size() + 10);
// paired_fwdMetaIDs[i].set_empty_key(ref.metaCpGs.size() + 11);
// paired_revMetaIDs[i].set_empty_key(ref.metaCpGs.size() + 11);
paired_fwdMetaIDs[i] = tsl::hopscotch_map<uint32_t, std::tuple<uint8_t, uint8_t, bool, bool>, MetaHash>();
paired_revMetaIDs[i] = tsl::hopscotch_map<uint32_t, std::tuple<uint8_t, uint8_t, bool, bool>, MetaHash>();
}
// fill array mapping - locale specific filling
lmap['A'%16] = 0;
lmap['C'%16] = 1;
lmap['G'%16] = 2;
lmap['T'%16] = 3;
}
ReadQueue::ReadQueue(const char* scOutputPath, RefGenome& reference, const bool isGZ, const bool bsFlag, const bool isP) :
ref(reference)
, readBuffer(MyConst::CHUNKSIZE)
, readBuffer2(MyConst::CHUNKSIZE)
, isPaired(isP)
, isSC(true)
, methLevels(ref.cpgTable.size())
, methLevelsStart(ref.cpgStartTable.size())
, methLevelsSc(ref.cpgTable.size())
, scOutput(scOutputPath)
, bothStrandsFlag(bsFlag)
, r1FwdMatches(0)
, r1RevMatches(0)
, matchR1Fwd(true)
// TODO
, of("errOut.txt")
{
// fill array mapping - locale specific filling
lmap['A'%16] = 0;
lmap['C'%16] = 1;
lmap['G'%16] = 2;
lmap['T'%16] = 3;
// initialize sc output file
scOutput << "\nSC_ID\tCount_Type\t";
for (size_t cpgID = 0; cpgID < ref.cpgTable.size(); ++cpgID)
{
scOutput << ref.chrMap[ref.cpgTable[cpgID].chrom] << "\t";
}
scOutput << "\nSC_ID\tCount_Type\t";
for (size_t cpgID = 0; cpgID < ref.cpgTable.size(); ++cpgID)
{
scOutput << ref.cpgTable[cpgID].pos + MyConst::READLEN - 2 << "\t";
}
}
bool ReadQueue::parseChunk(unsigned int& procReads)
{
// std::cout << "Start reading chunk of reads\n";
//
std::string id;
// counter on how many reads have been read so far
unsigned int readCounter = 0;
// read first line of read (aka @'SEQID')
while (std::getline(file, id))
{
// read the next line (aka raw sequence)
std::string seq;
std::getline(file, seq);
// construct read and push it to buffer
readBuffer[readCounter] = Read(seq, id);
// read the rest of read (aka +'SEQID' and quality score sequence)
std::getline(file,id);
std::getline(file,seq);
++readCounter;
// if buffer is read completely, return
if (readCounter >= MyConst::CHUNKSIZE)
{
procReads = MyConst::CHUNKSIZE;
break;
}
}
// if needed, read paired reads
if (isPaired)
{
unsigned int readCounter2 = 0;
// read first line of read (aka @'SEQID')
while (std::getline(file2, id))
{
// read the next line (aka raw sequence)
std::string seq;
std::getline(file2, seq);
// construct read and push it to buffer
readBuffer2[readCounter2] = Read(seq, id);
// read the rest of read (aka +'SEQID' and quality score sequence)
std::getline(file2,id);
std::getline(file2,seq);
++readCounter2;
// if buffer is read completely, return
if (readCounter2 >= MyConst::CHUNKSIZE)
{
procReads = MyConst::CHUNKSIZE;
return true;
}
}
// check if same number of reads is processed so far
if (readCounter != readCounter2)
{
std::cerr << "Not the same number of reads available in the paired read files! \
Make sure that you paired all reads. \
Single reads have to be processed separately.\n\n";
exit(1);
}
} else {
if (readCounter >= MyConst::CHUNKSIZE)
return true;
}
procReads = readCounter;
return false;
}
bool ReadQueue::parseChunkGZ(unsigned int& procReads)
{
std::string id;
// counter on how many reads have been read so far
unsigned int readCounter = 0;
// read first line of read (aka @'SEQID')
while (std::getline(igz, id))
{
// read the next line (aka raw sequence)
std::string seq;
std::getline(igz, seq);
// construct read and push it to buffer
readBuffer[readCounter] = Read(seq, id);
// read the rest of read (aka +'SEQID' and quality score sequence)
std::getline(igz,id);
std::getline(igz,seq);
++readCounter;
// if buffer is read completely, return
if (readCounter >= MyConst::CHUNKSIZE)
{
procReads = MyConst::CHUNKSIZE;
break;
}
}
// if needed, read paired reads
if (isPaired)
{
unsigned int readCounter2 = 0;
// read first line of read (aka @'SEQID')
while (std::getline(igz2, id))
{
// read the next line (aka raw sequence)
std::string seq;
std::getline(igz2, seq);
// construct read and push it to buffer
readBuffer2[readCounter2] = Read(seq, id);
// read the rest of read (aka +'SEQID' and quality score sequence)
std::getline(igz2,id);
std::getline(igz2,seq);
++readCounter2;
// if buffer is read completely, return
if (readCounter2 >= MyConst::CHUNKSIZE)
{
procReads = MyConst::CHUNKSIZE;
return true;
}
}
// check if same number of reads is processed so far
if (readCounter != readCounter2)
{
std::cerr << "Not the same number of reads available in the paired read files! \
Make sure that you paired all reads. \
Single reads have to be processed separately.\n\n";
exit(1);
}
} else {
if (readCounter >= MyConst::CHUNKSIZE)
return true;
}
procReads = readCounter;
return false;
}
void ReadQueue::decideStrand()
{
std::cout << "\nFwd matches: " << r1FwdMatches;
std::cout << "\nRev matches: " << r1RevMatches;
const double odds = (double)(r1FwdMatches + 1)/(double)(r1RevMatches + 1);
std::cout << "\nOdds of matching to fwd/rev strand: " << odds << "\n\n";
if (odds > 0.005 && odds < 200)
{
std::cout << "Warning! Many of the reads are mapped in different orientation\
Stranding might harm the prediction performance.\n\
If you built a library where read 1 can occur as C->T or G->A converted, consider running the tool with\n\
\"--non_stranded\"\n\
flag.\n\n";
}
std::cout << "Deciding conversion status of read 1.\n";
if (r1FwdMatches > r1RevMatches)
{
std::cout << "\tMatching read 1 as C->T converted.\n\n";
matchR1Fwd = true;
} else {
std::cout << "\tMatching read 1 as G->A converted.\n\n";
matchR1Fwd = false;
}
}
bool ReadQueue::matchReads(const unsigned int& procReads, uint64_t& succMatch, uint64_t& nonUniqueMatch, uint64_t& unSuccMatch, const bool getStranded)
{
// reset all counters
for (unsigned int i = 0; i < CORENUM; ++i)
{
matchStats[i] = 0;
nonUniqueStats[i] = 0;
noMatchStats[i] = 0;
}
#ifdef _OPENMP
#pragma omp parallel for num_threads(CORENUM) schedule(static)
#endif
for (unsigned int i = 0; i < procReads; ++i)
{
int threadnum = omp_get_thread_num();
uint64_t& succMatchT = matchStats[threadnum];
uint64_t& nonUniqueMatchT = nonUniqueStats[threadnum];
uint64_t& unSuccMatchT = noMatchStats[threadnum];
Read& r = readBuffer[i];
const size_t readSize = r.seq.size();
if (readSize < MyConst::READLEN - 20)
{
r.isInvalid = true;
continue;
}
// flag stating if read contains N
// reads with N are ignored
bool nflag = false;
// get correct offset for reverse strand (strand orientation must be correct)
size_t revPos = readSize - 1;
// std::chrono::high_resolution_clock::time_point startTime = std::chrono::high_resolution_clock::now();
// string containing reverse complement (under FULL alphabet)
std::string revSeq;
revSeq.resize(readSize);
// construct reduced alphabet sequence for forward and reverse strand
for (size_t pos = 0; pos < readSize; ++pos, --revPos)
{
switch (r.seq[pos])
{
case 'A':
revSeq[revPos] = 'T';
break;
case 'C':
revSeq[revPos] = 'G';
break;
case 'G':
revSeq[revPos] = 'C';
break;
case 'T':
revSeq[revPos] = 'A';
break;
case 'N':
nflag = true;
break;
default:
std::cerr << "Unknown character '" << r.seq[pos] << "' in read with sequence id " << r.id << std::endl;
}
}
if (nflag)
{
r.isInvalid = true;
continue;
}
// std::chrono::high_resolution_clock::time_point endTime = std::chrono::high_resolution_clock::now();
// auto runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
//
// of << runtime << "\n";
//
// set qgram threshold
uint16_t qThreshold = MyConst::QTHRESH;
// TODO
// startTime = std::chrono::high_resolution_clock::now();
MATCH::match matchFwd = 0;
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
// if (runtime > 3000)
// {
// of << r.seq << "\n";
// of << "Overall meta CpGs: " << fwdMetaIDs[omp_get_thread_num()].size() + revMetaIDs[omp_get_thread_num()].size() << "\n";
// // uint16_t qThreshold = readSize - MyConst::KMERLEN - (MyConst::KMERLEN * MyConst::MISCOUNT);
// // check for overflow (i.e. read is to small for lemma)
// // if (qThreshold > readSize)
// // qThreshold = 0;
// uint64_t qcount = 0;
// for (const auto& m : fwdMetaIDs[omp_get_thread_num()])
// {
// if (m.second >= qThreshold)
// ++qcount;
// }
// for (const auto& m : revMetaIDs[omp_get_thread_num()])
// {
// if (m.second >= qThreshold)
// ++qcount;
// }
// of << "Meta CpGs passing q-gram (q=" << qThreshold << ") filter: " << qcount << "\n";
// }
// startTime = std::chrono::high_resolution_clock::now();
int succQueryFwd = 0;
if (bothStrandsFlag || getStranded || matchR1Fwd)
{
getSeedRefs(r.seq, readSize, qThreshold);
ShiftAnd<MyConst::MISCOUNT + MyConst::ADDMIS> saFwd(r.seq, lmap);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\t";
// startTime = std::chrono::high_resolution_clock::now();
// of << "--------------------------------\n\n";
// of << "Matching read " << r.id << "\n\n";
succQueryFwd = saQuerySeedSetRef(saFwd, matchFwd, qThreshold);
// succQueryFwd = matchSingle(r.seq, qThreshold, saFwd, matchFwd, threadnum);
}
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
//
// startTime = std::chrono::high_resolution_clock::now();
MATCH::match matchRev = 0;
// if (!succFlag)
// {
// ++readCount;
// // of << "\n";
// r.isInvalid = true;
// continue;
// }
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
// if (runtime > 3000)
// {
// of << r.seq << "\n";
// of << "Overall meta CpGs: " << fwdMetaIDs[omp_get_thread_num()].size() + revMetaIDs[omp_get_thread_num()].size() << "\n";
// // uint16_t qThreshold = readSize - MyConst::KMERLEN - (MyConst::KMERLEN * MyConst::MISCOUNT);
// // check for overflow (i.e. read is to small for lemma)
// // if (qThreshold > readSize)
// // qThreshold = 0;
// uint64_t qcount = 0;
// for (const auto& m : fwdMetaIDs[omp_get_thread_num()])
// {
// if (m.second >= qThreshold)
// ++qcount;
// }
// for (const auto& m : revMetaIDs[omp_get_thread_num()])
// {
// if (m.second >= qThreshold)
// ++qcount;
// }
// of << "Meta CpGs passing q-gram (q=" << qThreshold << ") filter: " << qcount << "\n";
// }
// of << "\nMeta id count fwd: " << fwdMetaIDs[omp_get_thread_num()][319746] << "\n";
// of << "\nMeta id count rev: " << revMetaIDs[omp_get_thread_num()][319746] << "\n";
// if (runtime > 100)
// {
// of << r.seq << "\n";
// of << "Overall meta CpGs: " << fwdMetaIDs[omp_get_thread_num()].size() + revMetaIDs[omp_get_thread_num()].size() << "\n";
// }
// startTime = std::chrono::high_resolution_clock::now();
// std::cout << revSeq << "\n";
int succQueryRev = 0;
if (bothStrandsFlag || getStranded || !matchR1Fwd)
{
getSeedRefs(revSeq, readSize, qThreshold);
ShiftAnd<MyConst::MISCOUNT + MyConst::ADDMIS> saRev(revSeq, lmap);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
// startTime = std::chrono::high_resolution_clock::now();
succQueryRev = saQuerySeedSetRef(saRev, matchRev, qThreshold);
// succQueryRev = matchSingle(revSeq, qThreshold, saRev, matchRev, threadnum);
}
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
// if (runtime > 5000)
// {
// of << r.seq << "\n";
// of << "Overall meta CpGs: " << fwdMetaIDs[omp_get_thread_num()].size() + revMetaIDs[omp_get_thread_num()].size() << "\n";
// // uint16_t qThreshold = readSize - MyConst::KMERLEN - (MyConst::KMERLEN * MyConst::MISCOUNT);
// // check for overflow (i.e. read is to small for lemma)
// // if (qThreshold > readSize)
// // qThreshold = 0;
// qcount = 0;
// for (const auto& m : fwdMetaIDs[omp_get_thread_num()])
// {
// if (m.second >= qThreshold)
// ++qcount;
// }
// for (const auto& m : revMetaIDs[omp_get_thread_num()])
// {
// if (m.second >= qThreshold)
// ++qcount;
// }
// of << "Meta CpGs passing q-gram (q=" << qThreshold << ") filter: " << qcount << "\n";
// }
// found match for fwd and rev automaton
if (succQueryFwd == 1 && succQueryRev == 1)
{
uint8_t fwdErr = MATCH::getErrNum(matchFwd);
uint8_t revErr = MATCH::getErrNum(matchRev);
// of << "Found match with read and reverse complement. Errors (fwd/rev): " << fwdErr << "/" << revErr;
// of << "\nMatching strands are (fwd/rev): " << MATCH::isFwd(matchFwd) << "/" << MATCH::isFwd(matchRev) << "\n";
// check which one has fewer errors
if (fwdErr < revErr)
{
if (getStranded)
#pragma omp atomic
++r1FwdMatches;
++succMatchT;
r.mat = matchFwd;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchFwd, r.seq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
} else {
if (fwdErr > revErr)
{
if (getStranded)
#pragma omp atomic
++r1RevMatches;
++succMatchT;
r.mat = matchRev;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchRev, revSeq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
// if same number of errors, then not unique
} else {
const uint32_t metaFwd = MATCH::getMetaID(matchFwd);
const uint32_t metaRev = MATCH::getMetaID(matchRev);
const uint64_t offFwd = MATCH::getOffset(matchFwd);
const uint64_t offRev = MATCH::getOffset(matchRev);
const bool m1_isFwd = MATCH::isFwd(matchFwd);
const bool m2_isFwd = MATCH::isFwd(matchRev);
const bool m1_isStart = MATCH::isStart(matchFwd);
const bool m2_isStart = MATCH::isStart(matchRev);
uint32_t m1_pos;
uint32_t m2_pos;
if (m1_isStart && m2_isStart)
{
m1_pos = offFwd;
m2_pos = offRev;
} else {
m1_pos = ref.metaWindows[metaFwd].startPos + offFwd;
m2_pos = ref.metaWindows[metaRev].startPos + offRev;
}
// test if same match in same region
if ((m1_isStart == m2_isStart) && (m1_isFwd == m2_isFwd) && (m1_pos == m2_pos))
{
++succMatchT;
if (getStranded)
#pragma omp atomic
++r1FwdMatches;
r.mat = matchFwd;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchFwd, r.seq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
} else {
++nonUniqueMatchT;
r.isInvalid = true;
}
}
}
// unique match on forward strand
} else if (succQueryFwd == 1) {
if (succQueryRev == -1)
{
if (MATCH::getErrNum(matchFwd) < MATCH::getErrNum(matchRev))
{
// of << "Match with FWD automaton. Strand is " << MATCH::isFwd(matchFwd) << "\n";
++succMatchT;
if (getStranded)
#pragma omp atomic
++r1FwdMatches;
r.mat = matchFwd;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchFwd, r.seq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
} else {
++nonUniqueMatchT;
r.isInvalid = true;
}
} else {
// of << "Match with FWD automaton. Strand is " << MATCH::isFwd(matchFwd) << "\n";
++succMatchT;
if (getStranded)
#pragma omp atomic
++r1FwdMatches;
r.mat = matchFwd;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchFwd, r.seq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
}
// unique match on backward strand
} else if (succQueryRev == 1) {
if (succQueryFwd == -1)
{
if (MATCH::getErrNum(matchRev) < MATCH::getErrNum(matchFwd))
{
// of << "Match with REV automaton. Strand is " << MATCH::isFwd(matchRev) << "\n";
++succMatchT;
if (getStranded)
#pragma omp atomic
++r1RevMatches;
r.mat = matchRev;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchRev, revSeq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
} else {
++nonUniqueMatchT;
r.isInvalid = true;
}
} else {
// of << "Match with REV automaton. Strand is " << MATCH::isFwd(matchRev) << "\n";
++succMatchT;
if (getStranded)
#pragma omp atomic
++r1RevMatches;
r.mat = matchRev;
// startTime = std::chrono::high_resolution_clock::now();
computeMethLvl(matchRev, revSeq);
// endTime = std::chrono::high_resolution_clock::now();
// runtime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
// of << runtime << "\n";
}
// no match found at all
} else {
r.isInvalid = true;
if (succQueryFwd == -1 || succQueryRev == -1)
{
// of << "Nonunique match.\n";
++nonUniqueMatchT;
} else {
// of << "No match.\n";
++unSuccMatchT;
// }
// }
// #ifdef _OPENMP
// #pragma omp critical
// #endif
// {
// // construct hash and look up the hash table entries
// size_t lPos = r.id.find_last_of('_');
// std::string stringOffset (r.id.begin() + lPos + 1, r.id.end());
// size_t rPos = r.id.find_last_of('R');
// std::string stringChrom (r.id.begin() + 1 + rPos, r.id.begin() + lPos);
// uint8_t chrom = std::stoul(stringChrom);
// unsigned long offset = std::stoul(stringOffset);
// of << "\nreal seq/real revSeq/sequence in genome: " << r.id << "\n" << r.seq << "\n" << revSeq << "\n" << std::string(ref.fullSeq[chrom].begin() + offset, ref.fullSeq[chrom].begin() + offset + 100) << "\n\n\n";
//
//
// uint64_t hVal;
// uint64_t sVal = ntHash::NTPS64(r.seq.data(), MyConst::SEED, MyConst::KMERLEN, hVal) % MyConst::HTABSIZE;
// auto startIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal];
// auto endIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal + 1];
// auto tit = ref.strandTable.begin() + ref.tabIndex[sVal];
// for (auto it = startIt; it != endIt; ++it, ++tit)
// {
// KMER_S::kmer& k = *it;
// const uint32_t m = KMER_S::getMetaCpG(k);
// const bool isStart = KMER_S::isStartCpG(k);
// if (!isStart)
// {
// const struct CpG& startCpg = ref.cpgTable[ref.metaCpGs[m].start];
// if (*tit)
// {
// auto stIt = ref.fullSeq[startCpg.chrom].begin() + startCpg.pos;
// auto enIt = ref.fullSeq[startCpg.chrom].begin() + 2*MyConst::READLEN - 2 + startCpg.pos;
// of << std::string(stIt, enIt) << "\n";
// }
// }
// }
// of << "Last Sequence part:\n";
// hVal = 0;
// sVal = ntHash::NTPS64(r.seq.data()+70, MyConst::SEED, MyConst::KMERLEN, hVal) % MyConst::HTABSIZE;
// startIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal];
// endIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal + 1];
// tit = ref.strandTable.begin() + ref.tabIndex[sVal];
// for (auto it = startIt; it != endIt; ++it, ++tit)
// {
// KMER_S::kmer& k = *it;
// const uint32_t m = KMER_S::getMetaCpG(k);
// const bool isStart = KMER_S::isStartCpG(k);
// if (!isStart)
// {
// const struct CpG& startCpg = ref.cpgTable[ref.metaCpGs[m].start];
// if (*tit)
// {
// auto stIt = ref.fullSeq[startCpg.chrom].begin() + startCpg.pos;
// auto enIt = ref.fullSeq[startCpg.chrom].begin() + 2*MyConst::READLEN - 2 + startCpg.pos;
// of << std::string(stIt, enIt) << "\n";
// }
// }
// }
//
// of << "\n\nReverse seq matches\n";
// hVal = 0;
// sVal = ntHash::NTPS64(revSeq.data(), MyConst::SEED, MyConst::KMERLEN, hVal) % MyConst::HTABSIZE;
// startIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal];
// endIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal + 1];
// tit = ref.strandTable.begin() + ref.tabIndex[sVal];
// for (auto it = startIt; it != endIt; ++it, ++tit)
// {
// KMER_S::kmer& k = *it;
// const uint64_t m = KMER_S::getMetaCpG(k);
// const bool isStart = KMER_S::isStartCpG(k);
// if (!isStart)
// {
// const struct CpG& startCpg = ref.cpgTable[ref.metaCpGs[m].start];
// if (*tit)
// {
// auto stIt = ref.fullSeq[startCpg.chrom].begin() + startCpg.pos;
// auto enIt = ref.fullSeq[startCpg.chrom].begin() + 2*MyConst::READLEN - 2 + startCpg.pos;
// of << std::string(stIt, enIt) << "\n";
// }
// }
// }
// of << "Last Sequence part:\n";
// hVal = 0;
// sVal = ntHash::NTPS64(revSeq.data()+70, MyConst::SEED, MyConst::KMERLEN, hVal) % MyConst::HTABSIZE;
// startIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal];
// endIt = ref.kmerTableSmall.begin() + ref.tabIndex[sVal + 1];
// tit = ref.strandTable.begin() + ref.tabIndex[sVal];
// for (auto it = startIt; it != endIt; ++it, ++tit)
// {
// KMER_S::kmer& k = *it;
// const uint64_t m = KMER_S::getMetaCpG(k);
// const bool isStart = KMER_S::isStartCpG(k);
// if (!isStart)
// {
// const struct CpG& startCpg = ref.cpgTable[ref.metaCpGs[m].start];
// if (*tit)
// {
// auto stIt = ref.fullSeq[startCpg.chrom].begin() + startCpg.pos;
// auto enIt = ref.fullSeq[startCpg.chrom].begin() + 2*MyConst::READLEN - 2 + startCpg.pos;
// of << std::string(stIt, enIt) << "\n";
// }
// }
// }
// of << "\n\n--------------------\n\n";
//
//
// // END PRAGMA OMP CRITICAL
// }
}
// if (unSuccMatch > 10)
// {
// of.close();
// exit(1);
// }
}
}
// sum up counts
for (unsigned int i = 0; i < CORENUM; ++i)
{
succMatch += matchStats[i];
nonUniqueMatch += nonUniqueStats[i];
unSuccMatch += noMatchStats[i];
}
return true;
}
bool ReadQueue::matchPairedReads(const unsigned int& procReads, uint64_t& succMatch, uint64_t& nonUniqueMatch, uint64_t& unSuccMatch, uint64_t& succPairedMatch, uint64_t& tooShortCountMatch, const bool getStranded)
{
// reset all counters
for (unsigned int i = 0; i < CORENUM; ++i)
{
matchStats[i] = 0;
nonUniqueStats[i] = 0;
noMatchStats[i] = 0;
matchPairedStats[i] = 0;
tooShortCounts[i] = 0;
}
// TODO
// std::ofstream of2 ("errOut2.txt");
#ifdef _OPENMP
#pragma omp parallel for num_threads(CORENUM) schedule(dynamic,50)
#endif
for (unsigned int i = 0; i < procReads; ++i)
{
int threadnum = omp_get_thread_num();
uint64_t& succMatchT = matchStats[threadnum];
uint64_t& nonUniqueMatchT = nonUniqueStats[threadnum];
uint64_t& unSuccMatchT = noMatchStats[threadnum];
uint64_t& succPairedMatchT = matchPairedStats[threadnum];
uint64_t& tooShortCount = tooShortCounts[threadnum];
Read& r1 = readBuffer[i];
Read& r2 = readBuffer2[i];
const size_t readSize1 = r1.seq.size();
const size_t readSize2 = r2.seq.size();
if (readSize1 < ceil((float)MyConst::READLEN*0.75))
{
r1.isInvalid = true;
}
if (readSize2 < ceil((float)MyConst::READLEN*0.75))
{
r2.isInvalid = true;
}
if (r1.isInvalid || r2.isInvalid)
{
++tooShortCount;
}
// get correct offset for reverse strand (strand orientation must be correct)
size_t revPos = readSize1 - 1;
// string containing reverse complement (under FULL alphabet)
std::string revSeq1;
revSeq1.resize(readSize1);
// construct reduced alphabet sequence for forward and reverse strand
for (size_t pos = 0; pos < readSize1; ++pos, --revPos)
{
switch (r1.seq[pos])
{
case 'A':