-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGlacBedfilter.cpp
255 lines (195 loc) · 6.33 KB
/
GlacBedfilter.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
#include "GlacBedfilter.h"
using namespace std;
GlacBedfilter::GlacBedfilter(){
}
GlacBedfilter::~GlacBedfilter(){
}
string GlacBedfilter::usage() const{
string usage=string("glactools")+" bedfilter [options] <ACF or GLF file> <sorted BED file>"+
"\nThis will keep only the positions in the bed file\n"+
"\n"+
"Options:\n"+
"\t"+"-v" + "\t\t\t"+"Print what the program is doing (default: "+booleanAsString(verbose)+")\n"+
"\t"+"-u" + "\t\t\t"+"Produce binary but uncompressed output (default: "+booleanAsString(uncompressed)+")\n";
return usage;
}
int GlacBedfilter::run(int argc, char *argv[]){
//int main (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;
//unsigned int firstPopInd=0;
//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]) == "-v"){
verbose=true;
continue;
}
cerr<<"Error unknown option "<<argv[i]<<endl;
return 1;
}
if(lastOpt != (argc-2)){
cerr<<"The last 2 arguments are the <ACF/GLF file> <BED file> "<<endl;
return 1;
}
string fileglf = string(argv[lastOpt]);
string bedFileRegions = string(argv[lastOpt+1]);
map< string, vector<GenomicRange> * > * bedRegionsToFilter;
if(verbose){
cerr<<"bedfilter: begin reading bed file "<<bedFileRegions<<endl;
}
bedRegionsToFilter = readBEDSortedfile(bedFileRegions,verbose);
if(verbose){
cerr<<"bedfilter: done reading bed file "<<bedFileRegions<<endl;
}
map< string, uint32_t > * coordinateOfVec=new map< string, uint32_t >();
for(map<string,vector<GenomicRange> * >::iterator it = bedRegionsToFilter->begin();
it != bedRegionsToFilter->end();
++it) {
//cout<<it->first<<endl;
coordinateOfVec->insert( pair<string ,uint32_t>(it->first,0) );
}
uint16_t chrNameIdx=-1;
uint32_t previousCoordinate=0;
vector<GenomicRange> * currentGr=0;
unsigned int currentIndex=0;
bool chrFoundInBed=false;
GlacParser gp (fileglf);
AlleleRecords * arr;
GlacWriter * gw = new GlacWriter(gp.getSizePops(),
gp.isGLFormat(),
gp.isGLFormat()?1:2,
1,//compression threads
uncompressed);
stringstream newheader;
if(gp.isGLFormat())
newheader<<"#GLF\n";
else
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<<"#BEDFILTER:"<<"\n";
newheader<<gp.getHeaderNoSQNoDefline("#\t")<<"\n";
newheader<<gp.getHeaderSQ("")<<"\n";
newheader<<gp.getDefline()<<"\n";
if(!gw->writeHeader(newheader.str())){
cerr<<"GlacBedfilter: error writing header "<<endl;
exit(1);
}
uint64_t totalRecords=0;
uint64_t keptRecords=0;
while(gp.hasData()){
arr = gp.getData();
totalRecords++;
if(verbose){
if( (totalRecords%10000) == 0){
cerr<<"bedfilter: processed "<<thousandSeparator(totalRecords)<<" records"<<endl;
}
}
if(arr->chri != chrNameIdx){//new chr
if(bedRegionsToFilter->find(gp.getChromosomeName(arr->chri)) == bedRegionsToFilter->end() ){
chrFoundInBed=false;
currentGr = 0;
}else{
chrFoundInBed=true;
currentGr = bedRegionsToFilter->at(arr->chr) ;
currentIndex = coordinateOfVec->at(arr->chr) ;
if(currentIndex!=0){
cerr<<"There seems to be a mix of chromosomes in the glactools file, needs to be sorted chr: "<<chrNameIdx<<endl;
return 1;
}
}
previousCoordinate = arr->coordinate;
chrNameIdx = arr->chri;
}else{
if(previousCoordinate >= arr->coordinate){
cerr<<"There seems to be a unsorted coordinate in the glactools file, needs to be sorted coordinate: "<<previousCoordinate<<endl;
return 1;
}
}
if(!chrFoundInBed )
goto nextacfrecord;
while(1){
if(currentIndex == currentGr->size())//end of bed file
goto nextacfrecord;
//ignore
// |---------|
// *
if( arr->coordinate < currentGr->at(currentIndex).getStartCoord() ){//ignore
// cout<<"case 1"<<endl;
goto nextacfrecord;
}
//print
// |---------|
// *
if(arr->coordinate >= currentGr->at(currentIndex).getStartCoord() &&
arr->coordinate <= currentGr->at(currentIndex).getEndCoord() ){
// cout<<"case 2"<<endl;
//cout<<(*arr)<<endl;
if(!gw->writeAlleleRecord(arr)){
cerr<<"GlacBedfilter: error record "<<*arr<<endl;
exit(1);
}
keptRecords++;
goto nextacfrecord;
}
//we are running behind in the bed file
// |---------|
// *
if( arr->coordinate > currentGr->at(currentIndex).getEndCoord()){
// cout<<"case 3\t"<<currentIndex<<"\t"<<currentGr->size()<<endl;
if(currentIndex<currentGr->size()){//we move to next iteration
currentIndex++;
}else{
goto nextacfrecord; //we have reached the end of the vector, do nothing until next chr
}
}
}
nextacfrecord:
continue;
// cout<<arr->coordinate<<endl;
//totalRecords++;
// if(gp.isGLFormat()){
// for(unsigned j=firstPopInd;j<arr->vectorGLs->size();j++){
// if( arr->vectorGLs->at(j).alleleCountNull() )
// goto nextiterationnoundef;
// }
// }else{
// for(unsigned j=firstPopInd;j<arr->vectorAlleles->size();j++){
// if( arr->vectorAlleles->at(j).alleleCountNull() )
// goto nextiterationnoundef;
// }
// }
// if(!gw->writeAlleleRecord(arr)){
// cerr<<"GlacViewer: error record "<<*arr<<endl;
// exit(1);
// }
// keptRecords++;
// nextiterationnoundef:
// continue;
}
delete(gw);
cerr<<"Program bedfilter wrote "<<keptRecords<<" out of "<<totalRecords<<" terminated gracefully"<<endl;
return 0;
}