]> git.donarmstrong.com Git - fastq-tools.git/blobdiff - src/fastq-grep.c
Added a program to adjust quality scores by a fixed offset.
[fastq-tools.git] / src / fastq-grep.c
index 3eaffb79c53b89512a4ffe73d295981c7af7566f..994ab587324eb24133baecac8cb75824a4d52f77 100644 (file)
 #endif
 
 
+static const char* prog_name = "fastq-grep";
+
+
 void print_help()
 {
-    fprintf( stderr
+    fprintf(stdout
 "fastq-grep [OPTION]... PATTERN [FILE]...\n"
 "Search for PATTERN in the read sequences in each FILE or standard input.\n"
 "PATTERN, by default, is a perl compatible regular expression.\n\n"
 "Options:\n"
-"  -h, --help              print this message\n"
+"  -i, --id                match the read id (by default, sequence is matched)\n"
 "  -v, --invert-match      select nonmatching entries\n"
 "  -c, --count             output only the number of matching sequences\n"
+"  -h, --help              print this message\n"
+"  -V, --version           output version information and exit\n"
     );
 }
 
 static int invert_flag;
-static int help_flag;
 static int count_flag;
+static int id_flag;
 
 
 
@@ -57,16 +62,17 @@ void fastq_grep(FILE* fin, FILE* fout, pcre* re)
     seq_t* seq = fastq_alloc_seq();
 
     while (fastq_next(fqf, seq)) {
+
         rc = pcre_exec(re,          /* pattern */
-                       NULL,        /* extre data */
-                       seq->seq.s,  /* subject */
-                       seq->seq.n,  /* subject length */
+                       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) || rc >= 0) {
+        if ((invert_flag && rc == PCRE_ERROR_NOMATCH) || (!invert_flag && rc >= 0)) {
             if (count_flag) count++;
             else            fastq_print(fout, seq);
         }
@@ -94,8 +100,8 @@ int main(int argc, char* argv[])
 
 
     invert_flag  = 0;
-    help_flag    = 0;
     count_flag   = 0;
+    id_flag      = 0;
 
     int opt;
     int opt_idx;
@@ -103,16 +109,18 @@ int main(int argc, char* argv[])
 
     static struct option long_options[] =
         { 
-          {"help", no_argument, &help_flag, 1},
+          {"id",           no_argument, &id_flag,     1},
           {"invert-match", no_argument, &invert_flag, 1},
-          {"count", no_argument, &count_flag, 1},
+          {"count",        no_argument, &count_flag,  1},
+          {"help",         no_argument, NULL, 'h'},
+          {"version",      no_argument, NULL, 'V'},
           {0, 0, 0, 0}
         };
 
     while (1) {
-        opt = getopt_long(argc, argv, "hvc", long_options, &opt_idx);
+        opt = getopt_long(argc, argv, "ivchV", long_options, &opt_idx);
 
-        if( opt == -1 ) break;
+        if (opt == -1) break;
 
         switch (opt) {
             case 0:
@@ -121,8 +129,8 @@ int main(int argc, char* argv[])
                 }
                 break;
 
-            case 'h':
-                help_flag = 1;
+            case 'i':
+                id_flag = 1;
                 break;
 
             case 'v':
@@ -133,6 +141,14 @@ int main(int argc, char* argv[])
                 count_flag = 1;
                 break;
 
+            case 'h':
+                print_help();
+                return 0;
+
+            case 'V':
+                print_version(stdout, prog_name);
+                return 0;
+
             case '?':
                 return 1;
 
@@ -141,11 +157,6 @@ int main(int argc, char* argv[])
         }
     }
 
-    if (help_flag) {
-        print_help();
-        return 0;
-    }
-
     if (optind >= argc) {
         fprintf(stderr, "A pattern must be specified.\n");
         return 1;