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