annotate spp_wrapper.py @ 12:d063cc917090 draft

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