annotate cummerbund_wrapper.py @ 1:65940895e1c6

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