annotate cummerbund_wrapper.py @ 16:a29a35968ddb default tip

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