Created
June 25, 2015 21:12
-
-
Save walterst/17d9e777e6b8cadb9546 to your computer and use it in GitHub Desktop.
See -h help text with script for options. This script converts the tree tip IDs (OTU IDs) to the taxonomy strings that are in a supplied OTU table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env | |
__author__ = "William Walters" | |
__copyright__ = "Copyright 2011" | |
__credits__ = ["William Walters"] | |
__license__ = "GPL" | |
__version__ = "1.0" | |
__maintainer__ = "William Walters" | |
__email__ = "William.A.Walters@gmail.com" | |
# USAGE: See help text with python convert_tree_tips_to_taxa.py -h | |
# This script will convert the OTU IDs in an input tree file to match the taxa strings | |
# in the paired OTU table. | |
from sys import argv | |
from os.path import basename, join | |
from biom import load_table | |
from skbio import TreeNode | |
from qiime.util import parse_command_line_parameters, get_options_lookup,\ | |
make_option, create_dir, qiime_open | |
options_lookup = get_options_lookup() | |
script_info={} | |
script_info['brief_description']="""This script will convert the OTU IDs in an input tree file to match the taxa strings in the paired OTU table.""" | |
script_info['script_description']="""""" | |
script_info['output_description']="""A phylogenetic tree""" | |
script_info['required_options']= [\ | |
make_option('-i', '--otu_table_fp',type='existing_filepath', | |
help='input OTU table'), | |
make_option('-t', '--tree_fp',type='existing_filepath', | |
help='input tree filepath'), | |
options_lookup['output_dir'] | |
] | |
script_info['optional_options']= [\ | |
make_option('-d', '--taxa_depth',type='int', default=3, | |
help='Depth of taxa to include in the output plot, e.g. 1=domain level')] | |
script_info['version'] = __version__ | |
def main(): | |
option_parser, opts, args =\ | |
parse_command_line_parameters(suppress_verbose=True, **script_info) | |
taxa_depth = opts.taxa_depth | |
output_dir = opts.output_dir | |
tip_taxa = {} | |
otu_table = load_table(opts.otu_table_fp) | |
tree = TreeNode.from_file(opts.tree_fp) | |
create_dir(output_dir) | |
obs_ids = set(otu_table._observation_ids) | |
for x in obs_ids: | |
tip_taxa[x] = "_".join(otu_table.metadata(x,\ | |
axis="observation")['taxonomy'][0:taxa_depth]) | |
output_tree_fp = join(output_dir + "/filtered_tree.tre") | |
for tip in tree.tips(): | |
if tip.name in tip_taxa: | |
tip.name = tip_taxa[tip.name] | |
tree.write(output_tree_fp) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment