]> git.donarmstrong.com Git - fastq-tools.git/blobdiff - src/fastq-qualadj.c
Much simpler faster code for parsing fastq files.
[fastq-tools.git] / src / fastq-qualadj.c
index b14e8c9097264a5dea22afcd85f2ffe6eab1760a..51919b0c52691dd1a2782e12989218d46b6ebcfc 100644 (file)
@@ -44,14 +44,14 @@ void print_help()
 
 void fastq_qualadj(FILE* fin, FILE* fout, int offset)
 {
-    fastq_t* fqf = fastq_open(fin);
-    seq_t* seq = fastq_alloc_seq();
+    fastq_t* fqf = fastq_create(fin);
+    seq_t* seq = seq_create();
     size_t i;
     int c;
 
-    while (fastq_next(fqf, seq)) {
+    while (fastq_read(fqf, seq)) {
         for (i = 0; i < seq->qual.n; ++i) {
-            c = (int) seq->qual.s[i] - offset;
+            c = (int) seq->qual.s[i] + offset;
             c = c < 0 ? 0 : (c > 126 ? 126: c);
             seq->qual.s[i] = (char) c;
         }
@@ -59,8 +59,8 @@ void fastq_qualadj(FILE* fin, FILE* fout, int offset)
         fastq_print(fout, seq);
     }
 
-    fastq_free_seq(seq);
-    fastq_close(fqf);
+    seq_free(seq);
+    fastq_free(fqf);
 }
 
 
@@ -81,12 +81,40 @@ int main(int argc, char* argv[])
     int opt;
     int opt_idx;
 
+    char* tmp;
+    size_t tmplen;
+
     while (1) {
-        opt = getopt_long(argc, argv, "hV", long_options, &opt_idx);
+        opt = getopt_long(argc, argv, "hV0:1::2::3::4::5::6::7::8::9::", long_options, &opt_idx);
 
         if (opt == -1) break;
 
         switch(opt) {
+
+            /* this is a bit of a hack to prevent getopt from choking on
+             * negative numbers. */
+            case '0':
+            case '1':
+            case '2':
+            case '3':
+            case '4':
+            case '5':
+            case '6':
+            case '7':
+            case '8':
+            case '9':
+                tmplen = 2;
+                if (optarg) tmplen += strlen(optarg);
+                tmp = malloc(tmplen + 1);
+
+                if (optarg) snprintf(tmp, tmplen + 1, "-%c%s", (char) opt, optarg);
+                else        snprintf(tmp, tmplen + 1, "-%c",   (char) opt);
+
+                offset = atoi(tmp);
+                free(tmp);
+                break;
+
+
             case 'h':
                 print_help();
                 return 0;
@@ -103,13 +131,11 @@ int main(int argc, char* argv[])
         }
     }
 
-    if (optind >= argc) {
+    if (offset == 0 && optind >= argc) {
         fprintf(stderr, "An offset must be specified.\n");
         return 1;
     }
-
-    offset = atoi(argv[optind++]);
-
+    else if (offset == 0) offset = atoi(argv[optind++]);
 
     FILE* fin;