annotate spp_wrapper.py @ 0:1c5611e52fd0 draft

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