4 * fastq-grep: regular expression searches of the sequences within a fastq file
6 * Febuary 2011 / Daniel Jones <dcjones@cs.washington.edu>
18 KSEQ_INIT(gzFile, gzread)
21 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
24 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
26 # define SET_BINARY_MODE(file)
33 "fastq-grep [OPTION]... PATTERN [FILE]...\n"
34 "Search for PATTERN in the read sequences in each FILE or standard input.\n"
35 "PATTERN, by default, is a perl compatible regular expression.\n\n"
37 " -h, --help print this message\n"
38 " -v, --invert-match select nonmatching entries\n"
42 static int invert_flag;
48 void print_fastq_entry( FILE* fout, kseq_t* seq )
50 fprintf(fout, "@%s\n%s\n+%s\n%s\n",
58 void fastq_grep( gzFile fin, FILE* fout, pcre* re )
66 while (kseq_read(seq) >= 0) {
67 rc = pcre_exec(re, /* pattern */
68 NULL, /* extre data */
69 seq->seq.s, /* subject */
70 seq->seq.l, /* subject length */
71 0, /* subject offset */
73 ovector, /* output vector */
74 3 ); /* output vector length */
76 if ((invert_flag && rc == PCRE_ERROR_NOMATCH) || rc >= 0) {
77 print_fastq_entry( fout, seq );
86 int main(int argc, char* argv[])
90 const char* pat_error;
105 static struct option long_options[] =
107 {"help", no_argument, &help_flag, 1},
108 {"invert-match", no_argument, &invert_flag, 1},
113 opt = getopt_long(argc, argv, "hv", long_options, &opt_idx);
115 if( opt == -1 ) break;
119 if (long_options[opt_idx].flag != 0) break;
145 if (optind >= argc) {
146 fprintf(stderr, "A pattern must be specified.\n");
150 pat = argv[optind++];
151 re = pcre_compile( pat, PCRE_CASELESS, &pat_error, &pat_error_offset, NULL );
155 fprintf(stderr, "Syntax error in PCRE pattern at offset: %d: %s\n",
156 pat_error_offset, pat_error );
161 if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
162 gzfin = gzdopen( fileno(stdin), "rb" );
164 fprintf(stderr, "Malformed file 'stdin'.\n");
168 fastq_grep(gzfin, stdout, re);
173 for (; optind < argc; optind++) {
174 fin = fopen(argv[optind], "rb");
176 fprintf(stderr, "No such file '%s'.\n", argv[optind]);
180 gzfin = gzdopen(fileno(fin), "rb");
182 fprintf(stderr, "Malformed file '%s'.\n", argv[optind]);
186 fastq_grep(gzfin, stdout, re);