| 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" |
| 11 |
|
| 12 |
float *qual_to_probs(const char *qual, 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 = strlen(qual), q; |
| 27 |
float *probs = xmalloc(sizeof(float)*n); |
| 28 |
for (i = 0; i < n; i++) { |
| 29 |
q = (char) qual[i]-quality_contants[q_type][Q_OFFSET]; |
| 30 |
if (q < quality_contants[q_type][Q_MIN] || q > quality_contants[q_type][Q_MAX]) { |
| •31 |
fprintf(stderr, "Base quality out of range for specified quality type (%d): %d\n", q_type, q); |
| 32 |
exit(EXIT_FAILURE); |
| 33 |
} |
| 34 |
if (q_type == 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_data(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_set *posterior(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_set *ps = xmalloc(sizeof(posterior_set)); |
| 72 |
likelihood_set *ls = likelihood(matches, p_quals, p_match, n); |
| 73 |
double p_denom = p_data(matches, p_quals, p_prior, p_match, n); |
| 74 |
ps->contam = (ls->contam * p_prior)/p_denom; |
| 75 |
ps->random = (ls->random * (1-p_prior))/p_denom; |
| 76 |
ps->is_contam = ps->contam > ps->random; |
| 77 |
free(ls); |
| 78 |
return ps; |
| 79 |
} |
| 80 |
|
| 81 |
likelihood_set *likelihood(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_set *ls = xmalloc(sizeof(likelihood_set)); |
| 87 |
int i; |
| 88 |
ls->random = 1; |
| 89 |
ls->contam = 1; |
| 90 |
for (i = 0; i < n; i++) { |
| 91 |
if (matches[i] == 1) { |
| 92 |
ls->random *= p_match; /* prob match happened by chance */ |
| 93 |
ls->contam *= p_quals[i]; /* given contamination, prob seeing |
| 94 |
match despite base errors */ |
| 95 |
} else { |
| 96 |
ls->random *= 1 - p_match; |
| 97 |
ls->contam *= 1 - p_quals[i]; |
| 98 |
} |
| 99 |
} |
| 100 |
return ls; |
| 101 |
} |
| 102 |
|