-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathACF2GROSS.cpp
192 lines (142 loc) · 3.99 KB
/
ACF2GROSS.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
#include "ACF2GROSS.h"
using namespace std;
ACF2GROSS::ACF2GROSS(){
}
ACF2GROSS::~ACF2GROSS(){
}
string ACF2GROSS::usage() const{
string usage=string("")+"acf2gross [options] <ACF file>\n"+
"This program takes an ACF matrix and prints the segregating sites in GRoSS format (https://github.com/FerRacimo/GRoSS)\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 ACF2GROSS::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<<"ACF2GROSS Error: cannot specify --noroot and --anc "<<endl;
exit(1);
}
GlacParser gp (argv[lastOpt]);
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<<"CHROM\tPOS\tSNPID\t"<<vectorToString( toprintPop,"\t")<<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=stringify(test->chr)+"\t"+stringify(test->coordinate)+"\tSNP"+stringify(keptRecords)+"\t";
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+="\t";
}
}
//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;
return 0;
}