comparison spp_wrapper.py @ 0:d4236b60701f draft default tip

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