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