]> git.donarmstrong.com Git - fastq-tools.git/blobdiff - src/parse.c
A program to randomly sample reads from a fastq file.
[fastq-tools.git] / src / parse.c
index bec5ac346093a21cd39baef72b88e8625186728e..61e3efa3cab29160ce59e4823d9aaa726e335059 100644 (file)
@@ -67,7 +67,7 @@ typedef enum
 fastq_t* fastq_open(FILE* f)
 {
     fastq_t* fqf = malloc_or_die(sizeof(fastq_t));
-    or_die((int)(fqf->file = gzdopen(fileno(f), "rb")),
+    or_die((int)((fqf->file = gzdopen(fileno(f), "rb")) != NULL),
            "Can not open gzip file.");
     
     fqf->state = STATE_ID1;
@@ -113,7 +113,7 @@ void fastq_refill(fastq_t* f)
 
 void fastq_get_line(fastq_t* f, str_t* s)
 {
-    int i = 0;
+    size_t i = 0;
 
     if (f->state == STATE_EOF) goto fastq_get_line_done;
 
@@ -169,14 +169,16 @@ int fastq_next(fastq_t* f, seq_t* seq)
         }
 
         /* skip comments */
+        /*
         else if (*f->c == ';') {
             fastq_get_line(f, NULL);
             if (f->state == STATE_EOF) return 0;
         }
+        */
 
         /* read id1 */
         else if (f->state == STATE_ID1) {
-            if (*f->c == '@') {
+            if (*f->c == '@' || *f->c == '>') {
                 f->c++;
                 fastq_get_line(f, &seq->id1);
                 if (f->state == STATE_EOF) return 0;
@@ -184,7 +186,9 @@ int fastq_next(fastq_t* f, seq_t* seq)
                 f->state = STATE_SEQ;
             }
             else {
-                fprintf(stderr, "Malformed FASTQ file: expecting an '@', saw a '%c'\n", *f->c);
+                fprintf(stderr,
+                        "Malformed FASTQ file: expecting an '@' or '>', saw a '%c'\n",
+                        *f->c);
                 exit(1);
             }
         }
@@ -207,8 +211,12 @@ int fastq_next(fastq_t* f, seq_t* seq)
                 f->state = STATE_QUAL;
             }
             else {
-                fprintf(stderr, "Malformed FASTQ file: expecting an '+', saw a '%c'\n", *f->c);
-                exit(1);
+                /* fasta style entry */
+                seq->id2.s[0]  = '\0';
+                seq->qual.s[0] = '\0';
+
+                f->state = STATE_ID1;
+                break;
             }
         }
 
@@ -233,3 +241,32 @@ int fastq_next(fastq_t* f, seq_t* seq)
 }
 
 
+void fastq_rewind(fastq_t* fqf)
+{
+    gzrewind(fqf->file);
+    fqf->state = STATE_ID1;
+    fqf->buf[0] = '\0';
+    fqf->c = fqf->buf;
+}
+
+
+void fastq_print(FILE* fout, seq_t* seq)
+{
+    /* FASTQ */
+    if (seq->qual.n > 0) {
+        fprintf(fout, "@%s\n%s\n+%s\n%s\n",
+                      seq->id1.s,
+                      seq->seq.s,
+                      seq->id2.s,
+                      seq->qual.s );
+    }
+
+    /* FASTA */
+    else {
+        fprintf(fout, ">%s\n%s\n",
+                      seq->id1.s,
+                      seq->seq.s );
+    }
+}
+
+