-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20080621b.py
68 lines (62 loc) · 2.29 KB
/
20080621b.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
"""Make a newick tree from a distance matrix by splitting instead of joining.
"""
import numpy as np
from SnippetUtil import HandlingError
import Util
import MatrixUtil
import Clustering
import NeighborhoodJoining
import NewickIO
import Form
import FormOut
import const
# this is a perturbed distance matrix not an exact distance matrix
g_d = const.read('20100730p')
def get_form():
"""
@return: the body of a form
"""
lines = Util.get_stripped_lines(g_d.splitlines())
D = np.array(MatrixUtil.read_matrix(lines))
labels = list('xyabcmnp')
# define the form objects
form_objects = [
Form.Matrix('matrix', 'distance matrix',
D, MatrixUtil.assert_predistance),
Form.MultiLine('labels', 'ordered labels',
'\n'.join(labels)),
Form.RadioGroup('recourse', 'recourse for degenerate partitions', [
Form.RadioItem('njrecourse', 'neighbor joining', True),
Form.RadioItem('halvingrecourse', 'stem length halving')])]
return form_objects
def get_form_out():
return FormOut.Newick()
def get_response_content(fs):
# read the matrix
D = fs.matrix
if len(D) < 3:
raise HandlingError('the matrix should have at least three rows')
# read the ordered labels
ordered_labels = Util.get_stripped_lines(fs.labels.splitlines())
if len(ordered_labels) != len(D):
msg_a = 'the number of ordered labels should be the same '
msg_b = 'as the number of rows in the matrix'
raise HandlingError(msg_a + msg_b)
# create the tree building object
splitter = Clustering.StoneExactDMS()
tree_builder = NeighborhoodJoining.TreeBuilder(
D.tolist(), ordered_labels, splitter)
# Read the recourse string and set the corresponding method
# in the tree builder.
recourse_string = fs.getfirst('recourse')
if fs.njrecourse:
tree_builder.set_fallback_name('nj')
elif fs.halvingrecourse:
tree_builder.set_fallback_name('halving')
# assert that the computation will not take too long
if tree_builder.get_complexity() > 1000000:
raise HandlingError('this computation would take too long')
# build the tree
tree = tree_builder.build()
# return the response
return NewickIO.get_newick_string(tree) + '\n'