Skip to content

Commit ed87e71

Browse files
committed
Workarounds for ete2 and NEXSS.
1 parent c5ac2e9 commit ed87e71

File tree

1 file changed

+125
-69
lines changed

1 file changed

+125
-69
lines changed

examples/pyopentreesugar.py

Lines changed: 125 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def collapse_tnrs_match_names_results(results, method='first_hit_only'):
102102
return results
103103

104104
def write_tree_to_path(tree, path, schema='nexml'):
105-
tree.write_to_path(dest=path, schema='nexml')
105+
tree.write_to_path(dest=path, schema=schema)
106106

107107
def get_internal_nodes(tree, only_named_nodes=False, exclude_seed_node=False):
108108
node_iterator = tree.preorder_internal_node_iter(
@@ -187,7 +187,8 @@ def get_tol_induced_tree(ott_ids, keep_taxon_name=True, keep_ott_id=True):
187187
src=raw_api_data['subtree'],
188188
schema='newick',
189189
preserve_underscores=True,
190-
suppress_internal_node_taxa=False)
190+
suppress_internal_node_taxa=False,
191+
terminating_semicolon_required=False)
191192

192193
induced_tree = parse_opentree_taxon_labels_in_dendropy_tree(
193194
tree=induced_tree,
@@ -204,7 +205,8 @@ def get_tol_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
204205
src=raw_api_data['newick'],
205206
schema='newick',
206207
preserve_underscores=True,
207-
suppress_internal_node_taxa=False)
208+
suppress_internal_node_taxa=False,
209+
terminating_semicolon_required=False)
208210

209211
subtree = parse_opentree_taxon_labels_in_dendropy_tree(
210212
tree=subtree,
@@ -221,7 +223,8 @@ def get_taxonomy_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
221223
src=raw_api_data['subtree'],
222224
schema='newick',
223225
preserve_underscores=True,
224-
suppress_internal_node_taxa=False)
226+
suppress_internal_node_taxa=False,
227+
terminating_semicolon_required=False)
225228

226229
subtree = parse_opentree_taxon_labels_in_dendropy_tree(
227230
tree=subtree,
@@ -230,77 +233,108 @@ def get_taxonomy_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
230233

231234
return subtree
232235

236+
def tol_mrca(ott_ids):
237+
raw_api_data = pyopentree.tol_mrca(ott_ids=ott_ids.values(), node_ids=None)
238+
239+
return {raw_api_data['mrca_name']: raw_api_data['ott_id']}
240+
233241
if __name__ == "__main__":
234242

243+
# ott_ids = get_ott_ids(
244+
# names=['dog', 'cat', 'canada goose', 'african elephant', 'monarch butterfly', 'Solanum chilense', 'Prunus dulcis'],
245+
# context_name=None,
246+
# do_approximate_matching=False,
247+
# include_deprecated=False,
248+
# include_dubious=False)
249+
# print(ott_ids)
250+
251+
# ott_ids_collapsed = collapse_tnrs_match_names_results(
252+
# results=ott_ids,
253+
# method='first_hit_only')
254+
# print(ott_ids)
255+
256+
# induced_tree = get_tol_induced_tree(
257+
# ott_ids=ott_ids,
258+
# keep_taxon_name=True,
259+
# keep_ott_id=False)
260+
# print(induced_tree)
261+
262+
# internal_nodes = get_internal_nodes(
263+
# tree=induced_tree, only_named_nodes=True, exclude_seed_node=False)
264+
# for internal_node in internal_nodes:
265+
# print(internal_node)
266+
# print()
267+
268+
# leaf_nodes = get_leaf_nodes(
269+
# tree=induced_tree)
270+
# for leaf_node in leaf_nodes:
271+
# print(leaf_node)
272+
# print()
273+
274+
# colors = ['red', 'green', 'blue', 'orange', 'pink']
275+
# nexss = NEXSS()
276+
# internal_node_count = len(internal_nodes)
277+
# j = 0
278+
# for i, node in enumerate(internal_nodes):
279+
# if i % len(colors) == 0:
280+
# j = 0
281+
# else:
282+
# j = j + 1
283+
# style = {'color': colors[j]}
284+
# nexss.annotate_node(
285+
# node=node,
286+
# tag='clade',
287+
# content=node.taxon.label,
288+
# style=style)
289+
290+
# nexss.write_to_path('/Users/karolis/Desktop/induced_tree.nexss')
291+
292+
# induced_tree = normalize_branch_lengths(
293+
# tree=induced_tree,
294+
# branch_length=1.0)
295+
296+
# write_tree_to_path(
297+
# tree=induced_tree,
298+
# path='/Users/karolis/Desktop/induced_tree.nexml',
299+
# schema='nexml')
300+
301+
# Clade
302+
clade = 'Hominoidea'
235303
ott_ids = get_ott_ids(
236-
names=['dog', 'cat', 'canada goose', 'african elephant', 'monarch butterfly', 'Solanum chilense', 'Prunus dulcis'],
304+
names=[clade],
237305
context_name=None,
238306
do_approximate_matching=False,
239307
include_deprecated=False,
240308
include_dubious=False)
241309
print(ott_ids)
242310

243-
ott_ids_collapsed = collapse_tnrs_match_names_results(
244-
results=ott_ids,
245-
method='first_hit_only')
246-
print(ott_ids)
247-
248-
induced_tree = get_tol_induced_tree(
249-
ott_ids=ott_ids,
250-
keep_taxon_name=True,
251-
keep_ott_id=False)
252-
print(induced_tree)
253-
254-
internal_nodes = get_internal_nodes(
255-
tree=induced_tree, only_named_nodes=True, exclude_seed_node=False)
256-
for internal_node in internal_nodes:
257-
print(internal_node)
258-
print()
259-
260-
leaf_nodes = get_leaf_nodes(
261-
tree=induced_tree)
262-
for leaf_node in leaf_nodes:
263-
print(leaf_node)
264-
print()
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')
311+
# ott_ids = get_ott_ids(
312+
# names=['Geospiza', 'Iridophanes'],
313+
# context_name=None,
314+
# do_approximate_matching=False,
315+
# include_deprecated=False,
316+
# include_dubious=False)
317+
# print(ott_ids)
283318

284-
induced_tree = normalize_branch_lengths(
285-
tree=induced_tree,
286-
branch_length=1.0)
319+
# ott_ids_collapsed = collapse_tnrs_match_names_results(
320+
# results=ott_ids,
321+
# method='first_hit_only')
322+
# print(ott_ids)
287323

288-
write_tree_to_path(
289-
tree=induced_tree,
290-
path='/Users/karolis/Desktop/induced_tree.nexml',
291-
schema='nexml')
324+
# mrca = tol_mrca(ott_ids)
292325

326+
# clade = mrca.keys()[0]
293327

294-
ott_ids = get_ott_ids(
295-
names=['Solanaceae'],
296-
context_name=None,
297-
do_approximate_matching=False,
298-
include_deprecated=False,
299-
include_dubious=False)
300-
print(ott_ids)
328+
# ott_ids = get_ott_ids(
329+
# names=[clade],
330+
# context_name=None,
331+
# do_approximate_matching=False,
332+
# include_deprecated=False,
333+
# include_dubious=False)
334+
# print(ott_ids)
301335

302336
tol_subtree = get_tol_subtree(
303-
ott_id=ott_ids['Solanaceae'][0],
337+
ott_id=ott_ids[clade][0],
304338
keep_taxon_name=True,
305339
keep_ott_id=False)
306340
# print(tol_subtree)
@@ -317,7 +351,7 @@ def get_taxonomy_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
317351
# print(leaf_node)
318352
# print()
319353

320-
colors = ['red', 'green', 'blue', 'orange', 'pink']
354+
colors = ['red', 'green', 'blue', 'orange']
321355
nexss = NEXSS()
322356
internal_node_count = len(internal_nodes)
323357
j = 0
@@ -333,20 +367,42 @@ def get_taxonomy_subtree(ott_id, keep_taxon_name=True, keep_ott_id=True):
333367
content=node.taxon.label,
334368
style=style)
335369

