6
6
import dendropy
7
7
import pyopentree
8
8
9
+ # NEXSS sugar ------------------------------------------------------------------
10
+
11
+ class NEXSS (object ):
12
+
13
+ def __init__ (self ):
14
+ self .datatype_hint = 'xsd:string'
15
+ self .name_prefix = 'nexss'
16
+ self .namespace = 'http://phylotastic.org/nexss#'
17
+ self .styles = list ()
18
+
19
+ def style_id_index (self , style_id ):
20
+ for i , style in enumerate (self .styles ):
21
+ if style [0 ] == style_id :
22
+ return i
23
+ break
24
+ return - 1
25
+
26
+ def write_to_path (self , path ):
27
+ with open (path , 'w' ) as nexss_file :
28
+ for style in self .styles :
29
+ nexss_file .write (style [1 ] + '\n \n ' )
30
+
31
+ def annotate_node (self , node , tag , content , style ):
32
+
33
+ node .annotations .drop (name = tag )
34
+
35
+ node .annotations .add_new (
36
+ name = tag ,
37
+ value = content ,
38
+ datatype_hint = self .datatype_hint ,
39
+ name_prefix = self .name_prefix ,
40
+ namespace = self .namespace ,
41
+ name_is_prefixed = False ,
42
+ is_attribute = False ,
43
+ annotate_as_reference = False ,
44
+ is_hidden = False )
45
+
46
+ style_id = 'node[' + self .name_prefix + ':' + tag + '=' + content + ']'
47
+
48
+ sid_idx = self .style_id_index (style_id )
49
+ if sid_idx >= 0 :
50
+ self .styles .pop (sid_idx )
51
+
52
+ style_str_left = style_id + ' {\n '
53
+ style_str_right = '}'
54
+ style_str = style_str_left
55
+
56
+ for k in style :
57
+ style_str = style_str + '\t ' + k + ': ' + style [k ] + ';\n '
58
+
59
+ style_str = style_str + style_str_right
60
+
61
+ self .styles .append ([style_id , style_str ])
62
+
9
63
# Utility functions ------------------------------------------------------------
10
64
11
65
def parse_opentree_taxon_label (taxon_label ):
@@ -67,6 +121,13 @@ def get_leaf_nodes(tree):
67
121
nodes .append (node )
68
122
return nodes
69
123
124
+ def normalize_branch_lengths (tree , branch_length = 1.0 ):
125
+ node_iterator = tree .preorder_node_iter (
126
+ filter_fn = None )
127
+ for node in node_iterator :
128
+ node .edge .length = branch_length
129
+ return tree
130
+
70
131
# Abstract pyopentree wrappers -------------------------------------------------
71
132
72
133
def extract_property_from_tnrs_match_names (
@@ -202,6 +263,33 @@ def get_taxonomy_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
202
263
print (leaf_node )
203
264
print ()
204
265
266
+ colors = ['red' , 'green' , 'blue' , 'orange' , 'pink' ]
267
+ nexss = NEXSS ()
268
+ internal_node_count = len (internal_nodes )
269
+ j = 0
270
+ for i , node in enumerate (internal_nodes ):
271
+ if i % len (colors ) == 0 :
272
+ j = 0
273
+ else :
274
+ j = j + 1
275
+ style = {'color' : colors [j ]}
276
+ nexss .annotate_node (
277
+ node = node ,
278
+ tag = 'clade' ,
279
+ content = node .taxon .label ,
280
+ style = style )
281
+
282
+ nexss .write_to_path ('/Users/karolis/Desktop/induced_tree.nexss' )
283
+
284
+ induced_tree = normalize_branch_lengths (
285
+ tree = induced_tree ,
286
+ branch_length = 1.0 )
287
+
288
+ write_tree_to_path (
289
+ tree = induced_tree ,
290
+ path = '/Users/karolis/Desktop/induced_tree.nexml' ,
291
+ schema = 'nexml' )
292
+
205
293
206
294
ott_ids = get_ott_ids (
207
295
names = ['Solanaceae' ],
@@ -217,9 +305,43 @@ def get_taxonomy_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
217
305
keep_ott_id = False )
218
306
# print(tol_subtree)
219
307
308
+ internal_nodes = get_internal_nodes (
309
+ tree = tol_subtree , only_named_nodes = True , exclude_seed_node = False )
310
+ # for internal_node in internal_nodes:
311
+ # print(internal_node)
312
+ # print()
313
+
314
+ leaf_nodes = get_leaf_nodes (
315
+ tree = tol_subtree )
316
+ # for leaf_node in leaf_nodes:
317
+ # print(leaf_node)
318
+ # print()
319
+
320
+ colors = ['red' , 'green' , 'blue' , 'orange' , 'pink' ]
321
+ nexss = NEXSS ()
322
+ internal_node_count = len (internal_nodes )
323
+ j = 0
324
+ for i , node in enumerate (internal_nodes ):
325
+ if i % len (colors ) == 0 :
326
+ j = 0
327
+ else :
328
+ j = j + 1
329
+ style = {'color' : colors [j ]}
330
+ nexss .annotate_node (
331
+ node = node ,
332
+ tag = 'clade' ,
333
+ content = node .taxon .label ,
334
+ style = style )
335
+
336
+ nexss .write_to_path ('/Users/karolis/Desktop/Solanaceae.nexss' )
337
+
338
+ tol_subtree = normalize_branch_lengths (
339
+ tree = tol_subtree ,
340
+ branch_length = 1.0 )
341
+
220
342
write_tree_to_path (
221
343
tree = tol_subtree ,
222
- path = '~ /Desktop/Solanaceae.nexml' ,
344
+ path = '/Users/karolis /Desktop/Solanaceae.nexml' ,
223
345
schema = 'nexml' )
224
346
225
347
0 commit comments