-
Notifications
You must be signed in to change notification settings - Fork 0
/
alu_insert.cpp
executable file
·1821 lines (1706 loc) · 74.6 KB
/
alu_insert.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
#define SEQAN_HAS_ZLIB 1
#include "insert_utils.h"
struct PAR_DELETE0 {
int CLIP_Q;
};
struct REALIGNS {
int rid, pos_key, aBegin, aEnd;
string qName;
};
void alu_mate_flag( BamFileHandler * bam_fh, string fn_output, string &header, map <string, int> & rg_to_idx){
ofstream fout (fn_output.c_str() );
fout << header ;
seqan::BamAlignmentRecord record;
while ( bam_fh -> fetch_a_read(record) ) {
if ( ! QC_insert_read(record) ) continue;
if ( ! key_exists(bam_fh->rID_chrn, record.rID) ) continue;
int len_seq = length(record.seq);
if ( count_non_match(record) >= CLIP_BP ) continue; //////alternative strategy: if ( !is_aluflag_cnt(record)) continue;
///// allow pair to be mapped to decoy, key_exists(bam_fh->rID_chrn, record.rNextId)
if ( record.rID < record.rNextId or
(record.rID == record.rNextId and record.beginPos < record.pNext and abs(record.tLen) > DISCORDANT_LEN) ) {
fout << record.qName << " " << record.rID << " " << record.rNextId << " " << record.beginPos << " " << record.pNext
<< " " << hasFlagRC(record) << " " << hasFlagNextRC(record) << " " << len_seq << " " << get_rgIdx(rg_to_idx, record) << endl;
}
}
fout.close();
}
int get_type_cnts(map <int, set <string> > & type_cnts, int key){
if ( type_cnts.find(key) == type_cnts.end()) return 0;
return type_cnts[key].size();
}
void parse_line1(string &line, int pos, int offset, vector <int> & clipls, vector <int> & cliprs) {
stringstream ss;
ss.str(line);
string pn, tmpv;
int clipLen, p;
ss >> pn >> clipLen >> p;
if ( abs( p - pos) <= offset ) {
if (clipLen < 0 ) clipls.push_back(p);
else cliprs.push_back(p);
}
}
int parse_line3( string &line, int & pos) {
int lr_num, rr_num;
stringstream ss;
ss.str(line);
ss >> pos >> lr_num >> rr_num;
return lr_num + rr_num;
}
void get_scan_pos(string fn, list <int> & pos_to_scan, int combine_bp){
pos_to_scan.clear();
stringstream ss;
string line, qname;
int this_rID, this_pos, len_read, previous_end;
bool this_isRC;
ifstream fin( fn.c_str());
assert(fin); // init, 700 * 30 /100 = 210
getline(fin, line);
while (pos_to_scan.empty()) {
getline(fin, line);
ss.clear(); ss.str(line);
ss >> qname >> this_rID >> this_pos >> this_isRC >> len_read;
if ( this_pos > SCAN_WIN_LEN ) pos_to_scan.push_back(this_pos);
}
while (getline(fin, line)) {
previous_end = this_pos + len_read;
ss.clear(); ss.str(line);
ss >> qname >> this_rID >> this_pos >> this_isRC >> len_read;
if (this_pos - previous_end > 3 * combine_bp) pos_to_scan.push_back(previous_end + combine_bp);
if (this_pos - pos_to_scan.back() > 2 * combine_bp) pos_to_scan.push_back(this_pos - combine_bp);
}
}
// need smarter way to combine positions ==> minimum number of regions to cover all reads info
void join_location(string file1, string file2, int pos_dif, int max_region_len){
ifstream fin( file1.c_str());
assert(fin);
string line;
getline(fin, line); // skip header
getline(fin, line);
int pos, pos_pre;
int nowCount = parse_line3(line, pos_pre);
//cout << "nowC " << nowCount << endl;
int maxCount = nowCount;
vector<int> maxCount_pos;
maxCount_pos.push_back(pos_pre);
stringstream sout;
while (getline(fin, line)) {
nowCount = parse_line3(line, pos);
if ( pos - pos_pre <= pos_dif and pos - (*maxCount_pos.begin()) <= max_region_len) {// same block
if ( nowCount > maxCount) {
maxCount = nowCount;
maxCount_pos.clear();
maxCount_pos.push_back(pos);
} else if ( nowCount == maxCount) {
maxCount_pos.push_back(pos);
}
} else { // create new block
sout << maxCount << " " << maxCount_pos.front() << " " << maxCount_pos.back() << endl;
maxCount_pos.clear();
maxCount_pos.push_back(pos);
maxCount = nowCount;
}
pos_pre = pos;
}
fin.close();
ofstream fout( file2.c_str());
fout << sout.str();
fout.close();
}
void alumate_counts_filter(string & path0, string pn, string fn_suffix_input, string fn_suffix_output, string fn_suffix_output2, vector<string> &chrns, int th_cnt){
int lr_num, rr_num, this_rID, this_pos, len_read;
string line, qname;
bool this_isRC;
stringstream ss;
for (vector<string>::iterator ci = chrns.begin(); ci != chrns.end(); ci++) {
string chrn = *ci;
string f_input = path0 + chrn + "/" + pn + fn_suffix_input;
string f_output = path0 + chrn + "/" + pn + fn_suffix_output;
string f_output2 = path0 + chrn + "/" + pn + fn_suffix_output2;
list <int> pos_to_scan;
get_scan_pos(f_input, pos_to_scan, 8); // no big diff when change 8 to 20
// cout << f_input << " " << pos_to_scan.size() << endl;
list< READ_INFO *> lr_reads;
ifstream fin ( f_input.c_str());
assert(fin);
ofstream fout( f_output.c_str());
fout << "check_pos lr_num rr_num\n";
getline(fin, line); // skip header
getline(fin, line);
ss.clear(); ss.str(line);
ss >> qname >> this_rID >> this_pos >> this_isRC >> len_read;
lr_reads.push_back(new READ_INFO(this_pos, len_read, !this_isRC, ""));
bool readable = true;
while ( readable and pos_to_scan.size()) {
int check_pos = pos_to_scan.front();
pos_to_scan.pop_front();
while (readable and (lr_reads.empty() or lr_reads.back()-> endPos < check_pos + SCAN_WIN_LEN) ) {
if (getline(fin, line)) {
ss.clear(); ss.str(line);
ss >> qname >> this_rID >> this_pos >> this_isRC >> len_read;
if ( len_read > 50)
lr_reads.push_back(new READ_INFO(this_pos, len_read, !this_isRC, ""));
} else
readable = false;
}
if (lr_reads.empty()) continue;
lr_num = 0; rr_num = 0;
list< READ_INFO *>::iterator ri = lr_reads.begin();
while (ri != lr_reads.end() ) {
if ( (*ri)->beginPos < check_pos - SCAN_WIN_LEN ) {
delete *ri;
ri = lr_reads.erase(ri); // <==> lr_reads.erase(ri++)
continue;
}
if ( (*ri)->endPos < check_pos and (*ri)->should_be_left) lr_num++;
else if ( (*ri)->beginPos > check_pos and !(*ri)->should_be_left ) rr_num++;
ri++;
if ( (*ri)->endPos >= check_pos + SCAN_WIN_LEN ) break;
}
if (lr_num + rr_num >= th_cnt) fout << check_pos << " " << lr_num << " " << rr_num << endl;
}
fin.close();
fout.close();
join_location(f_output, f_output2, MAX_POS_DIF, MAX_LEN_REGION);
cout << "done " << f_output << endl;
}
}
void filter_location_rep(string path0, string pn, string file1_suffix, string file2_suffix, vector<string> &chrns, RepMaskPos *repmaskPos){
for (vector<string>::iterator ci = chrns.begin(); ci != chrns.end(); ci++) {
string chrn = *ci;
ifstream fin( (path0 + chrn + "/" + pn + file1_suffix).c_str());
assert(fin);
ofstream fout( (path0 + chrn + "/" + pn + file2_suffix).c_str());
string line;
stringstream ss;
int readCnt, pos_left, pos_right;
vector<int>::iterator bi = repmaskPos->beginP[chrn].begin();
vector<int>::iterator ei = repmaskPos->endP[chrn].begin();
int bei = 0;
int be_size = repmaskPos->beginP[chrn].size();
while ( getline(fin, line) ) {
ss.clear(); ss.str( line );
ss >> readCnt >> pos_left >> pos_right;
if (pos_right <= 0) continue;
while ( pos_left >= (*ei) and bei < be_size) { bi++; ei++; bei++; }
if ( min(*ei, pos_right) - max(*bi, pos_left) < 0) // NB: < 0, not <= 0
fout << line << endl; // not in Alu
}
fin.close();
fout.close();
}
}
int read_sort_by_col(string fn, int coln, bool has_header, list< IntString> &rows_list) {
string line, tmpv;
int pos;
stringstream ss;
ifstream fin( fn.c_str());
assert(fin);
rows_list.clear();
if (has_header) getline(fin, line);
size_t rown = 0;
while (getline(fin, line)) {
rown++;
ss.clear(); ss.str( line );
for (int i = 0; i < coln-1; i++) ss >> tmpv;
ss >> pos;
rows_list.push_back( make_pair(pos, line) );
}
fin.close();
rows_list.sort(compare_IntString);
if (rows_list.size() != rown ) cerr << "##### ERROR #### " << fn << endl;
return rows_list.size();
}
// qname A_chr B_chr A_beginPos B_beginPos A_isRC B_isRC len_read
void read_match_rid(string fn, int col_val, list< AlumateINFO *> & alumate_list){
string line, qname;
int rid1, rid2, pos1, pos2, len_read, rgIdx;
bool rc1, rc2;
stringstream ss;
ifstream fin( fn.c_str());
assert(fin);
getline(fin, line);
while (getline(fin, line)) {
ss.clear(); ss.str( line );
ss >> qname >> rid1 >> rid2 >> pos1 >> pos2 >> rc1 >> rc2 >> len_read >> rgIdx;
if (rid1 == col_val)
alumate_list.push_back(new AlumateINFO(qname, len_read, rid2, rid1, pos2, pos1, rgIdx, rc2, rc1));
if (rid2 == col_val)
alumate_list.push_back(new AlumateINFO(qname, len_read, rid1, rid2, pos1, pos2, rgIdx, rc1, rc2));
}
}
void keep_alu_mate(BamFileHandler * bam_fh, int alu_rid, string file1, MapFO & fileMap, string file_alu){
const int ALU_MIN_LEN = 50; // min overlap in alu region
list< AlumateINFO *> alumate_list;
read_match_rid(file1, alu_rid, alumate_list);
alumate_list.sort(AlumateINFO::sort_pos2);
cout << "alu file " << file_alu << endl;
AluRefPos *alurefpos = new AluRefPos(file_alu, 100, 100); // combine nearby positions, considered as Alu
int bei = 0, n_alu = 0;
int be_size = alurefpos->db_size - 1; // -1 ! otherwise seg fault
int left_pos;
for (list< AlumateINFO *>::iterator ri = alumate_list.begin(); ri != alumate_list.end(); ri++) {
assert ( (*ri)->rid2 == alu_rid );
left_pos = (*ri)->pos2;
while ( ( left_pos >= alurefpos->get_endP() ) and bei < be_size) { alurefpos->nextdb(); bei++; }
if ( min( alurefpos->get_endP(), left_pos + (*ri)->len_read ) - max( alurefpos->get_beginP(), left_pos) > ALU_MIN_LEN and
key_exists(bam_fh->rID_chrn, (*ri)->rid1) and n_alu++)
*(fileMap[(*ri)->rid1]) << (*ri)->qname << " " << (*ri)->rid1 << " " << (*ri)->pos1 << " " << (*ri)->RC1 << " " << (*ri)->len_read
<< " " << (*ri)->rid2 << " " << left_pos << " " << (*ri)->RC2 << " " << (*ri)->rgIdx << " " << alurefpos->get_type() << endl ;
}
////// about 18% err counts are mapped to Alu region(normal chrns)
cout << file1 << " err counts: " << alumate_list.size() << ", alu mate: " << n_alu << endl;
AlumateINFO::delete_list(alumate_list);
delete alurefpos;
}
bool write_tmpFile_all_loci( vector<string> &fn_inputs, vector <string> & fn_idx, string fn_output) {
stringstream ss;
string line;
ofstream fout;
vector<string>::iterator di;
if (!fn_idx.empty()) di = fn_idx.begin();
//cout << "####write to " << fn_output << " " << fn_inputs.size() << " " << fn_idx.size() << endl;
fout.open(fn_output.c_str());
for (vector<string>::iterator fi = fn_inputs.begin(); fi != fn_inputs.end(); fi ++ ) {
ifstream fin( (*fi).c_str());
if (!fin) {
cerr << "#### !!! " << *fi << " does not exists! skip it\n";
continue;
}
if ( fn_idx.empty() ) { while (getline(fin, line)) fout << line << endl; }
else { while (getline(fin, line)) fout << line << " " << *di << endl; di++;}
fin.close();
}
fout.close();
// sorting
list< IntString> rows_list;
read_sort_by_col(fn_output, 2, false, rows_list);
fout.open(fn_output.c_str());
for (list< IntString>::iterator ri = rows_list.begin(); ri!=rows_list.end(); ri++)
fout << (*ri).second << endl;
fout.close();
rows_list.clear();
return true;
}
void join_all_loci(string f_in, string f_out, int block_dist, int block_len) {
string line, pn;
int cnt, sum_cnt, pa, pb, pa_block, pb_block;
set <string> pns;
ifstream fin(f_in.c_str());
assert(fin);
ofstream fout(f_out.c_str());
stringstream ss;
getline(fin, line);
ss.str(line);
ss >> cnt >> pa_block >> pb_block;
while (ss >> pn) pns.insert(pn);
sum_cnt = cnt;
while ( getline(fin, line) ) {
ss.clear(); ss.str( line );
ss >> cnt >> pa >> pb;
if ( pa <= pb_block + block_dist and // same block
( pa <= pa_block + block_len or pa <= pb_block) ) {
pb_block = min( pa_block + block_len, max(pb_block, pb) );
sum_cnt += cnt;
} else { // new block
if (pb_block > 0) {
fout << sum_cnt << " " << pa_block << " " << pb_block;
for (set<string>::iterator si = pns.begin(); si != pns.end(); si++ ) fout << " " << *si;
fout << endl;
}
pns.clear();
pa_block = pa;
pb_block = pb;
sum_cnt = cnt;
}
while (ss >> pn) pns.insert(pn);
}
fout << sum_cnt << " " << pa_block << " " << pb_block;
for (set<string>::iterator si = pns.begin(); si != pns.end(); si++ ) fout << " " << *si;
fout << endl;
fin.close();
fout.close();
}
void combine_fn(vector<string> & fn_inputs, string fn_output, vector<string> & fn_idx){
write_tmpFile_all_loci(fn_inputs, fn_idx, fn_output + ".tmp");
join_all_loci(fn_output + ".tmp", fn_output, MAX_POS_DIF, 2 * MAX_LEN_REGION);
system( ("rm " + fn_output + ".tmp").c_str() );
}
bool combine_pn_pos(string chrn, map <int, string> &id_pn_map, string tmpn, string output_prefix, int group_size, string path0) {
string fn_prefix = output_prefix + chrn ; // some random names
// first iteration
map <string, vector<string> > fnOut_fnInput;
map <string, vector<string> >::iterator fni;
map <string, vector<string> > fnOut_pns;
string fnOut, fnInput_prefix, pn;
int i, j, n_size, n_group;
n_size = id_pn_map.size();
n_group = (int)(n_size / group_size);
for (i = 0; i < n_group; i++) {
fnOut = (n_size <= group_size) ? fn_prefix : (fn_prefix + "." + int_to_string(i) + ".tmp0");
for ( j = 0; j < group_size; j++) {
pn = id_pn_map[ i*group_size+j ];
fnOut_fnInput[fnOut].push_back(path0 + chrn + "/" + pn + tmpn);
fnOut_pns[fnOut].push_back(pn);
}
}
fnOut = (n_size <= group_size) ? fn_prefix : (fn_prefix + "." + int_to_string(n_group) + ".tmp0");
for ( j = n_group * group_size; j < n_size; j++) {
pn = id_pn_map[ j ];
fnOut_fnInput[fnOut].push_back(path0 + chrn + "/" + pn + tmpn);
fnOut_pns[fnOut].push_back(pn);
}
for (fni = fnOut_fnInput.begin(); fni != fnOut_fnInput.end(); fni ++)
combine_fn(fni->second, fni->first, fnOut_pns[fni->first]);
fnOut_pns.clear();
if ( fnOut_fnInput.size() <= 1) return true;
vector<string> empty_vec;
int fn_idx = 0;
while ( true ) {
n_size = fnOut_fnInput.size();
n_group = (int)( n_size / group_size);
fnOut_fnInput.clear();
for (i = 0; i < n_group; i++) {
fnOut = (n_size <= group_size) ? fn_prefix : (fn_prefix + "."+ int_to_string(i) + ".tmp"+ int_to_string(fn_idx+1) );
for ( j = 0; j < group_size; j++) fnOut_fnInput[fnOut].push_back( fn_prefix + "." + int_to_string(i*group_size+j) + ".tmp"+ int_to_string(fn_idx) );
}
fnOut = (n_size <= group_size) ? fn_prefix : (fn_prefix+ "." + int_to_string(n_group) + ".tmp"+ int_to_string(fn_idx+1) );
for ( j = n_group * group_size; j < n_size; j++) fnOut_fnInput[fnOut].push_back( fn_prefix + "." + int_to_string(j) + ".tmp" + int_to_string(fn_idx) );
for (fni = fnOut_fnInput.begin(); fni != fnOut_fnInput.end(); fni ++)
combine_fn(fni->second, fni->first, empty_vec);
fn_idx++;
if ( fnOut_fnInput.size() <= 1) break;
}
return true;
}
// scan candidate regions, write down clipReads if one of the following is valid (1) mate is discordant (2) partly aligned to consensus alu
// write down anyway if not sure
bool clipreads_at_insertPos(string pn, string chrn, BamFileHandler *bam_fh, string fin_pos, string fout_reads, int CLIP_Q, AluconsHandler *alucons_fh) {
const int MAX_REGION_LEN = 400;
ifstream fin( (fin_pos).c_str());
if (!fin) return false;
string line, numAluRead, _pn;
ofstream fout(fout_reads.c_str());
fout << "pn regionBegin regionEnd left_right clipPos qName cigar flag\n" ;
stringstream ss, ss_header;
int regionBegin, regionEnd, refBegin, refEnd;
int regionBegin0, regionEnd0;
while (getline(fin, line)) {
ss.clear(); ss.str( line );
ss >> numAluRead >> regionBegin >> regionEnd;
refBegin = regionBegin - SCAN_WIN_LEN;
refEnd = regionEnd + SCAN_WIN_LEN;
int _tmpwin = ( MAX_REGION_LEN - regionEnd + regionBegin) / 2;
regionBegin0 = regionBegin - _tmpwin;
regionEnd0 = regionEnd + _tmpwin;
bool pn_has_alumate = false;
while ( ss >> _pn )
if ( _pn == pn) { pn_has_alumate=true; break;}
if (! pn_has_alumate) continue;
if (! bam_fh->jump_to_region(chrn, refBegin, refEnd) ) continue;
ss_header.clear(); ss_header.str("");
ss_header << pn << " " << regionBegin << " " << regionEnd << " " ;
seqan::BamAlignmentRecord record;
map<int, int> confidentPos; // resolution of 10
while ( true ) {
string read_status = bam_fh->fetch_a_read(chrn, refBegin, refEnd, record);
if (read_status == "stop" ) break; // allow one pair is clipped, one pair is alu_read. More reads considered
if (read_status == "skip" or !QC_insert_read_qual(record)) continue;
/////if (!qname_match(record, "FCD1YM9ACXX:2:1114:6796:44598#GCTTAATG")) continue;
bool s1 = has_soft_last(record, CLIP_BP); // endPos
bool s2 = has_soft_first(record, CLIP_BP); // beginPos
if (s1 and s2) continue;
if (!s1 and !s2) continue;
if (!aluclip_RC_match(record, s1, s2) ) continue; // RC flag should be right if it is alu mate
int beginPos = record.beginPos;
int endPos = beginPos + getAlignmentLengthInRef(record);
int _cliplen;
seqan::CharString _qualSeq;
bool use_this_read = false;
if ( s1 and endPos >= regionBegin0 and endPos < regionEnd0 )
use_this_read = trim_clip_soft_last(record, _cliplen, _qualSeq, CLIP_Q);
if ( s2 and beginPos >= regionBegin0 and beginPos < regionEnd0 )
use_this_read = trim_clip_soft_first(record, _cliplen, _qualSeq, CLIP_Q);
if ( abs(_cliplen) < CLIP_BP or !use_this_read) continue;
string output_direct = s1 ? "L " : "R ";
int output_pos = s1 ? endPos : beginPos;
int output_pos_division = round_by_division(output_pos, 10);
if (record.rID != record.rNextId or abs(record.tLen) >= DISCORDANT_LEN) {
fout << ss_header.str() << output_direct << output_pos << " " << record.qName << " " << get_cigar(record) << " mate\n";
add3key(confidentPos, output_pos_division);
continue;
}
if ( confidentPos.find(output_pos_division) != confidentPos.end() ) {
fout << ss_header.str() << output_direct << output_pos << " " << record.qName << " " << get_cigar(record) << " previous\n";
continue;
}
string alu_type;
float sim_rate;
seqan::CharString clipped;
//cout << "cliplen " << _cliplen << " " << length(_qualSeq) << " " << get_cigar(record) << endl;
if ( s1 ) clipped = infix(_qualSeq, length(_qualSeq) - abs(_cliplen), length(_qualSeq));
else clipped = infix(_qualSeq, 0, abs(_cliplen));
alu_type = align_alu_cons_call(clipped, alucons_fh, sim_rate, 0.8, true);
if (!alu_type.empty()) {
add3key(confidentPos, output_pos_division);
fout << ss_header.str() << output_direct << output_pos << " " << record.qName << " " << get_cigar(record) << " aligned\n";
} else {
fout << ss_header.str() << output_direct << output_pos << " " << record.qName << " " << get_cigar(record) << " false\n";
}
}
}
fin.close();
fout.close();
return true;
}
// for each insertion region, combine reads from different pns
void combine_clipreads_by_pos(std::set<string> &pns_used, string path0, string path1) {
map< pair<string, string>, vector<string> > regionPos_lines;
for (std::set<string>::iterator pi = pns_used.begin(); pi != pns_used.end(); pi ++ ) {
string file_input_clip = path0 + *pi;
ifstream fin(file_input_clip.c_str());
if (!fin) {
cout << "error " << file_input_clip << " missing \n";
continue;
}
stringstream ss;
string line, pn, regionBegin, regionEnd;
getline(fin, line); // read header
while ( getline(fin, line) ) {
string flag = line.substr(line.size() - 5, line.size());
if ( flag == "false" ) continue; // false clip reads
ss.clear(); ss.str(line);
ss >> pn >> regionBegin >> regionEnd;
regionPos_lines[make_pair(regionBegin, regionEnd)].push_back(line);
}
fin.close();
}
map< pair<string, string>, vector<string> >::iterator ri;
for (ri = regionPos_lines.begin(); ri != regionPos_lines.end(); ri ++) {
string file_output_clip = path1 + (ri->first).first + "_" + (ri->first).second;
ofstream fout(file_output_clip.c_str() );
for (vector<string>::iterator ii = (ri->second).begin(); ii != (ri->second).end(); ii++)
fout << *ii << endl;
fout.close();
}
}
string pos_to_fn( pair<int, int> ps) {
return int_to_string(ps.first) + "_" + int_to_string(ps.second);
}
bool nonempty_files_sorted(string & path1, std::set < pair<int, int> > & regionPos_set) {
DIR *d;
struct dirent *dir;
d = opendir(path1.c_str());
string fn;
int regionBegin, regionEnd;
string s_regionBegin, s_regionEnd;
if (d) {
while ( (dir = readdir(d))!=NULL ) {
fn = dir->d_name;
if (fn[0] == '.' or check_file_size( path1 + fn)<=0 ) continue;
split_by_sep(fn, s_regionBegin, s_regionEnd, '_');
seqan::lexicalCast2(regionBegin, s_regionBegin);
seqan::lexicalCast2(regionEnd, s_regionEnd);
regionPos_set.insert( make_pair(regionBegin, regionEnd));
}
closedir(d);
return true;
}
return false;
}
void addcnt_if_pos_match( map <int, float> & clipLeft_cnt, int pos, int match_offset, float inc_val) {
int match_key = 0;
for ( map <int, float>::iterator mi = clipLeft_cnt.begin(); mi != clipLeft_cnt.end(); mi ++) {
if ( abs(mi->first - pos) <= match_offset ) {
match_offset = abs(mi->first - pos);
match_key = mi->first;
}
}
if ( match_key == 0) {
addKey(clipLeft_cnt, pos, inc_val);
} else {
if (match_key > pos) { // previously add clipRight
float cnt = clipLeft_cnt[match_key] + inc_val;
clipLeft_cnt.erase(match_key);
clipLeft_cnt[pos] = cnt;
// cout << "offset " << match_key << " " << pos << endl;
} else {
addKey(clipLeft_cnt, match_key, inc_val);
}
}
}
void region_pos_vote( string fn, int & cl1, int & cl2, int & cr1, int & cr2 ) {
const float MIN_VOTE_FREQ = 0.4;
const int MIN_PN_CLIP_CNT = 2; // otherwise ignore this pn
cl1 = 0; cl2 = 0; cr1 = 0; cr2 = 0;
ifstream fin(fn.c_str());
assert(fin);
stringstream ss;
string line, pn;
char left_right;
int region_begin, region_end, clipPos;
map <string, vector <pair<char, int> > > pn_splitori_pos;
while ( getline(fin, line)) {
ss.clear(); ss.str( line );
ss >> pn >> region_begin >> region_end >> left_right >> clipPos;
pn_splitori_pos[pn].push_back( make_pair( left_right, clipPos) );
}
fin.close();
vector < int > cls, crs;
map <string, vector <pair<char, int> > >::iterator psi;
vector <pair<char, int> >::iterator si;
for ( psi = pn_splitori_pos.begin(); psi != pn_splitori_pos.end(); psi++) {
int pl1 = 0, pl2 = 0, pr1 = 0, pr2 = 0;
vector<int> pls, prs;
for (si = (psi->second).begin(); si != (psi->second).end(); si ++ ) {
if ( (*si).first == 'L') pls.push_back( (*si).second );
else prs.push_back( (*si).second );
}
int fl1, fl2;
if ( (int)pls.size() >= MIN_PN_CLIP_CNT)
major_two_keys(pls, pl1, pl2, fl1, fl2, CLIP_BP_LEFT, MIN_VOTE_FREQ - 0.1); // each pn 2 votes
int fr1, fr2;
if ( (int)prs.size() >= MIN_PN_CLIP_CNT)
major_two_keys(prs, pr1, pr2, fr1, fr2, CLIP_BP_LEFT, MIN_VOTE_FREQ - 0.1);
if ( fl1 <= 1 and fr1 <= 1 and abs(pl1 - pr1) > MAX_POS_DIF ) {
pl1 = 0; pr1 = 0;
}
if (pl1 > 0) {
cls.push_back(pl1);
if (pl2 > 0) cls.push_back(pl2);
else cls.push_back(pl1);
}
if (pr1 > 0) {
crs.push_back(pr1);
if (pr2 > 0) crs.push_back(pr2);
else crs.push_back(pr1);
}
}
int fc1, fc2;
major_two_keys(cls, cl1, cl2, fc1, fc2, CLIP_BP_LEFT, MIN_VOTE_FREQ);
if ( cl1 and fc1 <= 2 and fc1 / (float) cls.size() < 0.99 ) cl1 = 0;
if ( fc2 <= 2) cl2 = 0;
major_two_keys(crs, cr1, cr2, fc1, fc2, CLIP_BP_LEFT, MIN_VOTE_FREQ);
if ( cr1 and fc1 <= 2 and fc1 / (float) crs.size() < 0.99 ) cr1 = 0;
////////if ( fc1 <= 2 ) cr1 = 0;
if ( fc2 <= 2 ) cr2 = 0;
//if (!cls.empty() and !crs.empty())
// cout << "##1 " << region_begin << " " << cls.size() << " " << crs.size() << " " << cl1 << " " << cr1 << endl;
}
string get_pos_pair(int cl1, int cl2, int cr1, int cr2){
if ( !cl1 and !cr1) return "";
std::set <int> cms;
cms.insert(0);
cms.insert(cl1);
cms.insert(cl2);
cms.insert(cr1);
cms.insert(cr2);
cms.erase(0);
std::set <int>::iterator ci = cms.begin();
stringstream ss;
int pre_pos = *ci;
ci++;
for (; ci != cms.end(); ci++) {
if ( abs(*ci - pre_pos) < MAX_POS_DIF ) {
pre_pos = (*ci + pre_pos)/2;
} else {
ss << " " << pre_pos;
pre_pos = *ci;
}
}
ss << " " << pre_pos;
return ss.str();
}
void regions_pos_vote( string path1, std::set < pair<int, int> > & regionPos_set, string fn_output) {
ofstream fout(fn_output.c_str());
fout << "region_begin region_end clipMid\n";
for (std::set < pair <int, int> >::iterator ri = regionPos_set.begin(); ri != regionPos_set.end(); ri++ ) {
int cl1, cl2, cr1, cr2;
region_pos_vote(path1 + pos_to_fn(*ri), cl1, cl2, cr1, cr2) ; // at least two reads to vote for this position
// if ( cl1 + cl2 + cr1 + cr2 > 0 )
// cout << pos_to_fn(*ri) << " " << cl1 << " " << cl2 << " " << cr1 << " " << cr2 << endl;
string cs = get_pos_pair(cl1, cl2, cr1, cr2);
if ( cs.size() > 3 )
fout << (*ri).first << " " << (*ri).second << cs << endl;
}
}
void neighbor_pos_filename(std::set < pair<int,int> > & regionPos_set, std::set <pair <int, int> > & fns, int clipLeft) {
const int FLANK_REGION_LEN = 80;
std::set < pair<int,int> >::iterator ri;
ri = regionPos_set.find( * (fns.begin()) );
while ( ri != regionPos_set.begin() and (*ri).first + FLANK_REGION_LEN >= clipLeft)
fns.insert( *ri-- );
ri = regionPos_set.find( * (fns.rbegin()) );
while ( ri !=regionPos_set.end() and (*ri).second - FLANK_REGION_LEN <= clipLeft )
fns.insert( *ri++ );
}
int approximate_pos_pns(string path1, string fn_input, string fn_output, std::set < pair<int,int> > & regionPos_set, int pos_dif){
ifstream fin;
fin.open(fn_input.c_str());
assert(fin);
map< int, std::set <pair<int, int> > > clipLeft_fn0;
map< int, std::set <pair<int, int> > > clipLeft_fn;
stringstream ss;
string line;
int region_begin, region_end, pos2, pos1;
getline(fin,line);
while (getline(fin,line)) {
ss.clear(); ss.str( line );
ss >> region_begin >> region_end;
while ( ss >> pos2) // in each region, several potential breakpoints
clipLeft_fn0[pos2].insert( make_pair(region_begin, region_end)) ;
}
fin.close();
if (clipLeft_fn0.empty() ) return 0;
map< int, std::set <pair<int, int> > >::iterator ci = clipLeft_fn0.begin();
int pos0 = ci->first;
clipLeft_fn[pos0].swap(ci->second);
ci ++;
for (; ci != clipLeft_fn0.end(); ci++) {
pos2 = ci->first;
if ( pos2 - pos0 <= pos_dif ) {
pos1 = (pos2 + pos0) / 2;
clipLeft_fn[pos1].swap(clipLeft_fn[pos0]);
clipLeft_fn.erase(pos0);
pos2 = pos1;
for ( std::set <pair<int, int> >::iterator fi = (ci->second).begin(); fi != (ci->second).end(); fi++ )
clipLeft_fn[pos2].insert(*fi);
} else {
clipLeft_fn[pos2].swap(ci->second);
}
pos0 = pos2;
}
//cout << "size0 " << clipLeft_fn0.size() << " " << clipLeft_fn.size() << endl;
clipLeft_fn0.clear();
ofstream fout(fn_output.c_str());
fout << "clipMid pn(count)\n";
for (map< int, std::set < pair<int, int> > >::iterator pi = clipLeft_fn.begin(); pi != clipLeft_fn.end(); pi++ ) {
int clipLeft = pi->first;
map <string, std::set<string> > pn_qName;
std::set < pair<int, int> > fns;
size_t fns_size = (pi->second).size() ; // num of regions voted for this clip pos
fns.swap(pi->second);
neighbor_pos_filename(regionPos_set, fns, clipLeft); // add more regions to fns
assert ( fns.size() >= fns_size );
for ( std::set < pair<int, int> >::iterator fi = fns.begin(); fi != fns.end(); fi++ ) {
fin.open( (path1 + pos_to_fn(*fi) ).c_str());
string line, pn, qName, tmp1, tmp2, tmp3;
while (getline(fin, line)) {
ss.clear(); ss.str( line );
int _p;
ss >> pn >> tmp1 >> tmp2 >> tmp3 >> _p >> qName;
if ( abs( _p - clipLeft ) <= MAX_POS_DIF)
pn_qName[pn].insert(qName); // some reads appear more than once
}
fin.close();
}
if ( pn_qName.empty() )
continue;
fout << clipLeft; // clipRight unknown for now
for (map <string, std::set<string> >::iterator pi = pn_qName.begin(); pi != pn_qName.end(); pi++)
fout << " " << pi->first << "," << (pi->second).size() ;
fout << endl;
}
fout.close();
return 1;
}
void vote_clipPos(int clip0, int & clipl, int & clipr, string path0, map <string, int> & pn_cnt, float freq_th){
clipl = clipr = 0;
vector <int> clipls, cliprs;
for (map <string, int>::iterator pi = pn_cnt.begin(); pi != pn_cnt.end(); pi++ ) {
ifstream fin( (path0 + pi->first).c_str() );
assert(fin);
string line, tmpv0;
char left_right;
int adj_clipPos, tmpv1, tmpv2;
stringstream ss;
getline(fin, line);
while ( getline(fin, line) ) {
ss.clear(); ss.str( line );
ss >> tmpv0 >> tmpv1 >> tmpv2 >> left_right >> adj_clipPos;
//cout << clip0 << " " << tmpv2 << " " << adj_clipPos << endl;
if ( clip0 + 300 < tmpv2 ) break;
if ( abs(clip0 - adj_clipPos) <= MAX_POS_DIF ) {
pi->second += 1;
if ( left_right == 'L' ) clipls.push_back(adj_clipPos);
else if ( left_right == 'R' ) cliprs.push_back(adj_clipPos);
}
}
fin.close();
}
//cout << "vote " << clip0 << " " << clipls.size() << " " << cliprs.size() << endl;
////if ( major_key_freq(clipls, clipl, MAX_POS_DIF, freq_th, 3) >= 2 // eg: freq_th = 0.7, at least 3 clip read exact the same
major_key_freq(clipls, clipl, MAX_POS_DIF, freq_th, 3); // eg: freq_th = 0.7, at least 3 clip read exact the same
major_key_freq(cliprs, clipr, MAX_POS_DIF, freq_th, 3);
}
void exact_pos_pns(string path0, string fn_input, string fn_output, float freq_th ) {
ifstream fin(fn_input.c_str());
assert(fin);
ofstream fout(fn_output.c_str());
stringstream ss;
string line, pn, cnt, tmpv;
int clip0, clipl, clipr;
getline(fin,line);
fout << "clipLeft clipRight pn(count)\n";
while (getline(fin, line)) {
ss.clear(); ss.str(line);
ss >> clip0;
map <string, int> pn_cnt;
while ( ss >> tmpv ) {
split_by_sep (tmpv, pn, cnt, ',');
pn_cnt[pn] = 0;
}
int tmpcnt;
seqan::lexicalCast2(tmpcnt, cnt);
if (pn_cnt.size() <= 1 and tmpcnt <= 2 ) continue; // at least 2 reads to support
vote_clipPos(clip0, clipl, clipr, path0, pn_cnt, freq_th);
//cout << clip0 << " " << pn_cnt.size() << " " << clipl << " " << clipr << endl;
if (!clipl and !clipr ) continue;
fout << clipl << " " << clipr;
for (map <string, int>::iterator pi = pn_cnt.begin(); pi != pn_cnt.end(); pi++ )
fout << " " << pi->first;
fout << endl;
}
fin.close();
fout.close();
}
string classify_read(map<int, int> & confidentPos, seqan::BamAlignmentRecord & record, int clipLeft, int clipRight, PAR_DELETE0 & pars,
int & clipLen, seqan::CharString & clipSeq) {
assert (clipLeft > 0 );
assert (clipRight > 0 );
bool s1 = has_soft_last(record, CLIP_BP);
bool s2 = has_soft_first(record, CLIP_BP);
if (s1 and s2 ) return "bad_read"; // ignore this pair
if ( (s1 or s2) and !aluclip_RC_match(record, s1, s2)) return "bad_read";
int thisEnd = record.beginPos + (int)getAlignmentLengthInRef(record);
int output_pos = s1 ? thisEnd : record.beginPos;
int output_pos_division = round_by_division(output_pos, 3); // very strict
if (record.rID != record.rNextId or abs(record.tLen) >= DISCORDANT_LEN) {
if ( ( s1 and trim_clip_soft_last(record, clipLen, clipSeq, 10) ) or
( s2 and trim_clip_soft_first(record, clipLen, clipSeq, 10 )) ) {
add3key(confidentPos, output_pos_division);
return "alu_clip";
}
if ( !hasFlagRC(record) and thisEnd < clipLeft + MAX_POS_DIF ) return "alu_read"; // this read is left_read
if ( hasFlagRC(record) and record.beginPos > clipRight - MAX_POS_DIF ) return "alu_read";
return "bad_read";
}
bool read_is_left = left_read(record);
// otherwise only look at proper mapped reads
if ( hasFlagRC(record) == hasFlagNextRC(record) or read_is_left == hasFlagRC(record) ) return "bad_read";
seqan::CharString clipped;
if ( s1 and trim_clip_soft_last(record, clipLen, clipSeq, pars.CLIP_Q) and abs( output_pos - clipLeft) < MAX_POS_DIF ) {
if ( confidentPos.find(output_pos_division) != confidentPos.end() ) return "clip_read";
clipped = infix(clipSeq, length(clipSeq) - abs(clipLen), length(clipSeq));
}
if ( s2 and trim_clip_soft_first(record, clipLen, clipSeq, pars.CLIP_Q) and abs( output_pos - clipRight ) < MAX_POS_DIF ) {
if ( confidentPos.find(output_pos_division) != confidentPos.end() ) return "clip_read";
clipped = infix(clipSeq, 0, abs(clipLen));
}
if ( length(clipped) > 10 ) {
/// no need to re-align to alu consensus, one of them has been aligned
///string alu_type = align_alu_cons_call(clipped, alucons_fh, sim_rate, 0.8, true);
///if (!alu_type.empty()) {
add3key(confidentPos, output_pos_division);
return "clip_read";
}
int trimb, trime;
if (!get_trim_length(record, trimb, trime, pars.CLIP_Q)) return "bad_read";
if (trimb + trime < (int) length(record.seq) - CLIP_BP and count_non_match(record) <= 5 and
record.beginPos + trimb <= min(clipLeft, clipRight) - CLIP_BP and thisEnd - trime >= max(clipLeft, clipRight) + CLIP_BP )
return "skip_read"; // after trimming, fewer reads are considered as skip_read, thus less skip_clip conflict, more clip read
int pair_begin = read_is_left ? record.beginPos : record.pNext;
int pair_end = pair_begin + abs(record.tLen);
if ( pair_begin < clipLeft - CLIP_BP and pair_end > clipLeft + CLIP_BP) {
if ( (read_is_left and s2 ) or (!read_is_left and s1 ) )
return "useless";
return "unknow_read";
}
return "useless";
}
//// fout => clip reads, fout2 => skip reads,
int tmp0_reads(string chrn, int clipLeft, int clipRight, BamFileHandler *bam_fh, ostream & fout, ostream & fout2, map < int, vector<ALUREAD_INFO> > & rid_alureads,
map<string, int> & rg_to_idx, PAR_DELETE0 & pars,bool test_print = false, int exact_left = 0, int exact_right = 0) {
int region_begin = clipLeft - ALU_FLANK; // no need for larger flank region, if i only want to build consensus
int region_end = clipRight + ALU_FLANK;
if (! bam_fh->jump_to_region(chrn, region_begin, region_end))
return 0;
map < seqan::CharString, string > rg_str;
map < seqan::CharString, string > qname_iread;
map < seqan::CharString, string > qname_clipInfo;
map < seqan::CharString, string > qname_skipInfo;
map<int, int> confidentPos;
while ( true ) {
seqan::BamAlignmentRecord record;
string read_status = bam_fh->fetch_a_read(chrn, region_begin, region_end, record);
//cout << record.tLen << " " << hasFlagRC(record) << " " ;debug_print_read(record);
if (read_status == "stop" ) break;
if (read_status == "skip" or !QC_insert_read_qual(record)) continue;
string iread2 = "";
get_mapVal(qname_iread, record.qName, iread2);
if ( !iread2.empty() and iread2.substr(0,3) == "alu") continue;
if ( iread2 == "bad_read") continue;
int clipLen;
seqan::CharString clipSeq;
/// could return: bad_read, [alu_read, alu_clip, clip_read, skip_read, unknow_read]
////////if ( abs (record.beginPos - 9834353 ) >= 30 ) continue;
string iread = classify_read(confidentPos, record, clipLeft, clipRight, pars, clipLen, clipSeq);
//// mate mapped to Alu. alu_clip read is written in both *.aludb and *.clip file
if ( iread == "alu_read" or iread == "alu_clip") {
int this_pos;
if ( hasFlagRC(record)) this_pos = -record.beginPos; // read is right_read, alu is left_read
else this_pos = record.beginPos;
ALUREAD_INFO one_aluRead = ALUREAD_INFO(record.qName, this_pos, clipLeft, record.pNext, length(record.seq), hasFlagRC(record)==hasFlagNextRC(record) );
rid_alureads[record.rNextId].push_back(one_aluRead);
}
bool conflict = false;
if ( (iread == "clip_read" and iread2 == "skip_read") or (iread == "skip_read" and iread2 == "clip_read") )
conflict = true;
bool write_unknow = false;
if ( iread == "unknow_read" and ( iread2.empty() or iread2 == "unknow_read") )
write_unknow = true;
if ( conflict or write_unknow) {
qname_iread[record.qName] = "unknow_read";
int unknow_exact = 0; // unknow for now, may belong to clip reads afterwards
if (has_soft_first(record, CLIP_BP)) unknow_exact = record.beginPos;
else if (has_soft_last(record, CLIP_BP)) unknow_exact = - ( record.beginPos + (int)getAlignmentLengthInRef(record));
if ( rg_str.find(record.qName) == rg_str.end() or unknow_exact != 0) {
stringstream rg_ss;
int rgIdx = get_rgIdx(rg_to_idx, record);
rg_ss << rgIdx << ":" << abs(record.tLen) << ":" << min(record.beginPos, record.pNext) << ":" << unknow_exact;
rg_str[record.qName] = rg_ss.str();
}
continue;
}
if (iread == "clip_read" or iread == "alu_clip") {
qname_iread[record.qName] = iread;
stringstream ssf;
ssf << clipLen << " ";
int rgIdx = get_rgIdx(rg_to_idx, record);
int _endPos = record.beginPos + (int)getAlignmentLengthInRef(record);
if (record.cigar[0].operation == 'S') {
ssf << record.beginPos << " " << get_cigar(record) << " " << rgIdx << " " << _endPos << " " ;
} else {
ssf << _endPos << " " << get_cigar(record) << " " << rgIdx << " " << record.beginPos << " " ;
}
if ( record.rID == record.rNextId ) ssf << record.tLen;
else ssf << DISCORDANT_LEN + 999 ;
qname_clipInfo[record.qName] = ssf.str();
continue;
}
if (iread == "skip_read") {
qname_iread[record.qName] = iread;
stringstream ssf;
ssf << record.beginPos << " " << record.beginPos + (int)getAlignmentLengthInRef(record);
qname_skipInfo[record.qName] = ssf.str();
continue;
}
}
if (!test_print) {
vector<string> str_unknowreads;
bool flag = false;
for (map < seqan::CharString, string >::iterator qi = qname_iread.begin(); qi != qname_iread.end(); qi++ ) {
if ( qi->second == "skip_read") {
fout2 << clipLeft << " " << qname_skipInfo[ qi->first] << " " << qi->first << endl;
} else if ( qi->second == "clip_read" or qi->second == "alu_clip") {
fout << clipLeft << " " << qname_clipInfo[ qi->first] << " " << qi->first << endl;
flag = true;
} else if ( qi->second == "unknow_read") {
str_unknowreads.push_back( rg_str[qi->first] );
}
}
///// at least one clip_read
if (flag and !str_unknowreads.empty()) {
fout2 << clipLeft << " UR " << str_unknowreads.size() ; // maybe clip reads. But definitely not alu reads
for ( vector< string > ::iterator si = str_unknowreads.begin(); si != str_unknowreads.end(); si++ )
fout2 << " " << *si ;
fout2 << endl;
}
} else { // print unknown reads
std::set <seqan::CharString> uds;
for (map < seqan::CharString, string >::iterator qi = qname_iread.begin(); qi != qname_iread.end(); qi++ )
if ( qi->second == "unknow_read")
uds.insert(qi->first);
cout << "unknown reads " << uds.size() << endl;
bam_fh->jump_to_region(chrn, region_begin, region_end);
int tmpmax = max (exact_left, exact_right);
int tmpmin = (!exact_left or !exact_right) ? tmpmax : min(exact_left, exact_right);
while ( true ) {
seqan::BamAlignmentRecord record;
string read_status = bam_fh->fetch_a_read(chrn, region_begin, region_end, record);
if (read_status == "stop" ) break;
if (read_status == "skip" or !QC_insert_read_qual(record)) continue;
if ( uds.find(record.qName) != uds.end() ) {
int pair_begin = min(record.beginPos, record.pNext);
if ( pair_begin < tmpmin - CLIP_BP_MID and pair_begin + abs(record.tLen)> tmpmax + CLIP_BP_MID)
debug_print_read(record);
}
}
}
return 1;
}
// too slow, not used any more
void alumate_reads1(BamFileHandler *bam_fh, string file_db, ofstream & fout, int query_rid, vector<ALUREAD_INFO> & aluinfos) {
map <string, string> qnames;
if (!file_db.empty()) {
ifstream fin(file_db.c_str());
assert(fin);
stringstream ss;
string line, qname, tmpv, alu_type;
getline(fin,line);
while (getline(fin, line)) {