Imported Upstream version 0.1.18
[samtools.git] / bcftools / bcfutils.c
index e11cfce2663029d480d03c8bebf7b338a9b3b973..0eab4c1f322b398750fdda9da4caec6eea8e7579 100644 (file)
@@ -5,6 +5,11 @@
 #include "khash.h"
 KHASH_MAP_INIT_STR(str2id, int)
 
 #include "khash.h"
 KHASH_MAP_INIT_STR(str2id, int)
 
+#ifdef _WIN32
+#define srand48(x) srand(x)
+#define drand48() ((double)rand() / RAND_MAX)
+#endif
+
 // FIXME: valgrind report a memory leak in this function. Probably it does not get deallocated...
 void *bcf_build_refhash(bcf_hdr_t *h)
 {
 // FIXME: valgrind report a memory leak in this function. Probably it does not get deallocated...
 void *bcf_build_refhash(bcf_hdr_t *h)
 {
@@ -349,8 +354,6 @@ int bcf_gl10(const bcf1_t *b, uint8_t *gl)
        for (i = 0; i < b->n_smpl; ++i) {
                const uint8_t *p = PL->data + i * PL->len; // the PL for the i-th individual
                uint8_t *g = gl + 10 * i;
        for (i = 0; i < b->n_smpl; ++i) {
                const uint8_t *p = PL->data + i * PL->len; // the PL for the i-th individual
                uint8_t *g = gl + 10 * i;
-               for (j = 0; j < PL->len; ++j)
-                       if (p[j]) break;
                for (k = j = 0; k < 4; ++k) {
                        for (l = k; l < 4; ++l) {
                                int t, x = map[k], y = map[l];
                for (k = j = 0; k < 4; ++k) {
                        for (l = k; l < 4; ++l) {
                                int t, x = map[k], y = map[l];
@@ -361,3 +364,27 @@ int bcf_gl10(const bcf1_t *b, uint8_t *gl)
        }
        return 0;
 }
        }
        return 0;
 }
+
+int bcf_gl10_indel(const bcf1_t *b, uint8_t *gl)
+{
+       int k, l, j, i;
+       const bcf_ginfo_t *PL;
+       if (b->alt[0] == 0) return -1; // no alternate allele
+       for (i = 0; i < b->n_gi; ++i)
+               if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
+       if (i == b->n_gi) return -1; // no PL
+       PL = b->gi + i;
+       for (i = 0; i < b->n_smpl; ++i) {
+               const uint8_t *p = PL->data + i * PL->len; // the PL for the i-th individual
+               uint8_t *g = gl + 10 * i;
+               for (k = j = 0; k < 4; ++k) {
+                       for (l = k; l < 4; ++l) {
+                               int t, x = k, y = l;
+                               if (x > y) t = x, x = y, y = t; // make sure x is the smaller
+                               x = y * (y+1) / 2 + x;
+                               g[j++] = x < PL->len? p[x] : 255;
+                       }
+               }
+       }
+       return 0;
+}