annotate assembly_stats_txt.py @ 0:8d1c7f2a3f5c draft default tip

Uploaded
author aaronpetkau
date Sat, 04 Jul 2015 09:43:13 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
1 #!/usr/bin/env python
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
2 #Version 1.01 - bugs kindly corrected by Jan van Haarst
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
3 import pkg_resources
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
4 import logging, os, string, sys, tempfile, glob, shutil, types, urllib
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
5 import shlex, subprocess
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
6 from optparse import OptionParser, OptionGroup
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
7 from stat import *
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
8
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
9
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
10 log = logging.getLogger( __name__ )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
11
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
12 assert sys.version_info[:2] >= ( 2, 4 )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
13
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
14 def stop_err( msg ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
15 sys.stderr.write( "%s\n" % msg )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
16 sys.exit()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
17
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
18 def __main__():
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
19 #Parse Command Line
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
20 s = 'assembly_stats_txt.py: argv = %s\n' % (sys.argv)
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
21 argcnt = len(sys.argv)
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
22 html_file = sys.argv[1]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
23 working_dir = sys.argv[2]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
24 type = sys.argv[3]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
25 bucket = sys.argv[4]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
26 input = sys.argv[5]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
27 stats = sys.argv[6]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
28 sortedcontigs = sys.argv[7]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
29 histogrampng = sys.argv[8]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
30 summedcontigspng = sys.argv[9]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
31 histogramdata = sys.argv[10]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
32 summedcontigdata = sys.argv[11]
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
33 try: # for test - needs this done
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
34 os.makedirs(working_dir)
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
35 except Exception, e:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
36 stop_err( 'Error running assembly_stats_txt.py ' + str( e ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
37
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
38
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
39 cmdline = '%s/fasta_summary.pl -i %s -t %s %s -o %s > /dev/null' % (os.path.dirname(sys.argv[0]),input, type, bucket, working_dir)
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
40 try:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
41 proc = subprocess.Popen( args=cmdline, shell=True, stderr=subprocess.PIPE )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
42 returncode = proc.wait()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
43 # get stderr, allowing for case where it's very large
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
44 stderr = ''
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
45 buffsize = 1048576
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
46 try:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
47 while True:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
48 stderr += proc.stderr.read( buffsize )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
49 if not stderr or len( stderr ) % buffsize != 0:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
50 break
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
51 except OverflowError:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
52 pass
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
53 if returncode != 0:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
54 raise Exception, stderr
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
55 except Exception, e:
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
56 stop_err( 'Error running assembly_stats.py ' + str( e ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
57
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
58 stats_path = os.path.join(working_dir,'stats.txt')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
59 sorted_contigs_path = os.path.join(working_dir,'sorted_contigs.fa')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
60 histogram_png_path = os.path.join(working_dir,'histogram_bins.dat.png')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
61 summed_contigs_path = os.path.join(working_dir,'summed_contig_lengths.dat.png')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
62 histogram_data_path = os.path.join(working_dir,'histogram_bins.dat')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
63 summed_contigs_data_path = os.path.join(working_dir,'summed_contig_lengths.dat')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
64
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
65 out = open(stats,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
66 for line in open( stats_path ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
67 out.write( "%s" % (line) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
68 out.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
69
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
70 out = open(sortedcontigs,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
71 for line in open(sorted_contigs_path ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
72 out.write( "%s" % (line) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
73 out.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
74
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
75 out = open(histogrampng,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
76 for line in open(histogram_png_path ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
77 out.write( "%s" % (line) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
78 out.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
79
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
80 out = open(summedcontigspng,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
81 for line in open(summed_contigs_path ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
82 out.write( "%s" % (line) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
83 out.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
84
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
85
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
86 out = open(histogramdata,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
87 for line in open(histogram_data_path ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
88 out.write( "%s" % (line) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
89 out.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
90
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
91 out = open(summedcontigdata,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
92 for line in open(summed_contigs_data_path ):
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
93 out.write( "%s" % (line) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
94 out.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
95
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
96
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
97
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
98
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
99
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
100
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
101
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
102
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
103
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
104 # rval = ['<html><head><title>Assembly stats Galaxy Composite Dataset </title></head><p/>']
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
105 # rval.append('<div>%s<p/></div>' % (cmdline) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
106 # rval.append('<div>This composite dataset is composed of the following files:<p/><ul>')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
107 # rval.append( '<li><a href="%s" type="text/plain">%s </a>%s</li>' % (stats_path,'stats.txt','stats.txt' ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
108 # rval.append( '<li><a href="%s" type="text/plain">%s </a>%s</li>' % (sorted_contigs_path,'sorted_contigs.fa','sorted_contigs.fa' ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
109 # rval.append( '<li><a href="%s" type="image/png">%s </a>%s</li>' % (histogram_png_path,'histogram_bins.dat.png','histogram_bins.dat.png' ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
110 # rval.append( '<li><a href="%s" type="image/png">%s </a>%s</li>' % (summed_contigs_path,'summed_contig_lengths.dat.png','summed_contig_lengths.dat.png' ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
111 # rval.append( '<li><a href="%s" type="text/plain">%s </a>%s</li>' % (histogram_data_path,'histogram_bins.dat','histogram_bins.dat' ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
112 # rval.append( '<li><a href="%s" type="text/plain">%s </a>%s</li>' % (summed_contigs_data_path,'summed_contig_lengths.dat','summed_contig_lengths.dat' ) )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
113
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
114
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
115 #
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
116 # rval.append( '</ul></div></html>' )
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
117 # f = file(html_file,'w')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
118 # f.write("\n".join( rval ))
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
119 # f.write('\n')
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
120 # f.close()
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
121
8d1c7f2a3f5c Uploaded
aaronpetkau
parents:
diff changeset
122 if __name__ == "__main__": __main__()