forked from marbl/canu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.C
473 lines (375 loc) · 16.5 KB
/
sequence.C
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/******************************************************************************
*
* This file is part of canu, a software program that assembles whole-genome
* sequencing reads into contigs.
*
* This software is based on:
* 'Celera Assembler' (http://wgs-assembler.sourceforge.net)
* the 'kmer package' (http://kmer.sourceforge.net)
* both originally distributed by Applera Corporation under the GNU General
* Public License, version 2.
*
* Canu branched from Celera Assembler at its revision 4587.
* Canu branched from the kmer project at its revision 1994.
*
* Modifications by:
*
* Brian P. Walenz beginning on 2018-JUL-21
* are a 'United States Government Work', and
* are released in the public domain
*
* File 'README.licenses' in the root directory of this distribution contains
* full conditions and disclaimers for each license.
*/
#include "sequence/sequence.H"
#include "files.H"
#include "strings.H"
enum opMode {
modeSummarize, // Summarize sequences in FASTA or FASTQ inputs
modeExtract, // Extract sequences or subsequences from FASTA or FASTQ inputs
modeGenerate, // Generate random sequences
modeSimulate, // Simulate reads from FASTA or FASTQ inputs (ontigs/scaffolds/chromosomes)
modeSample, // Extract random sequences from FASTA or FASTQ inputs
modeShift, // Generate sequence based on a shift register
modeMutate, // Randomly mutate bases
modeUnset // Cause an error
};
int
main(int argc, char **argv) {
vector<char *> inputs;
opMode mode = modeUnset;
summarizeParameters sumPar;
extractParameters extPar;
generateParameters genPar;
sampleParameters samPar;
shiftRegisterParameters srPar;
mutateParameters mutPar;
argc = AS_configure(argc, argv);
vector<char *> err;
int arg = 1;
while (arg < argc) {
// SUMMARIZE
if (strcmp(argv[arg], "summarize") == 0) {
mode = modeSummarize;
}
else if ((mode == modeSummarize) && (strcmp(argv[arg], "-size") == 0)) {
sumPar.genomeSize = strtoull(argv[++arg], NULL, 10);
}
else if ((mode == modeSummarize) && (strcmp(argv[arg], "-1x") == 0)) {
sumPar.limitTo1x = true;
}
else if ((mode == modeSummarize) && (strcmp(argv[arg], "-split-n") == 0)) {
sumPar.breakAtN = true;
}
else if ((mode == modeSummarize) && (strcmp(argv[arg], "-assequences") == 0)) {
sumPar.asSequences = true;
sumPar.asBases = false;
}
else if ((mode == modeSummarize) && (strcmp(argv[arg], "-asbases") == 0)) {
sumPar.asSequences = false;
sumPar.asBases = true;
}
// EXTRACT
else if (strcmp(argv[arg], "extract") == 0) {
mode = modeExtract;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-bases") == 0)) {
decodeRange(argv[++arg], extPar.baseBgn, extPar.baseEnd);
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-sequences") == 0)) {
decodeRange(argv[++arg], extPar.seqsBgn, extPar.seqsEnd);
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-reverse") == 0)) {
extPar.asReverse = true;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-complement") == 0)) {
extPar.asComplement = true;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-rc") == 0)) {
extPar.asReverse = true;
extPar.asComplement = true;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-upper") == 0)) {
extPar.asUpperCase = true;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-lower") == 0)) {
extPar.asLowerCase = true;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-length") == 0)) {
decodeRange(argv[++arg], extPar.lensBgn, extPar.lensEnd);
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-lowermask") == 0)) {
extPar.doMasking = true;
extPar.maskWithN = false;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "-nmask") == 0)) {
extPar.doMasking = true;
extPar.maskWithN = true;
}
else if ((mode == modeExtract) && (strcmp(argv[arg], "") == 0)) {
}
// GENERATE
else if (strcmp(argv[arg], "generate") == 0) {
mode = modeGenerate;
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-min") == 0)) {
genPar.minLength = strtouint64(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-max") == 0)) {
genPar.maxLength = strtouint64(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-sequences") == 0)) {
genPar.nSeqs = strtouint64(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-bases") == 0)) {
genPar.nBases = strtouint64(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-guassian") == 0)) {
genPar.useGaussian = true;
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-mirror") == 0)) {
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-gc") == 0)) {
double gc = strtodouble(argv[++arg]);
double at = 1.0 - gc;
genPar.gFreq = genPar.cFreq = gc / 2.0;
genPar.aFreq = genPar.tFreq = at / 2.0;
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-at") == 0)) {
double at = strtodouble(argv[++arg]);
double gc = 1.0 - at;
genPar.gFreq = genPar.cFreq = gc / 2.0;
genPar.aFreq = genPar.tFreq = at / 2.0;
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-a") == 0) ){
genPar.aFreq = strtodouble(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-c") == 0)) {
genPar.cFreq = strtodouble(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-g") == 0)) {
genPar.gFreq = strtodouble(argv[++arg]);
}
else if ((mode == modeGenerate) && (strcmp(argv[arg], "-t") == 0)) {
genPar.tFreq = strtodouble(argv[++arg]);
}
// SIMULATE
else if (strcmp(argv[arg], "simulate") == 0) {
mode = modeSimulate;
}
// SAMPLE
else if (strcmp(argv[arg], "sample") == 0) {
mode = modeSample;
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-paired") == 0)) {
samPar.isPaired = true;
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-output") == 0)) {
strncpy(samPar.output1, argv[++arg], FILENAME_MAX); // #'s in the name will be replaced
strncpy(samPar.output2, argv[ arg], FILENAME_MAX); // by '1' or '2' later.
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-coverage") == 0)) { // Sample reads up to some coverage C
samPar.desiredCoverage = strtodouble(argv[++arg]);
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-genomesize") == 0)) {
samPar.genomeSize = strtouint64(argv[++arg]);
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-bases") == 0)) { // Sample B bases
samPar.desiredNumBases = strtouint64(argv[++arg]);
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-reads") == 0)) { // Sample N reads
samPar.desiredNumReads = strtouint64(argv[++arg]);
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-pairs") == 0)) { // Sample N pairs of reads
samPar.desiredNumReads = strtouint64(argv[++arg]) * 2;
}
else if ((mode == modeSample) && (strcmp(argv[arg], "-fraction") == 0)) { // Sample F fraction
samPar.desiredFraction = strtodouble(argv[++arg]);
}
// SHIFT
else if (strcmp(argv[arg], "shift") == 0) {
mode = modeShift;
}
else if ((mode == modeShift) && (strcmp(argv[arg], "-len") == 0)) {
}
else if ((mode == modeShift) && (strcmp(argv[arg], "-init") == 0)) {
strcpy(srPar.sr, argv[++arg]);
}
else if ((mode == modeShift) && (strcmp(argv[arg], "-map") == 0)) {
strcpy(srPar.sv, argv[++arg]);
}
else if ((mode == modeShift) && (strcmp(argv[arg], "") == 0)) {
}
// MUTATE
else if (strcmp(argv[arg], "mutate") == 0) {
mode = modeMutate;
}
else if ((mode == modeMutate) && (strcmp(argv[arg], "-s") == 0)) {
double p = strtodouble(argv[arg+1]);
char a = argv[arg+2][0];
char b = argv[arg+3][0];
arg += 3;
mutPar.setProbabilitySubstititue(p, a, b);
}
else if ((mode == modeMutate) && (strcmp(argv[arg], "-i") == 0)) {
double p = strtodouble(argv[arg+1]);
char a = argv[arg+2][0];
arg += 2;
mutPar.setProbabilityInsert(p, a);
}
else if ((mode == modeMutate) && (strcmp(argv[arg], "-d") == 0)) {
double p = strtodouble(argv[arg+1]);
char a = argv[arg+2][0];
arg += 2;
mutPar.setProbabilityDelete(p, a);
}
// INPUTS
else if (fileExists(argv[arg]) == true) {
inputs.push_back(argv[arg]);
}
else {
char *s = new char [1024];
snprintf(s, 1024, "ERROR: Unknown parameter '%s'\n", argv[arg]);
err.push_back(s);
}
arg++;
}
if (mode == modeUnset)
err.push_back("ERROR: No mode (summarize, extract, generate or simulate) specified.\n");
if (inputs.size() == 0)
err.push_back("ERROR: No sequence files supplied.\n");
if (inputs.size() == 0)
err.push_back("ERROR: No input files supplied.\n");
if (err.size() > 0) {
fprintf(stderr, "usage: %s [mode] [options] [sequence_file ...]\n", argv[0]);
fprintf(stderr, "\n");
if (mode == modeUnset) {
fprintf(stderr, "MODES:\n");
fprintf(stderr, " summarize report N50, length histogram, mono-, di- and tri-nucleotide frequencies\n");
fprintf(stderr, " extract extract the specified sequences\n");
fprintf(stderr, " sample emit existing sequences randomly\n");
fprintf(stderr, " generate generate random sequences\n");
fprintf(stderr, " simulate errors in existing sequences\n");
fprintf(stderr, "\n");
}
if ((mode == modeUnset) || (mode == modeSummarize)) {
fprintf(stderr, "OPTIONS for summarize mode:\n");
fprintf(stderr, " -size base size to use for N50 statistics\n");
fprintf(stderr, " -1x limit NG table to 1x coverage\n");
fprintf(stderr, " -assequences load data as complete sequences (for testing)\n");
fprintf(stderr, " -asbases load data as blocks of bases (for testing)\n");
fprintf(stderr, "\n");
}
if ((mode == modeUnset) || (mode == modeExtract)) {
fprintf(stderr, "OPTIONS for extract mode:\n");
fprintf(stderr, " -bases baselist extract bases as specified in the 'list' from each sequence\n");
fprintf(stderr, " -sequences seqlist extract ordinal sequences as specified in the 'list'\n");
fprintf(stderr, " -reverse reverse the bases in the sequence\n");
fprintf(stderr, " -complement complement the bases in the sequence\n");
fprintf(stderr, " -rc alias for -reverse -complement\n");
fprintf(stderr, " -upcase\n");
fprintf(stderr, " -downcase\n");
fprintf(stderr, " -length min max print sequence if it is at least 'min' bases and at most 'max' bases long\n");
fprintf(stderr, " \n");
fprintf(stderr, " a 'baselist' is a set of integers formed from any combination\n");
fprintf(stderr, " of the following, seperated by a comma:\n");
fprintf(stderr, " num a single number\n");
fprintf(stderr, " bgn-end a range of numbers: bgn <= end\n");
fprintf(stderr, " bases are spaced-based; -bases 0-2,4 will print the bases between\n");
fprintf(stderr, " the first two spaces (the first two bases) and the base after the\n");
fprintf(stderr, " fourth space (the fifth base).\n");
fprintf(stderr, " \n");
fprintf(stderr, " a 'seqlist' is a set of integers formed from any combination\n");
fprintf(stderr, " of the following, seperated by a comma:\n");
fprintf(stderr, " num a single number\n");
fprintf(stderr, " bgn-end a range of numbers: bgn <= end\n");
fprintf(stderr, " sequences are 1-based; -sequences 1,3-5 will print the first, third,\n");
fprintf(stderr, " fourth and fifth sequences.\n");
fprintf(stderr, " \n");
}
if ((mode == modeUnset) || (mode == modeSample)) {
fprintf(stderr, "OPTIONS for sample mode:\n");
fprintf(stderr, " -paired treat inputs as paired sequences; the first two files form the\n");
fprintf(stderr, " first pair, and so on.\n");
fprintf(stderr, "\n");
fprintf(stderr, " -output O write output sequences to file O. If paired, two files must be supplied.\n");
fprintf(stderr, "\n");
fprintf(stderr, " -coverage C output C coverage of sequences, based on genome size G.\n");
fprintf(stderr, " -genomesize G \n");
fprintf(stderr, "\n");
fprintf(stderr, " -bases B output B bases.\n");
fprintf(stderr, "\n");
fprintf(stderr, " -reads R output R reads.\n");
fprintf(stderr, " -pairs P output P pairs (only if -paired).\n");
fprintf(stderr, "\n");
fprintf(stderr, " -fraction F output fraction F of the input bases.\n");
fprintf(stderr, "\n");
}
if ((mode == modeUnset) || (mode == modeGenerate)) {
fprintf(stderr, "OPTIONS for generate mode:\n");
fprintf(stderr, " -min M minimum sequence length\n");
fprintf(stderr, " -max M maximum sequence length\n");
fprintf(stderr, " -sequences N generate N sequences\n");
fprintf(stderr, " -bases B generate at least B bases, no more than B+maxLength-1 bases.\n");
fprintf(stderr, " -gaussian 99.73%% of the reads (3 standard deviations) will be between min and max\n");
fprintf(stderr, " -mirror F \n");
fprintf(stderr, " -gc bias sets GC/AT composition (default 0.50)\n");
fprintf(stderr, " -at bias sets GC/AT composition (default 0.50)\n");
fprintf(stderr, " -a freq sets frequency of A bases (default 0.25)\n");
fprintf(stderr, " -c freq sets frequency of C bases (default 0.25)\n");
fprintf(stderr, " -g freq sets frequency of G bases (default 0.25)\n");
fprintf(stderr, " -t freq sets frequency of T bases (default 0.25)\n");
fprintf(stderr, "\n");
fprintf(stderr, "The -gc option is a shortcut for setting all four base frequencies at once. Order matters!\n");
fprintf(stderr, " -gc 0.6 -a 0.1 -t 0.3 -- sets G = C = 0.3, A = 0.1, T = 0.3\n");
fprintf(stderr, " -a 0.1 -t 0.3 -gc 0.6 -- sets G = C = 0.3, A = T = 0.15\n");
fprintf(stderr, "\n");
fprintf(stderr, "Base frequencies are scaled to sum to 1.0.\n");
fprintf(stderr, " -a 1.25 -- results in a sum of 2.0 (1.25 + 0.25 + 0.25 + 0.25) so final frequencies will be:\n");
fprintf(stderr, " A = 1.25/2 = 0.625\n");
fprintf(stderr, " C = G = T = 0.25/2 = 0.125.\n");
fprintf(stderr, " -gc 0.8 -a 1.0 -t 0.2 -- sum is also 2.0, final frequencies will be:\n");
fprintf(stderr, " A = 1.00/2 = 0.5\n");
fprintf(stderr, " C = G = 0.40/2 = 0.2\n");
fprintf(stderr, " T = 0.20/2 = 0.1\n");
fprintf(stderr, "\n");
}
if ((mode == modeUnset) || (mode == modeSimulate)) {
fprintf(stderr, "OPTIONS for simulate mode:\n");
fprintf(stderr, "\n");
}
for (uint32 ii=0; ii<err.size(); ii++)
if (err[ii])
fputs(err[ii], stderr);
exit(1);
}
sumPar.finalize();
genPar.finalize();
extPar.finalize();
switch (mode) {
case modeSummarize:
doSummarize(inputs, sumPar);
break;
case modeExtract:
doExtract(inputs, extPar);
break;
case modeGenerate:
doGenerate(genPar);
break;
case modeSimulate:
doSimulate(inputs);
break;
case modeSample:
doSample(inputs, samPar);
break;
case modeShift:
doShiftRegister(srPar);
break;
case modeMutate:
doMutate(inputs, mutPar);
break;
default:
break;
}
return(0);
}