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