Mercurial > repos > stemcellcommons > spp_tool
comparison spp_wrapper.py @ 0:b15734276ca3 draft
Initial upload.
author | stemcellcommons |
---|---|
date | Thu, 17 Oct 2013 12:39:45 -0400 |
parents | |
children | 23b22c1692fa |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b15734276ca3 |
---|---|
1 #purpose: python wrapper to run spp | |
2 #author: Ziru Zhou | |
3 #Date: November 2012 | |
4 ##################### | |
5 | |
6 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip | |
7 from galaxy import eggs | |
8 import pkg_resources | |
9 pkg_resources.require( "simplejson" ) | |
10 import simplejson | |
11 | |
12 CHUNK_SIZE = 1024 | |
13 | |
14 def main(): | |
15 options = simplejson.load( open( sys.argv[1] ) ) | |
16 output_narrow_peak = sys.argv[2] | |
17 output_region_peak = sys.argv[3] | |
18 output_peakshift_file = sys.argv[4] | |
19 output_rdata_file = sys.argv[5] | |
20 output_plot_file = sys.argv[6] | |
21 output_default_file = sys.argv[7] | |
22 script_path = sys.argv[8] | |
23 | |
24 #set file extensions and set mandatory options | |
25 #====================================================================================== | |
26 experiment_name = '_'.join( options['experiment_name'].split() ) #save experiment name | |
27 | |
28 chip_file = "%s.bam" % (options['chip_file']) | |
29 subprocess.call(["cp", options['chip_file'], chip_file]) | |
30 | |
31 cmdline = "Rscript %s/run_spp.R -c=%s" % (script_path, chip_file ) | |
32 if 'input_file' in options: | |
33 input_file = "%s.bam" % (options['input_file']) | |
34 subprocess.call(["cp", options['input_file'], input_file]) | |
35 cmdline = "%s -i=%s" % ( cmdline, input_file ) | |
36 | |
37 #set additional options | |
38 #======================================================================================== | |
39 if (options['action'] == "cross_correlation"): | |
40 cmdline = "%s %s %s %s > default_output.txt" % ( cmdline, options['savp'], options['out'], options['rf'] ) | |
41 elif (options['action'] == "peak_calling"): | |
42 cmdline = "%s -fdr=%s -npeak=%s %s %s %s %s %s > default_output.txt" % ( cmdline, options['fdr'], options['npeak'], options['savr'], options['savd'], options['savn'], options['savp'], options['rf'] ) | |
43 elif (options['action'] == "idr"): | |
44 cmdline = "%s -npeak=%s %s %s %s %s > default_output.txt" % ( cmdline, options['npeak'], options['savr'], options['savp'], options['out'], options['rf'] ) | |
45 elif (options['action'] == "custom"): | |
46 cmdline = "%s -s=%s %s -x=%s -fdr=%s -npeak=%s %s %s" % ( cmdline, options['s'], options['speak'], options['x'], options['fdr'], options['npeak'], options['filtchr'], options['rf'] ) | |
47 cmdline = "%s %s %s %s %s %s > default_output.txt" % ( cmdline, options['out'], options['savn'], options['savr'], options['savp'], options['savd'] ) | |
48 | |
49 #run cmdline | |
50 #======================================================================================== | |
51 #tmp_dir = tempfile.mkdtemp() | |
52 tmp_dir = os.path.dirname(options['chip_file']) | |
53 stderr_name = tempfile.NamedTemporaryFile().name | |
54 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) ) | |
55 proc.wait() | |
56 | |
57 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup | |
58 #======================================================================================== | |
59 if proc.returncode: | |
60 stderr_f = open( stderr_name ) | |
61 while True: | |
62 chunk = stderr_f.read( CHUNK_SIZE ) | |
63 if not chunk: | |
64 stderr_f.close() | |
65 break | |
66 sys.stderr.write( chunk ) | |
67 | |
68 | |
69 #determine if the outputs are there, copy them to the appropriate dir and filename | |
70 #======================================================================================== | |
71 chip_name = os.path.basename(options['chip_file']) | |
72 input_name = os.path.basename(options['input_file']) | |
73 | |
74 created_default_file = os.path.join( tmp_dir, "default_output.txt" ) | |
75 if os.path.exists( created_default_file ): | |
76 shutil.move( created_default_file, output_default_file ) | |
77 | |
78 created_narrow_peak = os.path.join( tmp_dir, "%s_VS_%s.narrowPeak" % (chip_name, input_name) ) | |
79 if os.path.exists( created_narrow_peak ): | |
80 shutil.move( created_narrow_peak, output_narrow_peak ) | |
81 | |
82 created_region_peak = os.path.join( tmp_dir, "%s_VS_%s.regionPeak" % (chip_name, input_name) ) | |
83 if os.path.exists( created_region_peak ): | |
84 shutil.move( created_region_peak, output_region_peak ) | |
85 | |
86 created_peakshift_file = os.path.join( tmp_dir, "peakshift.txt" ) | |
87 if os.path.exists( created_peakshift_file ): | |
88 shutil.move( created_peakshift_file, output_peakshift_file ) | |
89 | |
90 created_rdata_file = os.path.join( tmp_dir, "%s.Rdata" % chip_name ) | |
91 if os.path.exists( created_rdata_file ): | |
92 shutil.move( created_rdata_file, output_rdata_file ) | |
93 | |
94 created_plot_file = os.path.join( tmp_dir, "%s.pdf" % chip_name ) | |
95 if os.path.exists( created_plot_file ): | |
96 shutil.move( created_plot_file, output_plot_file ) | |
97 | |
98 | |
99 os.unlink( stderr_name ) | |
100 #os.rmdir( tmp_dir ) | |
101 | |
102 if __name__ == "__main__": main() |