annotate macs_wrapper.py @ 19:5d98f6fe9424 draft default tip

Uploaded
author modencode-dcc
date Fri, 18 Jan 2013 16:12:45 -0500
parents 426a52a01655
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
1 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
2 from galaxy import eggs
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
3 import pkg_resources
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
4 pkg_resources.require( "simplejson" )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
5 import simplejson
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
6
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
7 CHUNK_SIZE = 1024
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
8
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
9 def gunzip_cat_glob_path( glob_path, target_filename, delete = False ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
10 out = open( target_filename, 'wb' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
11 for filename in glob.glob( glob_path ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
12 fh = gzip.open( filename, 'rb' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
13 while True:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
14 data = fh.read( CHUNK_SIZE )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
15 if data:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
16 out.write( data )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
17 else:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
18 break
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
19 fh.close()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
20 if delete:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
21 os.unlink( filename )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
22 out.close()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
23
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
24 def xls_to_interval( xls_file, interval_file, header = None ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
25 out = open( interval_file, 'wb' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
26 if header:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
27 out.write( '#%s\n' % header )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
28 wrote_header = False
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
29 #From macs readme: Coordinates in XLS is 1-based which is different with BED format.
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
30 for line in open( xls_file ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
31 #keep all existing comment lines
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
32 if line.startswith( '#' ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
33 out.write( line )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
34 elif not wrote_header:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
35 out.write( '#%s' % line )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
36 wrote_header = True
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
37 else:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
38 fields = line.split( '\t' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
39 if len( fields ) > 1:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
40 fields[1] = str( int( fields[1] ) - 1 )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
41 out.write( '\t'.join( fields ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
42 out.close()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
43
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
44 def main():
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
45 options = simplejson.load( open( sys.argv[1] ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
46 output_bed = sys.argv[2]
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
47 output_extra_html = sys.argv[3]
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
48 output_extra_path = sys.argv[4]
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
49 # Added peaks output argument
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
50 output_peaks = sys.argv[5]
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
51
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
52 experiment_name = '_'.join( options['experiment_name'].split() ) #save experiment name here, it will be used by macs for filenames (gzip of wig files will fail with spaces - macs doesn't properly escape them)..need to replace all whitespace, split makes this easier
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
53 cmdline = "macs -t %s" % ",".join( options['input_chipseq'] )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
54 if options['input_control']:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
55 cmdline = "%s -c %s" % ( cmdline, ",".join( options['input_control'] ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
56 cmdline = "%s --format='%s' --name='%s' --gsize='%s' --tsize='%s' --bw='%s' --pvalue='%s' --mfold='%s' %s --lambdaset='%s' %s" % ( cmdline, options['format'], experiment_name, options['gsize'], options['tsize'], options['bw'], options['pvalue'], options['mfold'], options['nolambda'], options['lambdaset'], options['futurefdr'] )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
57 if 'wig' in options:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
58 wigextend = int( options['wig']['wigextend'] )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
59 if wigextend >= 0:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
60 wigextend = "--wigextend='%s'" % wigextend
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
61 else:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
62 wigextend = ''
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
63 cmdline = "%s --wig %s --space='%s'" % ( cmdline, wigextend, options['wig']['space'] )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
64 if 'nomodel' in options:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
65 cmdline = "%s --nomodel --shiftsize='%s'" % ( cmdline, options['nomodel'] )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
66 if 'diag' in options:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
67 cmdline = "%s --diag --fe-min='%s' --fe-max='%s' --fe-step='%s'" % ( cmdline, options['diag']['fe-min'], options['diag']['fe-max'], options['diag']['fe-step'] )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
68
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
69 tmp_dir = tempfile.mkdtemp() #macs makes very messy output, need to contain it into a temp dir, then provide to user
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
70 stderr_name = tempfile.NamedTemporaryFile().name # redirect stderr here, macs provides lots of info via stderr, make it into a report
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
71 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
72 proc.wait()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
73 #We don't want to set tool run to error state if only warnings or info, e.g. mfold could be decreased to improve model, but let user view macs log
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
74 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
75 if proc.returncode:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
76 stderr_f = open( stderr_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
77 while True:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
78 chunk = stderr_f.read( CHUNK_SIZE )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
79 if not chunk:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
80 stderr_f.close()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
81 break
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
82 sys.stderr.write( chunk )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
83
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
84 #run R to create pdf from model script
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
85 if os.path.exists( os.path.join( tmp_dir, "%s_model.r" % experiment_name ) ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
86 cmdline = 'R --vanilla --slave < "%s_model.r" > "%s_model.r.log"' % ( experiment_name, experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
87 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
88 proc.wait()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
89
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
90
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
91 #move bed out to proper output file
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
92 created_bed_name = os.path.join( tmp_dir, "%s_peaks.bed" % experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
93 if os.path.exists( created_bed_name ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
94 shutil.move( created_bed_name, output_bed )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
95
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
96 #OICR peak_xls file
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
97 created_peak_xls_file = os.path.join( tmp_dir, "%s_peaks.xls" % experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
98 if os.path.exists( created_peak_xls_file ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
99 # shutil.copy( created_peak_xls_file, os.path.join ( "/mnt/galaxyData/tmp/", "%s_peaks.xls" % ( os.path.basename(output_extra_path) )))
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
100 shutil.copyfile( created_peak_xls_file, output_peaks )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
101
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
102 #parse xls files to interval files as needed
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
103 if options['xls_to_interval']:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
104 create_peak_xls_file = os.path.join( tmp_dir, '%s_peaks.xls' % experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
105 if os.path.exists( create_peak_xls_file ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
106 xls_to_interval( create_peak_xls_file, options['xls_to_interval']['peaks_file'], header = 'peaks file' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
107 create_peak_xls_file = os.path.join( tmp_dir, '%s_negative_peaks.xls' % experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
108 if os.path.exists( create_peak_xls_file ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
109 xls_to_interval( create_peak_xls_file, options['xls_to_interval']['negative_peaks_file'], header = 'negative peaks file' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
110
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
111 #merge and move wig files as needed, delete gz'd files and remove emptied dirs
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
112 if 'wig' in options:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
113 wig_base_dir = os.path.join( tmp_dir, "%s_MACS_wiggle" % experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
114 if os.path.exists( wig_base_dir ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
115 #treatment
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
116 treatment_dir = os.path.join( wig_base_dir, "treat" )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
117 if os.path.exists( treatment_dir ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
118 gunzip_cat_glob_path( os.path.join( treatment_dir, "*.wig.gz" ), options['wig']['output_treatment_file'], delete = True )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
119 os.rmdir( treatment_dir )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
120 #control
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
121 if options['input_control']:
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
122 control_dir = os.path.join( wig_base_dir, "control" )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
123 if os.path.exists( control_dir ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
124 gunzip_cat_glob_path( os.path.join( control_dir, "*.wig.gz" ), options['wig']['output_control_file'], delete = True )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
125 os.rmdir( control_dir )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
126 os.rmdir( wig_base_dir )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
127
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
128 #move all remaining files to extra files path of html file output to allow user download
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
129 out_html = open( output_extra_html, 'wb' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
130 out_html.write( '<html><head><title>Additional output created by MACS (%s)</title></head><body><h3>Additional Files:</h3><p><ul>\n' % experiment_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
131 os.mkdir( output_extra_path )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
132 for filename in sorted( os.listdir( tmp_dir ) ):
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
133 shutil.move( os.path.join( tmp_dir, filename ), os.path.join( output_extra_path, filename ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
134 out_html.write( '<li><a href="%s">%s</a></li>\n' % ( filename, filename ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
135 #out_html.write( '<li><a href="%s">%s</a>peakxls %s SomethingDifferent tmp_dir %s path %s exp_name %s</li>\n' % ( created_peak_xls_file, filename, filename, tmp_dir, output_extra_path, experiment_name ) )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
136 out_html.write( '</ul></p>\n' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
137 out_html.write( '<h3>Messages from MACS:</h3>\n<p><pre>%s</pre></p>\n' % open( stderr_name, 'rb' ).read() )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
138 out_html.write( '</body></html>\n' )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
139 out_html.close()
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
140
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
141 os.unlink( stderr_name )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
142 os.rmdir( tmp_dir )
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
143
426a52a01655 Uploaded
modencode-dcc
parents:
diff changeset
144 if __name__ == "__main__": main()