]> git.donarmstrong.com Git - samtools.git/commitdiff
On behalf of Petr: multi-allelic GT
authorHeng Li <lh3@me.com>
Fri, 9 Mar 2012 03:25:34 +0000 (22:25 -0500)
committerHeng Li <lh3@me.com>
Fri, 9 Mar 2012 03:25:34 +0000 (22:25 -0500)
bcftools/bcf.c

index de7bc116736c168fc206a1905af6daccc67005c2..a538e6e14d7a0a78af7927b92073d05da0844640 100644 (file)
@@ -233,7 +233,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);
@@ -251,14 +275,30 @@ void bcf_fmt_core(const bcf_hdr_t *h, bcf1_t *b, kstring_t *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;