-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAXT2ACF.cpp
603 lines (468 loc) · 16.9 KB
/
AXT2ACF.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
#include "AXT2ACF.h"
using namespace std;
AXT2ACF::AXT2ACF(){
}
AXT2ACF::~AXT2ACF(){
}
string AXT2ACF::usage() const{
const string usage=string("")+ " axt2acf [options] <chr name> <name sample> <axt file>\n"+
"\nThis program will parse an AXT alignment for a single chromosome and output an ACF file (to STDOUT)\n"+
"The first sequence has to be the genome of the reference. For instance, to import chimp for the human reference:\n"+
" glactools axt2acf 1 PanTro5 chr1.hg19.panTro5.net.axt.gz > output.acf.gz\n"
"\n"+
"\t"+"--fai [file]" + "\t\t"+"Fasta index for genome (produced by \"samtools faidx\") (default: none)\n"+
"\t"+"-u" + "\t\t\t"+"Produce uncopressed output (default: "+booleanAsString(uncompressed)+")\n"+
"";
return usage;
}
int AXT2ACF::run(int argc, char *argv[]){
// cout<<INT_MAX<<endl;
// bool ancAllele = false;
int lastOpt=1;
string fastaIndex;
//last arg is program name
for(int i=1;i<(argc);i++){
if((string(argv[i]) == "-") ){
lastOpt=i;
break;
}
if(string(argv[i])[0] != '-' ){
lastOpt=i;
break;
}
if(string(argv[i]) == "-u"){
uncompressed=true;
continue;
}
if(string(argv[i]) == "--fai"){
fastaIndex=string(argv[i+1]);
i++;
continue;
}
cerr<<"Wrong option "<<argv[i]<<endl;
exit(1);
}
if(lastOpt != (argc-3)){
cerr<<"The last 3 arguments are <chr name> <name sample> <axt file> "<<lastOpt<<" "<<argc<<endl;
return 1;
}
if(fastaIndex.size()==0){
cerr<<"Must specify fai file "<<endl;
return 1;
}
vector<chrinfo> chrFound;
uint64_t genomeLength;
readFastaIndex(fastaIndex,chrFound,genomeLength);
//cerr<<"Filter used: "<<*filtersVCF<<endl;
//VCFreader vcfr (string(argv[argc-3]),5);
// BAMTABLEreader btr (),5);
uint16_t chrIdx = UINT16_MAX;
string chrname = string(argv[lastOpt+0]);
string nameSample = string(argv[lastOpt+1]);
string axtfile = string(argv[lastOpt+2]);
bool foundChr=false;
for(unsigned int i=0;i<chrFound.size();i++){
if(chrFound[i].name == chrname){
chrIdx = uint16_t(i);
foundChr = true;
break;
}
}
if(!foundChr){
cerr << "Unable to find chromosome "<<chrname<<" in fasta index"<<endl;
return 1;
}
unsigned int totalRec=0;
unsigned int writtenRec=0;
string header="";
header+="#ACF\n";
string programLine;
for(int i=0;i<(argc);i++){
programLine+=(string(argv[i])+" ");
}
header+="#PG:"+programLine+"\n";
header+="#GITVERSION: "+returnGitHubVersion(argv[-1],"")+"\n";
header+="#DATE: "+getDateString()+"\n";
header+="#AXT2ACF:"+chrname+" "+nameSample+" "+axtfile+"\n";
map<string,uint16_t> chr2index;
uint16_t chrCurrentIndex=0;
for(unsigned j=0;j<(chrFound.size());j++){
header+= string("#SQ\tSN:")+chrFound[j].name+"\tLN:"+stringify(chrFound[j].length)+"\n";
chr2index[chrFound[j].name]=chrCurrentIndex++;
if(chrCurrentIndex == 0xFFFF){
cerr<<"Too many chromosomes for this build, more than 65535"<<endl;
return 1;
}
}
header+="#chr\tcoord\tREF,ALT\troot\tanc\t"+nameSample+"\n";
GlacWriter * gw=NULL;
gw = new GlacWriter(1, //gp.getSizePops(),
false, //gp.isGLFormat(),
2,//gp.isACFormat()?2:1,
1,//compression threads
uncompressed);
if(!gw->writeHeader(header)){
cerr<<"GlacViewer: error writing header "<<endl;
exit(1);
}
string line;
string lineSp1;
string lineSp2;
igzstream myAXTFile;
myAXTFile.open(axtfile.c_str(), ios::in);
SingleAllele root;
SingleAllele anc;
root.setRefCount(0); root.setAltCount(0); root.setIsCpg(false);
anc.setRefCount(0); anc.setAltCount(0); anc.setIsCpg(false);
if (myAXTFile.good()){
while ( getline (myAXTFile,line)){
if(strBeginsWith(line,"#"))
continue;
vector<string> allToks = allTokens(line,' ');
if(allToks[1] != chrname){
if(allToks[1] != "chr"+chrname){
cerr<<"The chromosome name "<<allToks[1]<<" does not match the one provided on the command line ("<<"chr"<<chrname<<"), full line:"<<line<<endl;
return 1;
}else{
chrname="chr"+chrname;
}
}
unsigned int startC = destringify<unsigned int>(allToks[2]);
unsigned int endC = destringify<unsigned int>(allToks[3]);
unsigned int coordC =startC;
unsigned int lastC = startC;
bool lastCharWasC=false;
if(allToks.size() == 9){
getline (myAXTFile,lineSp1);
getline (myAXTFile,lineSp2);
//string lastToPrintS="";
AlleleRecords lastToPrint (false);
bool lastToPrintSet = false;
for(unsigned i=0;i<lineSp1.size();i++){
totalRec++;
if(lineSp1[i] == '-'){
}else{
if(lineSp2[i] == '-'){
}else{
writtenRec++;
AlleleRecords arCurrent (false);
arCurrent.chri = chrIdx;
arCurrent.coordinate = coordC;
arCurrent.sizePops = 1;
arCurrent.vectorAlleles = new vector<SingleAllele> ();
//cout<<coordC<<"\t"<<lineSp1[i]<<lineSp2[i]<<endl;
//string toprintS=chrname+"\t"+stringify(coordC)+"\t";
SingleAllele sample;
char cRef= char(toupper(lineSp1[i]));
char cAlt= char(toupper(lineSp2[i]));
//string toprint;
if(cRef == cAlt){
//toprintS+=stringify(cRef)+",N\t";
//toprint = "1,0";
sample.setRefCount(1); sample.setAltCount(0);
arCurrent.ref = cRef;
arCurrent.alt = 'N';
}else{
//toprintS+=stringify(cRef)+","+stringify(cAlt)+"\t";
//toprint = "0,1";
sample.setRefCount(0); sample.setAltCount(1);
arCurrent.ref = cRef;
arCurrent.alt = cAlt;
}
bool cpgFlag=false;
if( (lastC+1) == coordC){
if(lastCharWasC && (cRef == 'G' || cAlt == 'G' )){
cpgFlag=true;
}
}
//sample.setIsCpg(cpgEPO);
arCurrent.vectorAlleles->push_back(root);
arCurrent.vectorAlleles->push_back(anc);
//arToWrite.vectorAlleles->push_back(sample);
if(cpgFlag){
if(lastToPrintSet){
lastToPrint.vectorAlleles->at(2).setIsCpg(true);//setting CpG for previous record
if(!gw->writeAlleleRecord(&lastToPrint)){
cerr<<"AXT2ACF: error writing header "<<endl;
exit(1);
}
}
sample.setIsCpg(true);
arCurrent.vectorAlleles->push_back(sample);
if(!gw->writeAlleleRecord(&arCurrent)){//printing for current record
cerr<<"AXT2ACF: error writing header "<<endl;
exit(1);
}
lastToPrintSet=false;//was flushed
// if(!lastToPrintS.empty())
// cout<<lastToPrintS<<":1"<<endl; //print last line
// toprintS+="0,0:0\t0,0:0\t"+toprint+":1"; //print current line
// cout<<toprintS<<endl; //print current line
// lastToPrintS="";
}else{
if(lastToPrintSet){
lastToPrint.vectorAlleles->at(2).setIsCpg(false);//setting CpG for previous record
if(!gw->writeAlleleRecord(&lastToPrint)){
cerr<<"AXT2ACF: error writing header "<<endl;
exit(1);
}
}
sample.setIsCpg(false);
arCurrent.vectorAlleles->push_back(sample);
lastToPrint = arCurrent;
lastToPrintSet = true;
// if(!lastToPrintS.empty())
// cout<<lastToPrintS<<":0"<<endl; //print last line
// toprintS+="0,0:0\t0,0:0\t"+toprint; //store current line
// lastToPrintS=toprintS;
}
//cout<<toPrintS<<endl;
//cpg
if(cRef == 'C' || cAlt == 'C')
lastCharWasC = true;
else
lastCharWasC = false;
lastC = coordC;
}
coordC++;
}
}//end loop
if(lastToPrintSet){
lastToPrint.vectorAlleles->at(2).setIsCpg(false);//setting CpG for previous record
if(!gw->writeAlleleRecord(&lastToPrint)){
cerr<<"AXT2ACF: error writing header "<<endl;
exit(1);
}
}
// if(!lastToPrintS.empty())
// cout<<lastToPrintS<<":0"<<endl;
if((coordC-1) != endC){
cerr<<"The block did not end with the proper coordinate got:"<<coordC<<" expected "<<endC<<endl;
return 1;
}
// cout<<startC<<"\t"<<endC<<endl;
// return 1;
getline (myAXTFile,line);
}else{
cerr<<"Wrong number of fields for line "<<line<<endl;
return 1;
}
}
myAXTFile.close();
}else{
cerr << "Unable to open file "<<axtfile<<endl;
return 1;
}
// string line;
// bool hasData= getline (myfile,line);
// while (hasData ){
// totalRec++;
// // while(btr.hasData()){
// // BAMTableObj * toprint=btr.getData();
// vector<string> token= allTokens(line,'\t');
// // cout<<"line "<<line<<endl;
// string chr = token[1];
// unsigned int pos = destringify<unsigned int>(token[2]);
// string genotype = token[3];
// // if(passedFilters(toprint,filtersVCF)){
// if(firstLine){
// firstLine=false;
// if(epoFileB){
// rtEPO = new ReadTabix( epoFile.c_str() ,
// epoFileidx.c_str() ,
// chr,
// int(pos),INT_MAX ); //the destructor should be called automatically
// setVarsEPO(rtEPO,epoChr,epoCoord,cpgEPO,allel_ref,allel_chimp,allel_anc,lineLeftEPO,lineFromEPO);
// }
// }
// if(epoFileB){
// if(!lineLeftEPO){
// cerr<<"Error, no data in the EPO file "<< chr <<":"<< int(pos) <<endl;
// return 1;
// }
// if(epoChr != chr){
// cerr<<"Error, the chromosome does not match the one in the EPO file = "<<epoChr <<" and not "<<chr<<endl;
// return 1;
// }
// // cout<<"2"<<endl;
// while(epoCoord != pos){
// if(epoCoord > pos){
// cerr<<"Error, are all the sites in EPO there? Difference between coords "<<(line)<<"\t"<<lineFromEPO<<endl;
// return 1;
// }
// if( (pos - epoCoord ) >= limitToReOpenFP){ //seeking in the file
// rtEPO->repositionIterator(chr , int(pos),INT_MAX);
// }
// setVarsEPO(rtEPO,epoChr,epoCoord,cpgEPO,allel_ref,allel_chimp,allel_anc,lineLeftEPO,lineFromEPO);
// // lineLeftEPO=(rtEPO->readLine( lineFromEPO ));
// // vector<string> fieldsEPO = allTokens(lineFromEPO,'\t');
// // epoChr = fieldsEPO[0];
// // epoCoord = string2uint(fieldsEPO[1]);
// // if(fieldsEPO[9] == "1")
// // cpgEPO=true;
// // else
// // cpgEPO=false;
// // if(ancAllele){
// // allel_chimp = fieldsEPO[3][0];//inferred ancestor
// // }else{
// // allel_chimp = fieldsEPO[4][0];//chimp;
// // }
// // if(!lineLeftEPO){
// // cerr<<"Error, missing data in the EPO file"<<*toprint<<endl;
// // return 1;
// // }
// }
// if(epoCoord != pos){
// cerr<<"Error, are all the sites in EPO there? Difference between coords "<<line<<"\t"<<lineFromEPO<<endl;
// return 1;
// }
// }
// //int refIdx =base2int(allel_ref);
// // int chimpIdx=base2int(allel_chimp);
// char alt='N';
// string s="ACGT";
// string chimpString;
// string ancString;
// SingleAllele root;
// SingleAllele anc;
// //need an allele that is A,C,G,T
// //cout<<pos<<"\t"<<allel_ref<<endl;
// if(!isResolvedDNA(allel_ref)){
// goto nextline;
// }
// // cout<<"3"<<endl;
// //determine alternative allele
// for(int i=0;i<4;i++){
// if(s[i] != allel_ref){
// if(genotype.find(s[i]) != string::npos){ //new non-ref allele found
// //toprint->hasAllele(i+1) ){
// alt=s[i];
// }
// }
// }
// // cout<<*toprint<<endl;
// // cout<<lineFromEPO<<endl;
// // SingleAllele root;
// // SingleAllele anc;
// if(!epoFileB){ //no epo file
// // chimpString="0,0:0";
// // ancString="0,0:0";
// root.setRefCount(0); root.setAltCount(0); root.setIsCpg(false);
// anc.setRefCount(0); anc.setAltCount(0); anc.setIsCpg(false);
// }else{
// //unresolved ancestral allele (A,C,G,T)
// if(!isResolvedDNA(allel_chimp)){
// //chimpString="0,0:0";
// root.setRefCount(0); root.setAltCount(0); root.setIsCpg(false);
// }
// //resolved ancestral allele
// else{
// if(allel_chimp == allel_ref){//no diff between chimp and reference
// //chimpString="1,0:"+string(cpgEPO?"1":"0");
// root.setRefCount(1); root.setAltCount(0); root.setIsCpg(cpgEPO);
// }else{
// if(alt == 'N'){//no alt defined, the chimp becomes the alt
// alt = allel_chimp;
// //chimpString="0,1:"+string(cpgEPO?"1":"0");
// root.setRefCount(0); root.setAltCount(1); root.setIsCpg(cpgEPO);
// }else{
// if(alt == allel_chimp){//alt is chimp
// //chimpString="0,1:"+string(cpgEPO?"1":"0");
// root.setRefCount(0); root.setAltCount(1); root.setIsCpg(cpgEPO);
// }else{ //tri-allelic site, discard
// //continue;
// goto nextline;
// }
// }
// }
// }
// if(!isResolvedDNA(allel_anc)){
// //ancString="0,0:0";
// root.setRefCount(0); root.setAltCount(1); root.setIsCpg(cpgEPO);
// }
// //resolved ancestral allele
// else{
// if(allel_anc == allel_ref){//no diff between ancestor and reference
// //ancString="1,0:"+string(cpgEPO?"1":"0");
// anc.setRefCount(1); anc.setAltCount(0); anc.setIsCpg(cpgEPO);
// }else{
// if(alt == 'N'){//no alt defined, the ancestor becomes the alt
// alt = allel_anc;
// //ancString="0,1:"+string(cpgEPO?"1":"0");
// anc.setRefCount(0); anc.setAltCount(1); anc.setIsCpg(cpgEPO);
// }else{
// if(alt == allel_anc){//alt is ancestor
// //ancString="0,1:"+string(cpgEPO?"1":"0");
// anc.setRefCount(0); anc.setAltCount(1); anc.setIsCpg(cpgEPO);
// }else{ //tri-allelic site, discard
// //continue;
// goto nextline;
// }
// }
// }
// }
// }
// // cout<<"4"<<endl;
// if( (alt!='N' && genotype.find(alt) != string::npos) ) // has alternative
// {//no alt in bam table
// int refCount=0;
// int altCount=0;
// string rr = stringify(allel_ref)+ stringify(allel_ref);
// string ra = stringify(allel_ref)+ stringify(alt);
// string ar = stringify(alt) + stringify(allel_ref);
// string aa = stringify(alt) + stringify(alt);
// if(genotype == rr ){
// refCount=2;
// }else{
// if(genotype == aa){
// altCount=2;
// }else{
// if( (genotype == ra) ||
// (genotype == ar) ){
// refCount=1;
// altCount=1;
// }else{
// //goto nextline;
// cerr<<"Error: Potential error in the 23 and me file where the reference allele is not there "<<line<<"\t"<<lineFromEPO<<endl; //error
// goto nextline;
// }
// }
// }
// //cout<<refIdx<<endl;
// writtenRec++;
// AlleleRecords arToWrite (false);
// arToWrite.chri = chr2index[chr];
// arToWrite.coordinate = pos;
// arToWrite.sizePops = 1;
// arToWrite.ref = allel_ref;
// arToWrite.alt = alt;
// arToWrite.vectorAlleles = new vector<SingleAllele> ();
// // SingleAllele root;
// // SingleAllele anc;
// SingleAllele sample (refCount, altCount, 0);//no CpG provided
// arToWrite.vectorAlleles->push_back(root);
// arToWrite.vectorAlleles->push_back(anc);
// arToWrite.vectorAlleles->push_back(sample);
// if(!gw->writeAlleleRecord(&arToWrite)){
// cerr<<"Vcf2ACF: error writing header "<<endl;
// exit(1);
// }
// // cout<<chr<<"\t"<< pos<<"\t"<<
// // allel_ref<<","<<
// // alt<<"\t"<<
// // chimpString<<"\t"<<
// // ancString<<"\t"<<
// // refCount<<","<<
// // altCount<<
// // ":"<<("0")<<endl;//no cpg info
// }else{
// goto nextline;
// //continue;//triallelic, skip
// }
// nextline:
// hasData = getline (myfile,line);
// }
delete(gw);
cerr<<"Program "<<argv[0]<<" looked at "<<totalRec<<" records, wrote "<<writtenRec<<" terminated gracefully"<<endl;
return 0;
}