forked from OpenTreeOfLife/peyotl
/
__init__.py
84 lines (74 loc) · 2.66 KB
/
__init__.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
Small library for conducting operations over the
entire set of NexSON files in one or more phylesystem
repositories.
"""
# Some imports to help our py2 code behave like py3
from __future__ import absolute_import, print_function, division
__version__ = '0.1.4dev' # sync with setup.py
from peyotl.utility import (get_config_setting,
get_config_object,
expand_path,
get_logger)
from peyotl.utility.input_output import pretty_dict_str
from peyotl.collections_store import (collection_to_included_trees,
concatenate_collections,
tree_is_in_collection)
from peyotl.nexson_syntax import (can_convert_nexson_forms,
convert_nexson_format,
detect_nexson_version,
get_ot_study_info_from_nexml,
read_as_json,
write_as_json,
write_obj_as_nexml,
_is_by_id_hbf)
from peyotl.phylesystem.phylesystem_umbrella import Phylesystem
from peyotl.utility.str_util import UNICODE, is_str_type
from peyotl.phylo.entities import OTULabelStyleEnum
def gen_otu_dict(nex_obj, nexson_version=None):
"""Takes a NexSON object and returns a dict of
otu_id -> otu_obj
"""
if nexson_version is None:
nexson_version = detect_nexson_version(nex_obj)
if _is_by_id_hbf(nexson_version):
otus = nex_obj['nexml']['otusById']
if len(otus) > 1:
d = {}
for v in otus.values():
d.update(v['otuById'])
return d
else:
return otus.values()[0]['otuById']
o_dict = {}
for ob in nex_obj.get('otus', []):
for o in ob.get('otu', []):
oid = o['@id']
o_dict[oid] = o
return o_dict
def iter_tree(nex_obj):
"""Generator over each tree object in the NexSON object."""
for tb in nex_obj.get('trees', []):
for tree in tb.get('tree', []):
yield tree
def iter_node(tree):
"""Generator over each node object in the tree object."""
for nd in tree.get('nodeById', {}).items():
yield nd
__all__ = ['utility',
'api',
'nexson_proxy',
'nexson_syntax',
'nexson_validation',
'ott',
'phylesystem',
'string',
'sugar',
'test',
'utility',
'external',
'manip',
'struct_diff',
'evaluate_tree',
]