Skip to content

Commit 058b4fd

Browse files
author
Xing Han Lu
committed
Added Phylogeny Example
1 parent 155c36c commit 058b4fd

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

demos/usage-phylogeny.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import dash_cytoscape
2+
import dash
3+
from dash.dependencies import Input, Output
4+
import dash_html_components as html
5+
import dash_core_components as dcc
6+
7+
try:
8+
from Bio import Phylo
9+
except ModuleNotFoundError as e:
10+
print(e, "Please make sure you have biopython installed correctly before running this example.")
11+
12+
13+
def generate_elements(tree):
14+
elements = []
15+
16+
def _add_to_elements(clade, clade_id):
17+
children = clade.clades
18+
19+
cy_source = {"data": {"id": clade_id}, 'classes': 'nonterminal'}
20+
elements.append(cy_source)
21+
22+
if clade.is_terminal():
23+
cy_source['data']['name'] = clade.name
24+
cy_source['classes'] = 'terminal'
25+
26+
for n, child in enumerate(children, 1):
27+
child_id = len(elements) + n
28+
29+
cy_edge = {'data': {
30+
'source': clade_id,
31+
'target': child_id,
32+
'length': clade.branch_length
33+
}}
34+
35+
if clade.confidence and clade.confidence.value:
36+
cy_source['data']['confidence'] = clade.confidence.value
37+
38+
elements.extend([cy_edge])
39+
40+
_add_to_elements(child, child_id)
41+
42+
_add_to_elements(tree.clade, 0)
43+
44+
return elements
45+
46+
47+
# Define elements, stylesheet and layout
48+
tree = Phylo.read('data/apaf.xml', 'phyloxml')
49+
elements = generate_elements(tree)
50+
51+
layout = {
52+
'name': 'breadthfirst',
53+
'directed': True
54+
}
55+
56+
stylesheet = [
57+
{
58+
'selector': '.nonterminal',
59+
'style': {
60+
'label': 'data(confidence)',
61+
'background-opacity': 0,
62+
"text-halign": "left",
63+
"text-valign": "top",
64+
}
65+
},
66+
{
67+
'selector': 'edge',
68+
'style': {
69+
"source-endpoint": "outside-to-node",
70+
}
71+
},
72+
{
73+
'selector': '.terminal',
74+
'style': {
75+
'label': 'data(name)',
76+
"shape": "roundrectangle",
77+
"width": 115,
78+
"height": 25,
79+
"text-valign": "center",
80+
'background-color': 'white',
81+
"border-width": 1.5,
82+
"border-style": "solid",
83+
"border-opacity": 1,
84+
}
85+
}
86+
]
87+
88+
89+
# Start the app
90+
app = dash.Dash(__name__)
91+
92+
app.scripts.config.serve_locally = True
93+
app.css.config.serve_locally = True
94+
95+
96+
app.layout = html.Div([
97+
dash_cytoscape.Cytoscape(
98+
id='cytoscape',
99+
elements=elements,
100+
stylesheet=stylesheet,
101+
layout=layout,
102+
style={
103+
'height': '95vh',
104+
'width': '100%'
105+
}
106+
)
107+
])
108+
109+
110+
if __name__ == '__main__':
111+
app.run_server(debug=True)

0 commit comments

Comments
 (0)