annotate makeTagDirectory.py @ 25:b6ea27745c45 draft

Uploaded
author kevyin
date Thu, 20 Dec 2012 18:25:12 -0500
parents 74c1fc7bb164
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
1 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
2
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
3
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
4 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
5 import re
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
6 import os
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
7 import sys
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
8 import subprocess
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
9 import optparse
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
10 import shutil
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
11 import tempfile
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
12
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
13 def getFileString(fpath, outpath):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
14 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
15 format a nice file size string
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
16 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
17 size = ''
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
18 fp = os.path.join(outpath, fpath)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
19 s = '? ?'
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
20 if os.path.isfile(fp):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
21 n = float(os.path.getsize(fp))
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
22 if n > 2**20:
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
23 size = ' (%1.1f MB)' % (n/2**20)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
24 elif n > 2**10:
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
25 size = ' (%1.1f KB)' % (n/2**10)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
26 elif n > 0:
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
27 size = ' (%d B)' % (int(n))
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
28 s = '%s %s' % (fpath, size)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
29 return s
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
30
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
31 class makeTagDirectory():
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
32 """wrapper
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
33 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
34
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
35 def __init__(self,opts=None, args=None):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
36 self.opts = opts
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
37 self.args = args
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
38
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
39 def run_makeTagDirectory(self):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
40 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
41 makeTagDirectory <Output Directory Name> [options] <alignment file1> [alignment file 2]
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
42
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
43 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
44 if self.opts.format != "bam":
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
45 cl = [self.opts.executable] + args + ["-format" , self.opts.format]
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
46 else:
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
47 cl = [self.opts.executable] + args
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
48 print cl
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
49 p = subprocess.Popen(cl)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
50 retval = p.wait()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
51
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
52
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
53 html = self.gen_html(args[0])
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
54 #html = self.gen_html()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
55 return html,retval
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
56
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
57 def gen_html(self, dr=os.getcwd()):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
58 flist = os.listdir(dr)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
59 print flist
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
60 """ add a list of all files in the tagdirectory
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
61 """
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
62 res = ['<div class="module"><h2>Files created by makeTagDirectory</h2><table cellspacing="2" cellpadding="2">\n']
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
63
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
64 flist.sort()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
65 for i,f in enumerate(flist):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
66 if not(os.path.isdir(f)):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
67 fn = os.path.split(f)[-1]
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
68 res.append('<tr><td><a href="%s">%s</a></td></tr>\n' % (fn,getFileString(fn, dr)))
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
69
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
70 res.append('</table>\n')
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
71
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
72 return res
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
73
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
74 if __name__ == '__main__':
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
75 op = optparse.OptionParser()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
76 op.add_option('-e', '--executable', default='makeTagDirectory')
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
77 op.add_option('-o', '--htmloutput', default=None)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
78 op.add_option('-f', '--format', default="sam")
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
79 opts, args = op.parse_args()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
80 #assert os.path.isfile(opts.executable),'## makeTagDirectory.py error - cannot find executable %s' % opts.executable
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
81
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
82 #if not os.path.exists(opts.outputdir):
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
83 #os.makedirs(opts.outputdir)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
84 f = makeTagDirectory(opts, args)
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
85
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
86 html,retval = f.run_makeTagDirectory()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
87 f = open(opts.htmloutput, 'w')
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
88 f.write(''.join(html))
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
89 f.close()
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
90 if retval <> 0:
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
91 print >> sys.stderr, serr # indicate failure
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
92
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
93
74c1fc7bb164 display tag directories with html
kevyin
parents:
diff changeset
94