annotate cummerbund_wrapper.py @ 8:fbbbc9fd8fb9

Use fasta_indexes tool_data_table
author Jim Johnson <jj@umn.edu>
date Mon, 13 Oct 2014 09:29:20 -0500
parents fdf01b3c1841
children
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 ) ):
2
fdf01b3c1841 Update to new cuffdiff wrapper, add cuffdb_info.txt to cummerbund html output
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
16 if fname.endswith(".txt"):
fdf01b3c1841 Update to new cuffdiff wrapper, add cuffdb_info.txt to cummerbund html output
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
17 html_out.write( '<li><a href="%s">%s</a></li>\n' % ( fname, fname ) )
fdf01b3c1841 Update to new cuffdiff wrapper, add cuffdb_info.txt to cummerbund html output
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
18 else:
fdf01b3c1841 Update to new cuffdiff wrapper, add cuffdb_info.txt to cummerbund html output
Jim Johnson <jj@umn.edu>
parents: 0
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
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
20 html_out.write( '</ul>\n</body>\n</html>\n' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
21
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
22 def __main__():
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
23 #Parse Command Line
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
24 parser = optparse.OptionParser()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
25
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
26 # wrapper options
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
27 parser.add_option('', '--r-script', dest='r_script', help='R script')
da7241f92ecf 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"')
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
29
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
30 (options, args) = parser.parse_args()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
31
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
32 (html_filename, html_dir) = options.html_report_from_directory
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
33
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
34 # Make html report directory for output.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
35 os.mkdir( html_dir )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
36
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
37 # Make a tmp dir
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
38 tmp_dir = tempfile.mkdtemp( prefix='tmp-cummeRbund-' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
39
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
40 # Build command.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
41 cmd = ( "Rscript --vanilla %s" % options.r_script )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
42
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
43 # Debugging.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
44 # print cmd
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
45
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
46 #liubo added, for test, look at the generated R script
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
47 # shutil.copy2(options.r_script, '/nfs/software/galaxy/r_script')
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
48
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
49
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
50 # Run command.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
51 try:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
52 tmp_name = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
53 tmp_stderr = open( tmp_name, 'wb' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
54 proc = subprocess.Popen( args=cmd, shell=True, cwd=html_dir, stderr=tmp_stderr.fileno() )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
55 returncode = proc.wait()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
56 tmp_stderr.close()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
57
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
58 # Get stderr, allowing for case where it's very large.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
59 tmp_stderr = open( tmp_name, 'rb' )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
60 stderr = ''
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
61 buffsize = 1048576
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
62 try:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
63 while True:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
64 stderr += tmp_stderr.read( buffsize )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
65 if not stderr or len( stderr ) % buffsize != 0:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
66 break
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
67 except OverflowError:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
68 pass
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
69 tmp_stderr.close()
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
70
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
71 # Error checking.
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
72 if returncode != 0:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
73 raise Exception, stderr
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
74 except Exception, e:
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
75 stop_err( 'Error running R script. ' + str( e ) )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
76
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
77 # write the html report
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
78 html_report_from_directory( open( html_filename, 'wb' ), html_dir )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
79
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
80 # Clean up temp dirs
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
81 if os.path.exists( tmp_dir ):
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
82 shutil.rmtree( tmp_dir )
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
83
da7241f92ecf Uploaded
jjohnson
parents:
diff changeset
84 if __name__=="__main__": __main__()