# HG changeset patch # User cjav # Date 1329238736 18000 # Node ID 65940895e1c69b6aac286605c7e5439b0ed9f828 # Parent 6e2abd76a853b0e120221144e732784b6dfeeb52 Uploaded diff -r 6e2abd76a853 -r 65940895e1c6 cummerbund_wrapper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cummerbund_wrapper.py Tue Feb 14 11:58:56 2012 -0500 @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +### Runs "r_script" and generates a HTML report +### Inspired on cuffdiff_wrapper.py and gatk_wrapper.py +### Carlos Borroto + +import optparse, os, shutil, subprocess, sys, tempfile + +def stop_err( msg ): + sys.stderr.write( "%s\n" % msg ) + sys.exit() + +def html_report_from_directory( html_out, dir ): + html_out.write( '\n\nGalaxy - cummeRbund Output\n\n\n

\n

\n\n\n' ) + +def __main__(): + #Parse Command Line + parser = optparse.OptionParser() + + # wrapper options + parser.add_option('', '--r-script', dest='r_script', help='R script') + parser.add_option('', '--html-report-from-directory', dest='html_report_from_directory', type="string", nargs=2, help='"Target HTML File" "Directory"') + + (options, args) = parser.parse_args() + + (html_filename, html_dir) = options.html_report_from_directory + + # Make html report directory for output. + os.mkdir( html_dir ) + + # Make a tmp dir + tmp_dir = tempfile.mkdtemp( prefix='tmp-cummeRbund-' ) + + # Build command. + cmd = ( "Rscript --vanilla %s" % options.r_script ) + + # Debugging. + print cmd + + # Run command. + try: + tmp_name = tempfile.NamedTemporaryFile( dir=tmp_dir ).name + tmp_stderr = open( tmp_name, 'wb' ) + proc = subprocess.Popen( args=cmd, shell=True, cwd=html_dir, stderr=tmp_stderr.fileno() ) + returncode = proc.wait() + tmp_stderr.close() + + # Get stderr, allowing for case where it's very large. + tmp_stderr = open( tmp_name, 'rb' ) + stderr = '' + buffsize = 1048576 + try: + while True: + stderr += tmp_stderr.read( buffsize ) + if not stderr or len( stderr ) % buffsize != 0: + break + except OverflowError: + pass + tmp_stderr.close() + + # Error checking. + if returncode != 0: + raise Exception, stderr + except Exception, e: + stop_err( 'Error running R script. ' + str( e ) ) + + # write the html report + html_report_from_directory( open( html_filename, 'wb' ), html_dir ) + + # Clean up temp dirs + if os.path.exists( tmp_dir ): + shutil.rmtree( tmp_dir ) + +if __name__=="__main__": __main__()