]> git.donarmstrong.com Git - samtools.git/blobdiff - bcftools/bcf.c
Revert one of my earlier changes - Heng was right, CIGAR P not sensible in a padded...
[samtools.git] / bcftools / bcf.c
index bdc0a5eef5a4321805533f657ec7d0e610366eec..0524408da6cf29f4deb6cee09c69d3dce61c4841 100644 (file)
@@ -13,9 +13,6 @@ bcf_t *bcf_open(const char *fn, const char *mode)
        } else {
                b->fp = strcmp(fn, "-")? bgzf_open(fn, mode) : bgzf_fdopen(fileno(stdin), mode);
        }
-#ifndef BCF_LITE
-       b->fp->ownede_file = 1;
-#endif
        return b;
 }
 
@@ -103,10 +100,16 @@ int bcf_sync(bcf1_t *b)
        ks_tokaux_t aux;
        // set ref, alt, flt, info, fmt
        b->ref = b->alt = b->flt = b->info = b->fmt = 0;
-       for (p = b->str, n = 0; p < b->str + b->l_str; ++p)
-               if (*p == 0 && p+1 != b->str + b->l_str) tmp[n++] = p + 1;
+       for (p = b->str, n = 0; p < b->str + b->l_str; ++p) {
+               if (*p == 0 && p+1 != b->str + b->l_str) {
+                       if (n == 5) {
+                               ++n;
+                               break;
+                       } else tmp[n++] = p + 1;
+               }
+       }
        if (n != 5) {
-               fprintf(stderr, "[%s] incorrect number of fields (%d != 5). Corrupted file?\n", __func__, n);
+               fprintf(stderr, "[%s] incorrect number of fields (%d != 5) at %d:%d\n", __func__, n, b->tid, b->pos);
                return -1;
        }
        b->ref = tmp[0]; b->alt = tmp[1]; b->flt = tmp[2]; b->info = tmp[3]; b->fmt = tmp[4];
@@ -134,12 +137,12 @@ int bcf_sync(bcf1_t *b)
        for (i = 0; i < b->n_gi; ++i) {
                if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
                        b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2;
-               } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2)) {
+               } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("HQ", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
                        b->gi[i].len = 2;
-               } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("GT", 2)
-                                  || b->gi[i].fmt == bcf_str2int("SP", 2))
-               {
+               } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("GT", 2)) {
                        b->gi[i].len = 1;
+               } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
+                       b->gi[i].len = 4;
                } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
                        b->gi[i].len = b->n_alleles * (b->n_alleles + 1) / 2 * 4;
                }
@@ -181,7 +184,7 @@ int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b)
        }
        bgzf_read(bp->fp, b->str, b->l_str);
        l = 12 + b->l_str;
-       bcf_sync(b);
+       if (bcf_sync(b) < 0) return -2;
        for (i = 0; i < b->n_gi; ++i) {
                bgzf_read(bp->fp, b->gi[i].data, b->gi[i].len * h->n_smpl);
                l += b->gi[i].len * h->n_smpl;
@@ -227,7 +230,31 @@ void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s)
        }
        x = b->n_alleles * (b->n_alleles + 1) / 2;
        if (b->n_gi == 0) return;
+    int iPL = -1;
+    if ( b->n_alleles > 2 ) {
+        for (i=0; i<b->n_gi; i++) {
+            if ( b->gi[i].fmt == bcf_str2int("PL", 2) ) {
+                iPL = i;
+                break;
+            }
+        }
+    }
        for (j = 0; j < h->n_smpl; ++j) {
+
+        // Determine GT with maximum PL (multiple ALT sites only)
+        int imax=-1;
+        if ( iPL!=-1 ) {
+            uint8_t *d = (uint8_t*)b->gi[iPL].data + j * x;
+            int k,identical=1;
+            imax=0;
+            for (k=1; k<x; k++)
+            {
+                if ( identical && d[k]!=d[k-1] ) identical = 0;
+                if ( d[k]<d[imax] ) imax = k;
+            }
+            // If all lks are identical, leave GT untouched
+            if ( identical ) imax = -1;
+        }
                kputc('\t', s);
                for (i = 0; i < b->n_gi; ++i) {
                        if (i) kputc(':', s);
@@ -238,19 +265,37 @@ void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s)
                                        if (k > 0) kputc(',', s);
                                        kputw(d[k], s);
                                }
-                       } else if (b->gi[i].fmt == bcf_str2int("DP", 2)) {
+                       } else if (b->gi[i].fmt == bcf_str2int("DP", 2) || b->gi[i].fmt == bcf_str2int("DV", 2)) {
                                kputw(((uint16_t*)b->gi[i].data)[j], s);
-                       } else if (b->gi[i].fmt == bcf_str2int("GQ", 2) || b->gi[i].fmt == bcf_str2int("SP", 2)) {
+                       } else if (b->gi[i].fmt == bcf_str2int("GQ", 2)) {
                                kputw(((uint8_t*)b->gi[i].data)[j], s);
+                       } else if (b->gi[i].fmt == bcf_str2int("SP", 2)) {
+                               kputw(((int32_t*)b->gi[i].data)[j], s);
                        } else if (b->gi[i].fmt == bcf_str2int("GT", 2)) {
-                               int y = ((uint8_t*)b->gi[i].data)[j];
-                               if (y>>7&1) {
-                                       kputsn("./.", 3, s);
-                               } else {
-                                       kputc('0' + (y>>3&7), s);
-                                       kputc("/|"[y>>6&1], s);
-                                       kputc('0' + (y&7), s);
-                               }
+                int y = ((uint8_t*)b->gi[i].data)[j];
+                if ( y>>7&1 )
+                    kputsn("./.", 3, s);
+                else if ( imax==-1 )
+                {
+                    kputc('0' + (y>>3&7), s);
+                    kputc("/|"[y>>6&1], s);
+                    kputc('0' + (y&7), s);
+                }
+                else
+                {
+                    // Arguably, the while loop will be faster than two sqrts
+                    int n = 0;
+                    int row = 1;
+                    while ( n<imax )
+                    {
+                        row++;
+                        n += row;
+                    }
+                    row--;
+                    kputw(imax-n+row, s);
+                    kputc("/|"[y>>6&1], s);
+                    kputw(row, s);
+                }
                        } else if (b->gi[i].fmt == bcf_str2int("GL", 2)) {
                                float *d = (float*)b->gi[i].data + j * x;
                                int k;
@@ -259,7 +304,7 @@ void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *s)
                                        if (k > 0) kputc(',', s);
                                        ksprintf(s, "%.2f", d[k]);
                                }
-                       }
+                       } else kputc('.', s); // custom fields
                }
        }
 }