Mercurial > repos > saketkc > oncodrivefm
comparison tools/oncodrivefm_tool/oncodrivefm_tool.py @ 0:8447ba178b06
Init
author | Saket Choudhary <saketkc@gmail.com> |
---|---|
date | Wed, 20 Nov 2013 01:15:34 +0530 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8447ba178b06 |
---|---|
1 #!/usr/bin/python | |
2 import argparse | |
3 import sys | |
4 import os | |
5 import tempfile | |
6 import shutil | |
7 import subprocess | |
8 import ntpath | |
9 | |
10 """ | |
11 -h, --help show this help message and exit | |
12 -o PATH, --output-path PATH | |
13 Directory where output files will be written | |
14 -n NAME Analysis name | |
15 --output-format FORMAT | |
16 The FORMAT for the output file | |
17 -N NUMBER, --samplings NUMBER | |
18 Number of samplings to compute the FM bias pvalue | |
19 -e ESTIMATOR, --estimator ESTIMATOR | |
20 Test estimator for computation. | |
21 --gt THRESHOLD, --gene-threshold THRESHOLD | |
22 Minimum number of mutations per gene to compute the FM | |
23 bias | |
24 --pt THRESHOLD, --pathway-threshold THRESHOLD | |
25 Minimum number of mutations per pathway to compute the | |
26 FM bias | |
27 -s SLICES, --slices SLICES | |
28 Slices to process separated by commas | |
29 -m PATH, --mapping PATH | |
30 File with mappings between genes and pathways to be | |
31 analysed | |
32 --save-data The input data matrix will be saved | |
33 --save-analysis The analysis results will be saved | |
34 -j CORES, --cores CORES | |
35 Number of cores to use for calculations. Default is 0 | |
36 that means all the available cores | |
37 -D KEY=VALUE Define external parameters to be saved in the results | |
38 -L LEVEL, --log-level LEVEL | |
39 Define log level: debug, info, warn, error, critical, | |
40 notset | |
41 """ | |
42 def stop_err( msg ): | |
43 sys.stderr.write( '%s\n' % msg ) | |
44 sys.exit() | |
45 def main(params): | |
46 parser = argparse.ArgumentParser() | |
47 ##TAKEN directly from the source code | |
48 parser.add_argument("-N", "--samplings", dest="num_samplings", type=int, default=10000, metavar="NUMBER", | |
49 help="Number of samplings to compute the FM bias pvalue") | |
50 parser.add_argument("-e", "--estimator", dest="estimator", metavar="ESTIMATOR", | |
51 choices=["mean", "median"], default="mean", | |
52 help="Test estimator for computation.") | |
53 parser.add_argument("--gt", "--gene-threshold", dest="mut_gene_threshold", type=int, default=2, metavar="THRESHOLD", | |
54 help="Minimum number of mutations per gene to compute the FM bias") | |
55 parser.add_argument("--pt", "--pathway-threshold", dest="mut_pathway_threshold", type=int, default=10, metavar="THRESHOLD", | |
56 help="Minimum number of mutations per pathway to compute the FM bias") | |
57 parser.add_argument("-s", "--slices", dest="slices", metavar="SLICES", | |
58 help="Slices to process separated by commas") | |
59 parser.add_argument("-m", "--mapping", dest="mapping", metavar="PATH", | |
60 help="File with mappings between genes and pathways to be analysed") | |
61 parser.add_argument("-f", "--filter", dest="filter", metavar="PATH", | |
62 help="File containing the features to be filtered. By default labels are includes," | |
63 " labels preceded with - are excludes.") | |
64 #parser.add_argument("-o", "--output_path", type=str, required=True, help="Directory where output files will be written") | |
65 parser.add_argument("-o1", "--output1", type=str, dest="output1", required=True) | |
66 | |
67 parser.add_argument("-o2", "--output2", type=str, dest="output2", required=False) | |
68 parser.add_argument("-n", "--analysis_name", type=str, required=False, help="Analysis name") | |
69 #parser.add_argument("-e", "--estimator", type=str, required=False, choices=["mean-empirical","median-empirical","mean-zscore","median-zscore"], help="Test estimator for computation") | |
70 parser.add_argument("--output-format", dest="output_format", required=False, | |
71 metavar="FORMAT", | |
72 choices=["tsv", "tsv.gz", "tsv.bz2"], | |
73 default="tsv", | |
74 help="The FORMAT for the output file") | |
75 parser.add_argument("-j", "--cores", dest="num_cores", type=int, | |
76 metavar="CORES", | |
77 help="Number of cores to use for calculations.\ | |
78 Default is 0 that means all the available cores") | |
79 parser.add_argument("-D", dest="defines", metavar="KEY=VALUE", action="append", help="Define external parameters to be saved in the results") | |
80 parser.add_argument("-L", "--log-level", dest="log_level", metavar="LEVEL", default=None, | |
81 choices=["debug", "info", "warn", "error", "critical", "notset"], | |
82 help="Define log level: debug, info, warn, error, critical, notset") | |
83 parser.add_argument("-i", "--input", dest="input_path", required=True, type=str, help="Path to input file") | |
84 args = vars(parser.parse_args(params)) | |
85 try: | |
86 mapping_path = args["mapping_path"] | |
87 except KeyError: | |
88 mapping_path = "no_mapping_path" | |
89 #if mapping_path=="no_mapping_path": | |
90 #params.remove(mapping_path) | |
91 #params.remove("-m") | |
92 output_dir = tempfile.mkdtemp() | |
93 params.append("-o") | |
94 params.append(output_dir) | |
95 params.append(args["input_path"]) | |
96 cmd = "oncodrivefm " | |
97 i=0 | |
98 while i<len(params): | |
99 p=params[i] | |
100 if p=="-i" or p=="-o1" or p=="-o2": | |
101 i+=2 | |
102 else: | |
103 i+=1 | |
104 cmd += " "+p | |
105 cmd += " 2>&1 " | |
106 #tmp = tempfile.NamedTemporaryFile( dir=output_dir ).name | |
107 #tmp_stderr = open( tmp, 'wb' ) | |
108 print cmd | |
109 proc = subprocess.Popen(args=cmd, shell=True) | |
110 returncode = proc.wait() | |
111 #tmp_stderr.close() | |
112 | |
113 if args['analysis_name'] is not None: | |
114 prefix = args["analysis_name"] | |
115 else: | |
116 ##refer: http://stackoverflow.com/a/8384788/756986 | |
117 prefix = ntpath.basename(args["input_path"]).split(".")[0] | |
118 if args["mapping"] is not None: | |
119 pathway_file = prefix+"-pathways" | |
120 else: | |
121 pathway_file = None | |
122 output_format = args["output_format"] | |
123 genes_output_file_name = os.path.join(output_dir, prefix+"-genes."+output_format) | |
124 shutil.move(genes_output_file_name,args["output1"]) | |
125 if pathway_file: | |
126 pathway_output_file_name = os.path.join(output_dir, pathway_file+"."+output_format) | |
127 shutil.move(pathway_output_file_name,args["output2"]) | |
128 if os.path.exists( output_dir ): | |
129 shutil.rmtree( output_dir ) | |
130 if __name__=="__main__": | |
131 main(sys.argv[1:]) |