annotate PsiCLASS-1.0.2/samtools-0.1.19/bam2bcf.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 <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdint.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include "bam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "bam2bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include "errmod.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include "bcftools/bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 extern void ks_introsort_uint32_t(size_t n, uint32_t a[]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 #define CALL_ETA 0.03f
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 #define CALL_MAX 256
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 #define CALL_DEFTHETA 0.83f
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 #define DEF_MAPQ 20
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 #define CAP_DIST 25
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 bcf_callaux_t *bca;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 if (theta <= 0.) theta = CALL_DEFTHETA;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 bca = calloc(1, sizeof(bcf_callaux_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 bca->capQ = 60;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 bca->min_baseQ = min_baseQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 bca->e = errmod_init(1. - theta);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 bca->min_frac = 0.002;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 bca->min_support = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 bca->per_sample_flt = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 bca->npos = 100;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 bca->ref_pos = calloc(bca->npos, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 bca->alt_pos = calloc(bca->npos, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 return bca;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 static int get_position(const bam_pileup1_t *p, int *len)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 int icig, n_tot_bases = 0, iread = 0, edist = p->qpos + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 for (icig=0; icig<p->b->core.n_cigar; icig++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 // Conversion from uint32_t to MIDNSHP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 // 0123456
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 // MIDNSHP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 int cig = bam1_cigar(p->b)[icig] & BAM_CIGAR_MASK;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 int ncig = bam1_cigar(p->b)[icig] >> BAM_CIGAR_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 if ( cig==0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 n_tot_bases += ncig;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 iread += ncig;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 else if ( cig==1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 n_tot_bases += ncig;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 iread += ncig;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 else if ( cig==4 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 iread += ncig;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 if ( iread<=p->qpos ) edist -= ncig;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 *len = n_tot_bases;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 return edist;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 void bcf_call_destroy(bcf_callaux_t *bca)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 if (bca == 0) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 errmod_destroy(bca->e);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 if (bca->npos) { free(bca->ref_pos); free(bca->alt_pos); bca->npos = 0; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 free(bca->bases); free(bca->inscns); free(bca);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 /* ref_base is the 4-bit representation of the reference base. It is
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 * negative if we are looking at an indel. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 int i, n, ref4, is_indel, ori_depth = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 memset(r, 0, sizeof(bcf_callret1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 if (ref_base >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 ref4 = bam_nt16_nt4_table[ref_base];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 is_indel = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 } else ref4 = 4, is_indel = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 if (_n == 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 // enlarge the bases array if necessary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 if (bca->max_bases < _n) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 bca->max_bases = _n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 kroundup32(bca->max_bases);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 // fill the bases array
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 for (i = n = r->n_supp = 0; i < _n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 const bam_pileup1_t *p = pl + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 // set base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 ++ori_depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 seqQ = is_indel? (p->aux>>8&0xff) : 99;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 if (q < bca->min_baseQ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 if (q > seqQ) q = seqQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 if (q > mapQ) q = mapQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 if (q > 63) q = 63;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if (q < 4) q = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 if (!is_indel) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 b = p->aux>>16&0x3f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 is_diff = (b != 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 if (is_diff) ++r->n_supp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 // collect annotations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 if (b < 4) r->qsum[b] += q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 min_dist = p->b->core.l_qseq - 1 - p->qpos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 if (min_dist > p->qpos) min_dist = p->qpos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 if (min_dist > CAP_DIST) min_dist = CAP_DIST;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 r->anno[1<<2|is_diff<<1|0] += baseQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 r->anno[2<<2|is_diff<<1|0] += mapQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 r->anno[3<<2|is_diff<<1|0] += min_dist;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 // collect read positions for ReadPosBias
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 int len, pos = get_position(p, &len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 int epos = (double)pos/(len+1) * bca->npos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 if ( bam1_seqi(bam1_seq(p->b),p->qpos) == ref_base )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 bca->ref_pos[epos]++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 bca->alt_pos[epos]++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 r->depth = n; r->ori_depth = ori_depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 // glfgen
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 errmod_cal(bca->e, n, 5, bca->bases, r->p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 return r->depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 double mann_whitney_1947(int n, int m, int U)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 if (U<0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 if (n==0||m==0) return U==0 ? 1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 return (double)n/(n+m)*mann_whitney_1947(n-1,m,U-m) + (double)m/(n+m)*mann_whitney_1947(n,m-1,U);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 void calc_ReadPosBias(bcf_callaux_t *bca, bcf_call_t *call)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 int i, nref = 0, nalt = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 unsigned long int U = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 for (i=0; i<bca->npos; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 nref += bca->ref_pos[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 nalt += bca->alt_pos[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 U += nref*bca->alt_pos[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 bca->ref_pos[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 bca->alt_pos[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 #if 0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 //todo
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 double var = 0, avg = (double)(nref+nalt)/bca->npos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 for (i=0; i<bca->npos; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 double ediff = bca->ref_pos[i] + bca->alt_pos[i] - avg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 var += ediff*ediff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 bca->ref_pos[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 bca->alt_pos[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 call->read_pos.avg = avg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 call->read_pos.var = sqrt(var/bca->npos);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 call->read_pos.dp = nref+nalt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if ( !nref || !nalt )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 call->read_pos_bias = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 if ( nref>=8 || nalt>=8 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 // normal approximation
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 double mean = ((double)nref*nalt+1.0)/2.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 double z = (U-mean)/sqrt(var2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 call->read_pos_bias = z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 //fprintf(stderr,"nref=%d nalt=%d U=%ld mean=%e var=%e zval=%e\n", nref,nalt,U,mean,sqrt(var2),call->read_pos_bias);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 double p = mann_whitney_1947(nalt,nref,U);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 // biased form claimed by GATK to behave better empirically
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 // double var2 = (1.0+1.0/(nref+nalt+1.0))*(double)nref*nalt*(nref+nalt+1.0)/12.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 double z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 if ( p >= 1./sqrt(var2*2*M_PI) ) z = 0; // equal to mean
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 if ( U >= nref*nalt/2. ) z = sqrt(-2*log(sqrt(var2*2*M_PI)*p));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 else z = -sqrt(-2*log(sqrt(var2*2*M_PI)*p));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 call->read_pos_bias = z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 //fprintf(stderr,"nref=%d nalt=%d U=%ld p=%e var2=%e zval=%e\n", nref,nalt,U, p,var2,call->read_pos_bias);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 float mean_diff_to_prob(float mdiff, int dp, int readlen)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 if ( dp==2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 if ( mdiff==0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 return (2.0*readlen + 4.0*(readlen-1.0))/((float)readlen*readlen);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 return 8.0*(readlen - 4.0*mdiff)/((float)readlen*readlen);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 // This is crude empirical approximation and is not very accurate for
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 // shorter read lengths (<100bp). There certainly is a room for
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 // improvement.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 const float mv[24][2] = { {0,0}, {0,0}, {0,0},
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 { 9.108, 4.934}, { 9.999, 3.991}, {10.273, 3.485}, {10.579, 3.160},
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 {10.828, 2.889}, {11.014, 2.703}, {11.028, 2.546}, {11.244, 2.391},
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 {11.231, 2.320}, {11.323, 2.138}, {11.403, 2.123}, {11.394, 1.994},
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 {11.451, 1.928}, {11.445, 1.862}, {11.516, 1.815}, {11.560, 1.761},
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 {11.544, 1.728}, {11.605, 1.674}, {11.592, 1.652}, {11.674, 1.613},
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 {11.641, 1.570} };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 float m, v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 if ( dp>=24 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 m = readlen/8.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 if (dp>100) dp = 100;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 v = 1.476/(0.182*pow(dp,0.514));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 v = v*(readlen/100.);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 m = mv[dp][0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 v = mv[dp][1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 m = m*readlen/100.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 v = v*readlen/100.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 v *= 1.2; // allow more variability
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 return 1.0/(v*sqrt(2*M_PI)) * exp(-0.5*((mdiff-m)/v)*((mdiff-m)/v));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 void calc_vdb(bcf_callaux_t *bca, bcf_call_t *call)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 int i, dp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 float mean_pos = 0, mean_diff = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 for (i=0; i<bca->npos; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 if ( !bca->alt_pos[i] ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 dp += bca->alt_pos[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 int j = i<bca->npos/2 ? i : bca->npos - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 mean_pos += bca->alt_pos[i]*j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 if ( dp<2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 call->vdb = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 mean_pos /= dp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 for (i=0; i<bca->npos; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 if ( !bca->alt_pos[i] ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 int j = i<bca->npos/2 ? i : bca->npos - i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 mean_diff += bca->alt_pos[i] * fabs(j - mean_pos);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 mean_diff /= dp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 call->vdb = mean_diff_to_prob(mean_diff, dp, bca->npos);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 /**
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 * bcf_call_combine() - sets the PL array and VDB, RPB annotations, finds the top two alleles
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 * @n: number of samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 * @calls: each sample's calls
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 * @bca: auxiliary data structure for holding temporary values
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 * @ref_base: the reference base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 * @call: filled with the annotations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 int ref4, i, j, qsum[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 int64_t tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 if (ref_base >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 if (ref4 > 4) ref4 = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 } else call->ori_ref = -1, ref4 = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 // calculate qsum
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 memset(qsum, 0, 4 * sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 for (i = 0; i < n; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 for (j = 0; j < 4; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 qsum[j] += calls[i].qsum[j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 int qsum_tot=0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 for (j=0; j<4; j++) { qsum_tot += qsum[j]; call->qsum[j] = 0; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 // find the top 2 alleles
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 for (i = 1; i < 4; ++i) // insertion sort
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 // set the reference allele and alternative allele(s)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 for (i = 0; i < 5; ++i) call->a[i] = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 call->unseen = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 call->a[0] = ref4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 for (i = 3, j = 1; i >= 0; --i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 if ((qsum[i]&3) != ref4) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 if (qsum[i]>>2 != 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 if ( j<4 ) call->qsum[j] = (float)(qsum[i]>>2)/qsum_tot; // ref N can make j>=4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 call->a[j++] = qsum[i]&3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 else break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 call->qsum[0] = (float)(qsum[i]>>2)/qsum_tot;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 if (ref_base >= 0) { // for SNPs, find the "unseen" base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 call->unseen = j, call->a[j++] = qsum[i]&3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 call->n_alleles = j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 call->n_alleles = j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 // set the PL array
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 if (call->n < n) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 call->n = n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 call->PL = realloc(call->PL, 15 * n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 int x, g[15], z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 double sum_min = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 x = call->n_alleles * (call->n_alleles + 1) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 // get the possible genotypes
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 for (i = z = 0; i < call->n_alleles; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 for (j = 0; j <= i; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 g[z++] = call->a[j] * 5 + call->a[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 uint8_t *PL = call->PL + x * i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 const bcf_callret1_t *r = calls + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 float min = 1e37;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 for (j = 0; j < x; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 if (min > r->p[g[j]]) min = r->p[g[j]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 sum_min += min;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 for (j = 0; j < x; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 int y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 y = (int)(r->p[g[j]] - min + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 if (y > 255) y = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 PL[j] = y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 // if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 call->shift = (int)(sum_min + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 // combine annotations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 memset(call->anno, 0, 16 * sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 call->depth += calls[i].depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 call->ori_depth += calls[i].ori_depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 calc_vdb(bca, call);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 calc_ReadPosBias(bca, call);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int fmt_flag,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 const bcf_callaux_t *bca, const char *ref)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 kstring_t s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 int i, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 b->n_smpl = bc->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 b->tid = tid; b->pos = pos; b->qual = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 s.s = b->str; s.m = b->m_str; s.l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 if (bc->ori_ref < 0) { // an indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 // write REF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 kputc(ref[pos], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 // write ALT
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 kputc(ref[pos], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 for (i = 1; i < 4; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 if (bc->a[i] < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 if (i > 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 kputc(',', &s); kputc(ref[pos], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 if (bca->indel_types[bc->a[i]] < 0) { // deletion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 kputc(ref[pos+1+j], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 } else { // insertion; cannot be a reference unless a bug
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 kputc("ACGTN"[(int)inscns[j]], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 } else { // a SNP
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 for (i = 1; i < 5; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 if (bc->a[i] < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 if (i > 1) kputc(',', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 // INFO
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 if (bc->ori_ref < 0) ksprintf(&s,"INDEL;IS=%d,%f;", bca->max_support, bca->max_frac);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 for (i = 0; i < 16; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 if (i) kputc(',', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 kputw(bc->anno[i], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 //ksprintf(&s,";RPS=%d,%f,%f", bc->read_pos.dp,bc->read_pos.avg,bc->read_pos.var);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 ksprintf(&s,";QS=%f,%f,%f,%f", bc->qsum[0],bc->qsum[1],bc->qsum[2],bc->qsum[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 if (bc->vdb != -1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 ksprintf(&s, ";VDB=%e", bc->vdb);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 if (bc->read_pos_bias != -1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 ksprintf(&s, ";RPB=%e", bc->read_pos_bias);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 // FMT
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 kputs("PL", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 if (bcr && fmt_flag) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 if (fmt_flag & B2B_FMT_DP) kputs(":DP", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 if (fmt_flag & B2B_FMT_DV) kputs(":DV", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 if (fmt_flag & B2B_FMT_SP) kputs(":SP", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 b->m_str = s.m; b->str = s.s; b->l_str = s.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 bcf_sync(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 if (bcr && fmt_flag) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 uint16_t *dp = (fmt_flag & B2B_FMT_DP)? b->gi[1].data : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 uint16_t *dv = (fmt_flag & B2B_FMT_DV)? b->gi[1 + ((fmt_flag & B2B_FMT_DP) != 0)].data : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 int32_t *sp = (fmt_flag & B2B_FMT_SP)? b->gi[1 + ((fmt_flag & B2B_FMT_DP) != 0) + ((fmt_flag & B2B_FMT_DV) != 0)].data : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 for (i = 0; i < bc->n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 bcf_callret1_t *p = bcr + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 if (dp) dp[i] = p->depth < 0xffff? p->depth : 0xffff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 if (dv) dv[i] = p->n_supp < 0xffff? p->n_supp : 0xffff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 if (sp) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 sp[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 double left, right, two;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 int x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 x = (int)(-4.343 * log(two) + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 if (x > 255) x = 255;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 sp[i] = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 }