comparison makeTagDirectory.py @ 23:71e1c2e46752 draft

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