-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathflash2.c
1495 lines (1370 loc) · 50.1 KB
/
flash2.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* flash.c: parse arguments and set up and run the FLASH pipeline.
*
* Please see combine_reads.c if you are looking for the core algorithm used to
* combine reads in FLASH.
*/
/*
* Copyright (C) 2012 Tanja Magoc
* Copyright (C) 2012, 2013, 2014 Eric Biggers
*
* This file is part of FLASH, a fast tool to merge overlapping paired-end
* reads.
*
* FLASH is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* FLASH is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with FLASH; if not, see http://www.gnu.org/licenses/.
*/
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "combine_reads.h"
#include "iostream.h"
#include "read.h"
#include "read_io.h"
#include "read_queue.h"
#include "util.h"
#define VERSION_STR "v2.2.00"
#ifdef __WIN32__
# define PAGER "more"
#else
# define PAGER "less"
#endif
#define TO_PERCENT(n, d) \
((d) == 0 ? 0 : ((double)(n) * 100 / (d)))
static void
usage(const char *argv0)
{
const char *usage_str =
"Usage: flash [OPTIONS] MATES_1.FASTQ MATES_2.FASTQ\n"
" flash [OPTIONS] --interleaved-input (MATES.FASTQ | -)\n"
" flash [OPTIONS] --tab-delimited-input (MATES.TAB | -)\n"
"\n"
"----------------------------------------------------------------------------\n"
" DESCRIPTION \n"
"----------------------------------------------------------------------------\n"
"\n"
"FLASH (Fast Length Adjustment of SHort reads) is an accurate and fast tool\n"
"to merge paired-end reads that were generated from DNA fragments whose\n"
"lengths are shorter than twice the length of reads. Merged read pairs result\n"
"in unpaired longer reads, which are generally more desired in genome\n"
"assembly and genome analysis processes.\n"
"\n"
"Briefly, the FLASH algorithm considers all possible overlaps at or above a\n"
"minimum length between the reads in a pair and chooses the overlap that\n"
"results in the lowest mismatch density (proportion of mismatched bases in\n"
"the overlapped region). Ties between multiple overlaps are broken by\n"
"considering quality scores at mismatch sites. When building the merged\n"
"sequence, FLASH computes a consensus sequence in the overlapped region.\n"
"More details can be found in the original publication\n"
"(http://bioinformatics.oxfordjournals.org/content/27/21/2957.full).\n"
"\n"
"Limitations of FLASH include:\n"
" - FLASH cannot merge paired-end reads that do not overlap.\n"
" - FLASH is not designed for data that has a significant amount of indel\n"
" errors (such as Sanger sequencing data). It is best suited for Illumina\n"
" data.\n"
"\n"
"----------------------------------------------------------------------------\n"
" MANDATORY INPUT\n"
"----------------------------------------------------------------------------\n"
"\n"
"The most common input to FLASH is two FASTQ files containing read 1 and read 2\n"
"of each mate pair, respectively, in the same order.\n"
"\n"
"Alternatively, you may provide one FASTQ file, which may be standard input,\n"
"containing paired-end reads in either interleaved FASTQ (see the\n"
"--interleaved-input option) or tab-delimited (see the --tab-delimited-input\n"
"option) format. In all cases, gzip compressed input is autodetected. Also,\n"
"in all cases, the PHRED offset is, by default, assumed to be 33; use the\n"
"--phred-offset option to change it.\n"
"\n"
"----------------------------------------------------------------------------\n"
" OUTPUT\n"
"----------------------------------------------------------------------------\n"
"\n"
"The default output of FLASH consists of the following files:\n"
"\n"
" - out.extendedFrags.fastq The merged reads.\n"
" - out.notCombined_1.fastq Read 1 of mate pairs that were not merged.\n"
" - out.notCombined_2.fastq Read 2 of mate pairs that were not merged.\n"
" - out.hist Numeric histogram of merged read lengths.\n"
" - out.histogram Visual histogram of merged read lengths.\n"
"\n"
"FLASH also logs informational messages to standard output. These can also be\n"
"redirected to a file, as in the following example:\n"
"\n"
" $ flash reads_1.fq reads_2.fq 2>&1 | tee flash.log\n"
"\n"
"In addition, FLASH supports several features affecting the output:\n"
"\n"
" - Writing the merged reads directly to standard output (--to-stdout)\n"
" - Writing gzip compressed output files (-z) or using an external\n"
" compression program (--compress-prog)\n"
" - Writing the uncombined read pairs in interleaved FASTQ format\n"
" (--interleaved-output)\n"
" - Writing all output reads to a single file in tab-delimited format\n"
" (--tab-delimited-output)\n"
"\n"
"----------------------------------------------------------------------------\n"
" OPTIONS\n"
"----------------------------------------------------------------------------\n"
"\n"
" -Q, --quality-cutoff=NUM The cut off number for the quality score\n"
" corresponding wtih the percent cutoff. Default:\n"
" 2.\n"
" -C, --percent-cutoff=NUM The cutoff percentage for each read that will\n"
" be discarded if it falls below -Q option. (0-100) Default:\n"
" 50.\n"
" -D, --no-discard This turns off the discard logic Default: false\n"
"\n"
" -m, --min-overlap=NUM The minimum required overlap length between two\n"
" reads to provide a confident overlap. Default 10bp.\n"
"\n"
" -M, --max-overlap=NUM Maximum overlap length expected in approximately\n"
" 90% of read pairs. It is by default set to 65bp,\n"
" which works well for 100bp reads generated from a\n"
" 180bp library, assuming a normal distribution of\n"
" fragment lengths. Overlaps longer than the maximum\n"
" overlap parameter are still considered as good\n"
" overlaps, but the mismatch density (explained below)\n"
" is calculated over the first max_overlap bases in\n"
" the overlapped region rather than the entire\n"
" overlap. Default: 65bp, or calculated from the\n"
" specified read length, fragment length, and fragment\n"
" length standard deviation.\n"
"\n"
" -e, --min-overlap-outie=NUM The minimum required overlap length between two\n"
" reads to provide a confident overlap in an outie scenario.\n"
" Default: 35bp.\n"
"\n"
" -x, --max-mismatch-density=NUM\n"
" Maximum allowed ratio between the number of\n"
" mismatched base pairs and the overlap length.\n"
" Two reads will not be combined with a given overlap\n"
" if that overlap results in a mismatched base density\n"
" higher than this value. Note: Any occurence of an\n"
" 'N' in either read is ignored and not counted\n"
" towards the mismatches or overlap length. Our\n"
" experimental results suggest that higher values of\n"
" the maximum mismatch density yield larger\n"
" numbers of correctly merged read pairs but at\n"
" the expense of higher numbers of incorrectly\n"
" merged read pairs. Default: 0.25.\n"
"\n"
" -O, --allow-outies Also try combining read pairs in the \"outie\"\n"
" orientation, e.g.\n"
"\n"
" Read 1: <-----------\n"
" Read 2: ------------>\n"
"\n"
" as opposed to only the \"innie\" orientation, e.g.\n"
"\n"
" Read 1: <------------\n"
" Read 2: ----------->\n"
"\n"
" FLASH uses the same parameters when trying each\n"
" orientation. If a read pair can be combined in\n"
" both \"innie\" and \"outie\" orientations, the\n"
" better-fitting one will be chosen using the same\n"
" scoring algorithm that FLASH normally uses.\n"
"\n"
" This option also causes extra .innie and .outie\n"
" histogram files to be produced.\n"
"\n"
" -p, --phred-offset=OFFSET\n"
" The smallest ASCII value of the characters used to\n"
" represent quality values of bases in FASTQ files.\n"
" It should be set to either 33, which corresponds\n"
" to the later Illumina platforms and Sanger\n"
" platforms, or 64, which corresponds to the\n"
" earlier Illumina platforms. Default: 33.\n"
"\n"
" -r, --read-len=LEN\n"
" -f, --fragment-len=LEN\n"
" -s, --fragment-len-stddev=LEN\n"
" Average read length, fragment length, and fragment\n"
" standard deviation. These are convenience parameters\n"
" only, as they are only used for calculating the\n"
" maximum overlap (--max-overlap) parameter.\n"
" The maximum overlap is calculated as the overlap of\n"
" average-length reads from an average-size fragment\n"
" plus 2.5 times the fragment length standard\n"
" deviation. The default values are -r 100, -f 180,\n"
" and -s 18, so this works out to a maximum overlap of\n"
" 65 bp. If --max-overlap is specified, then the\n"
" specified value overrides the calculated value.\n"
"\n"
" If you do not know the standard deviation of the\n"
" fragment library, you can probably assume that the\n"
" standard deviation is 10% of the average fragment\n"
" length.\n"
"\n"
" --cap-mismatch-quals Cap quality scores assigned at mismatch locations\n"
" to 2. This was the default behavior in FLASH v1.2.7\n"
" and earlier. Later versions will instead calculate\n"
" such scores as max(|q1 - q2|, 2); that is, the\n"
" absolute value of the difference in quality scores,\n"
" but at least 2. Essentially, the new behavior\n"
" prevents a low quality base call that is likely a\n"
" sequencing error from significantly bringing down\n"
" the quality of a high quality, likely correct base\n"
" call.\n"
"\n"
" --interleaved-input Instead of requiring files MATES_1.FASTQ and\n"
" MATES_2.FASTQ, allow a single file MATES.FASTQ that\n"
" has the paired-end reads interleaved. Specify \"-\"\n"
" to read from standard input.\n"
"\n"
" --interleaved-output Write the uncombined pairs in interleaved FASTQ\n"
" format.\n"
"\n"
" -I, --interleaved Equivalent to specifying both --interleaved-input\n"
" and --interleaved-output.\n"
"\n"
" -Ti, --tab-delimited-input\n"
" Assume the input is in tab-delimited format\n"
" rather than FASTQ, in the format described below in\n"
" '--tab-delimited-output'. In this mode you should\n"
" provide a single input file, each line of which must\n"
" contain either a read pair (5 fields) or a single\n"
" read (3 fields). FLASH will try to combine the read\n"
" pairs. Single reads will be written to the output\n"
" file as-is if also using --tab-delimited-output;\n"
" otherwise they will be ignored. Note that you may\n"
" specify \"-\" as the input file to read the\n"
" tab-delimited data from standard input.\n"
"\n"
" -To, --tab-delimited-output\n"
" Write output in tab-delimited format (not FASTQ).\n"
" Each line will contain either a combined pair in the\n"
" format 'tag <tab> seq <tab> qual' or an uncombined\n"
" pair in the format 'tag <tab> seq_1 <tab> qual_1\n"
" <tab> seq_2 <tab> qual_2'.\n"
"\n"
" -o, --output-prefix=PREFIX\n"
" Prefix of output files. Default: \"out\".\n"
"\n"
" -d, --output-directory=DIR\n"
" Path to directory for output files. Default:\n"
" current working directory.\n"
"\n"
" -c, --to-stdout Write the combined reads to standard output. In\n"
" this mode, with FASTQ output (the default) the\n"
" uncombined reads are discarded. With tab-delimited\n"
" output, uncombined reads are included in the\n"
" tab-delimited data written to standard output.\n"
" In both cases, histogram files are not written,\n"
" and informational messages are sent to standard\n"
" error rather than to standard output.\n"
"\n"
" -z, --compress Compress the output files directly with zlib,\n"
" using the gzip container format. Similar to\n"
" specifying --compress-prog=gzip and --suffix=gz,\n"
" but may be slightly faster.\n"
"\n"
" --compress-prog=PROG Pipe the output through the compression program\n"
" PROG, which will be called as `PROG -c -',\n"
" plus any arguments specified by --compress-prog-args.\n"
" PROG must read uncompressed data from standard input\n"
" and write compressed data to standard output when\n"
" invoked as noted above.\n"
" Examples: gzip, bzip2, xz, pigz.\n"
"\n"
" --compress-prog-args=ARGS\n"
" A string of additional arguments that will be passed\n"
" to the compression program if one is specified with\n"
" --compress-prog=PROG. (The arguments '-c -' are\n"
" still passed in addition to explicitly specified\n"
" arguments.)\n"
"\n"
" --suffix=SUFFIX, --output-suffix=SUFFIX\n"
" Use SUFFIX as the suffix of the output files\n"
" after \".fastq\". A dot before the suffix is assumed,\n"
" unless an empty suffix is provided. Default:\n"
" nothing; or 'gz' if -z is specified; or PROG if\n"
" --compress-prog=PROG is specified.\n"
"\n"
" -t, --threads=NTHREADS Set the number of worker threads. This is in\n"
" addition to the I/O threads. Default: number of\n"
" processors. Note: if you need FLASH's output to\n"
" appear deterministically or in the same order as\n"
" the original reads, you must specify -t 1\n"
" (--threads=1).\n"
"\n"
" -q, --quiet Do not print informational messages.\n"
"\n"
" -h, --help Display this help and exit.\n"
"\n"
" -v, --version Display version.\n"
;
fputs(usage_str, stdout);
if (isatty(STDOUT_FILENO)) {
/* Just to be extra user-friendly... */
printf("\nRun `%s --help | "PAGER"' to "
"prevent this text from scrolling by.\n", argv0);
}
}
static void
usage_short(const char *argv0)
{
fprintf(stderr,
"Usage: flash [OPTIONS] MATES_1.FASTQ MATES_2.FASTQ\n"
"Run `%s --help | "PAGER"' for more information.\n", argv0);
}
static void
version(void)
{
fputs(
"FLASH "VERSION_STR"\n"
"Copyright (C) 2012 Tanja Magoc\n"
"Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
"Modifications 2015 David Streett\n"
"License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"Report bugs to flash.comment@gmail.com or https://sourceforge.net/p/flashpage/bugs\n"
, stdout);
}
enum {
INTERLEAVED_INPUT_OPTION = 257,
INTERLEAVED_OUTPUT_OPTION,
CAP_MISMATCH_QUALS_OPTION,
COMPRESS_PROG_OPTION,
COMPRESS_PROG_ARGS_OPTION,
SUFFIX_OPTION,
TAB_DELIMITED_INPUT_OPTION,
TAB_DELIMITED_OUTPUT_OPTION,
};
//David Modification added e --min-overlap-engufl to arguements
static const char *optstring = "m:M:e:x:p:Or:f:s:IT:o:d:czt:qhvQ:C:DS";
static const struct option longopts[] = {
{"quality-cutoff", required_argument, NULL, 'Q'},
{"percent-cutoff", required_argument, NULL, 'C'},
{"no-discard", required_argument, NULL, 'D'},
{"min-overlap", required_argument, NULL, 'm'},
{"min-overlap", required_argument, NULL, 'm'},
{"min-overlap-outie", required_argument, NULL, 'e'},
{"max-overlap", required_argument, NULL, 'M'},
{"max-mismatch-density", required_argument, NULL, 'x'},
{"phred-offset", required_argument, NULL, 'p'},
{"allow-outies", no_argument, NULL, 'O'},
{"skip-overlap", required_argument, NULL, 'S'},
{"read-len", required_argument, NULL, 'r'},
{"fragment-len", required_argument, NULL, 'f'},
{"fragment-len-stddev", required_argument, NULL, 's'},
{"cap-mismatch-quals", no_argument, NULL, CAP_MISMATCH_QUALS_OPTION},
{"interleaved", no_argument, NULL, 'I'},
{"interleaved-input", no_argument, NULL, INTERLEAVED_INPUT_OPTION},
{"interleaved-output", no_argument, NULL, INTERLEAVED_OUTPUT_OPTION},
{"tab-delimited-input", no_argument, NULL, TAB_DELIMITED_INPUT_OPTION},
{"tab-delimited-output", no_argument, NULL, TAB_DELIMITED_OUTPUT_OPTION},
{"output-prefix", required_argument, NULL, 'o'},
{"output-directory", required_argument, NULL, 'd'},
{"to-stdout", no_argument, NULL, 'c'},
{"compress", no_argument, NULL, 'z'},
{"compress-prog", required_argument, NULL, COMPRESS_PROG_OPTION},
{"compress-prog-args", required_argument, NULL, COMPRESS_PROG_ARGS_OPTION},
{"suffix", required_argument, NULL, SUFFIX_OPTION},
{"output-suffix", required_argument, NULL, SUFFIX_OPTION},
{"threads", required_argument, NULL, 't'},
{"quiet", no_argument, NULL, 'q'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
static char *
input_format_str(char *buf, size_t bufsize,
const struct read_format_params *iparams,
bool interleaved)
{
switch (iparams->fmt) {
case READ_FORMAT_FASTQ:
snprintf(buf, bufsize, "FASTQ, phred_offset=%d%s",
iparams->phred_offset,
(interleaved ? ", interleaved" : ""));
break;
case READ_FORMAT_TAB_DELIMITED:
snprintf(buf, bufsize, "Tab-delimited, phred_offset=%d",
iparams->phred_offset);
break;
default:
assert(0);
break;
}
return buf;
}
static char *
output_format_str(char *buf, size_t bufsize,
const struct read_format_params *oparams,
bool interleaved_output,
enum out_compression_type out_ctype,
const char *compress_prog,
const char *compress_prog_args)
{
input_format_str(buf, bufsize, oparams, interleaved_output);
switch (out_ctype) {
case OUT_COMPRESSION_NONE:
if (compress_prog) {
char *p = strchr(buf, '\0');
snprintf(p, &buf[bufsize] - p,
", filtered through '%s %s'",
compress_prog, compress_prog_args);
}
break;
case OUT_COMPRESSION_GZIP:
{
char *p = strchr(buf, '\0');
snprintf(p, &buf[bufsize] - p, ", gzip");
}
break;
}
return buf;
}
/* This is just a dynamic array used as a histogram. It's needed to count the
* frequencies of the lengths of the combined reads. */
struct histogram {
uint64_t *array;
size_t len;
};
static void
hist_init(struct histogram *hist)
{
hist->array = NULL;
hist->len = 0;
}
static void
hist_destroy(struct histogram *hist)
{
xfree(hist->array, hist->len * sizeof(hist->array[0]));
}
static void
hist_add(struct histogram *hist, size_t idx, uint64_t amount)
{
uint64_t *array = hist->array;
size_t old_len = hist->len;
if (idx >= old_len) {
size_t new_len = idx + 1;
array = xrealloc(array, new_len * sizeof(array[0]));
memset(&array[old_len], 0,
(new_len - old_len) * sizeof(array[0]));
hist->len = new_len;
hist->array = array;
}
array[idx] += amount;
}
static void
hist_inc(struct histogram *hist, size_t idx)
{
hist_add(hist, idx, 1);
}
static void
hist_combine(struct histogram *hist, const struct histogram *other)
{
for (size_t i = 0; i < other->len; i++)
hist_add(hist, i, other->array[i]);
}
static uint64_t
hist_count_at(const struct histogram *hist, size_t idx)
{
assert(idx < hist->len);
return hist->array[idx];
}
static void
hist_stats(const struct histogram *hist, uint64_t *max_freq_ret,
long *first_nonzero_idx_ret, long *last_nonzero_idx_ret)
{
*max_freq_ret = 0;
*first_nonzero_idx_ret = -1;
*last_nonzero_idx_ret = -2;
for (size_t i = 1; i < hist->len; i++) {
uint64_t freq = hist->array[i];
if (freq != 0) {
if (*first_nonzero_idx_ret == -1)
*first_nonzero_idx_ret = i;
*last_nonzero_idx_ret = i;
if (freq > *max_freq_ret)
*max_freq_ret = freq;
}
}
}
static void
write_hist_file(const char *hist_file, const struct histogram *hist,
long first_nonzero_idx, long last_nonzero_idx)
{
FILE *fp = xfopen(hist_file, "w");
for (long i = first_nonzero_idx; i <= last_nonzero_idx; i++) {
uint64_t count = hist_count_at(hist, i);
if (count != 0)
if (fprintf(fp, "%ld\t%"PRIu64"\n", i, count) < 0)
goto write_error;
}
xfclose(fp, hist_file);
return;
write_error:
fatal_error_with_errno("Error writing to \"%s\"", hist_file);
}
static void
write_histogram_file(const char *histogram_file, const struct histogram *hist,
long first_nonzero_idx, long last_nonzero_idx,
uint64_t max_freq)
{
const double max_num_asterisks = 72;
double scale = max_num_asterisks / (double)max_freq;
FILE *fp = xfopen(histogram_file, "w");
for (long i = first_nonzero_idx; i <= last_nonzero_idx; i++) {
if (fprintf(fp, "%ld\t", i) < 0)
goto write_error;
size_t num_asterisks = (size_t)(scale * (double)hist_count_at(hist, i));
while (num_asterisks--)
if (fputc('*', fp) == EOF)
goto write_error;
if (fputc('\n', fp) == EOF)
goto write_error;
}
xfclose(fp, histogram_file);
return;
write_error:
fatal_error_with_errno("Error writing to \"%s\"", histogram_file);
}
struct flash_stats {
struct histogram innie_lens;
struct histogram outie_lens;
struct histogram overlap_lens;
uint64_t num_uncombined;
uint64_t num_innie;
uint64_t num_outie;
uint64_t num_discarded;
};
static void
flash_stats_init(struct flash_stats *stats)
{
hist_init(&stats->innie_lens);
hist_init(&stats->outie_lens);
hist_init(&stats->overlap_lens);
stats->num_uncombined = 0;
stats->num_innie = 0;
stats->num_outie = 0;
stats->num_discarded = 0;
}
static void
flash_stats_combine(struct flash_stats *stats, const struct flash_stats *other)
{
hist_combine(&stats->innie_lens, &other->innie_lens);
hist_combine(&stats->outie_lens, &other->outie_lens);
hist_combine(&stats->overlap_lens, &other->overlap_lens);
stats->num_uncombined += other->num_uncombined;
stats->num_innie += other->num_innie;
stats->num_outie += other->num_outie;
stats->num_discarded += other->num_discarded;
}
static void
flash_stats_destroy(struct flash_stats *stats)
{
hist_destroy(&stats->innie_lens);
hist_destroy(&stats->outie_lens);
hist_destroy(&stats->overlap_lens);
}
struct common_combiner_thread_params {
struct read_io_handle *iohandle;
struct combine_params alg_params;
};
struct combiner_thread_params {
struct common_combiner_thread_params *common;
struct flash_stats *stats;
};
/* Buffer for read_sets for which all the read pointers have been invalidated.
*/
struct empty_sets {
struct read_set *q1[2];
struct read_set *q2[2];
};
static void
hold_empty_set(struct read_set *q[2], struct read_set *s)
{
if (!q[0]) {
q[0] = s;
} else {
assert(!q[1]);
q[1] = s;
}
}
static void
hold_empty_sets(struct empty_sets *e, struct read_set *s1, struct read_set *s2)
{
hold_empty_set(e->q1, s1);
hold_empty_set(e->q2, s2);
}
static struct read_set *
get_empty_set(struct read_set *q[2])
{
struct read_set *s;
assert(q[0]);
s = q[0];
q[0] = q[1];
q[1] = NULL;
return s;
}
static void
get_empty_sets(struct empty_sets *e,
struct read_set **s1_ret, struct read_set **s2_ret)
{
*s1_ret = get_empty_set(e->q1);
*s2_ret = get_empty_set(e->q2);
}
static void
free_empty_sets(struct empty_sets *e)
{
free_read_set(e->q1[0]);
free_read_set(e->q1[1]);
free_read_set(e->q2[0]);
free_read_set(e->q2[1]);
}
/* This procedure is executed in parallel by all the combiner threads. */
static void *
combiner_thread_proc(void *_params)
{
struct combiner_thread_params *params = _params;
struct flash_stats *stats = params->stats;
struct read_io_handle *iohandle = params->common->iohandle;
const struct combine_params *alg_params = ¶ms->common->alg_params;
struct read_set *s_avail_1 = new_empty_read_set(iohandle);
struct read_set *s_avail_2 = new_empty_read_set(iohandle);
struct read_set *s_uncombined_1 = new_empty_read_set(iohandle);
struct read_set *s_uncombined_2 = new_empty_read_set(iohandle);
struct read_set *s_combined = get_avail_read_set(iohandle);
struct empty_sets empty = {};
struct read_set *s1;
struct read_set *s2;
/* While there are read pairs to process ... */
while (get_unprocessed_read_pairs(iohandle, &s1, &s2)) {
/* ... process each read pair. */
for (size_t i = 0; i < s1->filled; i++) {
struct read *r1 = s1->reads[i];
struct read *r2 = s2->reads[i];
struct read *r_combined;
enum combine_status status;
s1->reads[i] = NULL;
s2->reads[i] = NULL;
reverse_complement(r2);
/* Get available read in which to try the combination.
*/
r_combined = s_combined->reads[s_combined->filled];
/* Try combining the reads. */
status = combine_reads(r1, r2, r_combined, alg_params);
switch (status) {
case COMBINED_AS_INNIE:
stats->num_innie++;
hist_inc(&stats->innie_lens, r_combined->seq_len);
goto combined;
case COMBINED_AS_OUTIE:
stats->num_outie++;
hist_inc(&stats->outie_lens, r_combined->seq_len);
goto combined;
combined:
/* Combination was successful. */
hist_inc(&stats->overlap_lens,
r1->seq_len + r2->seq_len - r_combined->seq_len);
/* Uncombined read structures are unneeded; mark
* them as available. */
if (s_avail_1->filled == s_avail_1->num_reads) {
put_avail_read_pairs(iohandle,
s_avail_1,
s_avail_2);
get_empty_sets(&empty,
&s_avail_1,
&s_avail_2);
}
s_avail_1->reads[s_avail_1->filled++] = r1;
s_avail_2->reads[s_avail_2->filled++] = r2;
/* Compute tag for combined read. */
get_combined_tag(r1, r2, r_combined);
/* Send combined read. */
if (++s_combined->filled == s_combined->num_reads) {
put_combined_reads(iohandle, s_combined);
s_combined = get_avail_read_set(iohandle);
}
break;
case NOT_COMBINED:
stats->num_uncombined++;
/* Send uncombined reads. */
if (s_uncombined_1->filled == s_uncombined_1->num_reads) {
put_uncombined_read_pairs(iohandle,
s_uncombined_1,
s_uncombined_2);
get_empty_sets(&empty,
&s_uncombined_1,
&s_uncombined_2);
}
s_uncombined_1->reads[s_uncombined_1->filled++] = r1;
s_uncombined_2->reads[s_uncombined_2->filled++] = r2;
reverse_complement(r2);
break;
case JUNK_READ:
stats->num_discarded++;
if (s_avail_1->filled == s_avail_1->num_reads) {
put_avail_read_pairs(iohandle,
s_avail_1,
s_avail_2);
get_empty_sets(&empty,
&s_avail_1,
&s_avail_2);
}
s_avail_1->reads[s_avail_1->filled++] = r1;
s_avail_2->reads[s_avail_2->filled++] = r2;
break;
}
}
s1->filled = 0;
s2->filled = 0;
hold_empty_sets(&empty, s1, s2);
}
/* No more reads to combine. */
/* Free read sets owned by this thread */
free_read_set(s_avail_1);
free_read_set(s_avail_2);
free_empty_sets(&empty);
/* Send out any remaining uncombined and combined reads.
* If there are none, free the corresponding read sets. */
if (s_uncombined_1->filled) {
put_uncombined_read_pairs(iohandle, s_uncombined_1, s_uncombined_2);
} else {
free_read_set(s_uncombined_1);
free_read_set(s_uncombined_2);
}
if (s_combined->filled)
put_combined_reads(iohandle, s_combined);
else
free_read_set(s_combined);
notify_combiner_terminated(iohandle);
xfree(params, sizeof(*params));
return NULL;
}
static void
warn_about_long_overlaps(const struct flash_stats *stats, int max_overlap)
{
uint64_t long_overlap_count;
double long_overlap_percent;
long_overlap_count = 0;
for (size_t i = max_overlap + 1; i < stats->overlap_lens.len; i++)
long_overlap_count += hist_count_at(&stats->overlap_lens, i);
long_overlap_percent = TO_PERCENT(long_overlap_count,
stats->num_innie + stats->num_outie);
if (long_overlap_percent > 10.0) {
warning("An unexpectedly high proportion of combined pairs "
"(%.2f%%)\n"
"overlapped by more than %d bp, the --max-overlap (-M) "
"parameter. Consider\n"
"increasing this parameter. (As-is, FLASH is "
"penalizing overlaps longer than\n"
"%d bp when considering them for possible combining!)",
long_overlap_percent, max_overlap, max_overlap);
}
}
int
main(int argc, char **argv)
{
infofile = stdout;
const char *argv0 = argv[0];
struct combine_params alg_params = {
.max_overlap = 0,
.min_overlap = 10,
.min_overlap_outie = 35,
.max_mismatch_density = 0.25,
.cap_mismatch_quals = false,
.allow_outies = true,
.discard_reads = true,
.skip_overlap = false,
.qual_score_cutoff = 2,
.percent_cutoff = 50,
};
bool max_overlap_specified = false;
struct read_format_params iparams = {
.fmt = READ_FORMAT_FASTQ,
.phred_offset = 33,
};
struct read_format_params oparams = {
.fmt = READ_FORMAT_FASTQ,
.phred_offset = 33,
};
int read_len = 100;
int fragment_len = 180;
int fragment_len_stddev = 18;
bool read_len_specified = false;
bool fragment_len_specified = false;
bool fragment_len_stddev_specified = false;
const char *prefix = "out";
const char *output_dir = ".";
bool to_stdout = false;
bool verbose = true;
bool interleaved_input = false;
bool interleaved_output = false;
struct input_stream *mates1_in = NULL;
struct input_stream *mates2_in = NULL;
enum out_compression_type out_ctype = OUT_COMPRESSION_NONE;
const char *compress_prog = NULL;
char *compress_prog_args = "-c -";
bool compress_prog_args_allocated = false;
struct output_stream *out_combined = NULL;
struct output_stream *out_notcombined_1 = NULL;
struct output_stream *out_notcombined_2 = NULL;
const char *out_filetype = "fastq";
char *out_suffix = "";
bool out_suffix_allocated = false;
unsigned long num_combiner_threads = 0;
int c;
char *tmp;
struct timeval start_time;
gettimeofday(&start_time, NULL);
/*
int qual_score_cut_off = 2;
int percent_cutoff = 50;
bool discard_reads = true;
*/
/*Adapter trimming overlap is always on*/
while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
switch (c) {
case 'Q':
alg_params.qual_score_cutoff = strtol(optarg, &tmp, 10);
if (tmp == optarg || *tmp || alg_params.qual_score_cutoff < 1)
fatal_error("Quality score cutoff must be a "
"positive integer! Please check "
"option -Q.");
break;
case 'D':
alg_params.discard_reads = false;
break;
case 'C':
alg_params.percent_cutoff = strtol(optarg, &tmp, 10);
if (tmp == optarg || *tmp || alg_params.percent_cutoff < 1 || alg_params.percent_cutoff > 100)
fatal_error("Percent Cutoff must be a "
"positive integer (0-100)! Please check "
"option -C.");
break;
case 'm':
alg_params.min_overlap = strtol(optarg, &tmp, 10);
if (tmp == optarg || *tmp || alg_params.min_overlap < 1)
fatal_error("Minimum overlap must be a "
"positive integer! Please check "
"option -m.");
break;
case 'e':
alg_params.min_overlap_outie = strtol(optarg, &tmp, 10);
if (tmp == optarg || *tmp || alg_params.min_overlap_outie < 1)
fatal_error("Minimum overlap outie must be a "
"positive integer! Please check "
"option -e.");
break;
case 'M':
alg_params.max_overlap = strtol(optarg, &tmp, 10);
if (tmp == optarg || *tmp || alg_params.max_overlap < 1)
fatal_error("Maximum overlap must be "
"a positive integer! Please check "
"option -M.");
max_overlap_specified = true;
break;
case 'x':
alg_params.max_mismatch_density = strtod(optarg, &tmp);
if (tmp == optarg || *tmp || alg_params.max_mismatch_density < 0.0 ||
alg_params.max_mismatch_density > 1.0)
{
fatal_error("Max mismatch density must be a "
"number in the interval [0, 1]! "
"Please check option -x.");
}
break;
case 'p':
oparams.phred_offset =
iparams.phred_offset =
strtol(optarg, &tmp, 10);
if (tmp == optarg || *tmp ||
iparams.phred_offset < 0 ||
iparams.phred_offset > 127)
{
fatal_error("Phred offset must be an integer "
"in the range [0, 127]! Please "
"check option -p.");
}
if (iparams.phred_offset != 33 &&
iparams.phred_offset != 64)
{
warning("Phred offset is usually either "
"64 (for earlier Illumina data) or 33 "
"(for Sanger and later Illumina data).");
}
break;
case 'S':
alg_params.skip_overlap = true;
break;
case 'O':