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