File: run_tipp_tool.py

package info (click to toggle)
tipp 1.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 676 kB
  • sloc: java: 4,698; python: 1,892; xml: 62; makefile: 21
file content (82 lines) | stat: -rw-r--r-- 2,622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


'''
Created on May 13, 2014
@author: namphuon
'''
import argparse
import os
from argparse import Namespace
import sepp.config
import tipp.metagenomics
import sepp


def parse_args():
    parser = argparse.ArgumentParser(
        description='Performs various tools for TIPP.')
    parser.add_argument(
        "-g", "--gene",
        dest="gene", metavar="GENE",
        default=None,
        type=str,
        help="use GENE\'s reference package")
    parser.add_argument(
        "-o", "--output",
        dest="output", metavar="OUTPUT",
        default="output",
        type=sepp.config.valid_file_prefix,
        help="output files with prefix OUTPUT. [default: %(default)s]")
    parser.add_argument(
        "-d", "--outdir",
        dest="outdir", metavar="OUTPUT_DIR",
        default=os.path.curdir,
        type=sepp.config.valid_dir_path,
        help="output to OUTPUT_DIR directory. full-path required. "
             "[default: %(default)s]")
    parser.add_argument(
        "-i", "--input",
        dest="input", metavar="INPUT",
        default=None,
        type=str,
        help="input file. full-path required. "
             "(_classification.txt file from running run_tipp.py)")
    parser.add_argument(
        "-t", "--threshold",
        dest="threshold",  metavar="THRESHOLD",
        default=0.95,
        type=float,
        help="Threshold for classification [default: 0.95]")
    return parser.parse_args()


def profile(inputf, gene, output, prefix, threshold):
    home = os.path.expanduser("~")
    if os.path.isfile(home + "/.tipp/tipp.config"):
        tipp_config_path = home + "/.tipp/tipp.config"
    else:
        tipp_config_path = "/etc/tipp/tipp.config"
    sepp.config.set_main_config_path(tipp_config_path)
    opts = Namespace()
    sepp.config._read_config_file(open(tipp_config_path, 'r'), opts)
    (taxon_map, level_map, key_map) = tipp.metagenomics.load_taxonomy(
        "%s/%s.refpkg/all_taxon.taxonomy" % (opts.reference.path, gene))
    gene_classification = tipp.metagenomics.generate_classification(
        inputf, threshold)
    tipp.metagenomics.remove_unclassified_level(gene_classification)
    tstr = str("%d" % (threshold * 100))
    cfile = str("%s/%s.classification_%s.txt" % (output, prefix, tstr))
    tipp.metagenomics.write_classification(gene_classification, cfile)
    tipp.metagenomics.write_abundance(gene_classification, output)


def main():
    args = parse_args()
    profile(args.input, args.gene, args.outdir, args.output,
            args.threshold)


if __name__ == "__main__":
    main()