annotate cummerbund_wrapper.py @ 0:da7241f92ecf

Uploaded
author jjohnson
date Mon, 04 Feb 2013 19:50:25 -0500
parents
children fdf01b3c1841
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
2
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
3 ### Runs "r_script" and generates a HTML report
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
4 ### Inspired on cuffdiff_wrapper.py and gatk_wrapper.py
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
5 ### Carlos Borroto <carlos.borroto@gmail.com>
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
6
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
7 import optparse, os, shutil, subprocess, sys, tempfile
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
8
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
9 def stop_err( msg ):
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
10 sys.stderr.write( "%s\n" % msg )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
11 sys.exit(1)
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
12
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
13 def html_report_from_directory( html_out, dir ):
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
14 html_out.write( '<html>\n<head>\n<title>Galaxy - cummeRbund Output</title>\n</head>\n<body>\n<p/>\n<ul>\n' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
15 for fname in sorted( os.listdir( dir ) ):
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
16 # html_out.write( '<li><a href="%s">%s</a></li>\n' % ( fname, fname ) )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
17 html_out.write( '<li><a href="%s"><img src="%s" alt="" height="80" width="80">%s</a></li>\n' % ( fname, fname , fname ) )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
18 html_out.write( '</ul>\n</body>\n</html>\n' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
19
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
20 def __main__():
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
21 #Parse Command Line
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
22 parser = optparse.OptionParser()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
23
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
24 # wrapper options
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
25 parser.add_option('', '--r-script', dest='r_script', help='R script')
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
26 parser.add_option('', '--html-report-from-directory', dest='html_report_from_directory', type="string", nargs=2, help='"Target HTML File" "Directory"')
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
27
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
28 (options, args) = parser.parse_args()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
29
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
30 (html_filename, html_dir) = options.html_report_from_directory
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
31
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
32 # Make html report directory for output.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
33 os.mkdir( html_dir )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
34
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
35 # Make a tmp dir
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
36 tmp_dir = tempfile.mkdtemp( prefix='tmp-cummeRbund-' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
37
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
38 # Build command.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
39 cmd = ( "Rscript --vanilla %s" % options.r_script )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
40
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
41 # Debugging.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
42 # print cmd
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
43
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
44 #liubo added, for test, look at the generated R script
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
45 # shutil.copy2(options.r_script, '/nfs/software/galaxy/r_script')
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
46
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
47
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
48 # Run command.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
49 try:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
50 tmp_name = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
51 tmp_stderr = open( tmp_name, 'wb' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
52 proc = subprocess.Popen( args=cmd, shell=True, cwd=html_dir, stderr=tmp_stderr.fileno() )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
53 returncode = proc.wait()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
54 tmp_stderr.close()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
55
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
56 # Get stderr, allowing for case where it's very large.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
57 tmp_stderr = open( tmp_name, 'rb' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
58 stderr = ''
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
59 buffsize = 1048576
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
60 try:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
61 while True:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
62 stderr += tmp_stderr.read( buffsize )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
63 if not stderr or len( stderr ) % buffsize != 0:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
64 break
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
65 except OverflowError:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
66 pass
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
67 tmp_stderr.close()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
68
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
69 # Error checking.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
70 if returncode != 0:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
71 raise Exception, stderr
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
72 except Exception, e:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
73 stop_err( 'Error running R script. ' + str( e ) )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
74
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
75 # write the html report
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
76 html_report_from_directory( open( html_filename, 'wb' ), html_dir )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
77
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
78 # Clean up temp dirs
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
79 if os.path.exists( tmp_dir ):
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
80 shutil.rmtree( tmp_dir )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
81
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
82 if __name__=="__main__": __main__()