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(1)
|
|
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( '<li><a href="%s"><img src="%s" alt="" height="80" width="80">%s</a></li>\n' % ( fname, fname , fname ) )
|
|
18 html_out.write( '</ul>\n</body>\n</html>\n' )
|
|
19
|
|
20 def __main__():
|
|
21 #Parse Command Line
|
|
22 parser = optparse.OptionParser()
|
|
23
|
|
24 # wrapper options
|
|
25 parser.add_option('', '--r-script', dest='r_script', help='R script')
|
|
26 parser.add_option('', '--html-report-from-directory', dest='html_report_from_directory', type="string", nargs=2, help='"Target HTML File" "Directory"')
|
|
27
|
|
28 (options, args) = parser.parse_args()
|
|
29
|
|
30 (html_filename, html_dir) = options.html_report_from_directory
|
|
31
|
|
32 # Make html report directory for output.
|
|
33 os.mkdir( html_dir )
|
|
34
|
|
35 # Make a tmp dir
|
|
36 tmp_dir = tempfile.mkdtemp( prefix='tmp-cummeRbund-' )
|
|
37
|
|
38 # Build command.
|
|
39 cmd = ( "Rscript --vanilla %s" % options.r_script )
|
|
40
|
|
41 # Debugging.
|
|
42 # print cmd
|
|
43
|
|
44 #liubo added, for test, look at the generated R script
|
|
45 # shutil.copy2(options.r_script, '/nfs/software/galaxy/r_script')
|
|
46
|
|
47
|
|
48 # Run command.
|
|
49 try:
|
|
50 tmp_name = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
|
|
51 tmp_stderr = open( tmp_name, 'wb' )
|
|
52 proc = subprocess.Popen( args=cmd, shell=True, cwd=html_dir, stderr=tmp_stderr.fileno() )
|
|
53 returncode = proc.wait()
|
|
54 tmp_stderr.close()
|
|
55
|
|
56 # Get stderr, allowing for case where it's very large.
|
|
57 tmp_stderr = open( tmp_name, 'rb' )
|
|
58 stderr = ''
|
|
59 buffsize = 1048576
|
|
60 try:
|
|
61 while True:
|
|
62 stderr += tmp_stderr.read( buffsize )
|
|
63 if not stderr or len( stderr ) % buffsize != 0:
|
|
64 break
|
|
65 except OverflowError:
|
|
66 pass
|
|
67 tmp_stderr.close()
|
|
68
|
|
69 # Error checking.
|
|
70 if returncode != 0:
|
|
71 raise Exception, stderr
|
|
72 except Exception, e:
|
|
73 stop_err( 'Error running R script. ' + str( e ) )
|
|
74
|
|
75 # write the html report
|
|
76 html_report_from_directory( open( html_filename, 'wb' ), html_dir )
|
|
77
|
|
78 # Clean up temp dirs
|
|
79 if os.path.exists( tmp_dir ):
|
|
80 shutil.rmtree( tmp_dir )
|
|
81
|
|
82 if __name__=="__main__": __main__()
|