comparison run_lefse.py @ 0:e7cd19afda2e draft

Lefse
author george-weingart
date Tue, 13 May 2014 21:57:00 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e7cd19afda2e
1 #!/usr/bin/env python
2
3 import os,sys,math,pickle
4 from lefse import *
5
6 def read_params(args):
7 parser = argparse.ArgumentParser(description='LEfSe 1.0')
8 parser.add_argument('input_file', metavar='INPUT_FILE', type=str, help="the input file")
9 parser.add_argument('output_file', metavar='OUTPUT_FILE', type=str,
10 help="the output file containing the data for the visualization module")
11 parser.add_argument('-o',dest="out_text_file", metavar='str', type=str, default="",
12 help="set the file for exporting the result (only concise textual form)")
13 parser.add_argument('-a',dest="anova_alpha", metavar='float', type=float, default=0.05,
14 help="set the alpha value for the Anova test (default 0.05)")
15 parser.add_argument('-w',dest="wilcoxon_alpha", metavar='float', type=float, default=0.05,
16 help="set the alpha value for the Wilcoxon test (default 0.05)")
17 parser.add_argument('-l',dest="lda_abs_th", metavar='float', type=float, default=2.0,
18 help="set the threshold on the absolute value of the logarithmic LDA score (default 2.0)")
19 parser.add_argument('--nlogs',dest="nlogs", metavar='int', type=int, default=3,
20 help="max log ingluence of LDA coeff")
21 parser.add_argument('--verbose',dest="verbose", metavar='int', choices=[0,1], type=int, default=0,
22 help="verbose execution (default 0)")
23 parser.add_argument('--wilc',dest="wilc", metavar='int', choices=[0,1], type=int, default=1,
24 help="wheter to perform the Wicoxon step (default 1)")
25 parser.add_argument('-r',dest="rank_tec", metavar='str', choices=['lda','svm'], type=str, default='lda',
26 help="select LDA or SVM for effect size (default LDA)")
27 parser.add_argument('--svm_norm',dest="svm_norm", metavar='int', choices=[0,1], type=int, default=1,
28 help="whether to normalize the data in [0,1] for SVM feature waiting (default 1 strongly suggested)")
29 parser.add_argument('-b',dest="n_boots", metavar='int', type=int, default=30,
30 help="set the number of bootstrap iteration for LDA (default 30)")
31 parser.add_argument('-e',dest="only_same_subcl", metavar='int', type=int, default=0,
32 help="set whether perform the wilcoxon test only among the subclasses with the same name (default 0)")
33 parser.add_argument('-c',dest="curv", metavar='int', type=int, default=0,
34 help="set whether perform the wilcoxon test ing the Curtis's approach [BETA VERSION] (default 0)")
35 parser.add_argument('-f',dest="f_boots", metavar='float', type=float, default=0.67,
36 help="set the subsampling fraction value for each bootstrap iteration (default 0.66666)")
37 parser.add_argument('-s',dest="strict", choices=[0,1,2], type=int, default=0,
38 help="set the multiple testing correction options. 0 no correction (more strict, default), 1 correction for independent comparisons, 2 correction for independent comparison")
39 # parser.add_argument('-m',dest="m_boots", type=int, default=5,
40 # help="minimum cardinality of classes in each bootstrapping iteration (default 5)")
41 parser.add_argument('--min_c',dest="min_c", metavar='int', type=int, default=10,
42 help="minimum number of samples per subclass for performing wilcoxon test (default 10)")
43 parser.add_argument('-t',dest="title", metavar='str', type=str, default="",
44 help="set the title of the analysis (default input file without extension)")
45 parser.add_argument('-y',dest="multiclass_strat", choices=[0,1], type=int, default=0,
46 help="(for multiclass tasks) set whether the test is performed in a one-against-one ( 1 - more strict!) or in a one-against-all setting ( 0 - less strict) (default 0)")
47 args = parser.parse_args()
48
49 params = vars(args)
50 if params['title'] == "": params['title'] = params['input_file'].split("/")[-1].split('.')[0]
51 return params
52
53
54
55 if __name__ == '__main__':
56 init()
57 params = read_params(sys.argv)
58 feats,cls,class_sl,subclass_sl,class_hierarchy = load_data(params['input_file'])
59 kord,cls_means = get_class_means(class_sl,feats)
60 wilcoxon_res = {}
61 kw_n_ok = 0
62 nf = 0
63 for feat_name,feat_values in feats.items():
64 if params['verbose']:
65 print "Testing feature",str(nf),": ",feat_name,
66 nf += 1
67 kw_ok,pv = test_kw_r(cls,feat_values,params['anova_alpha'],sorted(cls.keys()))
68 if not kw_ok:
69 if params['verbose']: print "\tkw ko"
70 del feats[feat_name]
71 wilcoxon_res[feat_name] = "-"
72 continue
73 if params['verbose']: print "\tkw ok\t",
74
75 if not params['wilc']: continue
76 kw_n_ok += 1
77 res_wilcoxon_rep = test_rep_wilcoxon_r(subclass_sl,class_hierarchy,feat_values,params['wilcoxon_alpha'],params['multiclass_strat'],params['strict'],feat_name,params['min_c'],params['only_same_subcl'],params['curv'])
78 wilcoxon_res[feat_name] = str(pv) if res_wilcoxon_rep else "-"
79 if not res_wilcoxon_rep:
80 if params['verbose']: print "wilc ko"
81 del feats[feat_name]
82 elif params['verbose']: print "wilc ok\t"
83
84 if len(feats) > 0:
85 print "Number of significantly discriminative features:", len(feats), "(", kw_n_ok, ") before internal wilcoxon"
86 if params['lda_abs_th'] < 0.0:
87 lda_res,lda_res_th = dict([(k,0.0) for k,v in feats.items()]), dict([(k,v) for k,v in feats.items()])
88 else:
89 if params['rank_tec'] == 'lda': lda_res,lda_res_th = test_lda_r(cls,feats,class_sl,params['n_boots'],params['f_boots'],params['lda_abs_th'],0.0000000001,params['nlogs'])
90 elif params['rank_tec'] == 'svm': lda_res,lda_res_th = test_svm(cls,feats,class_sl,params['n_boots'],params['f_boots'],params['lda_abs_th'],0.0,params['svm_norm'])
91 else: lda_res,lda_res_th = dict([(k,0.0) for k,v in feats.items()]), dict([(k,v) for k,v in feats.items()])
92 else:
93 print "Number of significantly discriminative features:", len(feats), "(", kw_n_ok, ") before internal wilcoxon"
94 print "No features with significant differences between the two classes"
95 lda_res,lda_res_th = {},{}
96 outres = {}
97 outres['lda_res_th'] = lda_res_th
98 outres['lda_res'] = lda_res
99 outres['cls_means'] = cls_means
100 outres['cls_means_kord'] = kord
101 outres['wilcox_res'] = wilcoxon_res
102 print "Number of discriminative features with abs LDA score >",params['lda_abs_th'],":",len(lda_res_th)
103 save_res(outres,params["output_file"])