forked from grenaud/schmutzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogdiff.cpp
240 lines (186 loc) · 5.85 KB
/
logdiff.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
#include <gzstream.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <limits>
#include "utils.h"
using namespace std;
int main (int argc, char *argv[]) {
double qcutoff = 0;
double qcutoffIndel = 0;
string name = "MT";
// bool verbose=false;
const string usage=string("\t"+string(argv[0])+
" This program finds lines in multiple logs where they differ "+
" [options] [log file 1] [log file 2] .. "+"\n\n"+
"\n\tOptions:\n"+
//"\t\t"+"-v"+"\t\t" +"\t\t"+"Print the consensus and the input sequences"+"\n"+
// "\t\t"+"-name"+"\t[name]" +"\t\t\t\t"+"Name of the sequence (default "+stringify(name)+")\n"+
"\t\t"+"-q"+"\t[quality cutoff]" +"\t\t"+"Only consider sites with quality greater than this cutoff (default: 0)"+"\n"+
"\t\t"+"-indel"+"\t[quality cutoff]" +"\t\t"+"Filter indels according to this threshold (default: 0)"+"\n"+
// "\t\t"+"\t"+"\t\t" +"\t\t"+"Warning: This means that for indels, you will produce what the reference has"+"\n"+
// "\t\t"+"\t"+"\t\t" +"\t\t"+" hence adding a potential reference bias"+"\n"+
"");
if( (argc== 1) ||
(argc== 2 && string(argv[1]) == "-h") ||
(argc== 2 && string(argv[1]) == "-help") ||
(argc== 2 && string(argv[1]) == "--help") ){
cout<<"Usage:"<<endl;
cout<<""<<endl;
cout<<usage<<endl;
return 1;
}
int lastOpt=1;
for(int i=1;i<(argc-1);i++){ //all but the last arg
if(string(argv[i])[0] != '-' ){
//cout<<"end"<<i<<endl;
lastOpt=i;
break;
}
// if(string(argv[i]) == "-v" ){
// verbose=true;
// continue;
// }
if(string(argv[i]) == "-q" ){
qcutoff = destringify<double>(string(argv[i+1]));
i++;
continue;
}
if(string(argv[i]) == "-indel" ){
qcutoffIndel = destringify<double>(string(argv[i+1]));
i++;
continue;
}
// if(string(argv[i]) == "-name" ){
// name = string(argv[i+1]);
// i++;
// continue;
// }
cerr<<"Unknown option "<<argv[i] <<" exiting"<<endl;
return 1;
}
vector<string> logFiles;
for(int i=lastOpt;i<(argc);i++){
string logFile = string(argv[i]);
//cout<<logFile<<endl;
logFiles.push_back( logFile );
if(isDos(logFile) || isMac(logFile) ){
cerr << "File "<<logFile<<" must be unix formatted, exiting"<<endl;
return 1;
}
// logFD.open(logFile.c_str(),ios::in);
// if (logFD.good()){
}
string line;
vector< igzstream * > logFDs;
// logFDs.reserve(logFiles.size());
for(unsigned int i=0;i<(logFiles.size());i++){
string logFile = logFiles[i];
igzstream * toadd = new igzstream();
toadd->open(logFile.c_str(),ios::in);
if (toadd->good()){
getline (*toadd,line);//skip header
//toadd->close();
}else{
cerr << "Cannot open log file "<<logFile<<""<<endl;
return 1;
}
logFDs.push_back(toadd);
}
unsigned int currentPos=1;
vector<string> lines (logFDs.size(),"");
vector<bool> insertion (logFDs.size(),false);
//bool breakLoop;
while(true){
bool allDone=true;
for(unsigned int i=0;i<(logFDs.size());i++){
if(!insertion[i]){
getline ( *(logFDs[i]) ,line);//skip header
if(line.empty()){
//break;
continue;
}else{
allDone=allDone&&false;
}
lines[i]=line;
}else{
//read nothing if it had an insertion
}
//cout<<i<<"\t"<<currentPos<<"\t"<<lines[i]<<endl;
vector<string> fields = allTokens(lines[i],'\t');
string pos = fields[0];
string ref = fields[1];
string alt = fields[2];
if(strEndsWith(pos,"i")){ // indel
//deal with indel
insertion[i]=true;
continue;
}else{
insertion[i]=false;
}
unsigned int posUI = destringify<unsigned int>(pos);
if(currentPos!=posUI){
cerr << "Error with position "<<currentPos<<" at line: "<<lines[i]<<" in file "<<logFiles[i]<<endl;
return 1;
}
}
// cout<<"cp"<<currentPos<<endl;
//if(verbose){
//cout<<"----"<<endl;
vector<string> fields0 = allTokens(lines[0],'\t');
string alt = fields0[2];
long double qualB0 = numeric_limits<long double>::infinity(); //destringify<long double>(fields0[2] );
long double qualI0 = numeric_limits<long double>::infinity();
if(strEndsWith(fields0[0],"i") || alt == "D" ){
qualI0=destringify<long double>( fields0[3] );
}else{
qualB0=destringify<long double>( fields0[3] );
}
bool diff=false;
for(unsigned int i=1;i<(lines.size());i++){
vector<string> fields = allTokens(lines[i],'\t');
long double qualB = numeric_limits<long double>::infinity(); //destringify<long double>(fields0[2] );
long double qualI = numeric_limits<long double>::infinity();
if(strEndsWith(fields[0],"i") || alt == "D"){
qualI=destringify<long double>( fields[3] );
}else{
qualB=destringify<long double>( fields[3] );
}
if(alt != fields[2]){
if( qualB0 > qcutoff &&
qualB > qcutoff &&
qualI0 > qcutoffIndel &&
qualI > qcutoffIndel ){
diff=true;
}
}
}
if(diff){
cout<<vectorToString(lines,"\n")<<endl;
}
// cout<<"----"<<endl;
// }
// cout<<vectorToString(insertion)<<endl;
bool noneHaveIndel=true;
for(unsigned int i=0;i<(logFDs.size());i++){
//if(!insertion[i]){
noneHaveIndel=noneHaveIndel&&(!insertion[i]);
}
//cout<<noneHaveIndel<<endl;
if(!noneHaveIndel){
for(unsigned int i=0;i<(logFDs.size());i++){
insertion[i] = !insertion[i];
}
}
if(noneHaveIndel){
currentPos++;
}
if(allDone){
break;
}
}
cerr<<"program "<<argv[0]<<" finished successfully"<<endl;
return 0;
}