-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathoptions.cpp
357 lines (299 loc) · 13.6 KB
/
options.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "options.h"
#include "util.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include "fastareader.h"
Options::Options(){
in1 = "";
in2 = "";
out1 = "";
out2 = "";
reportTitle = "fastv report";
thread = 1;
compression = 2;
phred64 = false;
dontOverwrite = false;
inputFromSTDIN = false;
outputToSTDOUT = false;
readsToProcess = 0;
interleavedInput = false;
insertSizeMax = 512;
overlapRequire = 30;
overlapDiffLimit = 5;
overlapDiffPercentLimit = 20;
verbose = false;
seqLen1 = 151;
seqLen2 = 151;
kmerKeyLen = 0;
positiveThreshold = 0.1;
depthThreshold = 1.0;
statsBinSize = 0;
longReadThreshold = 200;
segmentLength = 100;
edThreshold = 8;
kcCoverageThreshold = 0.01;
kcCoverageHighConfidence = 0.9;
kcMedianHitHighConfidence = 10;
}
void Options::init() {
}
bool Options::isPaired() {
return in2.length() > 0 || interleavedInput;
}
bool Options::adapterCuttingEnabled() {
if(adapter.enabled){
if(isPaired() || !adapter.sequence.empty())
return true;
}
return false;
}
bool Options::polyXTrimmingEnabled() {
return polyXTrim.enabled;
}
void Options::loadFastaAdapters() {
if(adapter.fastaFile.empty()) {
adapter.hasFasta = false;
return;
}
check_file_valid(adapter.fastaFile);
FastaReader reader(adapter.fastaFile);
reader.readAll();
map<string, string> contigs = reader.contigs();
map<string, string>::iterator iter;
for(iter = contigs.begin(); iter != contigs.end(); iter++) {
if(iter->second.length()>=6) {
adapter.seqsInFasta.push_back(iter->second);
}
else {
cerr << "skip too short adapter sequence in " << adapter.fastaFile << " (6bp required): " << iter->second << endl;
}
}
if(adapter.seqsInFasta.size() > 0) {
adapter.hasFasta = true;
} else {
adapter.hasFasta = false;
}
}
bool Options::validate() {
if(in1.empty()) {
if(!in2.empty())
error_exit("read2 input is specified by <in2>, but read1 input is not specified by <in1>");
if(inputFromSTDIN)
in1 = "/dev/stdin";
else
error_exit("read1 input should be specified by --in1, or enable --stdin if you want to read STDIN");
} else {
check_file_valid(in1);
}
if(!in2.empty()) {
check_file_valid(in2);
}
if(!genomeFile.empty()) {
check_file_valid(genomeFile);
}
if(!kmerFile.empty()) {
check_file_valid(kmerFile);
}
if(!kmerCollectionFile.empty()) {
check_file_valid(kmerCollectionFile);
}
if(genomeFile.empty() && kmerFile.empty() && kmerCollectionFile.empty()) {
error_exit("You should at least specify one of KMER file (-k), one Genomes file (-g), or one KMER collection file (-a)");
}
// if output to STDOUT, then...
if(outputToSTDOUT) {
if(isPaired())
cerr << "Enable interleaved output mode for paired-end input." << endl;
}
if(in2.empty() && !interleavedInput && !out2.empty()) {
error_exit("read2 output is specified (--out2), but neighter read2 input is not specified (--in2), nor read1 is interleaved.");
}
if(!in2.empty() || interleavedInput) {
if(!out1.empty() && out2.empty()) {
error_exit("paired-end input, read1 output should be specified together with read2 output (--out2 needed) ");
}
if(out1.empty() && !out2.empty()) {
error_exit("paired-end input, read1 output should be specified (--out1 needed) together with read2 output ");
}
}
if(!in2.empty() && interleavedInput) {
error_exit("<in2> is not allowed when <in1> is specified as interleaved mode by (--interleaved_in)");
}
if(!out1.empty()) {
//check_file_writable(out1);
if(out1 == out2) {
error_exit("read1 output (--out1) and read1 output (--out2) should be different");
}
if(dontOverwrite && file_exists(out1)) {
error_exit(out1 + " already exists and you have set to not rewrite output files by --dont_overwrite");
}
}
if(!out2.empty()) {
//check_file_writable(out2);
if(dontOverwrite && file_exists(out2)) {
error_exit(out2 + " already exists and you have set to not rewrite output files by --dont_overwrite");
}
}
if(dontOverwrite) {
if(file_exists(jsonFile)) {
error_exit(jsonFile + " already exists and you have set to not rewrite output files by --dont_overwrite");
}
if(file_exists(htmlFile)) {
error_exit(htmlFile + " already exists and you have set to not rewrite output files by --dont_overwrite");
}
}
if(compression < 1 || compression > 9)
error_exit("compression level (--compression) should be between 1 ~ 9, 1 for fastest, 9 for smallest");
if(readsToProcess < 0)
error_exit("the number of reads to process (--reads_to_process) cannot be negative");
if(thread < 1) {
thread = 1;
} else if(thread > 16) {
cerr << "WARNING: fastv uses up to 16 threads although you specified " << thread << endl;
thread = 16;
}
if(positiveThreshold < 0.001 || positiveThreshold > 100)
error_exit("positive threshold (-p) should be 0.001 ~ 100, suggest 0.1");
if(depthThreshold < 0.001 || depthThreshold > 1000)
error_exit("depth threshold (-d) should be 0.001 ~ 1000, suggest 1");
if(longReadThreshold < 100 || longReadThreshold > 10000)
error_exit("long read threshold (--long_read_threshold) should be 100 ~ 10000, suggest 200");
if(kcCoverageThreshold < 0 || kcCoverageThreshold > 1)
error_exit("K-mer collection coverage threshold (--kc_coverage_threshold) should be 0 ~ 1.0, suggest 0.01");
if(kcCoverageHighConfidence < 0 || kcCoverageHighConfidence > 1)
error_exit("K-mer collection high confidence coverage threshold (--kc_high_confidence_coverage_threshold) should be 0 ~ 1.0, suggest 0.9");
if(kcMedianHitHighConfidence < 0 || kcMedianHitHighConfidence > 10000)
error_exit("K-mer collection high confidence median hits threshold (--kc_high_confidence_median_hit_threshold) should be 0 ~ 10000, suggest 5");
if(segmentLength < 50 || segmentLength > 5000)
error_exit("segment length for splitted long reads (--read_segment_len) should be 50 ~ 5000, suggest 100");
if(segmentLength >= longReadThreshold)
error_exit("segment length for splitted long reads (--read_segment_len) must less than long read threshold (--long_read_threshold)");
if(edThreshold < 0 || edThreshold > 50)
error_exit("edit distance threshold (-E) should be 0 ~ 50, suggest 8");
if(trim.front1 < 0 || trim.front1 > 30)
error_exit("trim_front1 (--trim_front1) should be 0 ~ 30, suggest 0 ~ 4");
if(trim.tail1 < 0 || trim.tail1 > 100)
error_exit("trim_tail1 (--trim_tail1) should be 0 ~ 100, suggest 0 ~ 4");
if(trim.front2 < 0 || trim.front2 > 30)
error_exit("trim_front2 (--trim_front2) should be 0 ~ 30, suggest 0 ~ 4");
if(trim.tail2 < 0 || trim.tail2 > 100)
error_exit("trim_tail2 (--trim_tail2) should be 0 ~ 100, suggest 0 ~ 4");
if(qualfilter.qualifiedQual - 33 < 0 || qualfilter.qualifiedQual - 33 > 93)
error_exit("qualitified phred (--qualified_quality_phred) should be 0 ~ 93, suggest 10 ~ 20");
if(qualfilter.avgQualReq < 0 || qualfilter.avgQualReq > 93)
error_exit("average quality score requirement (--average_qual) should be 0 ~ 93, suggest 20 ~ 30");
if(qualfilter.unqualifiedPercentLimit < 0 || qualfilter.unqualifiedPercentLimit > 100)
error_exit("unqualified percent limit (--unqualified_percent_limit) should be 0 ~ 100, suggest 20 ~ 60");
if(qualfilter.nBaseLimit < 0 || qualfilter.nBaseLimit > 50)
error_exit("N base limit (--n_base_limit) should be 0 ~ 50, suggest 3 ~ 10");
if(lengthFilter.requiredLength < 0 )
error_exit("length requirement (--length_required) should be >0, suggest 15 ~ 100");
if(qualityCut.enabledFront || qualityCut.enabledTail || qualityCut.enabledRight) {
if(qualityCut.windowSizeShared < 1 || qualityCut.windowSizeShared > 1000)
error_exit("the sliding window size for cutting by quality (--cut_window_size) should be between 1~1000.");
if(qualityCut.qualityShared < 1 || qualityCut.qualityShared > 30)
error_exit("the mean quality requirement for cutting by quality (--cut_mean_quality) should be 1 ~ 30, suggest 15 ~ 20.");
if(qualityCut.windowSizeFront < 1 || qualityCut.windowSizeFront > 1000)
error_exit("the sliding window size for cutting by quality (--cut_front_window_size) should be between 1~1000.");
if(qualityCut.qualityFront < 1 || qualityCut.qualityFront > 30)
error_exit("the mean quality requirement for cutting by quality (--cut_front_mean_quality) should be 1 ~ 30, suggest 15 ~ 20.");
if(qualityCut.windowSizeTail < 1 || qualityCut.windowSizeTail > 1000)
error_exit("the sliding window size for cutting by quality (--cut_tail_window_size) should be between 1~1000.");
if(qualityCut.qualityTail < 1 || qualityCut.qualityTail > 30)
error_exit("the mean quality requirement for cutting by quality (--cut_tail_mean_quality) should be 1 ~ 30, suggest 13 ~ 20.");
if(qualityCut.windowSizeRight < 1 || qualityCut.windowSizeRight > 1000)
error_exit("the sliding window size for cutting by quality (--cut_right_window_size) should be between 1~1000.");
if(qualityCut.qualityRight < 1 || qualityCut.qualityRight > 30)
error_exit("the mean quality requirement for cutting by quality (--cut_right_mean_quality) should be 1 ~ 30, suggest 15 ~ 20.");
}
if(adapter.sequence!="auto" && !adapter.sequence.empty()) {
// validate adapter sequence for single end adapter trimming
if(adapter.sequence.length() <= 3)
error_exit("the sequence of <adapter_sequence> should be longer than 3");
// validate bases
for(int i=0; i<adapter.sequence.length(); i++) {
char c = adapter.sequence[i];
if(c!='A' && c!='T' && c!='C' && c!='G') {
error_exit("the adapter <adapter_sequence> can only have bases in {A, T, C, G}, but the given sequence is: " + adapter.sequence);
}
}
adapter.hasSeqR1 = true;
}
if(adapter.sequenceR2!="auto" && !adapter.sequenceR2.empty()) {
// validate adapter sequenceR2 for single end adapter trimming
if(adapter.sequenceR2.length() <= 3)
error_exit("the sequence of <adapter_sequence_r2> should be longer than 3");
// validate bases
for(int i=0; i<adapter.sequenceR2.length(); i++) {
char c = adapter.sequenceR2[i];
if(c!='A' && c!='T' && c!='C' && c!='G') {
error_exit("the adapter <adapter_sequence_r2> can only have bases in {A, T, C, G}, but the given sequenceR2 is: " + adapter.sequenceR2);
}
}
adapter.hasSeqR2 = true;
}
if(correction.enabled && !isPaired()) {
cerr << "WARNING: base correction is only appliable for paired end data, ignoring -c/--correction" << endl;
correction.enabled = false;
}
if(umi.enabled) {
if(umi.location == UMI_LOC_READ1 || umi.location == UMI_LOC_READ2 || umi.location == UMI_LOC_PER_READ) {
if(umi.length<1 || umi.length>100)
error_exit("UMI length should be 1~100");
if(umi.skip<0 || umi.skip>100)
error_exit("The base number to skip after UMI <umi_skip> should be 0~100");
}else {
if(umi.skip>0)
error_exit("Only if the UMI location is in read1/read2/per_read, you can skip bases after UMI");
if(umi.length>0)
error_exit("Only if the UMI location is in read1/read2/per_read, you can set the UMI length");
}
if(!umi.prefix.empty()) {
if(umi.prefix.length() >= 10)
error_exit("UMI prefix should be shorter than 10");
for(int i=0; i<umi.prefix.length(); i++) {
char c = umi.prefix[i];
if( !(c>='A' && c<='Z') && !(c>='a' && c<='z') && !(c>='0' && c<='9')) {
error_exit("UMI prefix can only have characters and numbers, but the given is: " + umi.prefix);
}
}
}
if(!umi.separator.empty()) {
if(umi.separator.length()>10)
error_exit("UMI separator cannot be longer than 10 base pairs");
// validate bases
for(int i=0; i<umi.separator.length(); i++) {
char c = umi.separator[i];
if(c!='A' && c!='T' && c!='C' && c!='G') {
error_exit("UMI separator can only have bases in {A, T, C, G}, but the given sequence is: " + umi.separator);
}
}
}
}
return true;
}
bool Options::shallDetectAdapter(bool isR2) {
if(!adapter.enabled)
return false;
if(isR2) {
return isPaired() && adapter.detectAdapterForPE && adapter.sequenceR2 == "auto";
} else {
if(isPaired())
return adapter.detectAdapterForPE && adapter.sequence == "auto";
else
return adapter.sequence == "auto";
}
}
string Options::getAdapter1(){
if(adapter.sequence == "" || adapter.sequence == "auto")
return "unspecified";
else
return adapter.sequence;
}
string Options::getAdapter2(){
if(adapter.sequenceR2 == "" || adapter.sequenceR2 == "auto")
return "unspecified";
else
return adapter.sequenceR2;
}