-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20080223a.py
57 lines (53 loc) · 1.82 KB
/
20080223a.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
"""Visualize a codon rate matrix as a heat map.
"""
from SnippetUtil import HandlingError
import SnippetUtil
import MatrixUtil
import RateMatrix
import HeatMap
import Codon
import Form
import FormOut
def get_form():
"""
@return: the body of a form
"""
# define the default rate matrix
dictionary_rate_matrix = RateMatrix.get_sample_codon_rate_matrix()
labels = Codon.g_sorted_non_stop_codons
R = MatrixUtil.dict_to_row_major(dictionary_rate_matrix, labels, labels)
# define the form objects
form_objects = [
Form.Matrix('matrix', 'codon rate matrix',
R, MatrixUtil.assert_rate_matrix),
Form.Integer('maxcategories', 'maximum number of categories',
5, low=2)]
return form_objects
def get_form_out():
return FormOut.Html()
def get_response_content(fs):
# read the matrix from the form data
R = fs.matrix
nrows, ncols = R.shape
# assert that the number of rows and columns is valid for a codon matrix
states = Codon.g_sorted_non_stop_codons
if nrows != len(states):
msg = 'expected %d rows but got %d' % (len(states), nrows)
raise HandlingError(msg)
if ncols != len(states):
msg = 'expected %d columns but got %d' % (len(states), ncols)
raise HandlingError(msg)
# define the row and column labels
labels = []
for codon in states:
label = '%s.%s.' % (Codon.g_codon_to_aa_letter[codon], codon)
labels.append(label)
row_labels = labels
column_labels = labels
# initialize the base class with this row major matrix
heatmap = HeatMap.LabeledHeatMap(R.tolist(), fs.maxcategories,
row_labels, column_labels)
renderer = HeatMap.PreHeatMap(heatmap)
html_string = renderer.get_example_html()
# return the response
return html_string + '\n'