annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/mut.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdint.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include "bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #define MAX_GENO 359
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 int8_t seq_bitcnt[] = { 4, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 char *seq_nt16rev = "XACMGRSVTWYHKDBN";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 uint32_t *bcf_trio_prep(int is_x, int is_son)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 int i, j, k, n, map[10];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 uint32_t *ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 ret = calloc(MAX_GENO, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 for (i = 0, k = 0; i < 4; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 for (j = i; j < 4; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 map[k++] = 1<<i|1<<j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 for (i = 0, n = 1; i < 10; ++i) { // father
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 if (is_x && seq_bitcnt[map[i]] != 1) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 if (is_x && is_son) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 for (j = 0; j < 10; ++j) // mother
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 for (k = 0; k < 10; ++k) // child
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 if (seq_bitcnt[map[k]] == 1 && (map[j]&map[k]))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 ret[n++] = j<<16 | i<<8 | k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 for (j = 0; j < 10; ++j) // mother
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 for (k = 0; k < 10; ++k) // child
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 if ((map[i]&map[k]) && (map[j]&map[k]) && ((map[i]|map[j])&map[k]) == map[k])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 ret[n++] = j<<16 | i<<8 | k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 ret[0] = n - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 int bcf_trio_call(const uint32_t *prep, const bcf1_t *b, int *llr, int64_t *gt)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 int i, j, k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 const bcf_ginfo_t *PL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 uint8_t *gl10;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 int map[10];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 if (b->n_smpl != 3) return -1; // not a trio
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 for (i = 0; i < b->n_gi; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 if (i == b->n_gi) return -1; // no PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 gl10 = alloca(10 * b->n_smpl);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 if (bcf_gl10(b, gl10) < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 if (bcf_gl10_indel(b, gl10) < 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 PL = b->gi + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 for (i = 0, k = 0; i < 4; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 for (j = i; j < 4; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 map[k++] = seq_nt16rev[1<<i|1<<j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 for (j = 0; j < 3; ++j) // check if ref hom is the most probable in all members
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 if (((uint8_t*)PL->data)[j * PL->len] != 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 if (j < 3) { // we need to go through the complex procedure
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 uint8_t *g[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 int minc = 1<<30, minc_j = -1, minf = 0, gtf = 0, gtc = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 g[0] = gl10;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 g[1] = gl10 + 10;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 g[2] = gl10 + 20;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 for (j = 1; j <= (int)prep[0]; ++j) { // compute LK with constraint
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 int sum = g[0][prep[j]&0xff] + g[1][prep[j]>>8&0xff] + g[2][prep[j]>>16&0xff];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 if (sum < minc) minc = sum, minc_j = j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 gtc |= map[prep[minc_j]&0xff]; gtc |= map[prep[minc_j]>>8&0xff]<<8; gtc |= map[prep[minc_j]>>16]<<16;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 for (j = 0; j < 3; ++j) { // compute LK without constraint
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 int min = 1<<30, min_k = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 for (k = 0; k < 10; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 if (g[j][k] < min) min = g[j][k], min_k = k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 gtf |= map[min_k]<<(j*8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 minf += min;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 *llr = minc - minf; *gt = (int64_t)gtc<<32 | gtf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 } else *llr = 0, *gt = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 int bcf_pair_call(const bcf1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 int i, j, k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 const bcf_ginfo_t *PL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 if (b->n_smpl != 2) return -1; // not a pair
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 for (i = 0; i < b->n_gi; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if (i == b->n_gi) return -1; // no PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 PL = b->gi + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 for (j = 0; j < 2; ++j) // check if ref hom is the most probable in all members
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 if (((uint8_t*)PL->data)[j * PL->len] != 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 if (j < 2) { // we need to go through the complex procedure
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 uint8_t *g[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 int minc = 1<<30, minf = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 g[0] = PL->data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 g[1] = (uint8_t*)PL->data + PL->len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 for (j = 0; j < PL->len; ++j) // compute LK with constraint
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 minc = minc < g[0][j] + g[1][j]? minc : g[0][j] + g[1][j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 for (j = 0; j < 2; ++j) { // compute LK without constraint
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 int min = 1<<30;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 for (k = 0; k < PL->len; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 min = min < g[j][k]? min : g[j][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 minf += min;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 return minc - minf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 } else return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 int bcf_min_diff(const bcf1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 int i, min = 1<<30;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 const bcf_ginfo_t *PL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 for (i = 0; i < b->n_gi; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 if (i == b->n_gi) return -1; // no PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 PL = b->gi + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 for (i = 0; i < b->n_smpl; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 int m1, m2, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 const uint8_t *p = (uint8_t*)PL->data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 m1 = m2 = 1<<30;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 for (j = 0; j < PL->len; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 if ((int)p[j] < m1) m2 = m1, m1 = p[j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 else if ((int)p[j] < m2) m2 = p[j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 min = min < m2 - m1? min : m2 - m1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 return min;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 }