-
Notifications
You must be signed in to change notification settings - Fork 1
/
20080130b.py
34 lines (29 loc) · 978 Bytes
/
20080130b.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
"""Given a newick tree, merge segmented branches.
"""
from SnippetUtil import HandlingError
import Newick
import Form
import FormOut
#FIXME use const data
def get_form():
"""
@return: the body of a form
"""
# define the tree string
tree_string = '((((a:5)a:5, (b:5)b:5)A:1.5)A:1.5, (((c:5)c:5, (d:5)d:5)B:1.5)B:1.5);'
tree = Newick.parse(tree_string, Newick.NewickTree)
formatted_tree_string = Newick.get_narrow_newick_string(tree, 60)
# define the form objects
return [Form.MultiLine('tree', 'newick tree', formatted_tree_string)]
def get_form_out():
return FormOut.Newick()
def get_response_content(fs):
# get the tree
tree = Newick.parse(fs.tree, Newick.NewickTree)
tree.assert_valid()
# modify the tree
segmenting_nodes = [p for p in tree.preorder() if len(p.children) == 1]
for node in segmenting_nodes:
tree.remove_node(node)
# return the response
return tree.get_newick_string() + '\n'