-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPairedData.cpp
More file actions
executable file
·9997 lines (8648 loc) · 293 KB
/
PairedData.cpp
File metadata and controls
executable file
·9997 lines (8648 loc) · 293 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
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
/*
* PairedData.cpp
* Span1
*
* Created by Chip Stewart on 7/24/08.
* Copyright 2008 Boston College. All rights reserved.
*
*/
#include "PairedData.h"
//------------------------------------------------------------------------------
// function to enforce p0 and p1 to be bound within pos0 and pos1
//------------------------------------------------------------------------------
bool boundlimit(int p0, int p1, int pos0, int pos1) {
bool within= ((p0>=pos0)&&(p1<=pos1));
return within;
/*
if ((p1<pos0)|(p0>pos1)) {
return false;
}
// shift to region pos0 to pos1
p0=p0-pos0;
p1=p1-pos0;
if (p0<0) { p0=0;}
if (p1>(pos1-pos0)) {p1=pos1-pos0;}
return true;
*/
}
//------------------------------------------------------------------------------
// readmap (basic info for one read alignment)
//------------------------------------------------------------------------------
C_readmap::C_readmap() // Constructor
{
pos = 0;
anchor = 0;
len = 0;
sense = 0;
q=0;
mm=0;
q2=0;
nmap=0;
mob=" ";
}
C_readmap::C_readmap(const C_readmap ©in) // Copy constructor to handle pass by value.
{
pos = copyin.pos;
anchor= copyin.anchor;
len = copyin.len;
sense = copyin.sense;
q = copyin.q;
mm = copyin.mm;
q2=copyin.q2;
nmap=copyin.nmap;
mob=copyin.mob;
}
C_readmap::C_readmap(unsigned int pos1, unsigned short anchor1,unsigned short len1, char sense1, char q1, char mm1) {
pos = pos1;
anchor = anchor1;
len = len1;
sense = sense1;
q = q1;
mm = mm1;
}
ostream &operator<<(ostream &output, const C_readmap & x)
{
output << x.anchor << "\t" << x.pos << "\t" << x.len
<< "\t" << x.sense <<"\t" << int(x.q) <<"\t" << int(x.mm) ;
return output;
}
C_readmap& C_readmap::operator=(const C_readmap &rhs)
{
this->pos = rhs.pos;
this->anchor = rhs.anchor;
this->len = rhs.len;
this->sense = rhs.sense;
this->q = rhs.q;
this->mm = rhs.mm;
this->nmap = rhs.nmap;
this->mob = rhs.mob;
return *this;
}
int C_readmap::operator==(const C_readmap &rhs) const
{
if( this->pos != rhs.pos) return 0;
if( this->anchor != rhs.anchor) return 0;
if( this->len != rhs.len) return 0;
if( this->sense != rhs.sense) return 0;
if( this->q != rhs.q) return 0;
if( this->mm != rhs.mm) return 0;
if( this->q2 != rhs.q2) return 0;
if( this->nmap != rhs.nmap) return 0;
return 1;
}
// This function is required for built-in STL list functions like sort
int C_readmap::operator<(const C_readmap &rhs) const
{
if( this->anchor < rhs.anchor ) return 1;
if( this->anchor == rhs.anchor && this->pos < rhs.pos ) return 1;
if( this->anchor == rhs.anchor && this->pos == rhs.pos
&& this->len < rhs.len) return 1;
if( this->anchor == rhs.anchor && this->pos == rhs.pos
&& this->len == rhs.len && this->sense < rhs.sense) return 1;
if( this->anchor == rhs.anchor && this->pos == rhs.pos && this->len == rhs.len
&& this->sense == rhs.sense && this->q < rhs.q) return 1;
if( this->anchor == rhs.anchor && this->pos == rhs.pos && this->len == rhs.len
&& this->sense == rhs.sense && this->q == rhs.q && this->mm < rhs.mm) return 1;
if( this->anchor == rhs.anchor && this->pos == rhs.pos && this->len == rhs.len
&& this->sense == rhs.sense && this->q == rhs.q && this->mm == rhs.mm && this->nmap < rhs.nmap) return 1;
return 0;
}
//------------------------------------------------------------------------------
// readmaps (vector of readmap objects)
//------------------------------------------------------------------------------
C_readmaps::C_readmaps() {
Nalign = 0;
element = 0;
}
ostream &operator<<(ostream &output, const C_readmaps & rhs)
{
int N=rhs.align.size();
output << N << "\t " << rhs.element << endl;
for (int i=0; i<N; i++) {
output << "\t" << rhs.align[i] << endl;
}
return output;
}
C_readmaps::C_readmaps(const C_readmaps ©in)
{
align = copyin.align;
Nalign = copyin.Nalign;
element = copyin.element;
}
C_readmaps& C_readmaps::operator=(const C_readmaps &rhs)
{
this->align = rhs.align;
this->Nalign = rhs.Nalign;
this->element = rhs.element;
return *this;
}
//------------------------------------------------------------------------------
// pairedread (array of two readmaps)
//------------------------------------------------------------------------------
C_pairedread::C_pairedread() { // constructor
Nend=0;
ReadGroupCode=0;
Name="";
}
ostream &operator<<(ostream &output, const C_pairedread & rhs)
{
output << rhs.read[0] << endl;
output << rhs.read[1] << endl;
return output;
}
//------------------------------------------------------------------------------
// anchorinfo
//------------------------------------------------------------------------------
C_anchorinfo::C_anchorinfo() { // constructor
source = "";
use.clear();
}
//------------------------------------------------------------------------------
// anchorinfo copy constructor
//------------------------------------------------------------------------------
C_anchorinfo::C_anchorinfo(const C_anchorinfo & rhs) { // constructor
source = rhs.source;
L=rhs.L;
use=rhs.use;
element=rhs.element;
names=rhs.names;
}
C_anchorinfo::C_anchorinfo(string & anchorfile) { // constructor
// anchor file patterns
string patternHead("number.+\\s+(\\d+)$");
//string patternLine("(\\d+)\\.\\s+(\\S+)\\s+(\\d+)\\s*$");
string patternLine("^\\s*(\\d+)\\.\\s+(\\w+)\\s+(\\d+)\\s*$");
source=anchorfile;
fstream file1;
file1.open(anchorfile.c_str(), ios::in);
if (!file1) {
cerr << "Unable to open anchor file: " << anchorfile << endl;
exit(1);
}
string line;
string match,match2,match3;
int N=0;
while (getline(file1, line)) {
//================
if (RE2::FullMatch(line.c_str(),patternHead.c_str(),&match) ) {
N = string2Int(match);
} else if ( RE2::FullMatch(line.c_str(),patternLine.c_str(),&match,&match2,&match3) ) {
string cn1 = match2;
int L1 = string2Int(match3);
L[cn1]=L1;
names.push_back(cn1);
}
}
use.resize(N,true);
}
C_anchorinfo::C_anchorinfo(char t) { // constructor for build 36.1
source = "";
if (t==0) {
source = "AB";
for (int i = 0; i<25; i++) {
names.push_back(ABCHROMOSOMENAME[i]);
L[ABCHROMOSOMENAME[i]]=ABCHROMOSOMELENGTH[i];
}
cout << "\t ... using default ncbi build 36.1 anchor info" << endl;
use.resize(25,true);
}
}
//------------------------------------------------------------------------------
// Mosaik anchor info filler
//------------------------------------------------------------------------------
C_anchorinfo::C_anchorinfo(Mosaik::CAlignmentReader & ar1) {
source = "MSK";
// retrieve the reference sequence data
vector<Mosaik::ReferenceSequence> ref1=ar1.GetReferenceData();
int Nref = ref1.size();
// reset
names.clear();
L.clear();
use.clear();
element.clear();
// allocate names and use
names.resize(Nref,"");
use.resize(Nref,true);
element.resize(Nref,0);
// loop over ref
for (int id=0; id<Nref; id++) {
names[id]=ref1[id].Name;
L[names[id]]=ref1[id].NumBases;
// turn off SV detection from "NT_" (random) contigs
size_t found=names[id].find("NT_");
if (found==0) use[id]=false;
found=names[id].find("NC_");
if (found==0) use[id]=false;
found=names[id].find("GL0",0,3);
if (found==0) use[id]=false;
}
if (!anchorElements()) {
cerr << " anchorelements not found " << endl;
}
}
/*
//------------------------------------------------------------------------------
// Mosaik anchor info filler
//------------------------------------------------------------------------------
C_anchorinfo::C_anchorinfo(BamReader & br1) {
source = "BAM";
string ht=br1.GetHeaderText();
// retrieve the reference sequence data
vector<Mosaik::ReferenceSequence> ref1=GetAnchorInfoFromBamHeader(ht);
int Nref = ref1.size();
// reset
names.clear();
L.clear();
use.clear();
element.clear();
// allocate names and use
names.resize(Nref,"");
use.resize(Nref,true);
element.resize(Nref,0);
// loop over ref
for (int id=0; id<Nref; id++) {
names[id]=ref1[id].Name;
L[names[id]]=ref1[id].NumBases;
}
if (!anchorElements()) {
cerr << " anchorelements not found " << endl;
}
}
vector<Mosaik::ReferenceSequence> C_anchorinfo::GetAnchorInfoFromBamHeader(string & ht)
{
//@SQ SN:1 LN:247249719
vector<Mosaik::ReferenceSequence> References;
string line,thing;
stringstream stream1(ht); //stringstream::out | stringstream::in);
//stream1 << ht;
while( getline(stream1, line) ) {
if (line.find("@SQ")!=string::npos) {
cout << line << "\n";
Mosaik::ReferenceSequence RS1;
stringstream stream2(line);
while( getline(stream2,thing,'\t') ) {
string token="SN:";
size_t f1=thing.find(token);
if (f1!=string::npos) {
RS1.Name=thing.substr(f1+token.length());
}
token="LN:";
f1=thing.find(token);
if (f1!=string::npos) {
string nb=thing.substr(f1+token.length());
RS1.NumBases=string2Int(nb);
}
}
References.push_back(RS1);
}
}
return References;
}
*/
int C_anchorinfo::operator==(const C_anchorinfo &rhs) const
{
if (this->L.size()!=rhs.L.size() ) return 0;
/* another day
for(size_t i=0; i<names.size(); ++i) {
unsigned int L1 = L[names[i]];
if (rhs.L.count(names[i])>0) return 0;
if (rhs.L[names[i]] != L1) return 0;
}
*/
return 1;
}
//------------------------------------------------------------------------------
// Bam anchor info filler
//------------------------------------------------------------------------------
//C_anchorinfo::C_anchorinfo(BamReader & ar1) {
C_anchorinfo::C_anchorinfo(BamMultiReader & ar1) {
source = "BAM";
names.clear();
L.clear();
use.clear();
element.clear();
int maxId = 0;
RefVector refSeq1=ar1.GetReferenceData();
int Nref = ar1.GetReferenceCount();
names.resize(Nref,"");
use.resize(Nref,0);
element.resize(Nref,0);
for (RefVector::const_iterator ri = refSeq1.begin();
ri != refSeq1.end(); ri++) {
// retrieve reference info
RefData rd = *ri;
// get member data
string refName = rd.RefName;
L[rd.RefName]=rd.RefLength;
names[maxId]=refName;
// turn off detection from contigs that begin with NT_, NC_ or GL0
size_t found1=refName.find("NT_",0,3);
size_t found2=refName.find("NC_",0,3);
size_t found3=refName.find("GL0",0,3);
use[maxId]=(found1!=0)&&(found2!=0)&&(found3!=0);
maxId++;
}
/*
if (!anchorElements()) {
cerr << " anchorelements not found " << endl;
}
for (int i=0; i<use.size(); i++) {
if (use[i]) printf("%d\t%d\t%s\t%d\n",i+1,use[i],names[i].c_str(),L[names[i]]);
}
*/
}
void C_anchorinfo::anchorlimit(string & allow) {
string patternAllowContigsRegex(allow);
// regex match
string name=" ";
for (size_t i = 0; i<names.size(); i++) {
name= names[i];
// skip this ContigName if not present in AllowContigs
if (! RE2::PartialMatch(name.c_str(),patternAllowContigsRegex.c_str())) {
use[i]=false;
}
}
}
bool C_anchorinfo::anchorElements() {
string e0[] = {"moblist_ALU","moblist_L1","moblist_SVA","moblist_ERV"};
vector<string> elements1(e0, e0 + 4);
return(anchorElements(elements1));
}
//------------------------------------------------------------------------------
// add list of reference elements to anchorinfo
//------------------------------------------------------------------------------
bool C_anchorinfo::anchorElements(vector<string> & elements1) {
// init
bool done=false;
string name=" ";
string elem=" ";
element.clear();
element.resize(names.size(),-1);
// loop over anchors
for (size_t i = 0; i<names.size(); i++) {
name= names[i].substr(0,10);
// loop over elements
for (size_t e = 0; e<elements1.size(); e++) {
elem= elements1[e].substr(0,10);
// set element index
if (name==elem) {
element[i]=e;
// do not analyze coverage within these elements
use[i]=false;
done=true;
break;
}
}
}
return done;
}
//------------------------------------------------------------------------------
// get minimum element anchor index - return 999 if no elements
//------------------------------------------------------------------------------
unsigned int C_anchorinfo::anchorMinElement() {
unsigned int a1;
for(a1=0; a1<element.size(); a1++) {
if (element[a1]>=0) {
return a1;
}
}
a1=999;
return a1;
}
//------------------------------------------------------------------------------
// get anchor index - return -1 if no anchor name found
//------------------------------------------------------------------------------
char C_anchorinfo::anchorIndex(string & name1){
unsigned int a1;
for(a1=0; a1<names.size(); a1++) {
if (names[a1].compare(name1)==0) {
return a1;
}
}
a1=-1;
return a1;
}
char anchorIndex(string &); // return anchor index given anchor name
ostream &operator<<(ostream &output, C_anchorinfo & a)
{
output << "number of anchors:\t" << a.names.size() << endl ;
for(size_t i=0; i<a.names.size(); ++i) {
string s = a.names[i];
unsigned int L = a.L[s];
output << "\t" << i <<".\t" << s << "\t" << L << endl; //" " << a.use[i] << endl ;
}
return output;
}
void C_anchorinfo::push_anchor (string & name1, unsigned int L1) {
names.push_back(name1);
L[name1]=L1;
}
void C_anchorinfo::printAnchorInfo(string & name) {
// format: optimized to loadFragments.m matlab script
// open output binary file. bomb if unable to open
fstream output(name.c_str(), ios::out);
if (!output) {
cerr << "Unable to open anchor info output file: " << name << endl;
return;
}
int V = 207;
output << "Anchor Information, version " << V << endl;
output << "\t #.\t Anchor\t Length\t use" << endl;
output << *this;
output << endl ;
output.close();
}
//------------------------------------------------------------------------------
// Library info
//------------------------------------------------------------------------------
C_libraryinfo::C_libraryinfo() // constructor default
{
LM=0;
tailcut=0;
LMlow=0;
LMhigh=0;
LR=0;
LRmin=0;
LRmax=0;
NPair=0;
NSingle=0;
NPairRedundant=0;
NSingleRedundant=0;
}
C_libraryinfo::C_libraryinfo(const C_libraryinfo & rhs) // copy constructor
{
LM=rhs.LM;
tailcut=rhs.tailcut;
LMlow=rhs.LMlow;
LMhigh=rhs.LMhigh;
LR=rhs.LR;
LRmin=rhs.LRmin;
LRmax=rhs.LR;
NPair=rhs.NPair;
NSingle=rhs.NSingle;
NPairRedundant=rhs.NPairRedundant;
NSingleRedundant=rhs.NSingleRedundant;
fragHist=rhs.fragHist;
readLengthHist=rhs.readLengthHist;
Info.MedianFragmentLength=rhs.Info.MedianFragmentLength;
Info.ReadGroupCode=rhs.Info.ReadGroupCode;
Info.SequencingTechnology=rhs.Info.SequencingTechnology;
Info.CenterName=rhs.Info.CenterName;
Info.Description=rhs.Info.Description;
Info.LibraryName=rhs.Info.LibraryName;
Info.PlatformUnit=rhs.Info.PlatformUnit;
Info.ReadGroupID=rhs.Info.ReadGroupID;
Info.SampleName=rhs.Info.SampleName;
}
ostream &operator<<(ostream &output, C_libraryinfo & lib)
{
output << "Read Group:\t" << lib.Info.ReadGroupCode;
output << "\t ID:\t" << lib.Info.ReadGroupID << endl ;
output << "\t "<< lib.Info.SequencingTechnology;
output << "\t "<< lib.Info.CenterName;
output << "\t "<< lib.Info.Description << endl;
output << "\t "<< lib.Info.LibraryName;
output << "\t "<< lib.Info.PlatformUnit;
output << "\t "<< lib.Info.SampleName;
output << "\t Insert:\t" << lib.Info.MedianFragmentLength;
output << "\t "<< lib.LM;
output << "\t "<< lib.LMlow;
output << "\t "<< lib.LMhigh << endl;
output << "\t Read:\t"<< lib.LR;
output << "\t "<< lib.LRmin;
output << "\t "<< lib.LRmax << endl;
output << "\t NPE:\t"<< lib.NPair;
output << "\t Redundnat:\t"<< lib.NPairRedundant << endl;
output << "\t NSE:\t"<< lib.NSingle;
output << "\t Redundnat:\t"<< lib.NSingleRedundant << endl;
return output;
}
//------------------------------------------------------------------------------
// Libraries
//------------------------------------------------------------------------------
C_libraries::C_libraries() // constructor
{
}
//------------------------------------------------------------------------------
// binary file input
//------------------------------------------------------------------------------
C_libraries::C_libraries(string & infilename) // load lib info
{
fstream input(infilename.c_str(), ios::in | ios::binary);
if (!input) {
cerr << "Unable to open library.span file: " << infilename << endl;
}
C_headerSpan h(input);
C_librarymap::iterator it;
C_libraryinfo lib1;
int N= h.N;
//----------------------------------------------------------------------------
// fixed length part of lib record
//----------------------------------------------------------------------------
for(int i=0; i<N; i++) {
input.read(reinterpret_cast< char *>(&lib1.Info.ReadGroupCode), sizeof(unsigned int));
input.read(reinterpret_cast< char *>(&lib1.Info.MedianFragmentLength), sizeof(unsigned int));
input.read(reinterpret_cast< char *>(&lib1.Info.SequencingTechnology), sizeof(unsigned short));
input.read(reinterpret_cast< char *>(&lib1.LM), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.LMlow), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.LMhigh), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.tailcut), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.LR), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.LRmin), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.LRmax), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.NPair), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.NSingle), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.NPairRedundant), sizeof(int));
input.read(reinterpret_cast< char *>(&lib1.NSingleRedundant), sizeof(int));
this->libmap[lib1.Info.ReadGroupCode]=lib1;
}
//----------------------------------------------------------------------------
// variable length part of lib record
//----------------------------------------------------------------------------
int nchar = 0;
// buffer for loading strings
char buff[512];
for ( it=this->libmap.begin() ; it != this->libmap.end(); it++ )
{
lib1 = (*it).second;
input.read(reinterpret_cast< char *>(&lib1.Info.ReadGroupCode), sizeof(unsigned int));
input.read(reinterpret_cast< char *>(&lib1.Info.MedianFragmentLength), sizeof(unsigned int));
// ReadGroupID
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
// convert c-style string buff to string contigName
buff[nchar]=0;
lib1.Info.ReadGroupID=buff;
// CenterName
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.Info.CenterName=buff;
// Description
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.Info.Description=buff;
// LibraryName
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.Info.LibraryName=buff;
// PlatformUnit
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.Info.PlatformUnit=buff;
// SampleName
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.Info.SampleName=buff;
// LF distribution
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.fragHist.title=buff;
input.read(reinterpret_cast< char *>(&lib1.fragHist.Ntot), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.mean), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.std), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.median), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.Nin), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.Nunder), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.Nover), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.Nbin), sizeof(int));
lib1.fragHist.xc.resize(lib1.fragHist.Nbin);
lib1.fragHist.n.resize(lib1.fragHist.Nbin);
lib1.fragHist.c.resize(lib1.fragHist.Nbin);
for (int i=0; i<lib1.fragHist.Nbin; i++) {
input.read(reinterpret_cast< char *>(&lib1.fragHist.xc[i]), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.n[i]), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.fragHist.c[i]), sizeof(double));
}
lib1.fragHist.dx=lib1.fragHist.xc[1]-lib1.fragHist.xc[0];
lib1.fragHist.xlow=lib1.fragHist.xc[0]-lib1.fragHist.dx/2.0;
lib1.fragHist.xhigh=lib1.fragHist.xc[lib1.fragHist.Nbin-1]+lib1.fragHist.dx/2.0;
lib1.fragHist.Finalize();
// LR distribution
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
lib1.readLengthHist.title=buff;
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.Ntot), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.mean), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.std), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.median), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.Nin), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.Nunder), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.Nover), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.Nbin), sizeof(int));
lib1.readLengthHist.xc.resize(lib1.readLengthHist.Nbin);
lib1.readLengthHist.n.resize(lib1.readLengthHist.Nbin);
lib1.readLengthHist.c.resize(lib1.readLengthHist.Nbin);
for (int i=0; i<lib1.readLengthHist.Nbin; i++) {
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.xc[i]), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.n[i]), sizeof(double));
input.read(reinterpret_cast< char *>(&lib1.readLengthHist.c[i]), sizeof(double));
}
lib1.readLengthHist.dx=lib1.readLengthHist.xc[1]-lib1.readLengthHist.xc[0];
lib1.readLengthHist.xlow=lib1.readLengthHist.xc[0]-lib1.readLengthHist.dx/2.0;
lib1.readLengthHist.xhigh=lib1.readLengthHist.xc[lib1.readLengthHist.Nbin-1]+lib1.readLengthHist.dx/2.0;
lib1.readLengthHist.Finalize();
this->libmap[lib1.Info.ReadGroupCode]=lib1;
ReadGroupID2Code[lib1.Info.ReadGroupID]=lib1.Info.ReadGroupCode;
}
//----------------------------------------------------------------------------
// anchor info
//----------------------------------------------------------------------------
int Na;
input.read(reinterpret_cast< char *>(&Na), sizeof(int));
anchorinfo.names.resize(Na);
anchorinfo.use.resize(Na);
for(size_t i=0; int(i)<Na; ++i) {
input.read(reinterpret_cast < char * > (&nchar), sizeof(int));
input.read(buff, nchar);
buff[nchar]=0;
anchorinfo.names[i]=buff;
unsigned int L;
input.read(reinterpret_cast<char *>(&L), sizeof(unsigned int));
anchorinfo.L[anchorinfo.names[i]]=L;
bool use;
input.read(reinterpret_cast< char *>(&use), sizeof(bool));
anchorinfo.use[i]=use;
}
input.close();
}
C_libraries::C_libraries( const C_libraries & rhs) // constructor
{
libmap=rhs.libmap;
anchorinfo=rhs.anchorinfo;
ReadGroupID2Code=rhs.ReadGroupID2Code;
}
C_libraries::C_libraries(Mosaik::CAlignmentReader & ar1) // constructor - load from Mosaik file
{
vector<Mosaik::ReadGroup> readGroups;
readGroups=ar1.GetReadGroups();
int N = readGroups.size();
C_libraryinfo lib1;
for (int i=0; i<N; i++) {
unsigned int ReadGroupCode=readGroups[i].ReadGroupCode;
libmap[ReadGroupCode].Info=readGroups[i];
ReadGroupID2Code[readGroups[i].ReadGroupID]=ReadGroupCode;
}
}
//C_libraries::C_libraries(BamReader & br1) // constructor - load from BAM file
C_libraries::C_libraries(BamMultiReader & br1) // constructor - load from BAM file
{
string ht=br1.GetHeaderText();
vector<Mosaik::ReadGroup> readGroups=GetReadGroups(ht);
int N = readGroups.size();
if (N<1) {
cerr << "Need read group info in BAM file header" << endl;
exit(-1);
}
// C_libraryinfo lib1;
for (int i=0; i<N; i++) {
unsigned int ReadGroupCode=readGroups[i].ReadGroupCode;
libmap[ReadGroupCode].Info=readGroups[i];
ReadGroupID2Code[readGroups[i].ReadGroupID]=ReadGroupCode;
}
}
vector<Mosaik::ReadGroup> C_libraries::GetReadGroups(string & ht)
{
// @RG ID:ERR001607 PL:ILLUMINA PU:1000G-mpimg-081010-2_3 LB:NA 19210.11 PI:200 SM:NA19210 CN:MPIMG
vector<Mosaik::ReadGroup> readGroups;
string line,thing;
stringstream stream1(ht); //stringstream::out | stringstream::in);
//stream1 << ht;
while( getline(stream1, line) ) {
if (line.find("@RG")!=string::npos) {
cout << line << "\n";
Mosaik::ReadGroup RG1;
stringstream stream2(line); //stringstream::out | stringstream::in);
//string tab1="\t";
while( getline(stream2,thing,'\t') ) {
string token="ID:";
size_t f1=thing.find(token);
if (f1!=string::npos) {
RG1.ReadGroupID=thing.substr(f1+token.length());
}
token="PL:";
f1=thing.find(token);
if (f1!=string::npos) {
string technology=upperCase(thing.substr(f1+token.length()));
if (technology.find("ILLUMINA")!=string::npos) { RG1.SequencingTechnology=ST_ILLUMINA; }
if (technology.find("454")!=string::npos) { RG1.SequencingTechnology=ST_454; }
if (technology.find("HELICOS")!=string::npos) { RG1.SequencingTechnology=ST_HELICOS; }
if (technology.find("SOLID")!=string::npos) { RG1.SequencingTechnology=ST_SOLID; }
}
token="LB:";
f1=thing.find(token);
if (f1!=string::npos) {
RG1.LibraryName=thing.substr(f1+token.length());
}
token="PU:";
f1=thing.find(token);
if (f1!=string::npos) {
RG1.PlatformUnit=thing.substr(f1+token.length());
}
token="CN:";
f1=thing.find(token);
if (f1!=string::npos) {
RG1.CenterName=thing.substr(f1+token.length());
}
token="SM:";
f1=thing.find(token);
if (f1!=string::npos) {
RG1.SampleName=thing.substr(f1+token.length());
}
token="PI:";
f1=thing.find(token);
if (f1!=string::npos) {
string fraglen=thing.substr(f1+token.length());
RG1.MedianFragmentLength=string2Int(fraglen);
}
}
RG1.ReadGroupCode=CSHA1::GenerateReadGroupCode(RG1.ReadGroupID, RG1.SampleName);
readGroups.push_back(RG1);
}
}
return readGroups;
}
vector<char> C_libraries::getSequencingTechnology()
{
C_librarymap::iterator it;
int N = libmap.size();
vector<char> p;
p.resize(N);
int i=0;
for ( it=libmap.begin() ; it != libmap.end(); it++ )
{
//unsigned int ReadGroupCode=(*it).first;
C_libraryinfo lib1 = (*it).second;
p[i]=lib1.Info.SequencingTechnology;
i++;
}
return p;
}
ostream &operator<<(ostream &output, C_libraries & libs)
{
C_librarymap::iterator it;
int N = libs.libmap.size();
output << " Number of Libraries:\t " << N<< endl;
for ( it=libs.libmap.begin() ; it != libs.libmap.end(); it++ )
{
//unsigned int ReadGroupCode=(*it).first;
C_libraryinfo lib1 = (*it).second;
output << lib1 << endl;
}
return output;
}
void C_libraries::printLibraryInfo(string & file1) // print lib info
{
fstream output(file1.c_str(), ios::out);
if (!output) {
cerr << "Unable to open library info output file: " << file1 << endl;
return;
}
output << *this << endl;
output.close();
}
//------------------------------------------------------------------------------
// binary file output
//------------------------------------------------------------------------------
void C_libraries::writeLibraryInfo(string & outfilename, string & setName) // write lib info
{
C_librarymap::iterator it;
//int N = libmap.size();
// open output binary file. bomb if unable to open
fstream output(outfilename.c_str(), ios::out | ios::binary);
if (!output) {
cerr << "Unable to open file: " << outfilename << endl;
return;
}
C_headerSpan h;
h.setName = setName;
h.contigName = " ";
string typeName="library.span";
if (outfilename.find(typeName)==string::npos) {
cerr << "bad file name:\t" << outfilename << endl;
exit(-1);
}
h.typeName = typeName;
h.reclen = 2*sizeof(unsigned int)+9*sizeof(int)+2*sizeof(double)+sizeof(short);
h.N = this->libmap.size();
h.write(output);
//----------------------------------------------------------------------------
// fixed length part of lib record
//----------------------------------------------------------------------------
for ( it=libmap.begin() ; it != libmap.end(); it++ )
{
unsigned int ReadGroupCode=(*it).first;
C_libraryinfo lib1 = (*it).second;
output.write(reinterpret_cast<const char *>(&ReadGroupCode), sizeof(unsigned int));
output.write(reinterpret_cast<const char *>(&lib1.Info.MedianFragmentLength), sizeof(unsigned int));
output.write(reinterpret_cast<const char *>(&lib1.Info.SequencingTechnology), sizeof(unsigned short));
output.write(reinterpret_cast<const char *>(&lib1.LM), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.LMlow), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.LMhigh), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.tailcut), sizeof(double));
output.write(reinterpret_cast<const char *>(&lib1.LR), sizeof(double));
output.write(reinterpret_cast<const char *>(&lib1.LRmin), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.LRmax), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.NPair), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.NSingle), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.NPairRedundant), sizeof(int));
output.write(reinterpret_cast<const char *>(&lib1.NSingleRedundant), sizeof(int));
}
//----------------------------------------------------------------------------
// variable length part of lib record
//----------------------------------------------------------------------------
for ( it=libmap.begin() ; it != libmap.end(); it++ )
{
//unsigned int ReadGroupCode=(*it).first;
C_libraryinfo lib1 = (*it).second;
output.write(reinterpret_cast<const char *>(&lib1.Info.ReadGroupCode), sizeof(unsigned int));
output.write(reinterpret_cast<const char *>(&lib1.Info.MedianFragmentLength), sizeof(unsigned int));
// ReadGroupID
const char * cC = lib1.Info.ReadGroupID.c_str();
int cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
// CenterName
cC = lib1.Info.CenterName.c_str();
cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
// Description
cC = lib1.Info.Description.c_str();
cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
// LibraryName
cC = lib1.Info.LibraryName.c_str();
cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
// PlatformUnit
cC = lib1.Info.PlatformUnit.c_str();
cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
// SampleName
cC = lib1.Info.SampleName.c_str();
cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
// LF distribution
cC = lib1.fragHist.title.c_str();
cL = strlen(cC)+1;
output.write(reinterpret_cast<const char *>(&cL), sizeof(int));
output.write((const char *)(cC), cL * sizeof(char));
output.write(reinterpret_cast<const char *>(&lib1.fragHist.Ntot), sizeof(double));
output.write(reinterpret_cast<const char *>(&lib1.fragHist.mean), sizeof(double));
output.write(reinterpret_cast<const char *>(&lib1.fragHist.std), sizeof(double));
output.write(reinterpret_cast<const char *>(&lib1.fragHist.median), sizeof(double));
output.write(reinterpret_cast<const char *>(&lib1.fragHist.Nin), sizeof(double));