# HG changeset patch # User stemcellcommons # Date 1382028469 14400 # Node ID 642c0da30ca61f45da48c9bd533a7cbf0d034825 Initial upload. diff -r 000000000000 -r 642c0da30ca6 macs2_wrapper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macs2_wrapper.py Thu Oct 17 12:47:49 2013 -0400 @@ -0,0 +1,177 @@ +#purpose: macs2 python wrapper +#author: Ziru Zhou +#date: November, 2012 + +import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip +from galaxy import eggs +import pkg_resources +pkg_resources.require( "simplejson" ) +import simplejson + +CHUNK_SIZE = 1024 + +#========================================================================================== +#functions +#========================================================================================== +def gunzip_cat_glob_path( glob_path, target_filename, delete = False ): + out = open( target_filename, 'wb' ) + for filename in glob.glob( glob_path ): + fh = gzip.open( filename, 'rb' ) + while True: + data = fh.read( CHUNK_SIZE ) + if data: + out.write( data ) + else: + break + fh.close() + if delete: + os.unlink( filename ) + out.close() + +def xls_to_interval( xls_file, interval_file, header = None ): + out = open( interval_file, 'wb' ) + if header: + out.write( '#%s\n' % header ) + wrote_header = False + #From macs readme: Coordinates in XLS is 1-based which is different with BED format. + for line in open( xls_file ): + #keep all existing comment lines + if line.startswith( '#' ): + out.write( line ) + #added for macs2 since there is an extra newline + elif line.startswith( '\n' ): + out.write( line ) + elif not wrote_header: + out.write( '#%s' % line ) + print line + wrote_header = True + else: + fields = line.split( '\t' ) + if len( fields ) > 1: + fields[1] = str( int( fields[1] ) - 1 ) + out.write( '\t'.join( fields ) ) + out.close() + +#========================================================================================== +#main +#========================================================================================== +def main(): + #take in options file and output file names + options = simplejson.load( open( sys.argv[1] ) ) + outputs = simplejson.load( open( sys.argv[2] ) ) + + #================================================================================= + #parse options and execute macs2 + #================================================================================= + #default inputs that are in every major command + experiment_name = '_'.join( options['experiment_name'].split() ) #save experiment name here, it will be used by macs for some file names + cmdline = "macs2 %s -t %s" % ( options['command'], ",".join( options['input_chipseq'] ) ) + if options['input_control']: + cmdline = "%s -c %s" % ( cmdline, ",".join( options['input_control'] ) ) + + #================================================================================= + if (options['command'] == "callpeak"): + output_bed = outputs['output_bed_file'] + output_extra_html = outputs['output_extra_file'] + output_extra_path = outputs['output_extra_file_path'] + output_peaks = outputs['output_peaks_file'] + output_narrowpeaks = outputs['output_narrowpeaks_file'] + output_xls_to_interval_peaks_file = outputs['output_xls_to_interval_peaks_file'] + output_xls_to_interval_negative_peaks_file = outputs['output_xls_to_interval_negative_peaks_file'] + + if 'pvalue' in options: + cmdline = "%s --format='%s' --name='%s' --gsize='%s' --bw='%s' --pvalue='%s' --mfold %s %s %s %s" % ( cmdline, options['format'], experiment_name, options['gsize'], options['bw'], options['pvalue'], options['mfoldlo'], options['mfoldhi'], options['nolambda'], options['bdg'] ) + elif 'qvalue' in options: + cmdline = "%s --format='%s' --name='%s' --gsize='%s' --bw='%s' --qvalue='%s' --mfold %s %s %s %s" % ( cmdline, options['format'], experiment_name, options['gsize'], options['bw'], options['qvalue'], options['mfoldlo'], options['mfoldhi'], options['nolambda'], options['bdg'] ) + + if 'nomodel' in options: + cmdline = "%s --nomodel --shiftsize='%s'" % ( cmdline, options['nomodel'] ) + #================================================================================= + if (options['command'] == "bdgcmp"): + output_bdgcmp = outputs['output_bdgcmp_file'] + + cmdline = "%s -m %s -p %s -o bdgcmp_out.bdg" % ( cmdline, options['m'], options['pseudocount'] ) + #================================================================================= + + tmp_dir = tempfile.mkdtemp() #macs makes very messy output, need to contain it into a temp dir, then provide to user + stderr_name = tempfile.NamedTemporaryFile().name # redirect stderr here, macs provides lots of info via stderr, make it into a report + proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) ) + proc.wait() + #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 + #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup + if proc.returncode: + stderr_f = open( stderr_name ) + while True: + chunk = stderr_f.read( CHUNK_SIZE ) + if not chunk: + stderr_f.close() + break + sys.stderr.write( chunk ) + + #================================================================================= + #copy files created by macs2 to appripriate directory with the provided names + #================================================================================= + + #================================================================================= + #move files generated by callpeak command + if (options['command'] == "callpeak"): + #run R to create pdf from model script + if os.path.exists( os.path.join( tmp_dir, "%s_model.r" % experiment_name ) ): + cmdline = 'R --vanilla --slave < "%s_model.r" > "%s_model.r.log"' % ( experiment_name, experiment_name ) + proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir ) + proc.wait() + + #move bed out to proper output file + created_bed_name = os.path.join( tmp_dir, "%s_peaks.bed" % experiment_name ) + if os.path.exists( created_bed_name ): + shutil.move( created_bed_name, output_bed ) + + #OICR peak_xls file + created_peak_xls_file = os.path.join( tmp_dir, "%s_peaks.xls" % experiment_name ) + if os.path.exists( created_peak_xls_file ): + # shutil.copy( created_peak_xls_file, os.path.join ( "/mnt/galaxyData/tmp/", "%s_peaks.xls" % ( os.path.basename(output_extra_path) ))) + shutil.copyfile( created_peak_xls_file, output_peaks ) + + #peaks.encodepeaks (narrowpeaks) file + created_narrowpeak_file = os.path.join (tmp_dir, "%s_peaks.encodePeak" % experiment_name ) + if os.path.exists( created_narrowpeak_file ): + shutil.move (created_narrowpeak_file, output_narrowpeaks ) + + #parse xls files to interval files as needed + #if 'xls_to_interval' in options: + if (options['xls_to_interval'] == "True"): + create_peak_xls_file = os.path.join( tmp_dir, '%s_peaks.xls' % experiment_name ) + if os.path.exists( create_peak_xls_file ): + xls_to_interval( create_peak_xls_file, output_xls_to_interval_peaks_file, header = 'peaks file' ) + create_peak_xls_file = os.path.join( tmp_dir, '%s_negative_peaks.xls' % experiment_name ) + if os.path.exists( create_peak_xls_file ): + print "negative file exists" + xls_to_interval( create_peak_xls_file, output_xls_to_interval_negative_peaks_file, header = 'negative peaks file' ) + + #move all remaining files to extra files path of html file output to allow user download + out_html = open( output_extra_html, 'wb' ) + out_html.write( 'Additional output created by MACS (%s)

