-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdata.py
193 lines (168 loc) · 6.85 KB
/
data.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from numpy import *
import scipy.stats as stat
from scipy.special import gammaln
import util2 as u
class Datum(object):
def __init__(self, name, id, a, d, mu_r=0, mu_v=0):
self.name = name # SSM name, blank for CNV
self.id = id
self.a = a
self.d = d
self.mu_r = mu_r # 1-p_error
self.mu_v = mu_v
self._log_bin_norm_const = [u.log_bin_coeff(self.d[tp], self.a[tp]) for tp in arange(len(self.a))]
## all variables below are used by cnv related computations
self.nr = 0
self.nv = 0
self.node = None # this is the node where the datum resides
self.cnv = [] # for SSM, this is [(cnv,cp,cm)]
self.tssb = None # this is just a pointer to tssb (tree object), gets initialized in evolve.py
# for multiple samples
def _log_likelihood(self, phi,update_tree=True,new_state=0):
ntps = len(phi) # multi sample
return sum([self.__log_likelihood__(phi[tp],tp,update_tree,new_state) for tp in arange(ntps)])
# new_state is set to 0 or 1 during Metropolis-Hastings updates, defaults to 0 in all other places
def __log_likelihood__(self, phi, tp, update_tree=True,new_state=0):
if update_tree:
##################################################
## some useful info about the tree,
## used by CNV related computations,
u.set_node_height(self.tssb)
u.set_path_from_root_to_node(self.tssb)
u.map_datum_to_node(self.tssb)
##################################################
return self.__log_complete_likelihood__(phi, self.mu_r, self.mu_v, tp, new_state)
# for multiple samples
def _log_complete_likelihood(self, phi, mu_r, mu_v):
ntps = len(self.a)
return sum([self.__log_complete_likelihood__(phi, mu_r, mu_v,tp) for tp in arange(ntps)])
def __log_complete_likelihood__(self, phi, mu_r, mu_v, tp, new_state=0):
if self.cnv:
ll = []
poss_n_genomes = self.compute_n_genomes(tp,new_state)
poss_n_genomes = [x for x in poss_n_genomes if x[1] > 0]
for (nr,nv) in poss_n_genomes:
mu = (nr * mu_r + nv*(1-mu_r) ) / (nr+ nv)
ll.append(u.log_binomial_likelihood(self.a[tp], self.d[tp], mu) + log(1.0/len(poss_n_genomes)) + self._log_bin_norm_const[tp])
if len(poss_n_genomes) == 0:
ll.append(log(1e-99)) # to handle cases with zero likelihood
llh = u.logsumexp(ll)
else: ## CNV datum
mu = (1 - phi) * mu_r + phi*mu_v # (mu_r=0.999, mu_v=0.5)
llh = u.log_binomial_likelihood(self.a[tp], self.d[tp], mu) + self._log_bin_norm_const[tp]
return llh
# computes the binomial parameter
def compute_n_genomes(self,tp,new_state=0):
def descend(nd,new_state):
pi = nd.pi1[tp] if new_state else nd.pi[tp] # this is needed for Metropolis-Hastings likelihood computations
ssm_node = self.node.path[-1]
mr_cnv = self.find_most_recent_cnv(nd)
ancestors = nd.get_ancestors()
if (not ssm_node in ancestors) and (not mr_cnv):
self.nr1 += pi * 2
self.nr2 += pi * 2
self.nr3 += pi * 2
self.nr4 += pi * 2
elif ssm_node in ancestors and (not mr_cnv):
self.nr1 += pi
self.nv1 += pi
self.nr2 += pi
self.nv2 += pi
self.nr3 += pi
self.nv3 += pi
self.nr4 += pi
self.nv4 += pi
elif (not ssm_node in ancestors) and mr_cnv:
self.nr1 += pi * (mr_cnv[1] + mr_cnv[2])
self.nr2 += pi * (mr_cnv[1] + mr_cnv[2])
self.nr3 += pi * (mr_cnv[1] + mr_cnv[2])
self.nr4 += pi * (mr_cnv[1] + mr_cnv[2])
elif ssm_node in ancestors and mr_cnv:
self.nr3 += pi * max(0,(mr_cnv[1]+mr_cnv[2] - 1))
self.nv3 += pi * min(1,mr_cnv[1]+mr_cnv[2])
self.nr4 += pi * max(0,(mr_cnv[1] + mr_cnv[2] - 1))
self.nv4 += pi * min(1,mr_cnv[1]+mr_cnv[2])
if ssm_node in mr_cnv[0].node.get_ancestors():
self.nr1 = self.nr1 + pi * mr_cnv[1]
self.nv1 = self.nv1 + pi * mr_cnv[2]
self.nr2 = self.nr2 + pi * mr_cnv[2]
self.nv2 = self.nv2 + pi * mr_cnv[1]
else:
self.nr1 = self.nr1 + pi * max(0,(mr_cnv[1]+mr_cnv[2] - 1))
self.nv1 = self.nv1 + pi * min(1,mr_cnv[1]+mr_cnv[2])
self.nr2 = self.nr2 + pi * max(0,(mr_cnv[1] + mr_cnv[2] - 1))
self.nv2 = self.nv2 + pi * min(1,mr_cnv[1]+mr_cnv[2])
else:
print "PANIC"
nodes = self.tssb.root['node'].tssb.get_nodes()
self.nr1 = 0
self.nv1 = 0
self.nr2 = 0
self.nv2 = 0
self.nr3 = 0
self.nv3 = 0
self.nr4 = 0
self.nv4 = 0
for nd in nodes:
descend(nd, new_state)
if len(self.cnv) == 1 and self.node == self.cnv[0][0].node:
out = [(self.nr1,self.nv1),(self.nr2,self.nv2),(self.nr3,self.nv3),(self.nr4,self.nv4)]
else:
out = [(self.nr1,self.nv1),(self.nr2,self.nv2)]
return out
def find_most_recent_cnv(self, nd):
out = None
for n in nd.get_ancestors()[::-1]:
if n in [x[0].node for x in self.cnv]:
out = [x for x in self.cnv if x[0].node == n][0]
break
return out
########## old code, not in use, but don't delete #####################
def __log_complete_likelihood1__(self, phi, mu_r, mu_v, new_state=0):
llh = []
if self.cnv:
self.compute_n_genomes(0,new_state) # maternal
mu = (self.nr * mu_r + self.nv*(1-mu_r) ) / (self.nr+ self.nv)
llh.append(u.log_binomial_likelihood(self.a, self.d, mu) + log(0.5) + self._log_bin_norm_const)
self.compute_n_genomes(1,new_state) # paternal
mu = (self.nr * mu_r + self.nv*(1-mu_r) ) / (self.nr+ self.nv)
llh.append(u.log_binomial_likelihood(self.a, self.d, mu) + log(0.5) + self._log_bin_norm_const)
llh = u.logsumexp(ll)
else: ## CNV datum or SSM with no CNV
mu = (1 - phi) * mu_r + phi*mu_v # (mu_r=0.999, mu_v=0.5)
llh = u.log_binomial_likelihood(self.a, self.d, mu) + self._log_bin_norm_const
return llh
def compute_n_genomes1(self,maternal,new_state=0):
####### TEMPORARY ONLY ###############
maternal = True
self.nr=self.nv=0
wts,nodes = self.tssb.get_mixture()
ancestors = self.node.get_ancestors() # path from root to ssm node
mr_cnv = self.cnv[0] # CNV corresponding to this SSM
# do this until we encounter the SSM node,
# i.e., along the path from root to the SSM node
visited_cnv = False
for node in ancestors:
pi = node.pi1 if new_state else node.pi # this is needed for Metropolis-Hastings likelihood computations
if node != mr_cnv[0].node and visited_cnv==False: # until CNV is encountered
self.nr = self.nr + pi*2
else:
visited_cnv = True
self.nr = self.nr + pi*(mr_cnv[1]+mr_cnv[2])
# do this after the SSM node, i.e, for all nodes in the subtree below the SSM node
def descend(nd):
pi = nd.pi1 if new_state else nd.pi # this is needed for Metropolis-Hastings likelihood computations
if nd == mr_cnv[0].node:
if maternal:
self.nr = self.nr + pi*mr_cnv[1]
self.nv = self.nv + pi*mr_cnv[2]
else:
self.nr = self.nr + pi*mr_cnv[2]
self.nv = self.nv + pi*mr_cnv[1]
else:
self.nr = self.nr + pi * (mr_cnv[1]+mr_cnv[2] - 1)
self.nv = self.nv + pi
for child in nd.children():
descend(child)
# traverse the tree below the ssm node
for child in node.children(): descend(child)