]> git.donarmstrong.com Git - fastq-tools.git/blob - src/fastq-grep.c
(limited) support for fasta
[fastq-tools.git] / src / fastq-grep.c
1
2 /*
3  * This file is part of fastq-tools.
4  *
5  * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
6  *
7  * fastq-grep :
8  * Regular expression searches of the sequences within a FASTQ file.
9  *
10  */
11
12
13 #include "common.h"
14 #include "parse.h"
15 #include <stdio.h>
16 #include <string.h>
17 #include <getopt.h>
18 #include <zlib.h>
19 #include <pcre.h>
20
21
22 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
23 #  include <fcntl.h>
24 #  include <io.h>
25 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
26 #else
27 #  define SET_BINARY_MODE(file)
28 #endif
29
30
31 void print_help()
32 {
33     fprintf( stderr, 
34 "fastq-grep [OPTION]... PATTERN [FILE]...\n"
35 "Search for PATTERN in the read sequences in each FILE or standard input.\n"
36 "PATTERN, by default, is a perl compatible regular expression.\n\n"
37 "Options:\n"
38 "  -h, --help              print this message\n"
39 "  -v, --invert-match      select nonmatching entries\n"
40 "  -c, --count             output only the number of matching sequences\n"
41     );
42 }
43
44 static int invert_flag;
45 static int help_flag;
46 static int count_flag;
47
48
49
50 void fastq_grep(FILE* fin, FILE* fout, pcre* re)
51 {
52     int rc;
53     int ovector[3];
54     size_t count = 0;
55
56     fastq_t* fqf = fastq_open(fin);
57     seq_t* seq = fastq_alloc_seq();
58
59     while (fastq_next(fqf, seq)) {
60         rc = pcre_exec(re,          /* pattern */
61                        NULL,        /* extre data */
62                        seq->seq.s,  /* subject */
63                        seq->seq.n,  /* subject length */
64                        0,           /* subject offset */
65                        0,           /* options */
66                        ovector,     /* output vector */
67                        3         ); /* output vector length */
68
69         if ((invert_flag && rc == PCRE_ERROR_NOMATCH) || rc >= 0) {
70             if (count_flag) count++;
71             else            fastq_print(fout, seq);
72         }
73     }
74
75     fastq_free_seq(seq);
76     fastq_close(fqf);
77
78     if (count_flag) fprintf(fout, "%zu\n", count);
79 }
80
81
82
83 int main(int argc, char* argv[])
84 {
85     SET_BINARY_MODE(stdin);
86     SET_BINARY_MODE(stdout);
87
88     const char* pat;
89     pcre* re;
90     const char* pat_error;
91     int pat_error_offset;
92
93     FILE*  fin;
94
95
96     invert_flag  = 0;
97     help_flag    = 0;
98     count_flag   = 0;
99
100     int opt;
101     int opt_idx;
102
103
104     static struct option long_options[] =
105         { 
106           {"help", no_argument, &help_flag, 1},
107           {"invert-match", no_argument, &invert_flag, 1},
108           {"count", no_argument, &count_flag, 1},
109           {0, 0, 0, 0}
110         };
111
112     while (1) {
113         opt = getopt_long(argc, argv, "hvc", long_options, &opt_idx);
114
115         if( opt == -1 ) break;
116
117         switch (opt) {
118             case 0:
119                 if (long_options[opt_idx].flag != 0) break;
120                 if (optarg) {
121                 }
122                 break;
123
124             case 'h':
125                 help_flag = 1;
126                 break;
127
128             case 'v':
129                 invert_flag = 1;
130                 break;
131
132             case 'c':
133                 count_flag = 1;
134                 break;
135
136             case '?':
137                 return 1;
138
139             default:
140                 abort();
141         }
142     }
143
144     if (help_flag) {
145         print_help();
146         return 0;
147     }
148
149     if (optind >= argc) {
150         fprintf(stderr, "A pattern must be specified.\n");
151         return 1;
152     }
153
154     pat = argv[optind++];
155     re = pcre_compile( pat, PCRE_CASELESS, &pat_error, &pat_error_offset, NULL );
156
157
158     if (re == NULL) {
159         fprintf(stderr, "Syntax error in PCRE pattern at offset: %d: %s\n",
160                 pat_error_offset, pat_error );
161         return 1;
162     }
163
164
165     if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
166         fastq_grep(stdin, stdout, re);
167     }
168     else {
169         for (; optind < argc; optind++) {
170             fin = fopen(argv[optind], "rb");
171             if (fin == NULL) {
172                 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
173                 continue;
174             }
175
176             fastq_grep(fin, stdout, re);
177
178             fclose(fin);
179         }
180     }
181
182     pcre_free(re);
183
184     return 0;
185 }
186
187
188