-
Notifications
You must be signed in to change notification settings - Fork 2
/
GLF2ACF.cpp
174 lines (132 loc) · 4.16 KB
/
GLF2ACF.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
/*
* GLF2ACF
* Date: Jul-30-2017
* Author : Gabriel Renaud gabriel.reno [at sign here ] gmail.com
*
*/
#include "GLF2ACF.h"
//#define DEBUGPOS 9484430
GLF2ACF::GLF2ACF(){
}
GLF2ACF::~GLF2ACF(){
}
string GLF2ACF::usage() const{
return string(string("glactools") +" glf2acf [options] <glf file> "+"\n"+
"\nThis program converts a GLF file containing genotype likelihoods into ACF with allele counts (prints to the stdout)\n"+
"\t"+"-u" + "\t\t\t"+"Produce uncopressed output (default: "+booleanAsString(uncompressed)+")\n"+
"\t"+"--minPL [pl]" +"\t\t" +"Use this as the minimum difference of PL values for alleles (default: "+stringify(minPLdiffind)+")\n"
);
}
int GLF2ACF::run(int argc, char *argv[]){
int lastOpt=1;
for(int i=1;i<(argc);i++){
//cerr<<i<<"\t"<<string(argv[i])<<endl;
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]) == "--minPL" ){
minPLdiffind=destringify<int>(argv[i+1]);
// specifiedPL =true;
i++;
continue;
}
cerr<<"Error unknown option "<<argv[i]<<endl;
return 1;
}
//cerr<<lastOpt<<"\t"<<(argc-1)<<" "<<minPLdiffind<<endl;
// exit(1);
if(lastOpt != (argc-1)){
cerr<<"The last argument is the <glf file> "<<endl;
return 1;
}
string fileglf = string(argv[lastOpt]);
GlacParser gp (fileglf);
AlleleRecords * arr;
AlleleRecords * arw;
if(!gp.isGLFormat()){
cerr<<"The input file must be in GLF"<<endl;
return 1;
}
GlacWriter * gw = new GlacWriter(gp.getSizePops(),
false,
2,
1,//compression threads
uncompressed);
string newheader="";
newheader+="#ACF\n";
string programLine;
for(int i=0;i<(argc);i++){
programLine+=(string(argv[i])+" ");
}
newheader+="#PG:"+programLine+"\n";;
newheader+="#GITVERSION: "+returnGitHubVersion(argv[-1],"")+"\n";;
newheader+="#DATE: "+getDateString()+"\n";;
newheader+="#GLF2ACF:\n";
newheader+=gp.getHeaderNoSQNoDefline("#\t")+"\n";
newheader+=gp.getHeaderSQ("")+"\n";
newheader+=gp.getDefline()+"\n";
if(!gw->writeHeader(newheader)){
cerr<<"GlacViewer: error writing header "<<endl;
exit(1);
}
while(gp.hasData()){
arr = gp.getData();
arw = new AlleleRecords(false);
arw->chr = arr->chr;
arw->chri = arr->chri;
arw->coordinate = arr->coordinate;
#ifdef DEBUGPOS
bool debugPosition=false;
if(arw->coordinate == DEBUGPOS){
debugPosition=true;
cerr<<*arr<<endl;
}
#endif
//cerr<<arr->coordinate<<endl;
arw->sizePops = arr->sizePops;
arw->ref = arr->ref;
arw->alt = arr->alt;
arw->vectorAlleles = new vector<SingleAllele>();
bool foundAlt=false;
bool hasNonZero=false;
for(unsigned int i=0;i<arr->vectorGLs->size();i++){
pair<int,int> pairCount= arr->vectorGLs->at(i).returnLikelyAlleleCountForRefAlt(minPLdiffind);
#ifdef DEBUGPOS
if( debugPosition ){
cerr<<i<<"\t"<<pairCount.first<<"\t"<<pairCount.second<<"\t"<<arr->vectorGLs->at(i).getIsCpg()<<endl;
}
#endif
hasNonZero = hasNonZero || (pairCount.first != 0) || (pairCount.second != 0);
foundAlt = (pairCount.second!=0) || foundAlt;
SingleAllele saToWrite (pairCount.first, pairCount.second, arr->vectorGLs->at(i).getIsCpg());
arw->vectorAlleles->push_back(saToWrite);
}
if(!hasNonZero)//skip positions with no information
continue;
#ifdef DEBUGPOS
if( debugPosition ){
return 1;
}
#endif
if(!foundAlt)
arw->alt = 'N';
//cout<<"write"<<*arw<<endl;
if(!gw->writeAlleleRecord(arw)){
cerr<<"GlacViewer: error record "<<*arw<<endl;
exit(1);
}
//cout<<"delete"<<endl;
delete(arw);
}
delete(gw);
return 0;
}