~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to scripts/add_taxa.py

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
__copyright__ = "Copyright 2011, The QIIME Project"
7
7
__credits__ = ["Rob Knight","Justin Kuczynski","Greg Caporaso"]
8
8
__license__ = "GPL"
9
 
__version__ = "1.5.0"
 
9
__version__ = "1.7.0"
10
10
__maintainer__ = "Daniel McDonald"
11
11
__email__ = "wasade@gmail.com"
12
12
__status__ = "Release"
13
 
 
14
 
 
15
 
from qiime.util import parse_command_line_parameters
16
 
from qiime.util import make_option
17
 
from biom.parse import parse_biom_table
18
 
from qiime.parse import parse_taxonomy_to_otu_metadata
19
 
from qiime.format import format_biom_table
20
 
 
21
 
script_info={}
22
 
script_info['brief_description']="""Add taxa to OTU table"""
23
 
script_info['script_description']="""This script adds taxa to a biom-formatted OTU table."""
24
 
script_info['script_usage']=[]
25
 
 
26
 
script_info['script_usage'].append(("""Example:""","""Given an input otu table with no metadata (otu_table_no_tax.biom) and a tab-separated text file mapping OTU ids to taxonomic assignments and scores associated with those assignments (tax.txt), generate a new otu table that includes taxonomic assignments (otu_table_w_tax.biom).""","""%prog -i otu_table_no_tax.biom -o otu_table_w_tax.biom -t tax.txt"""))
27
 
 
28
 
script_info['script_usage'].append(("""Example:""","""Given an input otu table with no metadata (otu_table_no_tax.biom) and a tab-separated text file mapping OTU ids to taxonomic assignments and scores associated with those assignments (tax.txt), generate a new otu table that includes taxonomic assignments (otu_table_w_tax.biom) with alternate metadata identifiers.""","""%prog -i otu_table_no_tax.biom -o otu_table_w_alt_labeled_tax.biom -t tax.txt -l "Consensus Lineage,Score" """))
29
 
 
30
 
script_info['script_usage'].append(("""Example:""","""Given an input otu table with no metadata (otu_table_no_tax.biom) and a tab-separated text file mapping OTU ids to some value, generate a new otu table that includes that metadata category labeled as "Score" (otu_table_w_score.biom).""","""%prog -i otu_table_no_tax.biom -o otu_table_w_score.biom -t score_only.txt -l "Score" --all_strings"""))
31
 
 
32
 
script_info['output_description']="""An OTU table in biom format is written to the file specified as -o."""
33
 
script_info['required_options']=[\
34
 
    make_option('-i','--input_fp',type='existing_filepath',
35
 
                help='path to input otu table file in biom format'),
36
 
    make_option('-o','--output_fp',type='new_filepath',
37
 
                help='path to output file in biom format'),
38
 
    make_option('-t','--taxonomy_fp',type='existing_filepath',
39
 
                help='path to input taxonomy file (e.g., as generated by assign_taxonomy.py)'),
40
 
]
41
 
 
42
 
script_info['optional_options']=[
43
 
    make_option('-l','--labels',type='string',default='taxonomy,score',
44
 
                help='labels to be assigned to metadata in taxonomy_fp'),
45
 
    make_option('--all_strings',action='store_true',default=False,
46
 
                help='treat all metadata as strings, rather than casting to lists/floats (useful with --labels for adding arbitrary observation metadata) [default:%default]')]
47
 
script_info['version'] = __version__
48
 
 
49
 
def main():
50
 
    option_parser, opts, args = parse_command_line_parameters(**script_info)
51
 
    
52
 
    labels = opts.labels.split(',')
53
 
    if opts.all_strings:
54
 
        process_fs = [str] * len(labels)
55
 
        observation_metadata = parse_taxonomy_to_otu_metadata(\
56
 
                            open(opts.taxonomy_fp,'U'),labels=labels,process_fs=process_fs)
57
 
    else:
58
 
        observation_metadata = parse_taxonomy_to_otu_metadata(\
59
 
                            open(opts.taxonomy_fp,'U'),labels=labels)
60
 
    
61
 
 
62
 
    otu_table = parse_biom_table(open(opts.input_fp,'U'))
63
 
    
64
 
    if otu_table.ObservationMetadata != None:
65
 
        # if there is already metadata associated with the 
66
 
        # observations, confirm that none of the metadata names
67
 
        # are already present
68
 
        existing_keys = otu_table.ObservationMetadata[0].keys()
69
 
        for label in labels:
70
 
            if label in existing_keys:
71
 
                option_parser.error(\
72
 
                 "%s is already an observation metadata field." 
73
 
                 " Can't add it, so nothing is being added." % label)
74
 
    
75
 
    otu_table.addObservationMetadata(observation_metadata)
76
 
    
77
 
    output_f = open(opts.output_fp,'w')
78
 
    output_f.write(format_biom_table(otu_table))
79
 
    output_f.close()
80
 
    
81
 
    
82
 
    
83
 
 
84
 
if __name__ == "__main__":
85
 
    main()
 
13
 
 
14
print "\nThis script has been deprecated in favor of the more general add_metadata.py script. add_metadata.py is part of the biom-format package, a dependency of QIIME. For more details, call:\n\nadd_metadata.py -h\n"
 
 
b'\\ No newline at end of file'