comparison ete_tree_generator.py @ 0:276e3ee68c37 draft

planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/ete commit 230ae552ddeb1bfdef3a09becaa5c6d373529a05-dirty
author earlhaminst
date Thu, 15 Dec 2016 12:55:02 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:276e3ee68c37
1 import optparse
2
3 from ete3 import NCBITaxa
4
5 ncbi = NCBITaxa()
6
7 parser = optparse.OptionParser()
8 parser.add_option('-s', '--species', dest="input_species_filename",
9 help='Species list in text format one species in each line')
10
11 parser.add_option('-f', '--format', type='choice', choices=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '100'], dest="format",
12 default='8', help='outpur format for tree')
13
14 parser.add_option('-t', '--treebest', type='choice', choices=['yes', 'no'], dest="treebest",
15 default='no', help='To be used in TreeBest')
16
17 parser.add_option('-d', '--database', type='choice', choices=['yes', 'no'], dest="database",
18 default='no', help='Update database')
19
20 options, args = parser.parse_args()
21
22 if options.database == "yes":
23 try:
24 ncbi.update_taxonomy_database()
25 except:
26 pass
27
28 if options.input_species_filename is None:
29 raise Exception('-s option must be specified, Species list in text format one species in each line')
30
31 with open(options.input_species_filename) as f:
32 species_name = [_.strip().replace('_', ' ') for _ in f.readlines()]
33
34 name2taxid = ncbi.get_name_translator(species_name)
35
36 taxid = [name2taxid[_][0] for _ in species_name]
37
38 tree = ncbi.get_topology(taxid)
39
40 if options.treebest == "yes":
41 inv_map = {str(v[0]): k.replace(" ", "") + "*" for k, v in name2taxid.items()}
42 else:
43 inv_map = {str(v[0]): k for k, v in name2taxid.items()}
44
45
46 for leaf in tree:
47 leaf.name = inv_map[leaf.name]
48
49 newickTree = tree.write(format=int(options.format))
50
51 if options.treebest == "yes":
52 newickTree = newickTree.rstrip(';')
53 newickTree = newickTree + "root;"
54
55 with open('newickTree.nhx', 'w') as newickFile:
56 newickFile.write(newickTree)