-
Notifications
You must be signed in to change notification settings - Fork 1
/
20080929a.py
83 lines (76 loc) · 2.82 KB
/
20080929a.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
"""Follow the splits of a tree reconstruction algorithm. [UNFINISHED]
Follow the sequential splits of a distance based
tree reconstruction algorithm.
Compare neighbor joining to other methods.
"""
from StringIO import StringIO
import numpy as np
from SnippetUtil import HandlingError
import Util
import MatrixUtil
import NewickIO
import Clustering
import NeighborJoining
import NeighborhoodJoining
from Form import RadioItem
import Form
import FormOut
def get_form():
"""
@return: the body of a form
"""
# define the default distance matrix
D = np.array(NeighborJoining.g_mito_matrix)
# define the default label order
labels = ['gorilla', 'orangutan', 'human', 'chimp', 'gibbon']
# 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('criterion', 'tree reconstruction criterion', [
RadioItem('nj_specific', 'nj (specific implementation)'),
RadioItem('nj_general', 'nj (general implementation)'),
RadioItem('sign', 'spectral sign approximation', True),
RadioItem('random', 'random bipartition')])]
return form_objects
def get_form_out():
return FormOut.Report()
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(StringIO(fs.labels))
if len(ordered_labels) != len(D):
msg_a = 'the number of ordered labels should be the same as '
msg_b = 'the number of rows in the matrix'
raise HandlingError(msg_a + msg_b)
if len(set(ordered_labels)) != len(ordered_labels):
raise HandlingError('the ordered labels must be unique')
# read the criterion string, creating the splitter object
if fs.sign:
splitter = Clustering.StoneSpectralSignDMS()
elif fs.threshold:
splitter = Clustering.StoneSpectralThresholdDMS()
elif fs.nj_general:
splitter = Clustering.NeighborJoiningDMS()
elif fs.nj_specific:
splitter = None
# Make sure that the splitter object is appropriate for the size
# of the distance matrix.
if splitter.get_complexity(len(D)) > 1000000:
msg = 'use a smaller distance matrix or a faster bipartition function'
raise HandlingError(msg)
# create the tree builder
tree_builder = NeighborhoodJoining.TreeBuilder(
D.tolist(), ordered_labels, splitter)
tree_builder.set_fallback_name('nj')
# define the response
out = StringIO()
# build the tree
tree = tree_builder.build()
# write the response
return out.getvalue()