3
|
1 /*
|
|
2 FASTX-toolkit - FASTA/FASTQ preprocessing tools.
|
|
3 Copyright (C) 2009 A. Gordon (gordon@cshl.edu)
|
|
4
|
|
5 This program is free software: you can redistribute it and/or modify
|
|
6 it under the terms of the GNU Affero General Public License as
|
|
7 published by the Free Software Foundation, either version 3 of the
|
|
8 License, or (at your option) any later version.
|
|
9
|
|
10 This program is distributed in the hope that it will be useful,
|
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 GNU Affero General Public License for more details.
|
|
14
|
|
15 You should have received a copy of the GNU Affero General Public License
|
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 */
|
|
18 #include <limits.h>
|
|
19 #include <stdio.h>
|
|
20 #include <stdlib.h>
|
|
21 #include <string.h>
|
|
22 #include <getopt.h>
|
|
23 #include <errno.h>
|
|
24 #include <err.h>
|
|
25
|
|
26 #include <config.h>
|
|
27
|
|
28 #include "fastx.h"
|
|
29 #include "fastx_args.h"
|
|
30
|
|
31 const char* usage=
|
|
32 "usage: fastq_to_fasta [-h] [-r] [-n] [-v] [-z] [-i INFILE] [-o OUTFILE]\n" \
|
|
33 "\n" \
|
|
34 "version " VERSION "\n" \
|
|
35 " [-h] = This helpful help screen.\n" \
|
|
36 " [-r] = Rename sequence identifiers to numbers.\n" \
|
|
37 " [-n] = keep sequences with unknown (N) nucleotides.\n" \
|
|
38 " Default is to discard such sequences.\n" \
|
|
39 " [-v] = Verbose - report number of sequences.\n" \
|
|
40 " If [-o] is specified, report will be printed to STDOUT.\n" \
|
|
41 " If [-o] is not specified (and output goes to STDOUT),\n" \
|
|
42 " report will be printed to STDERR.\n" \
|
|
43 " [-z] = Compress output with GZIP.\n" \
|
|
44 " [-i INFILE] = FASTA/Q input file. default is STDIN.\n" \
|
|
45 " [-o OUTFILE] = FASTA output file. default is STDOUT.\n" \
|
|
46 "\n";
|
|
47
|
|
48 FASTX fastx;
|
|
49 int flag_rename_seqid = 0;
|
|
50 int flag_discard_N = 1 ;
|
|
51
|
|
52 int parse_program_args(int __attribute__((unused)) optind, int optc, char __attribute__((unused)) *optarg)
|
|
53 {
|
|
54 switch(optc) {
|
|
55 case 'n':
|
|
56 flag_discard_N = 0 ;
|
|
57 break;
|
|
58
|
|
59 case 'r':
|
|
60 flag_rename_seqid = 1;
|
|
61 break;
|
|
62 default:
|
|
63 errx(1, __FILE__ ":%d: Unknown argument (%c)", __LINE__, optc ) ;
|
|
64 }
|
|
65 return 1;
|
|
66 }
|
|
67
|
|
68
|
|
69 int main(int argc, char* argv[])
|
|
70 {
|
|
71 fastx_parse_cmdline(argc, argv, "rn", parse_program_args);
|
|
72
|
|
73 fastx_init_reader(&fastx, get_input_filename(),
|
|
74 FASTQ_ONLY, ALLOW_N, REQUIRE_UPPERCASE);
|
|
75
|
|
76 fastx_init_writer(&fastx, get_output_filename(), OUTPUT_FASTA, compress_output_flag());
|
|
77
|
|
78 while ( fastx_read_next_record(&fastx) ) {
|
|
79 //See if the input sequence contained 'N' nucleotides
|
|
80 if ( flag_discard_N && (strchr(fastx.nucleotides,'N') != NULL))
|
|
81 continue;
|
|
82
|
|
83 if ( flag_rename_seqid )
|
|
84 snprintf(fastx.name, sizeof(fastx.name), "%zu", num_output_reads(&fastx)+1) ;
|
|
85
|
|
86 fastx_write_record(&fastx);
|
|
87 }
|
|
88
|
|
89 //Print verbose report
|
|
90 if ( verbose_flag() ) {
|
|
91 fprintf(get_report_file(), "Input: %zu reads.\n", num_input_reads(&fastx) ) ;
|
|
92 fprintf(get_report_file(), "Output: %zu reads.\n", num_output_reads(&fastx) ) ;
|
|
93
|
|
94 if ( flag_discard_N ) {
|
|
95 size_t discarded = num_input_reads(&fastx) - num_output_reads(&fastx) ;
|
|
96 fprintf(get_report_file(), "discarded %zu (%zu%%) low-quality reads.\n",
|
|
97 discarded, (discarded*100)/( num_input_reads(&fastx) ) ) ;
|
|
98 }
|
|
99 }
|
|
100
|
|
101 return 0;
|
|
102 }
|