X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Ffastq-grep.c;h=f7b3913beb4b2869a5fd3d4298c24aa1fd0387f7;hb=48a5939e574874106f1450fd278f602b731d2a83;hp=ca072ebc05fd234cab16e1190aa9a2ac1c9e7716;hpb=9537fe433ae5b59162a2f0a0834398c9fb4b7907;p=fastq-tools.git diff --git a/src/fastq-grep.c b/src/fastq-grep.c index ca072eb..f7b3913 100644 --- a/src/fastq-grep.c +++ b/src/fastq-grep.c @@ -1,22 +1,23 @@ - -/* - * fastq-grep: regular expression searches of the sequences within a fastq file +/* + * This file is part of fastq-tools. + * + * Copyright (c) 2011 by Daniel C. Jones * - * Febuary 2011 / Daniel Jones + * fastq-grep : + * Regular expression searches of the sequences within a FASTQ file. * */ +#include "fastq-common.h" +#include "fastq-parse.h" #include #include #include #include #include -#include "kseq.h" -KSEQ_INIT(gzFile, gzread) - #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) # include @@ -36,67 +37,75 @@ void print_help() "Options:\n" " -h, --help print this message\n" " -v, --invert-match select nonmatching entries\n" +" -c, --count output only the number of matching sequences\n" ); } static int invert_flag; static int help_flag; -static int zout_flag; +static int count_flag; -void print_fastq_entry( FILE* fout, kseq_t* seq ) +void print_fastq_entry(FILE* fout, seq_t* seq) { fprintf(fout, "@%s\n%s\n+%s\n%s\n", - seq->name.s, + seq->id1.s, seq->seq.s, - seq->comment.s, + seq->id2.s, seq->qual.s ); } -void fastq_grep( gzFile fin, FILE* fout, pcre* re ) +void fastq_grep(FILE* fin, FILE* fout, pcre* re) { int rc; int ovector[3]; + size_t count = 0; - kseq_t* seq; - seq = kseq_init(fin); + fastq_t* fqf = fastq_open(fin); + seq_t* seq = fastq_alloc_seq(); - while (kseq_read(seq) >= 0) { + while (fastq_next(fqf, seq)) { rc = pcre_exec(re, /* pattern */ NULL, /* extre data */ seq->seq.s, /* subject */ - seq->seq.l, /* subject length */ + seq->seq.n, /* subject length */ 0, /* subject offset */ 0, /* options */ ovector, /* output vector */ 3 ); /* output vector length */ if ((invert_flag && rc == PCRE_ERROR_NOMATCH) || rc >= 0) { - print_fastq_entry( fout, seq ); + if (count_flag) count++; + else print_fastq_entry(fout, seq); } } - kseq_destroy(seq); + fastq_free_seq(seq); + fastq_close(fqf); + + if (count_flag) fprintf(fout, "%zu\n", count); } int main(int argc, char* argv[]) { + SET_BINARY_MODE(stdin); + SET_BINARY_MODE(stdout); + const char* pat; pcre* re; const char* pat_error; int pat_error_offset; FILE* fin; - gzFile gzfin; - invert_flag = 0; - help_flag = 0; - zout_flag = 0; + invert_flag = 0; + help_flag = 0; + count_flag = 0; int opt; int opt_idx; @@ -106,11 +115,12 @@ int main(int argc, char* argv[]) { {"help", no_argument, &help_flag, 1}, {"invert-match", no_argument, &invert_flag, 1}, + {"count", no_argument, &count_flag, 1}, {0, 0, 0, 0} }; while (1) { - opt = getopt_long(argc, argv, "hv", long_options, &opt_idx); + opt = getopt_long(argc, argv, "hvc", long_options, &opt_idx); if( opt == -1 ) break; @@ -118,7 +128,6 @@ int main(int argc, char* argv[]) case 0: if (long_options[opt_idx].flag != 0) break; if (optarg) { - /* TODO */ } break; @@ -130,6 +139,13 @@ int main(int argc, char* argv[]) invert_flag = 1; break; + case 'c': + count_flag = 1; + break; + + case '?': + return 1; + default: abort(); } @@ -157,15 +173,7 @@ int main(int argc, char* argv[]) if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) { - gzfin = gzdopen( fileno(stdin), "rb" ); - if (gzfin == NULL) { - fprintf(stderr, "Malformed file 'stdin'.\n"); - return 1; - } - - fastq_grep(gzfin, stdout, re); - - gzclose(gzfin); + fastq_grep(stdin, stdout, re); } else { for (; optind < argc; optind++) { @@ -175,15 +183,9 @@ int main(int argc, char* argv[]) continue; } - gzfin = gzdopen(fileno(fin), "rb"); - if (gzfin == NULL) { - fprintf(stderr, "Malformed file '%s'.\n", argv[optind]); - continue; - } - - fastq_grep(gzfin, stdout, re); + fastq_grep(fin, stdout, re); - gzclose(gzfin); + fclose(fin); } }