-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathACF2TREEMIX.cpp
198 lines (149 loc) · 4.14 KB
/
ACF2TREEMIX.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
#include "ACF2TREEMIX.h"
using namespace std;
ACF2TREEMIX::ACF2TREEMIX(){
}
ACF2TREEMIX::~ACF2TREEMIX(){
}
string ACF2TREEMIX::usage() const{
string usage=string("")+"acf2treemix [options] <ACF file>\n"+
"This program takes an ACF matrix and prints the segregating sites in Treemix format\n"+
"\n"+
"Options:\n"+
"\t--noroot\t\tDo not print the root/anc (Default "+boolStringify(noroot)+" )\n"+
"\t--justtransv\t\tOnly allow transversions (Default "+boolStringify(limitToTransversions)+" )\n"+
"\t--noprivate\t\tDo not allow private mutations (Default "+boolStringify(noprivate)+" )\n"+
"\t--anc\t\tPrint ancestral value, not the root (Default "+boolStringify(printAnc)+" )\n";
return usage;
}
int ACF2TREEMIX::run(int argc, char *argv[]){
if(argc == 1 ||
(argc == 2 && (string(argv[1]) == "-h" || string(argv[1]) == "--help") )
){
cerr << "Usage "<<usage()<<endl;
return 1;
}
int lastOpt=1;
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]) == "--noroot" ){
noroot=true;
continue;
}
if( string(argv[i]) == "--anc" ){
printAnc=true;
continue;
}
if( string(argv[i]) == "--justtransv" ){
limitToTransversions=true;
continue;
}
if( string(argv[i]) == "--noprivate" ){
noprivate=true;
continue;
}
cerr<<"Error unknown option "<<argv[i]<<endl;
exit(1);
}
if(printAnc && !noroot){
cerr<<"ACF2TREEMIX Error: cannot specify --noroot and --anc "<<endl;
exit(1);
}
GlacParser gp (argv[lastOpt]);
if(!gp.isACFormat()){
cerr<<"ACF2TREEMIX: Error the file "<<argv[lastOpt]<<" should be in ACF format"<<endl;
return 1;
}
vector<string> toprintPop;
unsigned jinit=0;
if(noroot){
jinit=2;
}
for(unsigned j=jinit;j<gp.getPopulationsNames()->size();j++){
if(j == 0){
if(printAnc)
continue;
toprintPop.push_back(gp.getPopulationsNames()->at(j));
continue;
}
if(j == 1){
if(!printAnc)
continue;
toprintPop.push_back(gp.getPopulationsNames()->at(j));
continue;
}
toprintPop.push_back(gp.getPopulationsNames()->at(j));
}
cout<<vectorToString( toprintPop," ")<<endl;
AlleleRecords * test;
unsigned int totalRecords=0;
unsigned int keptRecords=0;
while(gp.hasData()){
//cout<<"data"<<endl;
test = gp.getData();
totalRecords++;
if( (totalRecords%10000000) == 0){
cerr<<"Looked at "<<totalRecords<<" records @ "<<test->chr<<":"<<test->coordinate<<endl;
}
if(test->alt == 'N'){
continue;
}
if(limitToTransversions){
//skip potential transitions
if(isPotentialTransition(test->ref,test->alt))
continue;
}
string toprint="";
int counterIndRef=0;
int counterIndAlt=0;
for(unsigned j=jinit;j<test->vectorAlleles->size();j++){
if(j == 0){
if(printAnc)
continue;
}
if(j == 1){
if(!printAnc)
continue;
}
if( (test->vectorAlleles->at(j).getRefCount() == 0) &&
(test->vectorAlleles->at(j).getAltCount() == 0) ){
goto nextiteration;
}
if(test->vectorAlleles->at(j).getRefCount() != 0)
counterIndRef++;
if(test->vectorAlleles->at(j).getAltCount() != 0)
counterIndAlt++;
toprint+=stringify(test->vectorAlleles->at(j).getRefCount())+","+stringify(test->vectorAlleles->at(j).getAltCount());
if(j!= (test->vectorAlleles->size()-1)){
toprint+=" ";
}
}
//cout<<endl;
if(noprivate){
//each allele was observed more than once
if(counterIndRef >1 &&
counterIndAlt >1 ){
cout<<toprint<<endl;
keptRecords++;
}else{
continue;
}
}else{
cout<<toprint<<endl;
keptRecords++;
}
nextiteration:
continue;
}
cerr<<"Program "<<argv[0]<<" wrote "<<keptRecords<<" out of "<<totalRecords<<" terminated gracefully"<<endl;
if(keptRecords==0){
cerr<<"It seems you have no records that were produced, did you define the root population?"<<endl;
}
return 0;
}