| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <math.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <htslib/kfunc.h> |
| 29 |
#include "call.h" |
| 30 |
#include "kmin.h" |
| 31 |
#include "prob1.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
struct _ccall_t |
| 37 |
{ |
| 38 |
bcf_p1aux_t *p1; |
| 39 |
}; |
| 40 |
|
| 41 |
void ccall_init(call_t *call) |
| 42 |
{ |
| 43 |
call->cdat = (ccall_t*) calloc(1,sizeof(ccall_t)); |
| 44 |
call_init_pl2p(call); |
| 45 |
call->cdat->p1 = bcf_p1_init(bcf_hdr_nsamples(call->hdr), call->ploidy); |
| 46 |
call->gts = (int*) calloc(bcf_hdr_nsamples(call->hdr)*2,sizeof(int)); |
| 47 |
call->nals_map = 5; |
| 48 |
call->als_map = (int*) malloc(sizeof(int)*call->nals_map); |
| 49 |
|
| 50 |
bcf_hdr_append(call->hdr,"##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">"); |
| 51 |
if ( call->output_tags & CALL_FMT_GQ ) |
| 52 |
{ |
| 53 |
bcf_hdr_append(call->hdr,"##FORMAT=<ID=GQ,Number=1,Type=Integer,Description=\"Genotype Quality\">"); |
| 54 |
call->GQs = (int32_t*) malloc(sizeof(int32_t)*bcf_hdr_nsamples(call->hdr)); |
| 55 |
} |
| 56 |
if ( call->output_tags & CALL_FMT_GP ) |
| 57 |
error("Sorry, -f GP is not supported with -c\n"); |
| 58 |
bcf_hdr_append(call->hdr,"##INFO=<ID=AF1,Number=1,Type=Float,Description=\"Max-likelihood estimate of the first ALT allele frequency (assuming HWE)\">"); |
| 59 |
|
| 60 |
bcf_hdr_append(call->hdr,"##INFO=<ID=AF2,Number=1,Type=Float,Description=\"Max-likelihood estimate of the first and second group ALT allele frequency (assuming HWE)\">"); |
| 61 |
bcf_hdr_append(call->hdr,"##INFO=<ID=AC1,Number=1,Type=Float,Description=\"Max-likelihood estimate of the first ALT allele count (no HWE assumption)\">"); |
| 62 |
bcf_hdr_append(call->hdr,"##INFO=<ID=MQ,Number=1,Type=Integer,Description=\"Root-mean-square mapping quality of covering reads\">\n"); |
| 63 |
bcf_hdr_append(call->hdr,"##INFO=<ID=FQ,Number=1,Type=Float,Description=\"Phred probability of all samples being the same\">\n"); |
| 64 |
bcf_hdr_append(call->hdr,"##INFO=<ID=PV4,Number=4,Type=Float,Description=\"P-values for strand bias, baseQ bias, mapQ bias and tail distance bias\">\n"); |
| 65 |
bcf_hdr_append(call->hdr,"##INFO=<ID=G3,Number=3,Type=Float,Description=\"ML estimate of genotype frequencies\">\n"); |
| 66 |
bcf_hdr_append(call->hdr,"##INFO=<ID=HWE,Number=1,Type=Float,Description=\"Chi^2 based HWE test P-value based on G3\">\n"); |
| 67 |
|
| 68 |
|
| 69 |
bcf_hdr_append(call->hdr,"##INFO=<ID=DP4,Number=4,Type=Integer,Description=\"Number of high-quality ref-forward , ref-reverse, alt-forward and alt-reverse bases\">"); |
| 70 |
|
| 71 |
return; |
| 72 |
} |
| 73 |
void ccall_destroy(call_t *call) |
| 74 |
{ |
| 75 |
free(call->itmp); |
| 76 |
free(call->als_map); |
| 77 |
free(call->gts); |
| 78 |
free(call->anno16); |
| 79 |
free(call->PLs); |
| 80 |
free(call->GQs); |
| 81 |
free(call->pdg); |
| 82 |
bcf_p1_destroy(call->cdat->p1); |
| 83 |
free(call->cdat); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static void set_pdg3(double *pl2p, int *PLs, double *pdg, int n_smpl, int n_gt) |
| 91 |
{ |
| 92 |
int i; |
| 93 |
for (i=0; i<n_smpl; i++) |
| 94 |
{ |
| 95 |
pdg[2] = pl2p[ PLs[0] ]; |
| 96 |
pdg[1] = pl2p[ PLs[1] ]; |
| 97 |
pdg[0] = pl2p[ PLs[2] ]; |
| 98 |
PLs += n_gt; |
| 99 |
pdg += 3; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static double ttest(int n1, int n2, float a[4]) |
| 104 |
{ |
| 105 |
extern double kf_betai(double a, double b, double x); |
| 106 |
double t, v, u1, u2; |
| 107 |
if (n1 == 0 || n2 == 0 || n1 + n2 < 3) return 1.0; |
| 108 |
u1 = (double)a[0] / n1; u2 = (double)a[2] / n2; |
| 109 |
if (u1 <= u2) return 1.; |
| 110 |
t = (u1 - u2) / sqrt(((a[1] - n1 * u1 * u1) + (a[3] - n2 * u2 * u2)) / (n1 + n2 - 2) * (1./n1 + 1./n2)); |
| 111 |
v = n1 + n2 - 2; |
| 112 |
return t < 0.? 1. : .5 * kf_betai(.5*v, .5, v/(v+t*t)); |
| 113 |
} |
| 114 |
|
| 115 |
static int test16_core(float anno[16], anno16_t *a) |
| 116 |
{ |
| 117 |
extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two); |
| 118 |
double left, right; |
| 119 |
int i; |
| 120 |
a->p[0] = a->p[1] = a->p[2] = a->p[3] = 1.; |
| 121 |
for (i=0; i<4; i++) a->d[i] = anno[i]; |
| 122 |
a->depth = anno[0] + anno[1] + anno[2] + anno[3]; |
| 123 |
a->is_tested = (anno[0] + anno[1] > 0 && anno[2] + anno[3] > 0); |
| 124 |
if (a->depth == 0) return -1; |
| 125 |
a->mq = (int)(sqrt((anno[9] + anno[11]) / a->depth) + .499); |
| 126 |
kt_fisher_exact(anno[0], anno[1], anno[2], anno[3], &left, &right, &a->p[0]); |
| 127 |
for (i = 1; i < 4; ++i) |
| 128 |
a->p[i] = ttest(anno[0] + anno[1], anno[2] + anno[3], anno+4*i); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
int test16(float *anno16, anno16_t *a) |
| 133 |
{ |
| 134 |
a->p[0] = a->p[1] = a->p[2] = a->p[3] = 1.; |
| 135 |
a->d[0] = a->d[1] = a->d[2] = a->d[3] = 0.; |
| 136 |
a->mq = a->depth = a->is_tested = 0; |
| 137 |
return test16_core(anno16, a); |
| 138 |
} |
| 139 |
static int update_bcf1(call_t *call, bcf1_t *rec, const bcf_p1rst_t *pr, double em[10]) |
| 140 |
{ |
| 141 |
int has_I16, is_var; |
| 142 |
float fq, r; |
| 143 |
anno16_t a; |
| 144 |
float tmpf[4], tmpi; |
| 145 |
|
| 146 |
bcf_get_info_float(call->hdr, rec, "I16", &call->anno16, &call->n16); |
| 147 |
|
| 148 |
has_I16 = test16(call->anno16, &a) >= 0? 1 : 0; |
| 149 |
|
| 150 |
|
| 151 |
if (em[0] >= 0) |
| 152 |
{ |
| 153 |
tmpf[0] = 1 - em[0]; |
| 154 |
bcf_update_info_float(call->hdr, rec, "AF1", tmpf, 1); |
| 155 |
} |
| 156 |
if (em[4] >= 0 && em[4] <= 0.05) |
| 157 |
{ |
| 158 |
tmpf[0] = em[3]; tmpf[1] = em[2]; tmpf[2] = em[1]; tmpf[3] = em[4]; |
| 159 |
bcf_update_info_float(call->hdr, rec, "G3", tmpf, 3); |
| 160 |
bcf_update_info_float(call->hdr, rec, "HWE", &tmpf[3], 1); |
| 161 |
} |
| 162 |
if (em[5] >= 0 && em[6] >= 0) |
| 163 |
{ |
| 164 |
tmpf[0] = 1 - em[5]; tmpf[1] = 1 - em[6]; |
| 165 |
bcf_update_info_float(call->hdr, rec, "AF2", tmpf, 2); |
| 166 |
} |
| 167 |
if (em[7] >= 0) |
| 168 |
{ |
| 169 |
tmpf[0] = em[7]; |
| 170 |
bcf_update_info_float(call->hdr, rec, "LRT", tmpf, 1); |
| 171 |
} |
| 172 |
if (em[8] >= 0) |
| 173 |
{ |
| 174 |
tmpf[0] = em[8]; |
| 175 |
bcf_update_info_float(call->hdr, rec, "LRT2", tmpf, 1); |
| 176 |
} |
| 177 |
|
| 178 |
bcf_p1aux_t *p1 = call->cdat->p1; |
| 179 |
if (p1->cons_llr > 0) |
| 180 |
{ |
| 181 |
tmpi = p1->cons_llr; |
| 182 |
bcf_update_info_int32(call->hdr, rec, "CLR", &tmpi, 1); |
| 183 |
|
| 184 |
if (p1->cons_gt > 0) |
| 185 |
{ |
| 186 |
char tmp[4]; |
| 187 |
tmp[0] = p1->cons_gt&0xff; tmp[1] = p1->cons_gt>>8&0xff; tmp[2] = p1->cons_gt>>16&0xff; tmp[3] = 0; |
| 188 |
bcf_update_info_string(call->hdr, rec, "UGT", tmp); |
| 189 |
tmp[0] = p1->cons_gt>>32&0xff; tmp[1] = p1->cons_gt>>40&0xff; tmp[2] = p1->cons_gt>>48&0xff; |
| 190 |
bcf_update_info_string(call->hdr, rec, "CGT", tmp); |
| 191 |
} |
| 192 |
} |
| 193 |
is_var = (pr->p_ref < call->pref); |
| 194 |
r = is_var? pr->p_ref : pr->p_var; |
| 195 |
|
| 196 |
bcf_update_info_int32(call->hdr, rec, "AC1", &pr->ac, 1); |
| 197 |
int32_t dp[4]; dp[0] = call->anno16[0]; dp[1] = call->anno16[1]; dp[2] = call->anno16[2]; dp[3] = call->anno16[3]; |
| 198 |
bcf_update_info_int32(call->hdr, rec, "DP4", dp, 4); |
| 199 |
bcf_update_info_int32(call->hdr, rec, "MQ", &a.mq, 1); |
| 200 |
|
| 201 |
fq = pr->p_ref_folded < 0.5? -4.343 * log(pr->p_ref_folded) : 4.343 * log(pr->p_var_folded); |
| 202 |
if (fq < -999) fq = -999; |
| 203 |
if (fq > 999) fq = 999; |
| 204 |
bcf_update_info_float(call->hdr, rec, "FQ", &fq, 1); |
| 205 |
|
| 206 |
assert( pr->cmp[0]<0 ); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if (has_I16 && a.is_tested) |
| 221 |
{ |
| 222 |
int i; |
| 223 |
for (i=0; i<4; i++) tmpf[i] = a.p[i]; |
| 224 |
bcf_update_info_float(call->hdr, rec, "PV4", tmpf, 4); |
| 225 |
} |
| 226 |
bcf_update_info_int32(call->hdr, rec, "I16", NULL, 0); |
| 227 |
bcf_update_info_int32(call->hdr, rec, "QS", NULL, 0); |
| 228 |
|
| 229 |
rec->qual = r < 1e-100? 999 : -4.343 * log(r); |
| 230 |
if (rec->qual > 999) rec->qual = 999; |
| 231 |
|
| 232 |
|
| 233 |
int nals_ori = rec->n_allele, nals = !is_var && !(call->flag & CALL_KEEPALT) ? 1 : pr->rank0 < 2? 2 : pr->rank0+1; |
| 234 |
if ( call->flag & CALL_KEEPALT && call->unseen==nals-1 ) nals--; |
| 235 |
|
| 236 |
if ( nals<rec->n_allele ) |
| 237 |
{ |
| 238 |
bcf_update_alleles(call->hdr, rec, (const char**)rec->d.allele, nals); |
| 239 |
|
| 240 |
|
| 241 |
int npls_src = call->nPLs / rec->n_sample, npls_dst = nals*(nals+1)/2; |
| 242 |
int *pls_src = call->PLs - npls_src, *pls_dst = call->PLs - npls_dst; |
| 243 |
int isample, i; |
| 244 |
for (isample = 0; isample < rec->n_sample; isample++) |
| 245 |
{ |
| 246 |
pls_src += npls_src; |
| 247 |
pls_dst += npls_dst; |
| 248 |
if ( !call->ploidy || call->ploidy[isample]==2 ) |
| 249 |
{ |
| 250 |
for (i=0; i<npls_dst; i++) |
| 251 |
pls_dst[i] = pls_src[i]; |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
for (i=0; i<nals; i++) |
| 256 |
{ |
| 257 |
int isrc = (i+1)*(i+2)/2-1; |
| 258 |
pls_dst[i] = pls_src[isrc]; |
| 259 |
} |
| 260 |
if (i<npls_dst) pls_dst[i] = bcf_int32_vector_end; |
| 261 |
} |
| 262 |
} |
| 263 |
bcf_update_format_int32(call->hdr, rec, "PL", call->PLs, npls_dst*rec->n_sample); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
int i; |
| 268 |
for (i=0; i<rec->n_sample; i++) |
| 269 |
{ |
| 270 |
int x = ( is_var || call->output_tags & CALL_FMT_GQ ) ? bcf_p1_call_gt(p1, pr->f_exp, i, is_var) : 2; |
| 271 |
int gt = x&3; |
| 272 |
if ( !call->ploidy || call->ploidy[i]==2 ) |
| 273 |
{ |
| 274 |
if ( gt==1 ) |
| 275 |
{ |
| 276 |
call->gts[2*i] = bcf_gt_unphased(0); |
| 277 |
call->gts[2*i+1] = bcf_gt_unphased(1); |
| 278 |
} |
| 279 |
else if ( gt==0 ) |
| 280 |
{ |
| 281 |
call->gts[2*i] = bcf_gt_unphased(1); |
| 282 |
call->gts[2*i+1] = bcf_gt_unphased(1); |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
call->gts[2*i] = bcf_gt_unphased(0); |
| 287 |
call->gts[2*i+1] = bcf_gt_unphased(0); |
| 288 |
} |
| 289 |
if ( call->output_tags & CALL_FMT_GQ ) call->GQs[i] = x>>2; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
if ( gt==0 ) call->gts[2*i] = bcf_gt_unphased(1); |
| 294 |
else call->gts[2*i] = bcf_gt_unphased(0); |
| 295 |
call->gts[2*i+1] = bcf_int32_vector_end; |
| 296 |
if ( call->output_tags & CALL_FMT_GQ ) call->GQs[i] = bcf_int32_missing; |
| 297 |
} |
| 298 |
} |
| 299 |
bcf_update_genotypes(call->hdr, rec, call->gts, rec->n_sample*2); |
| 300 |
if ( call->output_tags & CALL_FMT_GQ ) |
| 301 |
bcf_update_format_int32(call->hdr, rec, "GQ", call->GQs, rec->n_sample); |
| 302 |
|
| 303 |
|
| 304 |
int out_als = 0; |
| 305 |
for (i=0; i<nals; i++) out_als |= 1<<i; |
| 306 |
init_allele_trimming_maps(call, nals_ori, out_als); |
| 307 |
mcall_trim_and_update_numberR(call, rec, nals_ori, nals); |
| 308 |
|
| 309 |
return is_var; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
int ccall(call_t *call, bcf1_t *rec) |
| 314 |
{ |
| 315 |
int nsmpl = bcf_hdr_nsamples(call->hdr); |
| 316 |
|
| 317 |
|
| 318 |
int nals = rec->n_allele; |
| 319 |
call->nPLs = bcf_get_format_int32(call->hdr, rec, "PL", &call->PLs, &call->mPLs); |
| 320 |
if ( call->nPLs!=nsmpl*nals*(nals+1)/2 && call->nPLs!=nsmpl*nals ) |
| 321 |
error("Wrong number of PL fields? nals=%d npl=%d\n", nals,call->nPLs); |
| 322 |
|
| 323 |
|
| 324 |
int ngts = nals*(nals+1)/2; |
| 325 |
hts_expand(double, 3*nsmpl, call->npdg, call->pdg); |
| 326 |
set_pdg3(call->pl2p, call->PLs, call->pdg, nsmpl, ngts); |
| 327 |
|
| 328 |
double em[10] = {-1.,-1.,-1.,-1.,-1.,-1.,-1.,-1.,-1.,-1.}; |
| 329 |
int ret = bcf_em1(call, rec, call->ngrp1_samples, 0x1ff, em); |
| 330 |
|
| 331 |
bcf_p1rst_t pr; |
| 332 |
int do_contrast = (em[7] >= 0 && em[7] < call->min_lrt) ? 1 : 0; |
| 333 |
ret = bcf_p1_cal(call, rec, do_contrast, call->cdat->p1, &pr); |
| 334 |
if (pr.p_ref >= call->pref && (call->flag & CALL_VARONLY)) return 0; |
| 335 |
if (ret >= 0) ret = update_bcf1(call, rec, &pr, em); |
| 336 |
return ret; |
| 337 |
} |
| 338 |
|