-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20080310c.py
199 lines (190 loc) · 7.71 KB
/
20080310c.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
"""Find the probability of a nt alignment given a tree and a Direct RNA model.
Find the probability of a nucleotide alignment
given a tree and a Direct RNA mixture model.
See "Population Genetics Without Intraspecific Data" by Thorne et al.
for more information about the Direct RNA model.
"""
import math
from StringIO import StringIO
import itertools
from SnippetUtil import HandlingError
import Newick
import Monospace
import HeatMap
import Fasta
import DirectRna
import Form
import FormOut
import iterutils
import const
g_sample_alignment_string = const.read('20100731c')
def get_form():
"""
@return: the body of a form
"""
# define the newick string
tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);'
tree = Newick.parse(tree_string, Newick.NewickTree)
formatted_tree_string = Newick.get_narrow_newick_string(tree, 60)
# define the form objects
form_objects = [
Form.MultiLine('tree', 'newick tree', formatted_tree_string),
Form.MultiLine('alignment', 'nucleotide alignment',
g_sample_alignment_string.strip()),
Form.MultiLine('model', 'Direct RNA mixture model',
DirectRna.get_sample_xml_string().strip())]
return form_objects
def get_form_out():
return FormOut.Html()
def get_response_content(fs):
# get the tree
tree = Newick.parse(fs.tree, Newick.NewickTree)
tree.assert_valid()
# get the nucleotide alignment
try:
alignment = Fasta.Alignment(fs.alignment.splitlines())
alignment.force_nucleotide()
except Fasta.AlignmentError as e:
raise HandlingError(e)
# get the normalized Direct RNA mixture model
mixture_model = DirectRna.deserialize_mixture_model(fs.model)
mixture_model.normalize()
# return the html string
return do_analysis(mixture_model, alignment, tree) + '\n'
def do_analysis_helper(labels, element_lists, w):
"""
Chop up the rows of data.
Yield lines of text to be displayed in an html pre tag.
@param labels: row labels to be left justified
@param element_lists: each row where each element is a letter or a span
@param w: the width; the number of elements allowed per page row
"""
if len(set(len(element_list) for element_list in element_lists)) != 1:
msg = 'each element list should have the same nonzero length'
raise ValueError(msg)
label_width = max(len(label) for label in labels) + 1
chopped_element_lists = [list(iterutils.chopped(element_list, w))
for element_list in element_lists]
page_rows = zip(*chopped_element_lists)
for i, page_row in enumerate(page_rows):
header = ''
header += ' ' * label_width
header += Monospace.get_ruler_line(i*w + 1, i*w + len(page_row[0]))
yield header
for label, element_list in zip(labels, page_row):
justified_label = label.ljust(label_width)
yield ''.join([justified_label] + list(element_list))
if i < len(page_rows) - 1:
yield ''
def do_analysis(mixture_model, alignment, tree):
"""
@param mixture_model: a mixture of nucleotide rate matrices
@param alignment: a nucleotide alignment
@param tree: the phylogenetic tree with branch lengths
@return: an html string representing a whole html file
"""
# For each column of the alignment get the likelihood for each category.
# The rest of the analysis can proceed from this data alone.
likelihood_columns = []
# create a hash table to help decorate the tree
header_to_node = {}
for header in alignment.headers:
try:
node = tree.get_unique_node(header)
except Newick.NewickSearchError as e:
raise HandlingError(e)
header_to_node[header] = node
# get the information for each column
for column in alignment.columns:
# decorate the tree with the ordered states of the current column
for header, state in zip(alignment.headers, column):
header_to_node[header].state = state
# get the likelihood for each category
likelihoods = []
for p, matrix in zip(mixture_model.mixture_parameters,
mixture_model.rate_matrices):
likelihoods.append(p * matrix.get_likelihood(tree))
likelihood_columns.append(likelihoods)
# The likelihood_columns variable
# has everything we need to write the response.
# Define the likelihood legend.
likelihood_column_sums = [sum(likelihoods)
for likelihoods in likelihood_columns]
likelihood_legend = HeatMap.Legend(likelihood_column_sums,
5, 'L', HeatMap.white_red_gradient)
# get the mixture for each column implied by the likelihoods at the column
mixture_columns = []
for likelihoods in likelihood_columns:
total = sum(likelihoods)
mixtures = [likelihood / total for likelihood in likelihoods]
mixture_columns.append(mixtures)
# get the conditional mixtures for the whole alignment
total_mixture = []
for proportions in zip(*mixture_columns):
total_mixture.append(sum(proportions) / len(alignment.columns))
# define the mixture legend
flattened_columns = list(itertools.chain.from_iterable(mixture_columns))
mixture_legend = HeatMap.Legend(flattened_columns,
5, 'M', HeatMap.white_blue_gradient)
# start writing the web page
out = StringIO()
print >> out, '<html>'
print >> out, '<head>'
print >> out, '<style>'
for legend in (likelihood_legend, mixture_legend):
for line in legend.gen_style_lines():
print >> out, line
print >> out, '</style>'
print >> out, '</head>'
print >> out, '<body>'
# write the log likelihood
log_likelihood = sum(math.log(sum(likelihoods))
for likelihoods in likelihood_columns)
print >> out, 'log likelihood:'
print >> out, '<br/>'
print >> out, '%f' % log_likelihood
# write the log likelihood per column
print >> out, '<br/><br/>'
print >> out, 'log likelihood per column:'
print >> out, '<br/>'
print >> out, '%f' % (log_likelihood / len(alignment.columns))
# write the conditional mixtures for the whole alignment
print >> out, '<br/><br/>'
print >> out, 'conditional mixture:'
print >> out, '<br/>'
for proportion in total_mixture:
print >> out, '%f</br>' % proportion
# begin the pre environment
print >> out, '<pre>'
# write the alignment
labels = alignment.headers
labels += ['category 1', 'category 2', 'category 3', 'likelihood']
element_lists = [list(seq) for seq in alignment.sequences]
for proportions in zip(*mixture_columns):
mixture_elements = []
for proportion in proportions:
css_class = mixture_legend.value_to_css_class(proportion)
mixture_elements.append('<span class="%s"> </span>' % css_class)
element_lists.append(mixture_elements)
likelihood_elements = []
for likelihood in likelihood_column_sums:
css_class = likelihood_legend.value_to_css_class(likelihood)
likelihood_elements.append('<span class="%s"> </span>' % css_class)
element_lists.append(likelihood_elements)
for line in do_analysis_helper(labels, element_lists, 60):
print >> out, line
# write the legend
print >> out, ''
print >> out, 'mixture key:'
for line in reversed(list(mixture_legend.gen_legend_lines())):
print >> out, line
print >> out, ''
print >> out, 'likelihood key:'
for line in reversed(list(likelihood_legend.gen_legend_lines())):
print >> out, line
# end the pre environment
print >> out, '</pre>'
# terminate the file
print >> out, '</body>'
print >> out, '</html>'
return out.getvalue()