-
Notifications
You must be signed in to change notification settings - Fork 7
/
ot-taxo-mrca-to-root.py
executable file
·53 lines (46 loc) · 1.71 KB
/
ot-taxo-mrca-to-root.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
#!/usr/bin/env python
"""Takes a series of at least 2 OTT ids and reports the OTT of their
least inclusive taxonomic ancestor and that taxon's ancestors.
"""
import sys
def get_taxonomic_ancestor_ids(ott_id):
from peyotl.sugar import taxonomy
info = taxonomy.taxon(ott_id,
include_lineage=True,
list_terminal_descendants=False,
wrap_response=True)
anc_id_list = []
while True:
anc_id_list.append(info.ott_id)
info = info.parent
if info is None:
return anc_id_list
def main(argv):
"""This function sets up a command-line option parser and then calls
to do all of the real work.
"""
import argparse
import codecs
# have to be ready to deal with utf-8 names
out = codecs.getwriter('utf-8')(sys.stdout)
description = '''Takes a series of at least 2 OTT ids and reports the OTT of their least inclusive taxonomic ancestor and that taxon's ancestors.'''
parser = argparse.ArgumentParser(prog='ot-taxo-mrca-to-root', description=description)
parser.add_argument('ids', nargs='+', type=int, help='OTT IDs')
args = parser.parse_args(argv)
id_list = args.ids
last_id = id_list.pop()
anc_list = get_taxonomic_ancestor_ids(last_id)
common_anc = set(anc_list)
for curr_id in id_list:
curr_anc_set = set(get_taxonomic_ancestor_ids(curr_id))
common_anc &= curr_anc_set
if not common_anc:
break
for anc_id in anc_list:
if anc_id in common_anc:
out.write('{}\n'.format(anc_id))
if __name__ == '__main__':
try:
main(sys.argv[1:])
except Exception as x:
sys.exit('{}\n'.format(str(x)))