-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20080317a.py
240 lines (212 loc) · 8.4 KB
/
20080317a.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Infer the mutational distribution from the nt and aa distributions. [FLAWED]
The idea was to the mutational stationary distribution
from the nucleotide and amino acid stationary distributions.
Given a nucleotide stationary distribution
and an amino acid stationary distribution,
infer the mutation and selection components.
The mutation component is the stationary distribution
of the nucleotide mutation process.
"""
from StringIO import StringIO
import math
import random
import numpy as np
import scipy
import scipy.optimize
from SnippetUtil import HandlingError
import SnippetUtil
import Codon
import DirectProtein
import Form
import FormOut
from Codon import g_sorted_nt_letters as nt_letters
from Codon import g_sorted_aa_letters as aa_letters
from Codon import g_sorted_non_stop_codons as codons
# The selection component is centered amino acid energy vector
# upon which selection operates.
def get_form():
"""
@return: the body of a form
"""
# define the default distributions
default_nt_string = '\n'.join(nt + ' : 1' for nt in nt_letters)
default_aa_string = '\n'.join(aa + ' : 1' for aa in aa_letters)
# define the form objects
form_objects = [
Form.MultiLine('nucleotides', 'nucleotide stationary distribution',
default_nt_string),
Form.MultiLine('aminoacids', 'amino acid stationary distribution',
default_aa_string)]
return form_objects
def get_form_out():
return FormOut.Report()
def get_response_content(fs):
# get the nucleotide distribution
nt_to_probability = SnippetUtil.get_distribution(fs.nucleotides,
'nucleotide', nt_letters)
# get the amino acid distribution
aa_to_probability = SnippetUtil.get_distribution(fs.aminoacids,
'amino acid', aa_letters)
# convert the dictionaries to lists
observed_nt_stationary_distribution = [nt_to_probability[nt]
for nt in nt_letters]
aa_distribution = [aa_to_probability[aa] for aa in aa_letters]
# define the objective function
objective_function = MyCodonObjective(aa_distribution,
observed_nt_stationary_distribution)
initial_stationary_guess = halpern_bruno_nt_estimate(nt_to_probability,
aa_to_probability)
A, C, G, T = initial_stationary_guess
initial_guess = (math.log(C/A), math.log(G/A), math.log(T/A))
iterations = 20
try:
best = scipy.optimize.nonlin.broyden2(objective_function,
initial_guess, iterations)
except Exception, e:
debugging_information = objective_function.get_history()
raise HandlingError(str(e) + '\n' + debugging_information)
x, y, z = best
best_mutation_weights = (1, math.exp(x), math.exp(y), math.exp(z))
best_mutation_distribution = normalized(best_mutation_weights)
# Given the mutation distribution and the amino acid distribution,
# get the stationary distribution.
result = DirectProtein.get_nt_distribution_and_aa_energies(
best_mutation_distribution, aa_distribution)
result_stationary_nt_dist, result_aa_energies = result
# make a results string
out = StringIO()
# write the stationary nucleotide distribution of the mutation process
print >> out, 'mutation nucleotide stationary distribution:'
for nt, probability in zip(nt_letters, best_mutation_distribution):
print >> out, '%s : %s' % (nt, probability)
# write the centered amino acid energies
print >> out, ''
print >> out, 'amino acid energies:'
for aa, energy in zip(aa_letters, result_aa_energies):
print >> out, '%s : %s' % (aa, energy)
# return the response
return out.getvalue()
# for sanity checking only
eps = .000000001
def almost_equals(a, b):
return abs(a-b) < eps
def normalized(distribution):
"""
@param distribution: a distribution
@return: a normalized distribution
"""
# allow the input distribution to be a generator
P = list(distribution)
total_weight = sum(P)
return [p / float(total_weight) for p in P]
class Objective:
"""
An objective function that stores guesses and responses.
"""
def __init__(self):
self.guesses = []
self.responses = []
def __call__(self, X):
"""
This is called by the optimization framework.
"""
self.guesses.append(X)
try:
response = self.evaluate(X)
except Exception, e:
self.responses.append('???')
raise e
self.responses.append(response)
return response
def get_history(self):
out = StringIO()
for guess, response in zip(self.guesses, self.responses):
print >> out, str(guess), '->', str(response)
return out.getvalue()
def evaluate(self, X):
raise NotImplementedError('override this member function')
class CodonObjective(Objective):
def __init__(self):
Objective.__init__(self)
self.energies = []
def __call__(self, X):
"""
This is called by the optimization framework.
"""
self.guesses.append(X)
try:
response, energies = self.evaluate(X)
except Exception, e:
self.responses.append('???')
self.energies.append('???')
raise e
self.responses.append(response)
self.energies.append(energies)
energy_penalty = sum(abs(energy) for energy in energies)
#return tuple(x + energy_penalty for x in response)
return response
def get_history(self):
out = StringIO()
for guess, response, energies in zip(self.guesses,
self.responses, self.energies):
print >> out, str(guess), '->', str(response), ':', str(energies)
return out.getvalue()
def evaluate(self, X):
raise NotImplementedError('override this member function')
class MyCodonObjective(CodonObjective):
"""
This is an objective function.
"""
def __init__(self, aa_stationary_distribution, nt_stationary_distribution):
"""
@param aa_stationary_distribution: an amino acid distribution
@param nt_stationary_distribution: a nucleotide distribution
"""
CodonObjective.__init__(self)
self.aa_dist = aa_stationary_distribution
self.nt_dist = nt_stationary_distribution
def evaluate(self, X):
"""
@param X: the three vars defining the mutation process nt distribution.
@return: a tuple which is the zero vector when the guess was right.
"""
if len(X) != 3:
raise ValueError('incorrect number of parameters')
x, y, z = X
mutation_nt_weights = (1, math.exp(x), math.exp(y), math.exp(z))
mutation_nt_dist = normalized(mutation_nt_weights)
if not almost_equals(sum(mutation_nt_dist), 1.0):
msg_a ='detected possibly invalid objective function in put: '
msg_b = str(X)
raise ValueError(msg_a + msg_b)
pair = DirectProtein.get_nt_distribution_and_aa_energies(
mutation_nt_dist, self.aa_dist)
stationary_nt_dist, aa_energies = pair
evaluation = tuple(math.log(a/b)
for a, b in zip(self.nt_dist[:3], stationary_nt_dist[:3]))
return evaluation, aa_energies
def halpern_bruno_nt_estimate(nt_to_weight, aa_to_weight):
"""
@param nt_to_weight: a dictionary specifying the nt stationary distribution
@param aa_to_weight: a dictionary specifying the aa stationary distribution
@return: a vector of estimated nucleotide proportions
"""
unnormalized_codon_distribution = []
for codon in codons:
aa = Codon.g_codon_to_aa_letter[codon]
sibling_codons = Codon.g_aa_letter_to_codons[aa]
codon_aa_weight = aa_to_weight[aa]
codon_nt_weight = np.prod([nt_to_weight[nt] for nt in codon])
sibling_nt_weight_sum = sum(np.prod([nt_to_weight[nt]
for nt in sibling]) for sibling in sibling_codons)
weight = (codon_aa_weight * codon_nt_weight) / sibling_nt_weight_sum
unnormalized_codon_distribution.append(weight)
codon_distribution = normalized(unnormalized_codon_distribution)
nt_to_weight = dict(zip(nt_letters, [0]*4))
for codon, p in zip(codons, codon_distribution):
for nt in codon:
nt_to_weight[nt] += p
implied_stationary_nt_distribution = normalized(nt_to_weight[nt]
for nt in nt_letters)
return implied_stationary_nt_distribution