]> git.donarmstrong.com Git - samtools.git/blobdiff - bcftools/bcfutils.c
Merge pull request #44 from peterjc/check_bins
[samtools.git] / bcftools / bcfutils.c
index 7988e580db1fde1d9d2f23d2cd6485b91e1d4126..7638085d4ffacce35f26b401ff1a90f48a975454 100644 (file)
@@ -74,15 +74,8 @@ void bcf_fit_alt(bcf1_t *b, int mask)
     int i,j,nals=0;
     for (i=0; i<sizeof(int); i++)
         if ( mask&1<<i) nals++;
-    
     if ( b->n_alleles <= nals ) return;
 
-#if DBG
-    printf("fit_alt: %s, %s, mask=%d, nals=%d: ", b->ref, b->alt, mask, nals);
-    for (i=0; i<sizeof(int); i++)
-        if ( mask&1<<i) printf(" %d", i);
-    printf("\n");
-#endif
     // update ALT, in principle any of the alleles can be removed
     char *p;
     if ( nals>1 ) 
@@ -118,9 +111,6 @@ void bcf_fit_alt(bcf1_t *b, int mask)
         p = dst;
     }
     else p = b->alt, *p = '\0';
-#if DBG
-    printf("fit_alt: %s, mask=%d\n", b->alt, mask);
-#endif
     p++;
     memmove(p, b->flt, b->str + b->l_str - b->flt);
     b->l_str -= b->flt - p;
@@ -180,6 +170,7 @@ void bcf_fit_alt(bcf1_t *b, int mask)
     }
     free(map);
     b->n_alleles = nals;
+    bcf_sync(b);
 }
 
 int bcf_shrink_alt(bcf1_t *b, int n)
@@ -249,7 +240,7 @@ int bcf_fix_gt(bcf1_t *b)
        bcf_ginfo_t gt;
        // check the presence of the GT FMT
        if ((s = strstr(b->fmt, ":GT")) == 0) return 0; // no GT or GT is already the first
-       if (s[3] != '\0' && s[3] != ':') return 0; // :GTX in fact
+       assert(s[3] == '\0' || s[3] == ':'); // :GTX in fact
        tmp = bcf_str2int("GT", 2);
        for (i = 0; i < b->n_gi; ++i)
                if (b->gi[i].fmt == tmp) break;
@@ -258,7 +249,10 @@ int bcf_fix_gt(bcf1_t *b)
        // move GT to the first
        for (; i > 0; --i) b->gi[i] = b->gi[i-1];
        b->gi[0] = gt;
-       memmove(b->fmt + 3, b->fmt, s + 1 - b->fmt);
+    if ( s[3]==0 )
+        memmove(b->fmt + 3, b->fmt, s - b->fmt);        // :GT
+    else
+        memmove(b->fmt + 3, b->fmt, s - b->fmt + 1);    // :GT:
        b->fmt[0] = 'G'; b->fmt[1] = 'T'; b->fmt[2] = ':';
        return 0;
 }
@@ -403,7 +397,11 @@ bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int
                        kputs(samples[i], &s); kputc('\0', &s);
                }
        }
-       if (j < n) fprintf(stderr, "<%s> %d samples in the list but not in BCF.", __func__, n - j);
+       if (j < n) 
+    {
+        fprintf(stderr, "<%s> %d samples in the list but not in BCF.", __func__, n - j);
+        exit(1);
+    }
        kh_destroy(str2id, hash);
        h = calloc(1, sizeof(bcf_hdr_t));
        *h = *h0;