| 1 |
/* |
| 2 |
prob.c - probability calculations used in scythe. |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <math.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "scythe.h"cxx . . $ `<file>/src/scythe.h`/ |
| 11 |
|
| 12 |
float *qual_to_probscxx . . $ qual_to_probs(a661829a82ca3f60).(const char *qual, quality_typecxx . . $ quality_type# q_type) { |
| 13 |
/* |
| 14 |
Converts qualities to the probability the base is correct. |
| 15 |
There are two quality-to-probability conversion formulas. |
| 16 |
- Solexa (pre-1.3 pipeline): |
| 17 |
q = -10 log10 (p/(1 - p)) |
| 18 |
p = 1/(1 + 10^(q/10)) |
| 19 |
|
| 20 |
- Illumina/Sanger/PHRED: |
| 21 |
q = -10 log10 p |
| 22 |
p = 1/(10^(q/10)) |
| 23 |
|
| 24 |
From http://en.wikipedia.org/wiki/FASTQ. |
| 25 |
*/ |
| 26 |
int i, n = strlencxx . . $ strlen(751346f8406fd082).(qual), q; |
| 27 |
float *probs = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(sizeof(float)*n); |
| 28 |
for (i = 0; i < n; i++) { |
| 29 |
q = (char) qual[i]-quality_contantscxx . . $ quality_contants.[q_type][Q_OFFSETcxx . . $ `src/scythe.h:39:9`!]; |
| 30 |
if (q < quality_contantscxx . . $ quality_contants.[q_type][Q_MINcxx . . $ `src/scythe.h:40:9`!] || q > quality_contantscxx . . $ quality_contants.[q_type][Q_MAXcxx . . $ `src/scythe.h:41:9`!]) { |
| •31 |
fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Base quality out of range for specified quality type (%d): %d\n", q_type, q); |
| 32 |
exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 33 |
} |
| 34 |
if (q_type == SOLEXAcxx . . $ SOLEXA.) { |
| 35 |
probs[i] = 1 - 1/(1 + powf(10, (q/10.0))); |
| 36 |
} else { |
| 37 |
probs[i] = 1 - 1/(powf(10, (q/10.0))); |
| 38 |
} |
| 39 |
} |
| 40 |
return probs; |
| 41 |
} |
| 42 |
|
| 43 |
double p_datacxx . . $ p_data(9649a79eb69ac649).(const int *matches, float *p_quals, float p_prior_contam, float p_match, int n) { |
| 44 |
/* P(D), using the Law of Total Probability */ |
| 45 |
double p_not_contam = 1.0, p_contam = 1.0; |
| 46 |
int i; |
| 47 |
for (i = 0; i < n; i++) { |
| 48 |
if (matches[i] == 1) { |
| 49 |
p_not_contam *= p_match; |
| 50 |
p_contam *= p_quals[i]; |
| 51 |
} else { |
| 52 |
p_not_contam *= 1 - p_match; |
| 53 |
p_contam *= 1 - p_quals[i]; |
| 54 |
} |
| 55 |
} |
| 56 |
return p_prior_contam*p_contam + (1-p_prior_contam)*p_not_contam; |
| 57 |
} |
| 58 |
|
| 59 |
posterior_setcxx . . $ posterior_set# *posteriorcxx . . $ posterior(eda8e32dde0ec8a5).(const int *matches, float *p_quals, float p_prior, float p_match, int n) { |
| 60 |
/* The posterior for both the random model and the non-random |
| 61 |
model. |
| 62 |
|
| 63 |
`matches`: a int array of 0s and 1s indicating matching and |
| 64 |
mismatch positions. |
| 65 |
`p_quals`: quality values convered to probabilities. |
| 66 |
`p_prior`: prior probability |
| 67 |
`p_match`: probability of a match (for DNA: default is 0.25) |
| 68 |
`n`: match length. |
| 69 |
*/ |
| 70 |
|
| 71 |
posterior_setcxx . . $ posterior_set# *ps = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(sizeof(posterior_setcxx . . $ posterior_set#)); |
| 72 |
likelihood_setcxx . . $ likelihood_set# *ls = likelihoodcxx . . $ likelihood(e837e324fda52277).(matches, p_quals, p_match, n); |
| 73 |
double p_denom = p_datacxx . . $ p_data(9649a79eb69ac649).(matches, p_quals, p_prior, p_match, n); |
| 74 |
ps->contamcxx . . $ posterior_set#contam. = (ls->contamcxx . . $ likelihood_set#contam. * p_prior)/p_denom; |
| 75 |
ps->randomcxx . . $ posterior_set#random. = (ls->randomcxx . . $ likelihood_set#random. * (1-p_prior))/p_denom; |
| 76 |
ps->is_contamcxx . . $ posterior_set#is_contam. = ps->contamcxx . . $ posterior_set#contam. > ps->randomcxx . . $ posterior_set#random.; |
| 77 |
freecxx . . $ free(5d22bdacc48458e8).(ls); |
| 78 |
return ps; |
| 79 |
} |
| 80 |
|
| 81 |
likelihood_setcxx . . $ likelihood_set# *likelihoodcxx . . $ likelihood(e837e324fda52277).(const int *matches, float *p_quals, float p_match, int n) { |
| 82 |
/* |
| 83 |
Return a likelihood_set, which contains likelihoods of competing |
| 84 |
models: contamination and random matches. |
| 85 |
*/ |
| 86 |
likelihood_setcxx . . $ likelihood_set# *ls = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(sizeof(likelihood_setcxx . . $ likelihood_set#)); |
| 87 |
int i; |
| 88 |
ls->randomcxx . . $ likelihood_set#random. = 1; |
| 89 |
ls->contamcxx . . $ likelihood_set#contam. = 1; |
| 90 |
for (i = 0; i < n; i++) { |
| 91 |
if (matches[i] == 1) { |
| 92 |
ls->randomcxx . . $ likelihood_set#random. *= p_match; /* prob match happened by chance */ |
| 93 |
ls->contamcxx . . $ likelihood_set#contam. *= p_quals[i]; /* given contamination, prob seeing |
| 94 |
match despite base errors */ |
| 95 |
} else { |
| 96 |
ls->randomcxx . . $ likelihood_set#random. *= 1 - p_match; |
| 97 |
ls->contamcxx . . $ likelihood_set#contam. *= 1 - p_quals[i]; |
| 98 |
} |
| 99 |
} |
| 100 |
return ls; |
| 101 |
} |
| 102 |
|