+static int count_flag;
+static int id_flag;
+
+
+
+void fastq_grep(FILE* fin, FILE* fout, FILE* mismatch_file, pcre* re)
+{
+ int rc;
+ int ovector[3];
+ size_t count = 0;
+
+ fastq_t* fqf = fastq_create(fin);
+ seq_t* seq = seq_create();
+
+ while (fastq_read(fqf, seq)) {
+
+ rc = pcre_exec(re, /* pattern */
+ NULL, /* extra data */
+ id_flag ? seq->id1.s : seq->seq.s,
+ id_flag ? seq->id1.n : seq->seq.n,
+ 0, /* subject offset */
+ 0, /* options */
+ ovector, /* output vector */
+ 3 ); /* output vector length */
+
+ if ((invert_flag && rc == PCRE_ERROR_NOMATCH) || (!invert_flag && rc >= 0)) {
+ if (count_flag) count++;
+ else fastq_print(fout, seq);
+ }
+ else if (mismatch_file) {
+ fastq_print(mismatch_file, seq);
+ }
+ }
+
+ seq_free(seq);
+ fastq_free(fqf);
+
+ if (count_flag) fprintf(fout, "%zu\n", count);
+}
+
+