-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfill_tips_from_taxonomy.py
More file actions
executable file
·50 lines (38 loc) · 1.67 KB
/
fill_tips_from_taxonomy.py
File metadata and controls
executable file
·50 lines (38 loc) · 1.67 KB
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
#!/usr/bin/env python
if __name__ == '__main__':
import os, sys, newick3, phylo3, sqlite3
def get_child_taxa(parent_name, rank, db_cursor):
db_cursor.execute("SELECT left_value, right_value FROM taxonomy WHERE name LIKE ?", (parent_name.strip(),))
left_value = None
right_value = None
try:
left_value, right_value = db_cursor.fetchone()
except TypeError:
print("\nCould not find " + parent_name + "\n")
return []
print(parent_name)
db_cursor.execute("SELECT name FROM taxonomy WHERE left_value > ? AND " \
"right_value < ? AND name_class == 'scientific name' AND node_rank LIKE ?", (left_value, right_value, rank.strip()))
return [row[0] for row in db_cursor.fetchall()]
# for row in db_cursor.fetchall():
# print row
# return []
if len(sys.argv) < 4:
print("usage: fill_tips_from_taxonomy.py <treefile> <taxonomydb> <outfile>")
sys.exit(0)
tree = None
with open(sys.argv[1],"r") as intree_file:
tree = newick3.parse(intree_file.readline())
conn = sqlite3.connect(sys.argv[2])
c = conn.cursor()
for tip in tree.leaves():
for child_name in get_child_taxa(tip.label, "genus", c):
child = phylo3.Node()
child.label = child_name
child.istip = True
# print child.label
tip.add_child(child)
# print([t.label for t in tip.children])
with open(sys.argv[3],"w") as outfile:
# print(newick3.to_string(tree)+";")
outfile.write(newick3.to_string(tree)+";")