Debian Codegraph @

src/scythe.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /*
2 Scythe - A Simple Bayesian Adapter Contamination Trimmer
3 */
4
5 #include <assert.h>
6 #include <ctype.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <getopt.h>
12 #include <zlib.h>
13
14 #include "scythe.h"
15 #include "kseq.h"
16
17 __KSEQ_BASIC(static, gzFile)
18 __KS_GETC(gzread, BUFFER_SIZE)
19 __KS_GETUNTIL(gzread, BUFFER_SIZE)
20 __KSEQ_READ(static)
21
22 static const float default_prior = 0.3;
23
24 #ifndef PROGRAM_NAME
25 #define PROGRAM_NAME "scythe"
26 #endif
27
28 #ifndef AUTHORS
29 #define AUTHORS "Vince Buffalo, UC Davis\nEmail: <[email protected]> (poly-A tail removed)"
30 #endif
31
32 #ifndef VERSION
•33 #define VERSION "0.994"
34 #endif
35
36 /* Options drawn from GNU's coreutils/src/system.h */
37 /* These options are defined so as to avoid conflicting with option
38 values used by commands */
39 enum {
40 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
41 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
42 };
43 #define GETOPT_HELP_OPTION_DECL \
44 "help", no_argument, NULL, GETOPT_HELP_CHAR
45 #define GETOPT_VERSION_OPTION_DECL \
46 "version", no_argument, NULL, GETOPT_VERSION_CHAR
47 #define case_GETOPT_HELP_CHAR \
48 case GETOPT_HELP_CHAR: \
49 usage(EXIT_SUCCESS); \
50 break;
51 #define case_GETOPT_VERSION_CHAR(Program_name, Version, Authors) \
52 case GETOPT_VERSION_CHAR: \
•53 fprintf(stdout, "%s version %s\nCopyright (c) 2011 The Regents " \
54 "of University of California, Davis Campus.\n" \
55 "%s is free software and comes with ABSOLUTELY NO WARRANTY.\n" \
56 "Distributed under the MIT License.\n\nWritten by %s\n", \
•57 Program_name, Version, Program_name, Authors); \
58 exit(EXIT_SUCCESS); \
59 break;
60 /* end code drawn from system.h */
61
62
63 static struct option long_options[] = {
64 /* Options with an argument */
65 {"adapter-file", required_argument, 0, 'a'},
66 {"prior", required_argument, 0, 'p'},
67 {"partial", required_argument, 0, 'P'},
68 {"quality-type", required_argument, 0, 'q'},
69 {"matches-file", required_argument, 0, 'm'},
70 {"output-file", required_argument, 0, 'c'},
71 {"min-match", required_argument, 0, 'n'},
72 {"min-keep", required_argument, 0, 'M'},
73 {"quiet", no_argument, 0, 'Q'},
74 {"tag", no_argument, 0, 't'},
75 {GETOPT_HELP_OPTION_DECL},
76 {GETOPT_VERSION_OPTION_DECL},
77 {NULL, 0, NULL, 0}
78 };
79
80 void usage(int status) {
81 fputs("\nUsage: scythe -a adapter_file.fasta sequence_file.fastq\n\
82 Trim 3'-end adapter contaminants off sequence files. If no output file\n\
83 is specified, scythe will use stdout.\n\
84 \n\
85 Options:\n", stdout);
86 printf("\
87 -p, --prior prior (default: %0.3f)\n", default_prior);
88 fputs("\
89 -q, --quality-type quality type, either illumina, solexa, or sanger (default: sanger)\n\
90 -m, --matches-file matches file (default: no output)\n\
91 -o, --output-file output trimmed sequences file (default: stdout)\n\
92 -t, --tag add a tag to the header indicating Scythe cut a sequence (default: off)\n", stdout);
93 fputs("\
94 -n, --min-match smallest contaminant to consider (default: 5)\n\
95 -M, --min-keep filter sequnces less than or equal to this length (default: 35)\n\
96 --quiet don't output statistics about trimming to stdout (default: off)\n\
97 --help display this help and exit\n\
98 --version output version information and exit\n", stdout);
99 fputs("\n\
100 Information on quality schemes:\n\
101 phred PHRED quality scores (e.g. from Roche 454). ASCII with no offset, range: [4, 60].\n\
102 sanger Sanger are PHRED ASCII qualities with an offset of 33, range: [0, 93]. From \n\
103 NCBI SRA, or Illumina pipeline 1.8+.\n\
104 solexa Solexa (also very early Illumina - pipeline < 1.3). ASCII offset of\n", stdout);
105 fputs("\
106 64, range: [-5, 62]. Uses a different quality-to-probabilities conversion than other\n\
107 schemes.\n\
108 illumina Illumina output from pipeline versions between 1.3 and 1.7. ASCII offset of 64,\n\
109 range: [0, 62]\n", stdout);
110 exit(status);
111 }
112
113 int main(int argc, char *argv[]) {
114 kseq_t *seq;
115 int l, index, shift, min=5, min_keep=35;
116 int debug=0, verbose=1;
117 int contaminated=0, total=0;
118 quality_type qual_type=SANGER;
119 match *best_match;
120 float *qprobs, prior=default_prior;
121 adapter_array *aa;
122 gzFile adapter_fp=NULL, fp;
123 FILE *output_fp=stdout, *matches_fp=NULL;
124 int optc;
125 int add_tag = 0;
126 extern char *optarg;
127
128 while (1) {
129 int option_index = 0;
130 optc = getopt_long(argc, argv, "dtfp:a:o:q:m:o:n:M:", long_options, &option_index);
131
132 if (optc == -1)
133 break;
134 switch (optc) {
135 if (long_options[option_index].flag != 0)
136 break;
137 case 'a':
138 adapter_fp = gzopen(optarg, "r");
139 if (!adapter_fp) {
140 fprintf(stderr, "Could not open adapter file '%s'.\n", optarg);
141 return EXIT_FAILURE;
142 }
143 break;
144 case 'd':
145 debug = 1;
146 break;
147 case 't':
148 add_tag = 1;
149 break;
150 case 'Q':
151 verbose = 0;
152 break;
153 case 'o':
154 output_fp = fopen(optarg, "w");
155 if (!output_fp) {
156 fprintf(stderr, "Could not open output file '%s'.\n", optarg);
157 return EXIT_FAILURE;
158 }
159 break;
160 case 'm':
161 matches_fp = fopen(optarg, "w");
162 if (!matches_fp) {
163 fprintf(stderr, "Could not open matches file '%s'.\n", optarg);
164 return EXIT_FAILURE;
165 }
166 break;
167 case 'n':
168 min = atoi(optarg);
169 break;
170 case 'M':
171 min_keep = atoi(optarg);
172 break;
173 case 'q':
174 if (strcmp(optarg, "illumina") == 0)
175 qual_type = ILLUMINA;
176 else if (strcmp(optarg, "solexa") == 0)
177 qual_type = SOLEXA;
178 else if (strcmp(optarg, "sanger") == 0)
179 qual_type = SANGER;
180 else {
181 fprintf(stderr, "Unknown quality type '%s'.\n", optarg);
182 usage(EXIT_FAILURE);
183 }
184 break;
185 case 'p':
186 /* This truncation is acceptable... priors need not be doubles */
187 prior = (float) atof(optarg);
188 if (prior > 1 || prior < 0) {
189 fprintf(stderr, "Prior must be between 0 and 1\n");
190 usage(EXIT_FAILURE);
191 }
192 break;
193
194 case_GETOPT_HELP_CHAR;
195 case_GETOPT_VERSION_CHAR(PROGRAM_NAME, VERSION, AUTHORS);
196 case '?':
197 break;
198 default:
199 usage(EXIT_FAILURE);
200 }
201 }
202
203 if (debug) {
204 matches_fp = stdout;
205 output_fp = stdout;
206 }
207
208 if ((index = optind) == argc) {
209 fprintf(stderr, "No FASTQ file specified.\n");
210 usage(EXIT_FAILURE);
211 }
212
213 /* load all adapter sequences into memory */
214 if (!adapter_fp) {
215 fprintf(stderr, "No adapter file specified.\n");
216 usage(EXIT_FAILURE);
217 }
218 aa = load_adapters(adapter_fp);
219 gzclose(adapter_fp);
220
221 fp = strcmp(argv[index], "-") ? gzopen(argv[index], "r") : gzdopen(fileno(stdin), "r");
222
223 if (!fp) {
224 fprintf(stderr, "FASTQ file '%s' not found.\n", argv[index]);
225 return EXIT_FAILURE;
226 }
227
228 seq = kseq_init(fp);
229
230 /* Loop through entire sequence file. Write trimmed sequences to
•231 file (or stdout), and record matches in a match file if specified.
232 */
233 while ((l = kseq_read(seq)) >= 0) {
234 shift = -1;
235 if (!seq->qual.s) {
236 fputs("Sequence file missing or has malformed quality line.\n", stderr);
237 usage(EXIT_FAILURE);
238 }
239
240 qprobs = qual_to_probs(seq->qual.s, qual_type);
241 best_match = find_best_match(aa, seq->seq.s, qprobs, prior, 0.25, min);
242
243 if (best_match && best_match->ps->is_contam) {
244 contaminated++;
245 shift = best_match->shift;
246 if (matches_fp) print_match(seq, best_match, matches_fp, aa, qual_type);
247 /* TODO */
248 /* aa->adapters[best_match->adapter_index].occurrences[best_match->n-1]++; */
249 }
250 write_fastq(output_fp, seq, add_tag, shift, min_keep);
251 if (best_match) destroy_match(best_match);
252 free(qprobs);
253 total++;
254 }
255
256 if (verbose)
257 print_summary(aa, prior, total-contaminated, contaminated, total);
258
259 kseq_destroy(seq);
260 destroy_adapters(aa, MAX_ADAPTERS);
261 if (output_fp != stdout && output_fp != NULL)
262 fclose(output_fp);
263 gzclose(fp);
264 return 0;
265 }