comparison ete_species_tree_generator.py @ 2:03c10736e497 draft

planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/ete commit 91b634b8f9b131045bbbbf43cc8edbea59ac686b-dirty
author earlhaminst
date Tue, 07 Nov 2017 11:45:13 -0500
parents a4ba317fc713
children
comparison
equal deleted inserted replaced
1:a4ba317fc713 2:03c10736e497
1 import optparse 1 import optparse
2 import sys
2 3
3 from ete3 import NCBITaxa 4 from ete3 import NCBITaxa
4 5
5 ncbi = NCBITaxa()
6 6
7 parser = optparse.OptionParser() 7 parser = optparse.OptionParser()
8 parser.add_option('-s', '--species', dest="input_species_filename", 8 parser.add_option('-s', '--species', dest="input_species_filename",
9 help='Species list in text format one species in each line') 9 help='Species list in text format one species in each line')
10 10 parser.add_option('-d', '--database', dest="database", default=None,
11 help='ETE sqlite data base to use (default: ~/.etetoolkit/taxa.sqlite)')
12 parser.add_option('-o', '--output', dest="output", help='output file name (default: stdout)')
11 parser.add_option('-f', '--format', type='choice', choices=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '100'], dest="format", 13 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') 14 default='8', help='outpur format for tree')
13
14 parser.add_option('-t', '--treebest', type='choice', choices=['yes', 'no'], dest="treebest", 15 parser.add_option('-t', '--treebest', type='choice', choices=['yes', 'no'], dest="treebest",
15 default='no', help='To be used in TreeBest') 16 default='no', help='To be used in TreeBest')
17 options, args = parser.parse_args()
18 if options.input_species_filename is None:
19 parser.error("-s option must be specified, Species list in text format one species in each line")
16 20
17 parser.add_option('-d', '--database', type='choice', choices=['yes', 'no'], dest="database", 21 ncbi = NCBITaxa(dbfile=options.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: 22 with open(options.input_species_filename) as f:
32 species_name = [_.strip().replace('_', ' ') for _ in f.readlines()] 23 species_name = [_.strip().replace('_', ' ') for _ in f.readlines()]
33 24
34 name2taxid = ncbi.get_name_translator(species_name) 25 name2taxid = ncbi.get_name_translator(species_name)
35 26
49 newickTree = tree.write(format=int(options.format)) 40 newickTree = tree.write(format=int(options.format))
50 41
51 if options.treebest == "yes": 42 if options.treebest == "yes":
52 newickTree = newickTree.rstrip(';') 43 newickTree = newickTree.rstrip(';')
53 newickTree = newickTree + "root;" 44 newickTree = newickTree + "root;"
54 45 # setup output
55 with open('newickTree.nhx', 'w') as newickFile: 46 if not options.output: # if filename is not given
56 newickFile.write(newickTree) 47 of = sys.stdout
48 else:
49 of = open(options.output, "w")
50 of.write(newickTree)
51 of.close()