]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_md.c
Release samtools-0.1.5
[samtools.git] / bam_md.c
index a7d2ab42f525a5a690aa89bab4f0ea56bb610b85..a20f9b354d477c1f11a8d98854ff9ec7682d01c1 100644 (file)
--- a/bam_md.c
+++ b/bam_md.c
@@ -1,4 +1,5 @@
 #include <unistd.h>
+#include <assert.h>
 #include <string.h>
 #include <ctype.h>
 #include "faidx.h"
@@ -70,7 +71,7 @@ void bam_fillmd1(bam1_t *b, char *ref, int is_equal)
 
 int bam_fillmd(int argc, char *argv[])
 {
-       int c, is_equal = 0, tid = -1, ret, len;
+       int c, is_equal = 0, tid = -2, ret, len;
        bamFile fp, fpout = 0;
        bam_header_t *header;
        faidx_t *fai;
@@ -96,12 +97,14 @@ int bam_fillmd(int argc, char *argv[])
 
        b = bam_init1();
        while ((ret = bam_read1(fp, b)) >= 0) {
-               if (tid != b->core.tid) {
-                       free(ref);
-                       ref = fai_fetch(fai, header->target_name[b->core.tid], &len);
-                       tid = b->core.tid;
+               if (b->core.tid >= 0) {
+                       if (tid != b->core.tid) {
+                               free(ref);
+                               ref = fai_fetch(fai, header->target_name[b->core.tid], &len);
+                               tid = b->core.tid;
+                       }
+                       bam_fillmd1(b, ref, is_equal);
                }
-               bam_fillmd1(b, ref, is_equal);
                bam_write1(fpout, b);
        }
        bam_destroy1(b);