336-
nexss.write_to_path('/Users/karolis/Desktop/Solanaceae.nexss')
370+
tol_subtree.ladderize(ascending=True)
371+
372+
nexss.write_to_path('/Users/karolis/Desktop/' + clade + '.nexss')
337373

338374
tol_subtree = normalize_branch_lengths(
339375
tree=tol_subtree,
340376
branch_length=1.0)
341377

342378
write_tree_to_path(
343379
tree=tol_subtree,
344-
path='/Users/karolis/Desktop/Solanaceae.nexml',
380+
path='/Users/karolis/Desktop/' + clade + '.nexml',
345381
schema='nexml')
346382

347-
348-
# taxonomy_subtree = get_taxonomy_subtree(
349-
# ott_id='541933',
350-
# keep_taxon_name=True,
351-
# keep_ott_id=False)
352-
# print(taxonomy_subtree)
383+
write_tree_to_path(
384+
tree=tol_subtree,
385+
path='/Users/karolis/Desktop/' + clade + '.newick',
386+
schema='newick')
387+
388+
import subprocess
389+
# command = 'pstastic.py ~/Desktop/' + clade + '.nexml ~/Desktop/' + clade + '.nexss --output ~/Desktop/' + clade + '.pdf -ow 3000 -oh 8000 --dpi 300'
390+
command = 'pstastic.py ~/Desktop/' + clade + '.nexml ~/Desktop/' + clade + '.nexss --output ~/Desktop/' + clade + '.pdf'
391+
subprocess.call(
392+
command,
393+
# stdout=subprocess.PIPE,
394+
# stderr=subprocess.PIPE,
395+
shell=True)
396+
397+
subprocess.call(
398+
'open ~/Desktop/' + clade + '.pdf',
399+
# stdout=subprocess.PIPE,
400+
# stderr=subprocess.PIPE,
401+
shell=True)
402+
403+
404+
taxonomy_subtree = get_taxonomy_subtree(
405+
ott_id='541933',
406+
keep_taxon_name=True,
407+
keep_ott_id=False)
408+
print(taxonomy_subtree)

0 commit comments

Comments
 (0)