Mercurial > repos > modencode-dcc > peakranger
comparison ranger_wrapper.py @ 17:d2319b4df472 draft
Uploaded
author | modencode-dcc |
---|---|
date | Mon, 21 Jan 2013 14:00:26 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
16:5b356acc1a4a | 17:d2319b4df472 |
---|---|
1 #purpose: python wrapper to run peak ranger | |
2 #author: Ziru Zhou | |
3 #Date: November 2012 | |
4 | |
5 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip | |
6 from galaxy import eggs | |
7 import pkg_resources | |
8 pkg_resources.require( "simplejson" ) | |
9 import simplejson | |
10 import glob | |
11 import datetime | |
12 | |
13 CHUNK_SIZE = 1024 | |
14 | |
15 def main(): | |
16 options = simplejson.load( open( sys.argv[1] ) ) | |
17 outputs = simplejson.load( open( sys.argv[2] ) ) | |
18 script_path = sys.argv[3] | |
19 | |
20 | |
21 #sets experiment name and sets the chip/input files | |
22 #======================================================================================== | |
23 experiment_name = '_'.join( options['experiment_name'].split() ) #save experiment name | |
24 | |
25 #cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC_tools/peakranger/peakranger %s -d %s --format bam" % ( options['action'], options['chip_file'] ) | |
26 cmdline = "peakranger %s -d %s" % ( options['action'], options['chip_file'] ) | |
27 if 'input_file' in options: | |
28 cmdline = "%s -c %s" % ( cmdline, options['input_file'] ) | |
29 | |
30 #set additional options | |
31 #======================================================================================== | |
32 if (options['action'] == "nr"): | |
33 output_ranger_file = outputs['output_ranger_file'] | |
34 | |
35 cmdline = "%s --format bam -l %s --verbose > default_output.txt" % ( cmdline, options['extension'] ) | |
36 elif (options['action'] == "lc"): | |
37 output_ranger_file = outputs['output_ranger_file'] | |
38 | |
39 cmdline = "%s --verbose > default_output.txt" % ( cmdline ) | |
40 elif (options['action'] == "wig"): | |
41 output_wigzip_file = outputs['output_wigzip_file'] | |
42 | |
43 cmdline = "%s --format bam -l %s %s %s %s -o ranger_wig" % ( cmdline, options['extension'], options['split'], options['strand'], options['gzip'] ) | |
44 elif (options['action'] == "wigpe"): | |
45 output_wigzip_file = outputs['output_wigzip_file'] | |
46 | |
47 cmdline = "%s -l %s %s %s %s -o ranger_wig" % ( cmdline, options['extension'], options['split'], options['strand'], options['gzip'] ) | |
48 elif (options['action'] == "ranger"): | |
49 output_summit_file = outputs['output_summit_file'] | |
50 output_region_file = outputs['output_region_file'] | |
51 output_details_file = outputs['output_details_file'] | |
52 output_report_file = outputs['output_report_file'] | |
53 | |
54 if (options['gene_annotate_file'] != "None"): | |
55 gene_annotate_file = "--gene_annot_file %s/gene_annotation_files/%s" % ( script_path, options['gene_annotate_file'] ) | |
56 report = "--report" | |
57 elif (options['gene_annotate_file'] == "Upload"): | |
58 gene_annotate_file = options['usr_annot_file'] | |
59 report = "--report" | |
60 else: | |
61 gene_annotate_file = "" | |
62 report = "" | |
63 | |
64 cmdline = "%s -t %s --format bam %s --plot_region %s -l %s -p %s -q %s -r %s -b %s %s %s -o ranger_peak" % ( cmdline, options['threads'], gene_annotate_file, options['plot_region'], options['extension'], options['pvalue'], options['fdr'], options['delta'], options['bandwith'], options['pad'], report ) | |
65 elif (options['action'] == "ccat"): | |
66 output_summit_file = outputs['output_summit_file'] | |
67 output_region_file = outputs['output_region_file'] | |
68 output_details_file = outputs['output_details_file'] | |
69 output_report_file = outputs['output_report_file'] | |
70 output_ranger_file = outputs['output_ranger_file'] | |
71 | |
72 if (options['gene_annotate_file'] != "None"): | |
73 gene_annotate_file = "--gene_annot_file %s/gene_annotation_files/%s" % ( script_path, options['gene_annotate_file'] ) | |
74 report = "--report" | |
75 elif (options['gene_annotate_file'] == "Upload"): | |
76 gene_annotate_file = options['usr_annot_file'] | |
77 report = "--report" | |
78 else: | |
79 gene_annotate_file = "" | |
80 report = "" | |
81 | |
82 cmdline = "%s --format bam %s --plot_region %s -l %s -q %s --win_size %s --win_step %s --min_count %s --min_score %s %s -o ranger_peak > default_output.txt" % ( cmdline, gene_annotate_file, options['plot_region'], options['extension'], options['fdr'], options['winsize'], options['winstep'], options['mincount'], options['minscore'], report ) | |
83 | |
84 #run cmdline | |
85 #======================================================================================== | |
86 tmp_dir = tempfile.mkdtemp() | |
87 stderr_name = tempfile.NamedTemporaryFile().name | |
88 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) ) | |
89 proc.wait() | |
90 | |
91 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup | |
92 #======================================================================================== | |
93 if proc.returncode: | |
94 stderr_f = open( stderr_name ) | |
95 while True: | |
96 chunk = stderr_f.read( CHUNK_SIZE ) | |
97 if not chunk: | |
98 stderr_f.close() | |
99 break | |
100 sys.stderr.write( chunk ) | |
101 | |
102 | |
103 #determine if the outputs are there, copy them to the appropriate dir and filename | |
104 #======================================================================================== | |
105 if(options['action'] == "nr" or options['action'] == "lc" or options['action'] == "ccat"): | |
106 created_ranger_file = os.path.join( tmp_dir, "default_output.txt" ) | |
107 if os.path.exists( created_ranger_file ): | |
108 shutil.move( created_ranger_file, output_ranger_file ) | |
109 | |
110 if(options['action'] == "wig" or options['action'] == "wigpe"): | |
111 if(options['split'] == "-s" or options['strand'] == "-x"): | |
112 if(options['gzip'] == "-z"): | |
113 files = str( glob.glob('%s/*.wig.gz' % tmp_dir) ) | |
114 files = files.replace("[", "") | |
115 files = files.replace("]", "") | |
116 files = files.replace(",", "") | |
117 files = files.replace("'", "") | |
118 cmd = "zip -j %s/ranger_wig.zip %s > /dev/null" % (tmp_dir, files) | |
119 #cmd = "tar -czvf %s/ranger_wig.tar %s > /dev/null" % (tmp_dir, files) | |
120 os.system(cmd) | |
121 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.zip" ) | |
122 else: | |
123 files = str( glob.glob('%s/*.wig' % tmp_dir) ) | |
124 files = files.replace("[", "") | |
125 files = files.replace("]", "") | |
126 files = files.replace(",", "") | |
127 files = files.replace("'", "") | |
128 cmd = "zip -j %s/ranger_wig.zip %s > /dev/null" % (tmp_dir, files) | |
129 #cmd = "tar -czvf %s/ranger_wig.tar %s > /dev/null" % (tmp_dir, files) | |
130 os.system(cmd) | |
131 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.zip" ) | |
132 else: | |
133 if(options['gzip'] == "-z"): | |
134 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.wig.gz" ) | |
135 else: | |
136 created_wigzip_file = os.path.join( tmp_dir, "ranger_wig.wig" ) | |
137 | |
138 if os.path.exists( created_wigzip_file ): | |
139 shutil.move( created_wigzip_file, output_wigzip_file ) | |
140 | |
141 if(options['action'] == "ranger" or options['action'] == "ccat"): | |
142 created_summit_file = os.path.join( tmp_dir, "ranger_peak_summit.bed" ) | |
143 if os.path.exists( created_summit_file ): | |
144 shutil.move( created_summit_file, output_summit_file ) | |
145 | |
146 created_region_file = os.path.join( tmp_dir, "ranger_peak_region.bed" ) | |
147 if os.path.exists( created_region_file ): | |
148 shutil.move( created_region_file, output_region_file ) | |
149 | |
150 created_details_file = os.path.join( tmp_dir, "ranger_peak_details" ) | |
151 if os.path.exists( created_details_file ): | |
152 shutil.move( created_details_file, output_details_file ) | |
153 | |
154 #zips the html report and puts it in history, whole report is too big and display in galaxy is very unformatted | |
155 filename = os.path.splitext(os.path.basename(options['chip_file']))[0] | |
156 filename = filename.upper() | |
157 extension = os.path.splitext(options['chip_file'])[1] | |
158 extension = extension.replace(".", "") | |
159 extension = extension.upper() | |
160 now = datetime.datetime.now() | |
161 date = now.strftime("%Y-%m-%d") | |
162 foldername = "%s_%s_REPORT_%s" % (filename, extension, date) | |
163 | |
164 created_report_file = os.path.join( tmp_dir, foldername ) | |
165 if os.path.exists ( created_report_file ): | |
166 #os.system("cp -rf %s %s" % (created_report_file, "/mnt/galaxyData/files/000/")) | |
167 os.system("cp -rf %s ." % created_report_file) | |
168 os.system("zip -r created_report.zip %s > /dev/null" % foldername) | |
169 #os.system("zip -r created_report.zip /mnt/galaxyData/files/000/%s > /dev/null" % foldername) | |
170 shutil.move( "created_report.zip", output_report_file) | |
171 | |
172 | |
173 #os.system("ln -fs %s/index.html %s" %( foldername, output_report_file )) | |
174 #datafoldername = os.path.splitext(os.path.basename(output_report_file)) | |
175 #datafolder = os.path.join ("/mnt/galaxyData/files/000/" datafoldername) | |
176 #print "datafolder %s" % datafolder | |
177 #if os.path.exists( datafolder ) | |
178 # os.system("rm -rf %s" % datafolder) | |
179 # os.system("cp -rf %s/%s/imgs /mnt/galaxyData/files/000/%s" % (tmp_dir, foldername, datafolder)) | |
180 # os.system("cp -rf %s/%s/scripts /mnt/galaxyData/files/000/%s" % (tmp_dir, foldername, datafolder)) | |
181 | |
182 os.unlink( stderr_name ) | |
183 shutil.rmtree( tmp_dir ) | |
184 | |
185 if __name__ == "__main__": main() |