/
20110327a.py
62 lines (56 loc) · 2.02 KB
/
20110327a.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
""" Draw trees annotated with roots of eigenfunctions.
Use the harmonic extensions of eigenvectors
of the Schur complement Laplacian matrix.
"""
from SnippetUtil import HandlingError
import Newick
import SpatialTree
import FastDaylightLayout
import Form
import FormOut
import CairoUtil
import DrawEigenLacing
import Harmonic
def get_form():
"""
@return: the body of a form
"""
# define the tree string
tree_string = Newick.daylight_example_tree
tree = Newick.parse(tree_string, Newick.NewickTree)
formatted_tree_string = Newick.get_narrow_newick_string(tree, 60)
# define the form objects
form_objects = [
Form.MultiLine('tree', 'newick tree', formatted_tree_string),
Form.IntegerInterval(
'first_index', 'last_index',
'eigenfunction index range (1 means Fiedler)',
0, 4, low=0),
Form.CheckGroup('check_options', 'output options', [
Form.CheckItem('draw_background', 'draw background', True),
Form.CheckItem('draw_vertices', 'draw vertices'),
Form.CheckItem('draw_labels', 'draw labels', True)]),
Form.ImageFormat()]
return form_objects
def get_form_out():
return FormOut.Image('tree')
def get_response_content(fs):
# get a properly formatted newick tree with branch lengths
tree = Newick.parse(fs.tree, SpatialTree.SpatialTree)
# get the vertex valuations
valuations = [Harmonic.get_harmonic_valuations(
tree, i) for i in range(fs.first_index, fs.last_index+1)]
# do the layout
try:
layout = FastDaylightLayout.StraightBranchLayout()
layout.do_layout(tree)
except RuntimeError as e:
pass
# draw the image
try:
ext = Form.g_imageformat_to_ext[fs.imageformat]
return DrawEigenLacing.get_forest_image(
tree, (640, 480), ext, valuations,
fs.draw_background, fs.draw_vertices, fs.draw_labels)
except CairoUtil.CairoUtilError as e:
raise HandlingError(e)