Additional Files:

\n' ) + out_html.write( '

Messages from MACS:

\n

%s

\n' % open( stderr_name, 'rb' ).read() ) + out_html.write( '\n' ) + out_html.close() + + #================================================================================= + #move files generated by bdgcmp command + if (options['command'] == "bdgcmp"): + created_bdgcmp_file = os.path.join (tmp_dir, "bdgcmp_out.bdg" ) + if os.path.exists( created_bdgcmp_file ): + shutil.move (created_bdgcmp_file, output_bdgcmp ) + + #================================================================================= + #cleanup + #================================================================================= + os.unlink( stderr_name ) + os.rmdir( tmp_dir ) + +if __name__ == "__main__": main() diff -r 000000000000 -r 642c0da30ca6 macs2_wrapper.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macs2_wrapper.xml Thu Oct 17 12:47:49 2013 -0400 @@ -0,0 +1,228 @@ + + Model-based Analysis of ChIP-Seq + macs2_wrapper.py $options_file $outputs_file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + major_command['major_command_selector'] == 'callpeak' + + + major_command['major_command_selector'] == 'callpeak' + + + major_command['major_command_selector'] == 'callpeak' + + + major_command['major_command_selector'] == 'callpeak' + + + major_command['xls_to_interval'] is True + major_command['major_command_selector'] == 'callpeak' + + + major_command['xls_to_interval'] is True + major_command['input_control_file1'] is not None + major_command['major_command_selector'] == 'callpeak' + + + + major_command['major_command_selector'] == 'bdgcmp' + + + + + <% +import simplejson +%> +##======================================================================================= +#set $__outputs = { 'command':str( $major_command.major_command_selector ) } +#if str( $major_command.major_command_selector ) == 'callpeak': + #set $__outputs['output_bed_file'] = str( $output_bed_file ) + #set $__outputs['output_extra_file'] = str( $output_extra_files ) + #set $__outputs['output_extra_file_path'] = str( $output_extra_files.files_path ) + #set $__outputs['output_peaks_file'] = str( $output_peaks_file ) + #set $__outputs['output_narrowpeaks_file'] = str( $output_narrowpeaks_file ) + #set $__outputs['output_xls_to_interval_peaks_file'] = str( $output_xls_to_interval_peaks_file ) + #set $__outputs['output_xls_to_interval_negative_peaks_file'] = str( $output_xls_to_interval_negative_peaks_file ) +#end if +##======================================================================================= +#if str( $major_command.major_command_selector ) == 'bdgcmp': + #set $__outputs['output_bdgcmp_file'] = str( $output_bdgcmp_file ) +#end if + +${ simplejson.dumps( __outputs ) } + + <% +import simplejson +%> +##======================================================================================= +#set $__options = { 'experiment_name':str( $experiment_name ) } +##treatment/tag input files and format +#set $__options['input_chipseq'] = [ str( $major_command.input_chipseq_file1 ) ] +#set $__options['format'] = $major_command.input_chipseq_file1.extension.upper() + +##control/input files +#set $__options['input_control'] = [] +#if str( $major_command.input_control_file1 ) != 'None': + #set $_hole = __options['input_control'].append( str( $major_command.input_control_file1 ) ) +#end if + +#if str( $major_command.major_command_selector ) == 'callpeak': + #set $__options['command'] = str( "callpeak" ) + #set $__options['gsize'] = int( $major_command.gsize ) + #set $__options['bw'] = str( $major_command.bw ) + #set $__options['bdg'] = str( $major_command.bdg ) + #set $__options['xls_to_interval'] = str( $major_command.xls_to_interval ) + + ##advanced options + #if str( $major_command.advanced_options.advanced_options_selector ) == 'on': + #set $__options['mfoldlo'] = int( $major_command.advanced_options.mfoldlo ) + #set $__options['mfoldhi'] = int( $major_command.advanced_options.mfoldhi ) + #set $__options['nolambda'] = str( $major_command.advanced_options.nolambda ) + #else: + #set $__options['mfoldlo'] = int( "10" ) + #set $__options['mfoldhi'] = int( "30" ) + #set $__options['nolambda'] = str( "" ) + #end if + + ##enable xls file options + ##if str( $major_command.xls_to_interval ) == 'create': + ##set $__options['xls_to_interval'] = { 'peaks_file': str( $output_xls_to_interval_peaks_file ), 'negative_peaks_file': str( $output_xls_to_interval_negative_peaks_file ) } + ##end if + + ##pq value select options + #if str( $major_command.pq_options.pq_options_selector ) == 'qvalue': + #set $__options['qvalue'] = str( $major_command.pq_options.qvalue ) + #else: + #set $__options['pvalue'] = str( $major_command.pq_options.pvalue ) + #end if + + ##model options + #if str( $major_command.nomodel_type.nomodel_type_selector ) == 'nomodel': + #set $__options['nomodel'] = str( $major_command.nomodel_type.shiftsize ) + #end if +#end if +##======================================================================================= +#if str( $major_command.major_command_selector ) == 'bdgcmp': + #set $__options['command'] = str( "bdgcmp" ) + #set $__options['pseudocount'] = float( str( $major_command.pseudocount ) ) + #set $__options['m'] = str( $major_command.bdgcmp_options.bdgcmp_options_selector ) +#end if +##======================================================================================= + +${ simplejson.dumps( __options ) } + + + + + + +**What it does** + +With the improvement of sequencing techniques, chromatin immunoprecipitation followed by high throughput sequencing (ChIP-Seq) +is getting popular to study genome-wide protein-DNA interactions. To address the lack of powerful ChIP-Seq analysis method, we present a novel algorithm, named Model-based Analysis of ChIP-Seq (MACS), for +identifying transcript factor binding sites. MACS captures the influence of genome complexity to evaluate the significance of enriched ChIP regions, and MACS improves the spatial resolution of +binding sites through combining the information of both sequencing tag position and orientation. MACS can be easily used for ChIP-Seq data alone, or with control sample with the increase of specificity. + +View the original MACS2 documentation: https://github.com/taoliu/MACS/blob/master/README + +------ + +**Usage** + +**Peak Calling**: Main MACS2 Function to Call peaks from alignment results. + +**Compare .bdg files**: Deduct noise by comparing two signal tracks in bedGraph. + + +------ + +**Citation** + +For the underlying tool, please cite Zhang Y, Liu T, Meyer CA, Eeckhoute J, Johnson DS, Bernstein BE, Nusbaum C, Myers RM, Brown M, Li W, Liu XS. Model-based analysis of ChIP-Seq (MACS). Genome Biol. 2008;9(9):R137. + +Integration of MACS2 with Galaxy performed by Ziru Zhou ( ziruzhou@gmail.com ). Please send your comments/questions to modENCODE DCC at help@modencode.org. + +