Mercurial > repos > sblanck > mpagenomics_wrappers
comparison mpagenomics_normalize-7dc6ce39fb89/preprocess.py @ 0:84b13b0e2b85
Uploaded
author | sblanck |
---|---|
date | Thu, 07 May 2015 08:22:36 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:84b13b0e2b85 |
---|---|
1 import os | |
2 import re | |
3 import shutil | |
4 import sys | |
5 import subprocess | |
6 import zipfile | |
7 import optparse | |
8 | |
9 def main(): | |
10 | |
11 parser = optparse.OptionParser() | |
12 parser.add_option('-s', action="store", dest='summary') | |
13 parser.add_option('-e', action="store", dest='dataSetName') | |
14 parser.add_option('-p', action="store", dest='new_file_path') | |
15 parser.add_option('-c', action="store", dest='inputcdffull_name') | |
16 parser.add_option('-f', action="store", dest='inputufl_name') | |
17 parser.add_option('-g', action="store", dest='inputugp_name') | |
18 parser.add_option('-a', action="store", dest='inputacs_name') | |
19 parser.add_option('-d', action="store", dest='inputcdffull') | |
20 parser.add_option('-v', action="store", dest='inputufl') | |
21 parser.add_option('-w', action="store", dest='inputugp') | |
22 parser.add_option('-b', action="store", dest='inputacs') | |
23 parser.add_option('-t', action="store", dest='tumorcsv') | |
24 parser.add_option('-y', action="store", dest='settingsType') | |
25 parser.add_option('-o', action="store", dest='outputgraph') | |
26 parser.add_option('-z', action="store", dest='zipfigures') | |
27 parser.add_option('-k', action="store", dest='outputlog') | |
28 parser.add_option('-l', action="store", dest='log') | |
29 parser.add_option('-u', action="store", dest='user_id') | |
30 | |
31 parser.add_option('-i', action="append", dest='inputFile', default=[]) | |
32 parser.add_option('-n', action='append', dest='inputFileName', default=[]) | |
33 | |
34 options, args = parser.parse_args() | |
35 | |
36 dataSetName=options.dataSetName | |
37 destinationPath=os.path.join(options.new_file_path, options.user_id, dataSetName) | |
38 | |
39 mpagenomics_dir = os.path.join(options.new_file_path,"mpagenomics",options.user_id) | |
40 data_dir = os.path.join(options.new_file_path, options.user_id) | |
41 | |
42 try: | |
43 os.makedirs(data_dir) | |
44 except: | |
45 shutil.rmtree(data_dir) | |
46 os.makedirs(data_dir) | |
47 | |
48 if (not os.path.isdir(mpagenomics_dir)): | |
49 os.makedirs(mpagenomics_dir) | |
50 | |
51 for inputFile, inputFileName in zip(options.inputFile,options.inputFileName): | |
52 source = inputFile | |
53 destination=os.path.join(data_dir,inputFileName) | |
54 _copy(source,destination) | |
55 | |
56 | |
57 cdffull_name=options.inputcdffull_name | |
58 if (cdffull_name.count(",") != 0): | |
59 chipType=cdffull_name.split(",",1)[0] | |
60 tagExt=cdffull_name.split(",",1)[1] | |
61 tag=tagExt.split(".",1)[0] | |
62 else: | |
63 chipType=cdffull_name.split(".",1)[0] | |
64 tag="" | |
65 | |
66 _copy(options.inputcdffull,os.path.join(data_dir, options.inputcdffull_name)) | |
67 _copy(options.inputugp,os.path.join(data_dir, options.inputugp_name)) | |
68 _copy(options.inputufl,os.path.join(data_dir, options.inputufl_name)) | |
69 _copy(options.inputacs,os.path.join(data_dir, options.inputacs_name)) | |
70 | |
71 | |
72 fig_dir = os.path.join("mpagenomics", options.user_id, "figures", dataSetName, "signal") | |
73 abs_fig_dir = os.path.join(options.new_file_path, fig_dir) | |
74 | |
75 | |
76 retcode = _preprocess(chipType, dataSetName, mpagenomics_dir, data_dir, options.new_file_path, options.tumorcsv, options.settingsType, options.outputgraph, options.outputlog, options.log, tag) | |
77 | |
78 if (retcode == 0): | |
79 if (os.path.isdir(abs_fig_dir)) and (options.outputgraph == "TRUE"): | |
80 | |
81 new_files = os.listdir(abs_fig_dir) | |
82 zipbuf = zipfile.ZipFile(os.path.join(abs_fig_dir, options.zipfigures), 'w', zipfile.ZIP_DEFLATED) | |
83 for current_file in new_files: | |
84 fn = os.path.join(abs_fig_dir, current_file) | |
85 relfn = fn[len(abs_fig_dir) + len(os.sep):] | |
86 zipbuf.write(fn, relfn) | |
87 | |
88 f = open(options.summary, "w") | |
89 # Create report | |
90 try: | |
91 for inputFileName in options.inputFileName: | |
92 f.write("%s\t%s\t%s\n" %(inputFileName,dataSetName,chipType)) | |
93 finally: | |
94 shutil.rmtree(data_dir) | |
95 f.close() | |
96 | |
97 sys.exit(retcode) | |
98 | |
99 sys.exit(retcode) | |
100 | |
101 | |
102 def _copy(source, destination): | |
103 try: | |
104 os.symlink(source, destination) | |
105 except: | |
106 shutil.copy(source, destination) | |
107 | |
108 def _preprocess (chipType,dataset,mpagenomics_dir,data_dir,tmp_dir,tumor,settingType,outputgraph,outputlog,log,tag): | |
109 script_dir=os.path.dirname(os.path.abspath(__file__)) | |
110 | |
111 if (outputlog=="TRUE"): | |
112 errfile=open(log,'w') | |
113 else: | |
114 errfile=open(os.path.join(tmp_dir,"errfile.log"),'w') | |
115 | |
116 retcode = subprocess.call(["Rscript", os.path.join(script_dir,"preprocess.R"), chipType, dataset, mpagenomics_dir, data_dir, tumor, settingType, outputgraph, tag], stdout = errfile, stderr = errfile) | |
117 return(retcode) | |
118 | |
119 | |
120 if __name__ == "__main__": | |
121 main() |