0
|
1 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip
|
|
2 from galaxy import eggs
|
|
3 import pkg_resources
|
|
4 pkg_resources.require( "simplejson" )
|
|
5 import simplejson
|
|
6
|
|
7 CHUNK_SIZE = 1024
|
|
8
|
|
9 def gunzip_cat_glob_path( glob_path, target_filename, delete = False ):
|
|
10 out = open( target_filename, 'wb' )
|
|
11 for filename in glob.glob( glob_path ):
|
|
12 fh = gzip.open( filename, 'rb' )
|
|
13 while True:
|
|
14 data = fh.read( CHUNK_SIZE )
|
|
15 if data:
|
|
16 out.write( data )
|
|
17 else:
|
|
18 break
|
|
19 fh.close()
|
|
20 if delete:
|
|
21 os.unlink( filename )
|
|
22 out.close()
|
|
23
|
|
24 def xls_to_interval( xls_file, interval_file, header = None ):
|
|
25 out = open( interval_file, 'wb' )
|
|
26 if header:
|
|
27 out.write( '#%s\n' % header )
|
|
28 wrote_header = False
|
|
29 #From macs readme: Coordinates in XLS is 1-based which is different with BED format.
|
|
30 for line in open( xls_file ):
|
|
31 #keep all existing comment lines
|
|
32 if line.startswith( '#' ):
|
|
33 out.write( line )
|
|
34 elif not wrote_header:
|
|
35 out.write( '#%s' % line )
|
|
36 wrote_header = True
|
|
37 else:
|
|
38 fields = line.split( '\t' )
|
|
39 if len( fields ) > 1:
|
|
40 fields[1] = str( int( fields[1] ) - 1 )
|
|
41 out.write( '\t'.join( fields ) )
|
|
42 out.close()
|
|
43
|
|
44 def main():
|
|
45 options = simplejson.load( open( sys.argv[1] ) )
|
|
46 output_bed = sys.argv[2]
|
|
47 output_extra_html = sys.argv[3]
|
|
48 output_extra_path = sys.argv[4]
|
|
49
|
|
50 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
|
|
51 cmdline = "macs -t %s" % ",".join( options['input_chipseq'] )
|
|
52 if options['input_control']:
|
|
53 cmdline = "%s -c %s" % ( cmdline, ",".join( options['input_control'] ) )
|
|
54 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'] )
|
|
55 if 'wig' in options:
|
|
56 wigextend = int( options['wig']['wigextend'] )
|
|
57 if wigextend >= 0:
|
|
58 wigextend = "--wigextend='%s'" % wigextend
|
|
59 else:
|
|
60 wigextend = ''
|
|
61 cmdline = "%s --wig %s --space='%s'" % ( cmdline, wigextend, options['wig']['space'] )
|
|
62 if 'nomodel' in options:
|
|
63 cmdline = "%s --nomodel --shiftsize='%s'" % ( cmdline, options['nomodel'] )
|
|
64 if 'diag' in options:
|
|
65 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'] )
|
|
66
|
|
67 tmp_dir = tempfile.mkdtemp() #macs makes very messy output, need to contain it into a temp dir, then provide to user
|
|
68 stderr_name = tempfile.NamedTemporaryFile().name # redirect stderr here, macs provides lots of info via stderr, make it into a report
|
|
69 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
|
|
70 proc.wait()
|
|
71 #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
|
|
72 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
|
|
73 if proc.returncode:
|
|
74 stderr_f = open( stderr_name )
|
|
75 while True:
|
|
76 chunk = stderr_f.read( CHUNK_SIZE )
|
|
77 if not chunk:
|
|
78 stderr_f.close()
|
|
79 break
|
|
80 sys.stderr.write( chunk )
|
|
81
|
|
82 #run R to create pdf from model script
|
|
83 if os.path.exists( os.path.join( tmp_dir, "%s_model.r" % experiment_name ) ):
|
|
84 cmdline = 'R --vanilla --slave < "%s_model.r" > "%s_model.r.log"' % ( experiment_name, experiment_name )
|
|
85 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir )
|
|
86 proc.wait()
|
|
87
|
|
88
|
|
89 #move bed out to proper output file
|
|
90 created_bed_name = os.path.join( tmp_dir, "%s_peaks.bed" % experiment_name )
|
|
91 if os.path.exists( created_bed_name ):
|
|
92 shutil.move( created_bed_name, output_bed )
|
|
93
|
|
94 #parse xls files to interval files as needed
|
|
95 if options['xls_to_interval']:
|
|
96 create_peak_xls_file = os.path.join( tmp_dir, '%s_peaks.xls' % experiment_name )
|
|
97 if os.path.exists( create_peak_xls_file ):
|
|
98 xls_to_interval( create_peak_xls_file, options['xls_to_interval']['peaks_file'], header = 'peaks file' )
|
|
99 create_peak_xls_file = os.path.join( tmp_dir, '%s_negative_peaks.xls' % experiment_name )
|
|
100 if os.path.exists( create_peak_xls_file ):
|
|
101 xls_to_interval( create_peak_xls_file, options['xls_to_interval']['negative_peaks_file'], header = 'negative peaks file' )
|
|
102
|
|
103 #merge and move wig files as needed, delete gz'd files and remove emptied dirs
|
|
104 if 'wig' in options:
|
|
105 wig_base_dir = os.path.join( tmp_dir, "%s_MACS_wiggle" % experiment_name )
|
|
106 if os.path.exists( wig_base_dir ):
|
|
107 #treatment
|
|
108 treatment_dir = os.path.join( wig_base_dir, "treat" )
|
|
109 if os.path.exists( treatment_dir ):
|
|
110 gunzip_cat_glob_path( os.path.join( treatment_dir, "*.wig.gz" ), options['wig']['output_treatment_file'], delete = True )
|
|
111 os.rmdir( treatment_dir )
|
|
112 #control
|
|
113 if options['input_control']:
|
|
114 control_dir = os.path.join( wig_base_dir, "control" )
|
|
115 if os.path.exists( control_dir ):
|
|
116 gunzip_cat_glob_path( os.path.join( control_dir, "*.wig.gz" ), options['wig']['output_control_file'], delete = True )
|
|
117 os.rmdir( control_dir )
|
|
118 os.rmdir( wig_base_dir )
|
|
119
|
|
120 #move all remaining files to extra files path of html file output to allow user download
|
|
121 out_html = open( output_extra_html, 'wb' )
|
|
122 out_html.write( '<html><head><title>Additional output created by MACS (%s)</title></head><body><h3>Additional Files:</h3><p><ul>\n' % experiment_name )
|
|
123 os.mkdir( output_extra_path )
|
|
124 for filename in sorted( os.listdir( tmp_dir ) ):
|
|
125 shutil.move( os.path.join( tmp_dir, filename ), os.path.join( output_extra_path, filename ) )
|
|
126 out_html.write( '<li><a href="%s">%s</a></li>\n' % ( filename, filename ) )
|
|
127 out_html.write( '</ul></p>\n' )
|
|
128 out_html.write( '<h3>Messages from MACS:</h3>\n<p><pre>%s</pre></p>\n' % open( stderr_name, 'rb' ).read() )
|
|
129 out_html.write( '</body></html>\n' )
|
|
130 out_html.close()
|
|
131
|
|
132 os.unlink( stderr_name )
|
|
133 os.rmdir( tmp_dir )
|
|
134
|
|
135 if __name__ == "__main__": main()
|