annotate spp_wrapper.py @ 0:b15734276ca3 draft

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