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