-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20080629a.py
73 lines (68 loc) · 2.74 KB
/
20080629a.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
"""Given a newick tree, determine if a bipartition is in the split set.
Given a newick tree, determine if a bipartition
is a member of the split set of the tree.
"""
from SnippetUtil import HandlingError
import Util
import FelTree
import NewickIO
import Form
import FormOut
def get_form():
"""
@return: the body of a form
"""
# define the default tree
tree_string = NewickIO.daylight_example_tree
tree = NewickIO.parse(tree_string, FelTree.NewickTree)
formatted_tree_string = NewickIO.get_narrow_newick_string(tree, 60)
# define the selected taxa
selection = list('ABFG')
# define the form objects
form_objects = [
Form.MultiLine('tree', 'newick tree',
formatted_tree_string),
Form.MultiLine('selection', 'selected taxa',
'\n'.join(selection))]
return form_objects
def get_form_out():
return FormOut.Report()
def get_response_content(fs):
# get a properly formatted newick tree without caring about branch lengths
tree = NewickIO.parse(fs.tree, FelTree.NewickTree)
# make sure that each leaf has a unique name
tip_names = [tip.get_name() for tip in tree.gen_tips()]
if len(tip_names) < 4:
raise HandlingError('expected the tree to have at least four leaves')
if any(name is None for name in tip_names):
raise HandlingError('each leaf must be named')
if len(set(tip_names)) != len(tip_names):
raise HandlingError('each leaf name must be unique')
# get the selected taxa
selected_taxa = set(Util.get_stripped_lines(fs.selection.splitlines()))
# assert that the selected names are actually leaf names
if set(selected_taxa) - set(tip_names):
msg = 'one or more selected taxa are not leaf names in the tree'
raise HandlingError(msg)
# assert that the selection is not degenerate
n_selection = len(selected_taxa)
n_complement = len(tip_names) - n_selection
if n_selection == 0:
raise HandlingError('degenerate split: no taxa were selected')
if n_complement == 0:
raise HandlingError('degenerate split: all taxa were selected')
if n_selection == 1:
msg_a = 'degenerate split: a single selected taxon '
msg_b = 'can always be separated from the others'
raise HandlingError(msg_a + msg_b)
if n_complement == 1:
msg_a = 'degenerate split: a single selected taxon '
msg_b = 'can always be separated from the others'
raise HandlingError(msg_a + msg_b)
# define the response
tip_selection = [tip for tip in tree.gen_tips()
if tip.get_name() in selected_taxa]
if tree.get_split_branch(tip_selection):
return 'this split is valid and nontrivial'
else:
return 'this split is invalid'