annotate ngsutils/support/llh.py @ 2:7a68005de299 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
author iuc
date Sun, 27 Nov 2016 15:01:21 -0500
parents 4e4e4093d65d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
1 '''
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
2 Methods for calculating log-likelihoods for nucleotide frequencies
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
3 '''
2
7a68005de299 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents: 0
diff changeset
4 import collections
0
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
5 import math
2
7a68005de299 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents: 0
diff changeset
6
0
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
7 from ngsutils.support import memoize
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
8
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
9 _default_background = {'A': 0.3, 'T': 0.3, 'C': 0.2, 'G': 0.2}
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
10
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
11 NucleotideLogLikelihood = collections.namedtuple('NucleotideLogLikelihood', 'A C G T pseudo')
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
12
2
7a68005de299 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 9a243c616a4a3156347e38fdb5f35863ae5133f9
iuc
parents: 0
diff changeset
13
0
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
14 @memoize
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
15 def pseudo_count(N, bg):
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
16 '''
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
17 >>> pseudo_count(100, _default_background['A'])
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
18 3
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
19 >>> pseudo_count(100, _default_background['C'])
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
20 2
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
21 '''
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
22
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
23 return bg * math.sqrt(N)
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
24
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
25
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
26 def calc_llh(A, C, G, T, bg=_default_background, pseudo='auto'):
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
27 if pseudo == 'auto':
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
28 N = A + C + G + T
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
29 Ap = float(A) + pseudo_count(N, bg['A'])
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
30 Cp = float(C) + pseudo_count(N, bg['C'])
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
31 Gp = float(G) + pseudo_count(N, bg['G'])
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
32 Tp = float(T) + pseudo_count(N, bg['T'])
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
33 elif pseudo:
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
34 Ap = float(A) + pseudo
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
35 Cp = float(C) + pseudo
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
36 Gp = float(G) + pseudo
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
37 Tp = float(T) + pseudo
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
38 else:
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
39 Ap = float(A)
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
40 Cp = float(C)
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
41 Gp = float(G)
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
42 Tp = float(T)
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
43
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
44 Np = Ap + Cp + Gp + Tp
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
45
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
46 freqA = Ap / Np
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
47 freqC = Cp / Np
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
48 freqG = Gp / Np
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
49 freqT = Tp / Np
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
50
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
51 return NucleotideLogLikelihood(math.log(freqA / bg['A']), math.log(freqC / bg['C']), math.log(freqG / bg['G']), math.log(freqT / bg['T']), pseudo)
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
52
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
53
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
54 if __name__ == '__main__':
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
55 import doctest
4e4e4093d65d planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff changeset
56 doctest.testmod()