]> git.donarmstrong.com Git - fastq-tools.git/blobdiff - src/fastq-uniq.c
Much simpler faster code for parsing fastq files.
[fastq-tools.git] / src / fastq-uniq.c
index fddb52a7073fdb7d7120a22df855988779083652..3a92565c8a5118dbd2b13fd4720347058f619709 100644 (file)
@@ -13,6 +13,7 @@
 #include "hash.h"
 #include "parse.h"
 #include <string.h>
+#include <inttypes.h>
 #include <zlib.h>
 #include <getopt.h>
 
 #  define SET_BINARY_MODE(file)
 #endif
 
-
-static int help_flag;
-static int verbose_flag;
-size_t total_reads;
+static const char* prog_name = "fastq-uniq";
 
 void print_help()
 {
@@ -37,18 +35,24 @@ void print_help()
 "Output a non-redundant FASTQ file, in which there are no duplicate reads.\n"
 "(Warning: this program can be somewhat memory intensive.)\n\n"
 "Options:\n"
-"  -h, --help              print this message\n"
 "  -v, --verbose           print status along the way\n"
+"  -h, --help              print this message\n"
+"  -V, --version           output version information and exit\n"
     );
 }
 
 
+static int verbose_flag;
+static size_t total_reads;
+
+
+
 void fastq_hash(FILE* fin, hash_table* T)
 {
-    fastq_t* fqf = fastq_open(fin);
-    seq_t* seq = fastq_alloc_seq();
+    fastq_t* fqf = fastq_create(fin);
+    seq_t* seq = seq_create();
 
-    while (fastq_next(fqf, seq)) {
+    while (fastq_read(fqf, seq)) {
         inc_hash_table(T, seq->seq.s, seq->seq.n);
 
         total_reads++;
@@ -57,8 +61,8 @@ void fastq_hash(FILE* fin, hash_table* T)
         }
     }
 
-    fastq_free_seq(seq);
-    fastq_close(fqf);
+    seq_free(seq);
+    fastq_free(fqf);
 }
 
 
@@ -81,7 +85,7 @@ void print_hash_table(FILE* fout, hash_table* T)
 
     size_t i;
     for (i = 0; i < T->m; i++) {
-        fprintf(fout, ">unique-read-%07zu (%zu copies)\n", i, S[i]->count);
+        fprintf(fout, ">unique-read-%07zu (%"PRIu32" copies)\n", i, S[i]->count);
         fwrite(S[i]->value, S[i]->len, sizeof(char), fout);
         fprintf(fout, "\n");
     }
@@ -99,20 +103,19 @@ int main(int argc, char* argv[])
 
     FILE* fin   ;
 
-    help_flag = 0;
-
     int opt;
     int opt_idx;
 
     static struct option long_options[] =
     {
-        {"help",    no_argument, &help_flag,    1},
         {"verbose", no_argument, &verbose_flag, 1},
+        {"help",    no_argument, NULL,          'h'},
+        {"version", no_argument, NULL,          'V'},
         {0, 0, 0, 0}
     };
 
     while (1) {
-        opt = getopt_long(argc, argv, "hv", long_options, &opt_idx);
+        opt = getopt_long(argc, argv, "vhV", long_options, &opt_idx);
 
         if (opt == -1) break;
 
@@ -123,10 +126,6 @@ int main(int argc, char* argv[])
                 }
                 break;
 
-            case 'h':
-                help_flag = 1;
-                break;
-
             case 'v':
                 verbose_flag = 1;
                 break;
@@ -134,15 +133,19 @@ int main(int argc, char* argv[])
             case '?':
                 return 1;
 
+            case 'h':
+                print_help();
+                return 0;
+
+            case 'V':
+                print_version(stdout, prog_name);
+                return 0;
+
             default:
                 abort();
         }
     }
 
-    if (help_flag) {
-        print_help();
-        return 0;
-    }
 
     if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
         fastq_hash(stdin, T);