-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfalco.cpp
More file actions
748 lines (643 loc) · 28.5 KB
/
falco.cpp
File metadata and controls
748 lines (643 loc) · 28.5 KB
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
/* falco: quality control for sequencing read files
*
* Copyright (C) 2019-2022 Guilherme De Sena Brandine and
* Andrew D. Smith
* Authors: Guilherme De Sena Brandine, Andrew Smith, and Masaru Nakajima
*
* This program 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.
*
* This program 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.
*/
#include <chrono>
#include <filesystem>
#include <fstream>
#include "FalcoConfig.hpp"
#include "FastqStats.hpp"
#include "HtmlMaker.hpp"
#include "Module.hpp"
#include "OptionParser.hpp"
#include "StreamReader.hpp"
#include "smithlab_utils.hpp"
using std::chrono::duration_cast;
using std::chrono::system_clock;
using time_point = std::chrono::time_point<std::chrono::system_clock>;
// Function to get seconds elapsed in program
static size_t
get_seconds_since(const time_point &start_time) {
const auto current_time = system_clock::now();
const auto time_difference = current_time - start_time;
return duration_cast<std::chrono::seconds>(time_difference).count();
}
// Function to print progress nicely
static inline void
log_process(const std::string &s) {
auto tmp = system_clock::to_time_t(system_clock::now());
std::string time_fmt(std::ctime(&tmp));
time_fmt.pop_back();
std::cerr << "[" << time_fmt << "] " << s << '\n';
}
// Function to check existance of directory
static bool
dir_exists(const std::string &path) {
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
}
// Read any file type until the end and logs progress
// in is an StreamReader object type
template <typename T>
void
read_stream_into_stats(T &in, FastqStats &stats, FalcoConfig &falco_config) {
// open file
size_t file_size = std::max(in.load(), static_cast<size_t>(1));
size_t tot_bytes_read = 0;
// Read record by record
const bool report_progress = falco_config.progress;
ProgressBar progress(file_size, "running falco");
if (report_progress)
progress.report(std::cerr, 0);
while (in.read_entry(stats, tot_bytes_read)) {
if (report_progress && progress.time_to_report(tot_bytes_read))
progress.report(std::cerr, tot_bytes_read);
}
// if I could not get tile information from read names, I need to tell this to
// config so it does not output tile data on the summary or html
if (in.tile_ignore)
falco_config.do_tile = false;
if (report_progress && tot_bytes_read < file_size)
progress.report(std::cerr, file_size);
}
// Write module content into html maker if requested
template <typename T>
void
write_if_requested(T module, FastqStats &stats, const bool requested,
const bool skip_text, const bool skip_html,
const bool skip_short_summary, const std::string &filename,
std::ostream &summary_txt, std::ostream &qc_data_txt,
HtmlMaker &html_maker) {
html_maker.put_comment(module.placeholder_cs, module.placeholder_ce,
requested);
// If module has not been requested we put nothing where the data is
if (!requested) {
// puts the actual data (table, graph, etc)
html_maker.put_data(module.placeholder_data, "");
return;
}
// calculates module summary, mandatory before writing
module.summarize(stats);
// writes what was requested
if (!skip_short_summary)
module.write_short_summary(summary_txt, filename);
if (!skip_text)
module.write(qc_data_txt);
if (!skip_html) {
// puts the module name
html_maker.put_data(module.placeholder_name, T::module_name);
// puts the grade
html_maker.put_data(module.placeholder_grade, module.grade);
// puts the actual data (table, graph, etc)
html_maker.put_data(module.placeholder_data, module.html_data);
}
}
void
write_results(const FalcoConfig &falco_config, FastqStats &stats,
const bool skip_text, const bool skip_html,
const bool skip_short_summary, const bool do_call,
const std::string &file_prefix, const std::string &outdir,
const std::string &summary_filename,
const std::string &data_filename,
const std::string &report_filename) {
// Here we open the short summary ofstream
std::ofstream summary_txt;
if (!skip_short_summary) {
const std::string summary_file =
summary_filename.empty() ? outdir + "/" + file_prefix + "summary.txt"
: summary_filename;
summary_txt.open(summary_file, std::ofstream::binary);
if (!summary_txt.good())
throw std::runtime_error("Failed to create output summary file: " +
summary_file);
if (!falco_config.quiet)
log_process("Writing summary to " + summary_file);
}
// Here we open the full text summary
std::ofstream qc_data_txt;
if (!skip_text) {
const std::string qc_data_file =
(data_filename.empty() ? (outdir + "/" + file_prefix + "fastqc_data.txt")
: (data_filename));
qc_data_txt.open(qc_data_file, std::ofstream::binary);
if (!qc_data_txt.good())
throw std::runtime_error("Failed to create output data file: " +
qc_data_file);
if (!falco_config.quiet)
log_process("Writing text report to " + qc_data_file);
// put header
qc_data_txt << "##Falco\t" + FalcoConfig::FalcoVersion + "\n";
if (do_call)
qc_data_txt << "##Call\t" << falco_config.call << "\n";
}
// Here we open the html ostream and maker object
HtmlMaker html_maker = HtmlMaker();
std::ofstream html;
if (!skip_html) {
// Decide html filename based on input
const std::string html_file =
(report_filename.empty()
? (outdir + "/" + file_prefix + "fastqc_report.html")
: (report_filename));
if (!falco_config.quiet)
log_process("Writing HTML report to " + html_file);
html.open(html_file, std::ofstream::binary);
if (!html.good())
throw std::runtime_error("Failed to create output HTML report file: " +
html_file);
html_maker.put_file_details(falco_config);
}
// Now we create modules if requested, summarize them and kill them
// Basic Statistics
write_if_requested(ModuleBasicStatistics(falco_config), stats, true,
skip_text, skip_html, skip_short_summary,
falco_config.filename_stripped, summary_txt, qc_data_txt,
html_maker);
// Per base sequence quality
write_if_requested(ModulePerBaseSequenceQuality(falco_config), stats,
falco_config.do_quality_sequence, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Per tile sequence quality
write_if_requested(ModulePerTileSequenceQuality(falco_config), stats,
falco_config.do_tile, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Per sequence quality scores
write_if_requested(ModulePerSequenceQualityScores(falco_config), stats,
falco_config.do_quality_sequence, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Per base sequence content
write_if_requested(ModulePerBaseSequenceContent(falco_config), stats,
falco_config.do_sequence, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Per sequence GC content
write_if_requested(ModulePerSequenceGCContent(falco_config), stats,
falco_config.do_gc_sequence, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Per base N content
write_if_requested(ModulePerBaseNContent(falco_config), stats,
falco_config.do_n_content, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Sequence Length Distribution
write_if_requested(ModuleSequenceLengthDistribution(falco_config), stats,
falco_config.do_sequence_length, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Sequence Duplication Levels
write_if_requested(ModuleSequenceDuplicationLevels(falco_config), stats,
falco_config.do_duplication, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Overrepresented sequences
write_if_requested(ModuleOverrepresentedSequences(falco_config), stats,
falco_config.do_overrepresented, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Adapter Content
write_if_requested(ModuleAdapterContent(falco_config), stats,
falco_config.do_adapter, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
// Kmer Content
write_if_requested(ModuleKmerContent(falco_config), stats,
falco_config.do_kmer, skip_text, skip_html,
skip_short_summary, falco_config.filename_stripped,
summary_txt, qc_data_txt, html_maker);
if (!skip_html)
html << html_maker.html_boilerplate;
}
[[nodiscard]] inline bool
file_exists(const std::string &file_name) {
return access(file_name.data(), F_OK) == 0;
}
int
main(int argc, char *argv[]) {
try {
static const std::string FALCO_VERSION =
"falco " + FalcoConfig::FalcoVersion;
bool help = false;
bool version = false;
// skip outputs
bool skip_text = false;
bool skip_html = false;
bool skip_short_summary = false;
bool do_call = false;
bool allow_empty_input = false;
// a tmp boolean to keep compatibility with FastQC
bool tmp_compatibility_only = false;
std::string tmp_compatibility_only_str;
FalcoConfig falco_config(argc, argv);
// if defined, read file as the file format specified by the user
std::string forced_file_format;
// output directory in which to put files
std::string outdir;
// custom output filenames (if provided)
std::string data_filename = "";
std::string report_filename = "";
std::string summary_filename = "";
/****************** COMMAND LINE OPTIONS ********************/
OptionParser opt_parse(argv[0],
"A high throughput sequence QC analysis tool",
"<seqfile1> <seqfile2> ...");
opt_parse.set_show_defaults();
/***************** FASTQC PARAMETERS ********************/
opt_parse.add_opt("-help", 'h', "Print this help file and exit", false,
help);
opt_parse.add_opt("-version", 'v',
"Print the version of the program and exit", false,
version);
opt_parse.add_opt(
"-outdir", 'o',
"Create all output files in the specified output directory. "
"FALCO-SPECIFIC: If the directory does not exists, the "
"program will create it. "
"If this option is not set then the output file for each "
"sequence file is created in the same directory as the "
"sequence file which was processed.",
false, outdir);
opt_parse.add_opt(
"-casava", '\0',
"[IGNORED BY FALCO] "
"Files come from raw casava output. Files in the same sample "
"group (differing only by the group number) will be analysed "
"as a set rather than individually. Sequences with the filter "
"flag set in the header will be excluded from the analysis. "
"Files must have the same names given to them by casava "
"(including being gzipped and ending with .gz) otherwise they "
"won't be grouped together correctly.",
false, falco_config.casava);
opt_parse.add_opt(
"-nano", '\0',
"[IGNORED BY FALCO] "
"Files come from nanopore sequences and are in fast5 format. In "
"this mode you can pass in directories to process and the program "
"will take in all fast5 files within those directories and produce "
" a single output file from the sequences found in all files.",
false, falco_config.nanopore);
opt_parse.add_opt(
"-nofilter", '\0',
"[IGNORED BY FALCO] "
"If running with --casava then don't remove read flagged by "
"casava as poor quality when performing the QC analysis. ",
false, falco_config.nofilter);
opt_parse.add_opt(
"-extract", '\0',
"[ALWAYS ON IN FALCO] "
"If set then the zipped output file will be uncompressed in "
"the same directory after it has been created. By default "
"this option will be set if fastqc is run in non-interactive "
"mode.",
false, tmp_compatibility_only);
opt_parse.add_opt(
"-java", 'j',
"[IGNORED BY FALCO] "
"Provides the full path to the java binary you want to use to "
"launch fastqc. If not supplied then java is assumed to be in "
"your path.",
false, tmp_compatibility_only_str);
opt_parse.add_opt(
"-noextract", '\0',
"[IGNORED BY FALCO] "
"Do not uncompress the output file after creating it. You "
"should set this option if you do not wish to uncompress "
"the output when running in non-interactive mode.",
false, falco_config.casava);
opt_parse.add_opt(
"-nogroup", '\0',
"Disable grouping of bases for reads >50bp. All reports will "
"show data for every base in the read. WARNING: When using this "
"option, your plots may end up a ridiculous size. "
"You have been warned!",
false, falco_config.nogroup);
opt_parse.add_opt(
"-min_length", '\0',
"[NOT YET IMPLEMENTED IN FALCO] "
"Sets an artificial lower limit on the length of the sequence "
"to be shown in the report. As long as you set this to a value "
"greater or equal to your longest read length then this will be "
"the sequence length used to create your read groups. This can "
"be useful for making directly comaparable statistics from "
"datasets with somewhat variable read lengths. ",
false, tmp_compatibility_only_str);
opt_parse.add_opt("-format", 'f',
"Bypasses the normal sequence file format detection and "
"forces the program to use the specified format. Valid"
"formats are bam, sam, bam_mapped, sam_mapped, fastq, "
"fq, fastq.gz or fq.gz.",
false, forced_file_format);
opt_parse.add_opt("-threads", 't',
"[NOT YET IMPLEMENTED IN FALCO] "
"Specifies the number of files which can be processed "
"simultaneously. Each thread will be allocated 250MB of "
"memory so you shouldn't run more threads than your "
"available memory will cope with, and not more than "
"6 threads on a 32 bit machine",
false, falco_config.threads);
opt_parse.add_opt(
"-contaminants", 'c',
"Specifies a non-default file which contains the list of "
"contaminants to screen overrepresented sequences against. "
"The file must contain sets of named contaminants in the "
"form name[tab]sequence. Lines prefixed with a hash will "
"be ignored. Default: ",
false, falco_config.contaminants_file);
opt_parse.add_opt(
"-adapters", 'a',
"Specifies a non-default file which contains the list of "
"adapter sequences which will be explicity searched against "
"the library. The file must contain sets of named adapters "
"in the form name[tab]sequence. Lines prefixed with a hash "
"will be ignored. Default: ",
false, falco_config.adapters_file);
opt_parse.add_opt(
"-limits", 'l',
"Specifies a non-default file which contains a set of criteria "
"which will be used to determine the warn/error limits for the "
"various modules. This file can also be used to selectively "
"remove some modules from the output all together. The format "
"needs to mirror the default limits.txt file found in the "
"Configuration folder. Default: ",
false, falco_config.limits_file);
opt_parse.add_opt(
"-kmers", 'k',
"[IGNORED BY FALCO AND ALWAYS SET TO 7] "
"Specifies the length of Kmer to look for in the Kmer content "
"module. Specified Kmer length must be between 2 and 10. Default "
"length is 7 if not specified.",
false, tmp_compatibility_only_str);
opt_parse.add_opt(
"-quiet", 'q',
"Supress all progress messages on stdout and only report errors.", false,
falco_config.quiet);
opt_parse.add_opt(
"-dir", 'd',
"[IGNORED: FALCO DOES NOT CREATE TMP FILES] "
"Selects a directory to be used for temporary files written when "
"generating report images. Defaults to system temp directory if "
"not specified. ",
false, tmp_compatibility_only_str);
/***************** FALCO ONLY ********************/
// falco-only options use a single dash as long argument
// e.g. -skip-text instead of --skip-text. They also
// use single-characters that do not overlap with the
// ones used in FastQC, so cannot use:
// h, v, o, j, f, t, c, a, l, k, q, d
opt_parse.add_opt("subsample", 's',
"[Falco only] makes falco faster "
"(but possibly less accurate) by only processing "
"reads that are multiple of this value (using 0-based "
"indexing to number reads).",
false, falco_config.read_step);
opt_parse.add_opt(
"bisulfite", 'b',
"[Falco only] reads are whole genome bisulfite sequencing, and more "
"Ts and fewer Cs are therefore expected and will be "
"accounted for in base content.",
false, falco_config.is_bisulfite);
opt_parse.add_opt(
"reverse-complement", 'r',
"[Falco only] The input is a reverse-complement. All modules will "
"be tested by swapping A/T and C/G",
false, falco_config.is_reverse_complement);
opt_parse.add_opt("skip-data", '\0',
"[Falco only] Do not create FastQC data text file.",
false, skip_text);
opt_parse.add_opt("skip-report", '\0',
"[Falco only] Do not create FastQC report HTML file.",
false, skip_html);
opt_parse.add_opt("skip-summary", '\0',
"[Falco only] Do not create FastQC summary file", false,
skip_short_summary);
opt_parse.add_opt(
"data-filename", 'D',
"[Falco only] Specify filename for FastQC data output (TXT). "
"If not specified, it will be called fastq_data.txt in either "
"the input file's directory or the one specified in the --output "
"flag. Only available when running falco with a single input. ",
false, data_filename);
opt_parse.add_opt(
"report-filename", 'R',
"[Falco only] Specify filename for FastQC report output (HTML). "
"If not specified, it will be called fastq_report.html in either "
"the input file's directory or the one specified in the --output "
"flag. Only available when running falco with a single input.",
false, report_filename);
opt_parse.add_opt(
"summary-filename", 'S',
"[Falco only] Specify filename for the short summary output (TXT). "
"If not specified, it will be called fastq_report.html in either "
"the input file's directory or the one specified in the --output "
"flag. Only available when running falco with a single input.",
false, summary_filename);
opt_parse.add_opt("add-call", 'K',
"[Falco only] add the command call call to"
" FastQC data output and FastQC report HTML (this"
" may break the parse of fastqc_data.txt"
" in programs that are very strict about the "
" FastQC output format).",
false, do_call);
opt_parse.add_opt(
"allow-empty-input", '\0',
"[Falco only] allow empty input files and generate empty output files "
"without en error state. WARNING: using this option can mask problems in "
"other parts of a workflow.",
false, allow_empty_input);
opt_parse.add_opt("progress", '\0',
"[Falco only] Report a progress bar while running.",
false, falco_config.progress);
std::vector<std::string> leftover_args;
opt_parse.parse(argc, argv, leftover_args);
if (argc == 1 || opt_parse.help_requested()) {
std::cerr << opt_parse.help_message() << '\n'
<< opt_parse.about_message() << '\n';
return EXIT_SUCCESS;
}
if (version) {
std::cerr << FALCO_VERSION << "\n";
return EXIT_SUCCESS;
}
if (opt_parse.about_requested()) {
std::cerr << opt_parse.about_message() << '\n';
return EXIT_SUCCESS;
}
if (opt_parse.option_missing()) {
std::cerr << opt_parse.option_missing_message() << '\n';
return EXIT_SUCCESS;
}
if (leftover_args.size() == 0) {
std::cerr << opt_parse.help_message() << '\n';
return EXIT_SUCCESS;
}
if (leftover_args.size() > 1 &&
(!summary_filename.empty() || !report_filename.empty() ||
!data_filename.empty())) {
std::cerr << "ERROR: summary, report or data filename provided, but"
<< " you are running falco with " << leftover_args.size()
<< " inputs. We cannot allow this because multiple inputs"
<< " require multiple outputs, so they will be resolved by"
<< " the input filenames instead\n";
return EXIT_FAILURE;
}
// ADS: make sure all input files are non-empty unless user oks it
if (!allow_empty_input) {
for (const auto &fn : leftover_args) {
std::error_code ec;
const bool empty_file = std::filesystem::is_empty(fn, ec);
if (ec) {
std::cerr << "Error reading file: " << fn << " (" << ec.message()
<< ")\n";
return EXIT_FAILURE;
}
else if (empty_file) {
std::cerr << "Input file is empty: " << fn << '\n';
return EXIT_FAILURE;
}
}
}
if (!outdir.empty()) {
if (!summary_filename.empty())
std::cerr << "[WARNING] specifying custom output directory but also "
<< "custom summary filename. Output " << outdir
<< " will be disregarded and summary file will be saved to "
<< summary_filename << '\n';
if (!data_filename.empty())
std::cerr << "[WARNING] specifying custom output directory but also "
<< "custom data filename. Output " << outdir
<< " will be disregarded and data file will be saved to "
<< data_filename << '\n';
if (!report_filename.empty())
std::cerr
<< "[WARNING] specifying custom output directory but also "
<< "custom HTML Report filename. Output " << outdir
<< " will be disregarded and HTML report file will be saved to "
<< report_filename << '\n';
if (!dir_exists(outdir)) {
if (!falco_config.quiet)
log_process("creating directory for output: " + outdir);
// makes directory with r and w permission
if (mkdir(outdir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
std::cerr << "failed to create directory: " << outdir
<< ". Make sure you have write permissions on it!\n";
return EXIT_FAILURE;
}
}
}
const std::vector<std::string> all_seq_filenames(leftover_args);
// check if all filenames exist
bool all_files_exist = true;
for (size_t i = 0; i < all_seq_filenames.size(); ++i) {
if (!file_exists(all_seq_filenames[i])) {
std::cerr << "ERROR! File does not exist: " << all_seq_filenames[i]
<< '\n';
all_files_exist = false;
}
}
if (!all_files_exist) {
throw std::runtime_error(
"not all input files exist. Check stderr for detailed list"
" of files that were not found");
}
/****************** END COMMAND LINE OPTIONS ********************/
for (const auto &filename : all_seq_filenames) {
const time_point file_start_time = system_clock::now();
falco_config.filename = filename;
// if format was not provided, we have to guess it by the filename
falco_config.format = forced_file_format;
/****************** BEGIN PROCESSING CONFIG ******************/
// define file type, read limits, adapters, contaminants and fail
// early if any of the required files is not present
//
falco_config.setup();
/****************** END PROCESSING CONFIG *******************/
if (!falco_config.quiet)
log_process("Started reading file " + falco_config.filename);
FastqStats stats; // allocate all space to summarize data
// Initializes a reader given the file format
if (falco_config.is_sam) {
if (!falco_config.quiet)
log_process("reading file as SAM format");
SamReader in(falco_config, stats.SHORT_READ_THRESHOLD);
read_stream_into_stats(in, stats, falco_config);
stats.adjust_tile_maps_len();
}
#ifdef USE_HTS
else if (falco_config.is_bam) {
if (!falco_config.quiet)
log_process("reading file as BAM format");
BamReader in(falco_config, stats.SHORT_READ_THRESHOLD);
read_stream_into_stats(in, stats, falco_config);
stats.adjust_tile_maps_len();
}
#endif
else if (falco_config.is_fastq_gz) {
if (!falco_config.quiet)
log_process("reading file as gzipped FASTQ format");
GzFastqReader in(falco_config, stats.SHORT_READ_THRESHOLD);
read_stream_into_stats(in, stats, falco_config);
}
else if (falco_config.is_fastq) {
if (!falco_config.quiet)
log_process("reading file as uncompressed FASTQ format");
FastqReader in(falco_config, stats.SHORT_READ_THRESHOLD);
read_stream_into_stats(in, stats, falco_config);
}
else {
throw std::runtime_error("Cannot recognize file format for file " +
falco_config.filename);
}
if (!falco_config.quiet) {
log_process("Finished reading file");
}
stats.summarize();
// if oudir is empty we will set it as the filename path
std::string cur_outdir;
const size_t last_slash_idx = filename.rfind('/');
std::string file_basename =
falco_config.filename.substr(last_slash_idx + 1);
if (outdir.empty()) {
// if file was given with relative path in the current dir, we set a dot
if (last_slash_idx == std::string::npos) {
cur_outdir = ".";
file_basename = filename;
}
else {
cur_outdir = falco_config.filename.substr(0, last_slash_idx);
}
}
else {
cur_outdir = outdir;
}
// Write results
const std::string file_prefix =
(all_seq_filenames.size() == 1) ? ("") : (file_basename + "_");
write_results(falco_config, stats, skip_text, skip_html,
skip_short_summary, do_call, file_prefix, cur_outdir,
summary_filename, data_filename, report_filename);
/************************** TIME SUMMARY *****************************/
if (!falco_config.quiet)
std::cerr << "Elapsed time for file " << filename << ": "
<< get_seconds_since(file_start_time) << "s\n";
}
}
catch (const std::exception &e) {
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}