forked from argriffing/xgcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20080122a.py
54 lines (49 loc) · 1.76 KB
/
20080122a.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
"""Use the pruning algorithm to get JC log likelihood of an nt alignment.
Use the pruning algorithm to calculate the logarithm
of the Jukes-Cantor likelihood of a nucleotide alignment.
"""
from SnippetUtil import HandlingError
import Newick
import Fasta
import PhyLikelihood
import RateMatrix
import MatrixUtil
import Form
import FormOut
def get_form():
"""
@return: the body of a form
"""
# define the tree and alignment strings
tree_string = Newick.brown_example_tree
tree = Newick.parse(tree_string, Newick.NewickTree)
formatted_tree_string = Newick.get_narrow_newick_string(tree, 60)
alignment_string = Fasta.brown_example_alignment.strip()
# define the form objects
form_objects = [
Form.MultiLine('tree', 'newick tree', formatted_tree_string),
Form.MultiLine('fasta', 'fasta alignment', alignment_string)]
return form_objects
def get_form_out():
return FormOut.Report()
def get_response_content(fs):
# get the tree
tree = Newick.parse(fs.tree, Newick.NewickTree)
tree.assert_valid()
# get the alignment
try:
alignment = Fasta.Alignment(fs.fasta.splitlines())
alignment.force_nucleotide()
except Fasta.AlignmentError as e:
raise HandlingError(e)
# get the log likelihood
dictionary_rate_matrix = RateMatrix.get_jukes_cantor_rate_matrix()
ordered_states = list('ACGT')
row_major_rate_matrix = MatrixUtil.dict_to_row_major(
dictionary_rate_matrix, ordered_states, ordered_states)
rate_matrix_object = RateMatrix.RateMatrix(
row_major_rate_matrix, ordered_states)
log_likelihood = PhyLikelihood.get_log_likelihood(
tree, alignment, rate_matrix_object)
# return the response
return str(log_likelihood) + '\n'