forked from argriffing/xgcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20080123b.py
64 lines (58 loc) · 2.16 KB
/
20080123b.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
"""Sample ancestral sequences using a Jukes-Cantor rate matrix.
Given a newick tree and a nucleotide alignment,
sample ancestral sequences using a Jukes-Cantor rate matrix.
"""
from SnippetUtil import HandlingError
import Newick
import Fasta
import PhyLikelihood
import RateMatrix
import MatrixUtil
import Form
import FormOut
#FIXME use const data
def get_form():
"""
@return: the body of a form
"""
# define the tree and alignment strings
tree_string = '(((Human:0.1, Chimpanzee:0.2)to-chimp:0.8, Gorilla:0.3)to-gorilla:0.7, Orangutan:0.4, Gibbon:0.5)all;'
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.Fasta()
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)
# define the jukes cantor rate matrix
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)
# simulate the ancestral alignment
try:
alignment = PhyLikelihood.simulate_ancestral_alignment(
tree, alignment, rate_matrix_object)
except PhyLikelihood.SimulationError as e:
raise HandlingError(e)
# get the alignment string using an ordering defined by the tree
arr = []
for node in tree.preorder():
arr.append(alignment.get_fasta_sequence(node.name))
# return the response
return '\n'.join(arr) + '\n'