-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparameters.cpp
458 lines (380 loc) · 15.3 KB
/
parameters.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//
// parameters.cpp
//
//
// Created by Deniz Kural on 2/19/13.
//
//
#include "parameters.h"
using namespace std;
// Levenshtein Distance Algorithm: C++ Implementation
// by Anders Sewerin Johansen
// http://www.merriampark.com/ldcpp.htm
int levenshteinDistance(const std::string source, const std::string target) {
// Step 1
const int n = source.length();
const int m = target.length();
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
// Good form to declare a TYPEDEF
typedef std::vector< std::vector<int> > Tmatrix;
Tmatrix matrix(n+1);
// Size the vectors in the 2.nd dimension. Unfortunately C++ doesn't
// allow for allocation on declaration of 2.nd dimension of vec of vec
for (int i = 0; i <= n; i++) {
matrix[i].resize(m+1);
}
// Step 2
for (int i = 0; i <= n; i++) {
matrix[i][0]=i;
}
for (int j = 0; j <= m; j++) {
matrix[0][j]=j;
}
// Step 3
for (int i = 1; i <= n; i++) {
const char s_i = source[i-1];
// Step 4
for (int j = 1; j <= m; j++) {
const char t_j = target[j-1];
// Step 5
int cost;
if (s_i == t_j) {
cost = 0;
}
else {
cost = 1;
}
// Step 6
const int above = matrix[i-1][j];
const int left = matrix[i][j-1];
const int diag = matrix[i-1][j-1];
int cell = min( above + 1, min(left + 1, diag + cost));
// Step 6A: Cover transposition, in addition to deletion,
// insertion and substitution. This step is taken from:
// Berghel, Hal ; Roach, David : "An Extension of Ukkonen's
// Enhanced Dynamic Programming ASM Algorithm"
// (http://www.acm.org/~hlb/publications/asm/asm.html)
if (i>2 && j>2) {
int trans=matrix[i-2][j-2]+1;
if (source[i-2]!=t_j) trans++;
if (s_i!=target[j-2]) trans++;
if (cell>trans) cell = trans;
}
matrix[i][j]=cell;
}
}
// Step 7
return matrix[n][m];
}
void Parameters::simpleUsage(char ** argv) {
cout
<< "usage: " << argv[0] << " -s [SEQUENCE] -f [REFERENCE] -t [TARGET] -v [VCF-FILE] > [OUTPUT]" << endl
<< "Please see README at http://www.github.com/denizkural/clia" << endl
<< "Use --help to read detailed options." << endl
<< "authors: Erik Garrison <[email protected]>" << endl
<< " Deniz Kural <[email protected]>" << endl;
}
void Parameters::usage(char** argv) {
cout
<< "usage: " << argv[0] << " -s [SEQUENCE] -f [REFERENCE] -t [TARGET] -v [VCF-FILE] > [OUTPUT]" << endl
<< "Please see README at http://www.github.com/denizkural/clia" << endl
<< "authors: Deniz Kural <[email protected]>" << endl
<< " Erik Garrison <[email protected]>" << endl
<< endl
<< "general parameters:" << endl
<< endl
<< " -h --help This dialog." << endl
//<< " -q --fastq-file FILE The fastq file from which to draw reads" << endl
<< " -f --fasta-reference FILE The reference sequence for alignment." << endl
<< " -v --vcf-file FILE The genome DAG, BGZIP'ed (.vcf.gz) and tabix-indexed (.tbi)" << endl
//<< " -o --output-file FILE Write alignments in BAM format to FILE." << endl
<< " -m --match N The alignment match score (integer, default 1)." << endl
<< " -M --mismatch N The alignment mismatch score (integer, default 4)." << endl
<< " -g --gap-open N Gap open penalty (integer, default 6)." << endl
<< " -e --gap-extend N Gap extension penalty (integer, default 1)." << endl
<< endl
<< "single-read alignment:" << endl
<< endl
<< " -s --sequence SEQ The sequence to align." << endl
<< " -t --target TARGET Target genomic region for alignment, e.g. chr2:1-20" << endl
<< " -D --display-dag Write DAG generated from variants to stdout." << endl
<< " -B --display-backtrace Write alignment matrix results to stdout." << endl
<< " -N --display-all-nodes Same as -B but also for nodes which are not traced." << endl
//<< " -P --display-alignment Print sequence from DAG and read sequence." << endl
<< " -d --debug Enable debugging output." << endl
<< " -r --reverse-complement Report the reverse complement if it provides a better alignment." << endl
<< endl
<< "local realignment:" << endl
<< endl
<< " -R --realign-bam Realign the BAM stream on stdin to the VCF file, adjusting" << endl
<< " position and flattening alignments back into the reference space" << endl
<< " where realignment to the graph provides better quality." << endl
<< " -U --flat-input-vcf The input VCF is normalized so that overlapping variants are" << endl
<< " represented in a single record (as by vcflib/bin/vcfcreatemulti)." << endl
<< " Do not decompose variant representation in VCF file to construct graph." << endl
/*
<< " -F --flatten-alignments Flatten output into reference-relative alignments where the DAG" << endl
<< " alignment provides a better match than the reference. Use this" << endl
<< " if you want to use alignment-based variant detectors on the output." << endl
*/
<< " -w --realignment-window Number of bp of window to assemble from VCF for realignment." << endl
<< " -u --unsorted-output Do not attempt streaming sort of alignments. This can save memory" << endl
<< " in high coverage areas, but will require sort of BAM file before" << endl
<< " it can be used for variant calling." << endl
<< " -S --soft-clip-qsum-threshold N" << endl
<< " If sum of qualities of soft clipped bases is >= N, realign (default ~inf)." << endl
<< " -Q --mismatch-qsum-threshold N" << endl
<< " If sum of qualities of mismatched bases is >= N, realign (default ~inf)." << endl
<< " -C --gap-count-threshold N" << endl
<< " If the count of gaps is >= N, realign (default: ~inf)." << endl
<< " -L --gap-length-threshold N" << endl
<< " If the total length of gaps is >= N, realign (default: ~inf)." << endl
<< " -Z --soft-clip-qsum-max N Accept realignment if qsum of softclips is < N." << endl
<< " -E --mismatch-qsum-max N Accept realignment if qsom of mismatches is < N." << endl
<< " -G --gap-count-max N Accept realignment if number of gaps is < N." << endl
<< " -X --dry-run If realigning, don't output BAM (helps for debugging)." << endl
<< " -O --only-realigned Emit only realigned records (debugging)." << endl
<< " -n --flatten-flank N Use this many bp of dummy sequence when flattening large" << endl
<< " insertions into reference coordinates (default: 2)" << endl;
}
Parameters::Parameters(int argc, char** argv) {
if (argc == 1) {
simpleUsage(argv);
exit(1);
}
// record command line parameters
commandline = argv[0];
for (int i = 1; i < argc; ++i) {
commandline += " ";
commandline += argv[i];
}
// set defaults
// i/o parameters:
read_input = ""; // -s --sequence
fastq_file = ""; // -q --fastq-file
fasta_reference = ""; // -f --fasta-reference
target = ""; // -t --target
vcf_file = ""; // -v --vcf-file
outputFile = ""; // -o --bam-output
// operation parameters
useFile = false; // -x --use-file
alignReverse = false; // -r --reverse-complement
dag_window_size = 1500;
match = 1;
mism = 4;
gap_open = 6;
gap_extend = 1;
display_dag = false;
display_backtrace = false;
display_all_nodes = false;
display_alignment = false;
debug = false;
dry_run = false;
only_realigned = false;
unsorted_output = false;
realign_bam = false;
softclip_qsum_threshold = 10000000;
mismatch_qsum_threshold = 10000000;
softclip_qsum_max = 10000000;
gap_count_threshold = 10000000;
gap_length_threshold = 10000000;
mismatch_qsum_max = 10000000;
gap_count_max = 10000000;
flat_input_vcf = false;
flatten_alignments = true;
flatten_flank = 2;
int c; // counter for getopt
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"sequence", required_argument, 0, 's'},
{"fastq-file", required_argument, 0, 'q'},
{"fasta-reference", required_argument, 0, 'f'},
{"target", required_argument, 0, 't'},
{"vcf-file", required_argument, 0, 'v'},
{"output-file", required_argument, 0, 'o'},
{"reverse-complement", no_argument, 0, 'r'},
{"gap-open", required_argument, 0, 'g'},
{"gap-extend", required_argument, 0, 'x'},
{"match", required_argument, 0, 'm'},
{"mismatch", required_argument, 0, 'M'},
{"display-backtrace", no_argument, 0, 'B'},
{"display-all-nodes", no_argument, 0, 'N'},
{"display-dag", no_argument, 0, 'D'},
{"debug", no_argument, 0, 'd'},
{"dry-run", no_argument, 0, 'X'},
{"realign-bam", no_argument, 0, 'R'},
{"soft-clip-qsum-threshold", required_argument, 0, 'S'},
{"mismatch-qsum-threshold", required_argument, 0, 'Q'},
{"gap-count-threshold", required_argument, 0, 'C'},
{"gap-length-threshold", required_argument, 0, 'L'},
{"soft-clip-qsum-max", required_argument, 0, 'Z'},
{"mismatch-qsum-max", required_argument, 0, 'E'},
{"gap-count-max", required_argument, 0, 'G'},
{"realignment-window", required_argument, 0, 'w'},
{"flatten-alignments", no_argument, 0, 'F'},
{"only-realigned", no_argument, 0, 'O'},
{"unsorted-output", no_argument, 0, 'u'},
{"flatten-flank", required_argument, 0, 'n'},
{"flat-input-vcf", no_argument, 0, 'U'},
{0, 0, 0, 0}
};
while (true) {
int option_index = 0;
c = getopt_long(argc, argv,
"hrXONBDFRuUds:q:f:t:v:o:g:x:m:M:w:Q:S:Z:E:G:n:C:L:",
long_options, &option_index);
if (c == -1) // end of options
break;
switch (c) {
// -s --sequence
case 's':
read_input = optarg;
break;
// -q --fastq-file
case 'q':
fastq_file = optarg;
break;
// -f --fasta-reference
case 'f':
fasta_reference = optarg;
break;
// -t --target
case 't':
target = optarg;
break;
// -v --vcf-file
case 'v':
vcf_file = optarg;
break;
// -o --output-file
case 'o':
outputFile = optarg;
break;
// -u --unsorted-output
case 'u':
unsorted_output = true;
break;
// -r --reverse-complement
case 'r':
alignReverse = true;
break;
// -N --display-all-nodes
case 'N':
display_all_nodes = true;
break;
// -B --display-backtrace
case 'B':
display_backtrace = true;
break;
// -X --dry-run
case 'X':
dry_run = true;
break;
case 'O':
only_realigned = true;
break;
// -D --display-dag
case 'D':
display_dag = true;
break;
// -d --debug
case 'd':
debug = true;
break;
// -m --match
case 'm':
match = atoi(optarg);
break;
// -M --mismatch
case 'M':
mism = atoi(optarg);
break;
// -g --gap-open
case 'g':
gap_open = atoi(optarg);
break;
// -x --gap-extend
case 'x':
gap_extend = atoi(optarg);
break;
// -R --realign-bam
case 'R':
realign_bam = true;
break;
case 'F':
flatten_alignments = true;
break;
case 'U':
flat_input_vcf = true;
break;
case 'n':
flatten_flank = atoi(optarg);
break;
case 'Q':
mismatch_qsum_threshold = atoi(optarg);
break;
case 'S':
softclip_qsum_threshold = atoi(optarg);
break;
case 'C':
gap_count_threshold = atoi(optarg);
break;
case 'L':
gap_length_threshold = atoi(optarg);
break;
case 'Z':
softclip_qsum_max = atoi(optarg);
break;
case 'E':
mismatch_qsum_max = atoi(optarg);
break;
case 'G':
gap_count_max = atoi(optarg);
break;
// -w --realignment-window
case 'w':
dag_window_size = atoi(optarg);
break;
// -h --help
case 'h':
usage(argv);
exit(0);
break;
case '?': // print a suggestion about the most-likely long option which the argument matches
{
string bad_arg(argv[optind - 1]);
option* opt = &long_options[0];
option* closest_opt = opt;
int shortest_distance = levenshteinDistance(opt->name, bad_arg);
++opt;
while (opt->name != 0) {
int distance = levenshteinDistance(opt->name, bad_arg);
if (distance < shortest_distance) {
shortest_distance = distance;
closest_opt = opt;
}
++opt;
}
cerr << "did you mean --" << closest_opt->name << " ?" << endl;
exit(1);
}
break;
default:
abort ();
}
}
// TODO: Add a lot more of this stuff.
if (fasta_reference == "") {
cerr << "Please specify a fasta reference file." << endl;
exit(1);
}
if (useFile == true && fastq_file == "") {
cerr << "Please specify a fastq input file." << endl;
}
}