annotate picard_wrapper.py @ 2:93ace7e49295 draft

Uploaded valid tools.
author devteam
date Fri, 21 Feb 2014 12:06:18 -0500
parents ff4ec13e496e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
1 #!/usr/bin/env python
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
2 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
3 Originally written by Kelly Vincent
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
4 pretty output and additional picard wrappers by Ross Lazarus for rgenetics
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
5 Runs all available wrapped Picard tools.
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
6 usage: picard_wrapper.py [options]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
7 code Ross wrote licensed under the LGPL
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
8 see http://www.gnu.org/copyleft/lesser.html
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
9 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
10
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
11 import optparse, os, sys, subprocess, tempfile, shutil, time, logging
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
12
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
13 galhtmlprefix = """<?xml version="1.0" encoding="utf-8" ?>
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
14 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
15 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
16 <head>
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
17 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
18 <meta name="generator" content="Galaxy %s tool output - see http://getgalaxy.org/" />
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
19 <title></title>
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
20 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
21 </head>
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
22 <body>
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
23 <div class="document">
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
24 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
25 galhtmlattr = """Galaxy tool %s run at %s</b><br/>"""
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
26 galhtmlpostfix = """</div></body></html>\n"""
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
27
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
28
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
29 def stop_err( msg ):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
30 sys.stderr.write( '%s\n' % msg )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
31 sys.exit()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
32
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
33
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
34 def timenow():
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
35 """return current time as a string
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
36 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
37 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
38
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
39
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
40 class PicardBase():
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
41 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
42 simple base class with some utilities for Picard
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
43 adapted and merged with Kelly Vincent's code april 2011 Ross
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
44 lots of changes...
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
45 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
46
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
47 def __init__(self, opts=None,arg0=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
48 """ common stuff needed at init for a picard tool
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
49 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
50 assert opts <> None, 'PicardBase needs opts at init'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
51 self.opts = opts
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
52 if self.opts.outdir == None:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
53 self.opts.outdir = os.getcwd() # fixmate has no html file eg so use temp dir
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
54 assert self.opts.outdir <> None,'## PicardBase needs a temp directory if no output directory passed in'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
55 self.picname = self.baseName(opts.jar)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
56 if self.picname.startswith('picard'):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
57 self.picname = opts.picard_cmd # special case for some tools like replaceheader?
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
58 self.progname = self.baseName(arg0)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
59 self.version = '0.002'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
60 self.delme = [] # list of files to destroy
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
61 self.title = opts.title
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
62 self.inputfile = opts.input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
63 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
64 os.makedirs(opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
65 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
66 pass
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
67 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
68 os.makedirs(opts.tmpdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
69 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
70 pass
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
71 self.log_filename = os.path.join(self.opts.outdir,'%s.log' % self.picname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
72 self.metricsOut = os.path.join(opts.outdir,'%s.metrics.txt' % self.picname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
73 self.setLogging(logfname=self.log_filename)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
74
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
75 def baseName(self,name=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
76 return os.path.splitext(os.path.basename(name))[0]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
77
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
78 def setLogging(self,logfname="picard_wrapper.log"):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
79 """setup a logger
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
80 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
81 logging.basicConfig(level=logging.INFO,
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
82 filename=logfname,
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
83 filemode='a')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
84
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
85
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
86 def readLarge(self,fname=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
87 """ read a potentially huge file.
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
88 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
89 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
90 # get stderr, allowing for case where it's very large
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
91 tmp = open( fname, 'rb' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
92 s = ''
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
93 buffsize = 1048576
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
94 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
95 while True:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
96 more = tmp.read( buffsize )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
97 if len(more) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
98 s += more
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
99 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
100 break
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
101 except OverflowError:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
102 pass
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
103 tmp.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
104 except Exception, e:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
105 stop_err( 'Read Large Exception : %s' % str( e ) )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
106 return s
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
107
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
108 def runCL(self,cl=None,output_dir=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
109 """ construct and run a command line
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
110 we have galaxy's temp path as opt.temp_dir so don't really need isolation
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
111 sometimes stdout is needed as the output - ugly hacks to deal with potentially vast artifacts
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
112 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
113 assert cl <> None, 'PicardBase runCL needs a command line as cl'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
114 if output_dir == None:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
115 output_dir = self.opts.outdir
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
116 if type(cl) == type([]):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
117 cl = ' '.join(cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
118 fd,templog = tempfile.mkstemp(dir=output_dir,suffix='rgtempRun.txt')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
119 tlf = open(templog,'wb')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
120 fd,temperr = tempfile.mkstemp(dir=output_dir,suffix='rgtempErr.txt')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
121 tef = open(temperr,'wb')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
122 process = subprocess.Popen(cl, shell=True, stderr=tef, stdout=tlf, cwd=output_dir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
123 rval = process.wait()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
124 tlf.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
125 tef.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
126 stderrs = self.readLarge(temperr)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
127 stdouts = self.readLarge(templog)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
128 if rval > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
129 s = '## executing %s returned status %d and stderr: \n%s\n' % (cl,rval,stderrs)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
130 stdouts = '%s\n%s' % (stdouts,stderrs)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
131 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
132 s = '## executing %s returned status %d and nothing on stderr\n' % (cl,rval)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
133 logging.info(s)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
134 os.unlink(templog) # always
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
135 os.unlink(temperr) # always
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
136 return s, stdouts, rval # sometimes s is an output
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
137
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
138 def runPic(self, jar, cl):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
139 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
140 cl should be everything after the jar file name in the command
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
141 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
142 runme = ['java -Xmx%s' % self.opts.maxjheap]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
143 runme.append(" -Djava.io.tmpdir='%s' " % self.opts.tmpdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
144 runme.append('-jar %s' % jar)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
145 runme += cl
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
146 s,stdouts,rval = self.runCL(cl=runme, output_dir=self.opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
147 return stdouts,rval
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
148
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
149 def samToBam(self,infile=None,outdir=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
150 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
151 use samtools view to convert sam to bam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
152 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
153 fd,tempbam = tempfile.mkstemp(dir=outdir,suffix='rgutilsTemp.bam')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
154 cl = ['samtools view -h -b -S -o ',tempbam,infile]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
155 tlog,stdouts,rval = self.runCL(cl,outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
156 return tlog,tempbam,rval
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
157
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
158 def sortSam(self, infile=None,outfile=None,outdir=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
159 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
160 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
161 print '## sortSam got infile=%s,outfile=%s,outdir=%s' % (infile,outfile,outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
162 cl = ['samtools sort',infile,outfile]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
163 tlog,stdouts,rval = self.runCL(cl,outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
164 return tlog
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
165
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
166 def cleanup(self):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
167 for fname in self.delme:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
168 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
169 os.unlink(fname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
170 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
171 pass
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
172
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
173 def prettyPicout(self,transpose,maxrows):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
174 """organize picard outpouts into a report html page
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
175 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
176 res = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
177 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
178 r = open(self.metricsOut,'r').readlines()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
179 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
180 r = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
181 if len(r) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
182 res.append('<b>Picard on line resources</b><ul>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
183 res.append('<li><a href="http://picard.sourceforge.net/index.shtml">Click here for Picard Documentation</a></li>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
184 res.append('<li><a href="http://picard.sourceforge.net/picard-metric-definitions.shtml">Click here for Picard Metrics definitions</a></li></ul><hr/>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
185 if transpose:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
186 res.append('<b>Picard output (transposed to make it easier to see)</b><hr/>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
187 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
188 res.append('<b>Picard output</b><hr/>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
189 res.append('<table cellpadding="3" >\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
190 dat = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
191 heads = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
192 lastr = len(r) - 1
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
193 # special case for estimate library complexity hist
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
194 thist = False
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
195 for i,row in enumerate(r):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
196 if row.strip() > '':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
197 srow = row.split('\t')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
198 if row.startswith('#'):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
199 heads.append(row.strip()) # want strings
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
200 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
201 dat.append(srow) # want lists
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
202 if row.startswith('## HISTOGRAM'):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
203 thist = True
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
204 if len(heads) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
205 hres = ['<tr class="d%d"><td colspan="2">%s</td></tr>' % (i % 2,x) for i,x in enumerate(heads)]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
206 res += hres
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
207 heads = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
208 if len(dat) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
209 if transpose and not thist:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
210 tdat = map(None,*dat) # transpose an arbitrary list of lists
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
211 tdat = ['<tr class="d%d"><td>%s</td><td>%s&nbsp;</td></tr>\n' % ((i+len(heads)) % 2,x[0],x[1]) for i,x in enumerate(tdat)]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
212 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
213 tdat = ['\t'.join(x).strip() for x in dat] # back to strings :(
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
214 tdat = ['<tr class="d%d"><td colspan="2">%s</td></tr>\n' % ((i+len(heads)) % 2,x) for i,x in enumerate(tdat)]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
215 res += tdat
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
216 dat = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
217 res.append('</table>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
218 return res
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
219
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
220 def fixPicardOutputs(self,transpose,maxloglines):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
221 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
222 picard produces long hard to read tab header files
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
223 make them available but present them transposed for readability
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
224 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
225 logging.shutdown()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
226 self.cleanup() # remove temp files stored in delme
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
227 rstyle="""<style type="text/css">
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
228 tr.d0 td {background-color: oldlace; color: black;}
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
229 tr.d1 td {background-color: aliceblue; color: black;}
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
230 </style>"""
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
231 res = [rstyle,]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
232 res.append(galhtmlprefix % self.progname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
233 res.append(galhtmlattr % (self.picname,timenow()))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
234 flist = [x for x in os.listdir(self.opts.outdir) if not x.startswith('.')]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
235 pdflist = [x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf']
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
236 if len(pdflist) > 0: # assumes all pdfs come with thumbnail .jpgs
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
237 for p in pdflist:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
238 pbase = os.path.splitext(p)[0] # removes .pdf
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
239 imghref = '%s.jpg' % pbase
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
240 mimghref = '%s-0.jpg' % pbase # multiple pages pdf -> multiple thumbnails without asking!
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
241 if mimghref in flist:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
242 imghref=mimghref # only one for thumbnail...it's a multi page pdf
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
243 res.append('<table cellpadding="10"><tr><td>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
244 res.append('<a href="%s"><img src="%s" title="Click image preview for a print quality PDF version" hspace="10" align="middle"></a>\n' % (p,imghref))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
245 res.append('</tr></td></table>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
246 if len(flist) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
247 res.append('<b>The following output files were created (click the filename to view/download a copy):</b><hr/>')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
248 res.append('<table>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
249 for i,f in enumerate(flist):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
250 fn = os.path.split(f)[-1]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
251 res.append('<tr><td><a href="%s">%s</a></td></tr>\n' % (fn,fn))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
252 res.append('</table><p/>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
253 pres = self.prettyPicout(transpose,maxloglines)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
254 if len(pres) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
255 res += pres
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
256 l = open(self.log_filename,'r').readlines()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
257 llen = len(l)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
258 if llen > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
259 res.append('<b>Picard Tool Run Log</b><hr/>\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
260 rlog = ['<pre>',]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
261 if llen > maxloglines:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
262 n = min(50,int(maxloglines/2))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
263 rlog += l[:n]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
264 rlog.append('------------ ## %d rows deleted ## --------------\n' % (llen-maxloglines))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
265 rlog += l[-n:]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
266 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
267 rlog += l
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
268 rlog.append('</pre>')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
269 if llen > maxloglines:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
270 rlog.append('\n<b>## WARNING - %d log lines truncated - <a href="%s">%s</a> contains entire output</b>' % (llen - maxloglines,self.log_filename,self.log_filename))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
271 res += rlog
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
272 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
273 res.append("### Odd, Picard left no log file %s - must have really barfed badly?\n" % self.log_filename)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
274 res.append('<hr/>The freely available <a href="http://picard.sourceforge.net/command-line-overview.shtml">Picard software</a> \n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
275 res.append( 'generated all outputs reported here running as a <a href="http://getgalaxy.org">Galaxy</a> tool')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
276 res.append(galhtmlpostfix)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
277 outf = open(self.opts.htmlout,'w')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
278 outf.write(''.join(res))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
279 outf.write('\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
280 outf.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
281
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
282 def makePicInterval(self,inbed=None,outf=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
283 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
284 picard wants bait and target files to have the same header length as the incoming bam/sam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
285 a meaningful (ie accurate) representation will fail because of this - so this hack
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
286 it would be far better to be able to supply the original bed untouched
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
287 Additional checking added Ross Lazarus Dec 2011 to deal with two 'bug' reports on the list
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
288 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
289 assert inbed <> None
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
290 bed = open(inbed,'r').readlines()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
291 sbed = [x.split('\t') for x in bed] # lengths MUST be 5
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
292 lens = [len(x) for x in sbed]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
293 strands = [x[3] for x in sbed if not x[3] in ['+','-']]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
294 maxl = max(lens)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
295 minl = min(lens)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
296 e = []
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
297 if maxl <> minl:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
298 e.append("## Input error: Inconsistent field count in %s - please read the documentation on bait/target format requirements, fix and try again" % inbed)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
299 if maxl <> 5:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
300 e.append("## Input error: %d fields found in %s, 5 required - please read the warning and documentation on bait/target format requirements, fix and try again" % (maxl,inbed))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
301 if len(strands) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
302 e.append("## Input error: Fourth column in %s is not the required strand (+ or -) - please read the warning and documentation on bait/target format requirements, fix and try again" % (inbed))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
303 if len(e) > 0: # write to stderr and quit
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
304 print >> sys.stderr, '\n'.join(e)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
305 sys.exit(1)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
306 thead = os.path.join(self.opts.outdir,'tempSamHead.txt')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
307 if self.opts.datatype == 'sam':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
308 cl = ['samtools view -H -S',self.opts.input,'>',thead]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
309 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
310 cl = ['samtools view -H',self.opts.input,'>',thead]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
311 self.runCL(cl=cl,output_dir=self.opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
312 head = open(thead,'r').readlines()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
313 s = '## got %d rows of header\n' % (len(head))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
314 logging.info(s)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
315 o = open(outf,'w')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
316 o.write(''.join(head))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
317 o.write(''.join(bed))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
318 o.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
319 return outf
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
320
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
321 def cleanSam(self, insam=None, newsam=None, picardErrors=[],outformat=None):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
322 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
323 interesting problem - if paired, must remove mate pair of errors too or we have a new set of errors after cleaning - missing mate pairs!
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
324 Do the work of removing all the error sequences
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
325 pysam is cool
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
326 infile = pysam.Samfile( "-", "r" )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
327 outfile = pysam.Samfile( "-", "w", template = infile )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
328 for s in infile: outfile.write(s)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
329
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
330 errors from ValidateSameFile.jar look like
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
331 WARNING: Record 32, Read name SRR006041.1202260, NM tag (nucleotide differences) is missing
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
332 ERROR: Record 33, Read name SRR006041.1042721, Empty sequence dictionary.
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
333 ERROR: Record 33, Read name SRR006041.1042721, RG ID on SAMRecord not found in header: SRR006041
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
334
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
335 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
336 assert os.path.isfile(insam), 'rgPicardValidate cleansam needs an input sam file - cannot find %s' % insam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
337 assert newsam <> None, 'rgPicardValidate cleansam needs an output new sam file path'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
338 removeNames = [x.split(',')[1].replace(' Read name ','') for x in picardErrors if len(x.split(',')) > 2]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
339 remDict = dict(zip(removeNames,range(len(removeNames))))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
340 infile = pysam.Samfile(insam,'rb')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
341 info = 'found %d error sequences in picardErrors, %d unique' % (len(removeNames),len(remDict))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
342 if len(removeNames) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
343 outfile = pysam.Samfile(newsam,'wb',template=infile) # template must be an open file
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
344 i = 0
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
345 j = 0
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
346 for row in infile:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
347 dropme = remDict.get(row.qname,None) # keep if None
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
348 if not dropme:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
349 outfile.write(row)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
350 j += 1
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
351 else: # discard
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
352 i += 1
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
353 info = '%s\n%s' % (info, 'Discarded %d lines writing %d to %s from %s' % (i,j,newsam,insam))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
354 outfile.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
355 infile.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
356 else: # we really want a nullop or a simple pointer copy
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
357 infile.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
358 if newsam:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
359 shutil.copy(insam,newsam)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
360 logging.info(info)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
361
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
362
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
363
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
364 def __main__():
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
365 doFix = False # tools returning htmlfile don't need this
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
366 doTranspose = True # default
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
367 maxloglines = 100 # default
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
368 #Parse Command Line
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
369 op = optparse.OptionParser()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
370 # All tools
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
371 op.add_option('-i', '--input', dest='input', help='Input SAM or BAM file' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
372 op.add_option('-e', '--inputext', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
373 op.add_option('-o', '--output', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
374 op.add_option('-n', '--title', default="Pick a Picard Tool")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
375 op.add_option('-t', '--htmlout', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
376 op.add_option('-d', '--outdir', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
377 op.add_option('-x', '--maxjheap', default='4g')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
378 op.add_option('-b', '--bisulphite', default='false')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
379 op.add_option('-s', '--sortorder', default='query')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
380 op.add_option('','--tmpdir', default='/tmp')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
381 op.add_option('-j','--jar',default='')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
382 op.add_option('','--picard-cmd',default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
383 # Many tools
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
384 op.add_option( '', '--output-format', dest='output_format', help='Output format' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
385 op.add_option( '', '--bai-file', dest='bai_file', help='The path to the index file for the input bam file' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
386 op.add_option( '', '--ref', dest='ref', help='Built-in reference with fasta and dict file', default=None )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
387 # CreateSequenceDictionary
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
388 op.add_option( '', '--ref-file', dest='ref_file', help='Fasta to use as reference', default=None )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
389 op.add_option( '', '--species-name', dest='species_name', help='Species name to use in creating dict file from fasta file' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
390 op.add_option( '', '--build-name', dest='build_name', help='Name of genome assembly to use in creating dict file from fasta file' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
391 op.add_option( '', '--trunc-names', dest='trunc_names', help='Truncate sequence names at first whitespace from fasta file' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
392 # MarkDuplicates
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
393 op.add_option( '', '--remdups', default='true', help='Remove duplicates from output file' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
394 op.add_option( '', '--optdupdist', default="100", help='Maximum pixels between two identical sequences in order to consider them optical duplicates.' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
395 # CollectInsertSizeMetrics
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
396 op.add_option('', '--taillimit', default="0")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
397 op.add_option('', '--histwidth', default="0")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
398 op.add_option('', '--minpct', default="0.01")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
399 op.add_option('', '--malevel', default='')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
400 op.add_option('', '--deviations', default="0.0")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
401 # CollectAlignmentSummaryMetrics
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
402 op.add_option('', '--maxinsert', default="20")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
403 op.add_option('', '--adaptors', default='')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
404 # FixMateInformation and validate
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
405 # CollectGcBiasMetrics
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
406 op.add_option('', '--windowsize', default='100')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
407 op.add_option('', '--mingenomefrac', default='0.00001')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
408 # AddOrReplaceReadGroups
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
409 op.add_option( '', '--rg-opts', dest='rg_opts', help='Specify extra (optional) arguments with full, otherwise preSet' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
410 op.add_option( '', '--rg-lb', dest='rg_library', help='Read Group Library' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
411 op.add_option( '', '--rg-pl', dest='rg_platform', help='Read Group platform (e.g. illumina, solid)' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
412 op.add_option( '', '--rg-pu', dest='rg_plat_unit', help='Read Group platform unit (eg. run barcode) ' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
413 op.add_option( '', '--rg-sm', dest='rg_sample', help='Read Group sample name' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
414 op.add_option( '', '--rg-id', dest='rg_id', help='Read Group ID' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
415 op.add_option( '', '--rg-cn', dest='rg_seq_center', help='Read Group sequencing center name' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
416 op.add_option( '', '--rg-ds', dest='rg_desc', help='Read Group description' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
417 # ReorderSam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
418 op.add_option( '', '--allow-inc-dict-concord', dest='allow_inc_dict_concord', help='Allow incomplete dict concordance' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
419 op.add_option( '', '--allow-contig-len-discord', dest='allow_contig_len_discord', help='Allow contig length discordance' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
420 # ReplaceSamHeader
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
421 op.add_option( '', '--header-file', dest='header_file', help='sam or bam file from which header will be read' )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
422
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
423 op.add_option('','--assumesorted', default='true')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
424 op.add_option('','--readregex', default="[a-zA-Z0-9]+:[0-9]:([0-9]+):([0-9]+):([0-9]+).*")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
425 #estimatelibrarycomplexity
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
426 op.add_option('','--minid', default="5")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
427 op.add_option('','--maxdiff', default="0.03")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
428 op.add_option('','--minmeanq', default="20")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
429 #hsmetrics
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
430 op.add_option('','--baitbed', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
431 op.add_option('','--targetbed', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
432 #validate
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
433 op.add_option('','--ignoreflags', action='append', type="string")
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
434 op.add_option('','--maxerrors', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
435 op.add_option('','--datatype', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
436 op.add_option('','--bamout', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
437 op.add_option('','--samout', default=None)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
438
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
439 opts, args = op.parse_args()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
440 opts.sortme = opts.assumesorted == 'false'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
441 assert opts.input <> None
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
442 # need to add
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
443 # instance that does all the work
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
444 pic = PicardBase(opts,sys.argv[0])
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
445
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
446 tmp_dir = opts.outdir
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
447 haveTempout = False # we use this where sam output is an option
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
448 rval = 0
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
449 stdouts = 'Not run yet'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
450 # set ref and dict files to use (create if necessary)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
451 ref_file_name = opts.ref
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
452 if opts.ref_file <> None:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
453 csd = 'CreateSequenceDictionary'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
454 realjarpath = os.path.split(opts.jar)[0]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
455 jarpath = os.path.join(realjarpath,'%s.jar' % csd) # for refseq
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
456 tmp_ref_fd, tmp_ref_name = tempfile.mkstemp( dir=opts.tmpdir , prefix = pic.picname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
457 ref_file_name = '%s.fasta' % tmp_ref_name
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
458 # build dict
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
459 dict_file_name = '%s.dict' % tmp_ref_name
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
460 os.symlink( opts.ref_file, ref_file_name )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
461 cl = ['REFERENCE=%s' % ref_file_name]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
462 cl.append('OUTPUT=%s' % dict_file_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
463 cl.append('URI=%s' % os.path.basename( opts.ref_file ))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
464 cl.append('TRUNCATE_NAMES_AT_WHITESPACE=%s' % opts.trunc_names)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
465 if opts.species_name:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
466 cl.append('SPECIES=%s' % opts.species_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
467 if opts.build_name:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
468 cl.append('GENOME_ASSEMBLY=%s' % opts.build_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
469 pic.delme.append(dict_file_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
470 pic.delme.append(ref_file_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
471 pic.delme.append(tmp_ref_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
472 stdouts,rval = pic.runPic(jarpath, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
473 # run relevant command(s)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
474
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
475 # define temporary output
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
476 # if output is sam, it must have that extension, otherwise bam will be produced
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
477 # specify sam or bam file with extension
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
478 if opts.output_format == 'sam':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
479 suff = '.sam'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
480 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
481 suff = ''
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
482 tmp_fd, tempout = tempfile.mkstemp( dir=opts.tmpdir, suffix=suff )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
483
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
484 cl = ['VALIDATION_STRINGENCY=LENIENT',]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
485
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
486 if pic.picname == 'AddOrReplaceReadGroups':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
487 # sort order to match Galaxy's default
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
488 cl.append('SORT_ORDER=coordinate')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
489 # input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
490 cl.append('INPUT=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
491 # outputs
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
492 cl.append('OUTPUT=%s' % tempout)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
493 # required read groups
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
494 cl.append('RGLB="%s"' % opts.rg_library)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
495 cl.append('RGPL="%s"' % opts.rg_platform)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
496 cl.append('RGPU="%s"' % opts.rg_plat_unit)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
497 cl.append('RGSM="%s"' % opts.rg_sample)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
498 if opts.rg_id:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
499 cl.append('RGID="%s"' % opts.rg_id)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
500 # optional read groups
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
501 if opts.rg_seq_center:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
502 cl.append('RGCN="%s"' % opts.rg_seq_center)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
503 if opts.rg_desc:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
504 cl.append('RGDS="%s"' % opts.rg_desc)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
505 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
506 haveTempout = True
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
507
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
508 elif pic.picname == 'BamIndexStats':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
509 tmp_fd, tmp_name = tempfile.mkstemp( dir=tmp_dir )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
510 tmp_bam_name = '%s.bam' % tmp_name
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
511 tmp_bai_name = '%s.bai' % tmp_bam_name
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
512 os.symlink( opts.input, tmp_bam_name )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
513 os.symlink( opts.bai_file, tmp_bai_name )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
514 cl.append('INPUT=%s' % ( tmp_bam_name ))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
515 pic.delme.append(tmp_bam_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
516 pic.delme.append(tmp_bai_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
517 pic.delme.append(tmp_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
518 stdouts,rval = pic.runPic( opts.jar, cl )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
519 f = open(pic.metricsOut,'a')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
520 f.write(stdouts) # got this on stdout from runCl
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
521 f.write('\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
522 f.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
523 doTranspose = False # but not transposed
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
524
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
525 elif pic.picname == 'EstimateLibraryComplexity':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
526 cl.append('I=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
527 cl.append('O=%s' % pic.metricsOut)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
528 if float(opts.minid) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
529 cl.append('MIN_IDENTICAL_BASES=%s' % opts.minid)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
530 if float(opts.maxdiff) > 0.0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
531 cl.append('MAX_DIFF_RATE=%s' % opts.maxdiff)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
532 if float(opts.minmeanq) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
533 cl.append('MIN_MEAN_QUALITY=%s' % opts.minmeanq)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
534 if opts.readregex > '':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
535 cl.append('READ_NAME_REGEX="%s"' % opts.readregex)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
536 if float(opts.optdupdist) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
537 cl.append('OPTICAL_DUPLICATE_PIXEL_DISTANCE=%s' % opts.optdupdist)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
538 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
539
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
540 elif pic.picname == 'CollectAlignmentSummaryMetrics':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
541 # Why do we do this fakefasta thing?
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
542 # Because we need NO fai to be available or picard barfs unless it matches the input data.
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
543 # why? Dunno Seems to work without complaining if the .bai file is AWOL....
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
544 fakefasta = os.path.join(opts.outdir,'%s_fake.fasta' % os.path.basename(ref_file_name))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
545 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
546 os.symlink(ref_file_name,fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
547 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
548 s = '## unable to symlink %s to %s - different devices? Will shutil.copy'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
549 info = s
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
550 shutil.copy(ref_file_name,fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
551 pic.delme.append(fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
552 cl.append('ASSUME_SORTED=true')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
553 adaptlist = opts.adaptors.split(',')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
554 adaptorseqs = ['ADAPTER_SEQUENCE=%s' % x for x in adaptlist]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
555 cl += adaptorseqs
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
556 cl.append('IS_BISULFITE_SEQUENCED=%s' % opts.bisulphite)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
557 cl.append('MAX_INSERT_SIZE=%s' % opts.maxinsert)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
558 cl.append('OUTPUT=%s' % pic.metricsOut)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
559 cl.append('R=%s' % fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
560 cl.append('TMP_DIR=%s' % opts.tmpdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
561 if not opts.assumesorted.lower() == 'true': # we need to sort input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
562 sortedfile = '%s.sorted' % os.path.basename(opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
563 if opts.datatype == 'sam': # need to work with a bam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
564 tlog,tempbam,trval = pic.samToBam(opts.input,opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
565 pic.delme.append(tempbam)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
566 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
567 tlog = pic.sortSam(tempbam,sortedfile,opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
568 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
569 print '## exception on sorting sam file %s' % opts.input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
570 else: # is already bam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
571 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
572 tlog = pic.sortSam(opts.input,sortedfile,opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
573 except : # bug - [bam_sort_core] not being ignored - TODO fixme
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
574 print '## exception %s on sorting bam file %s' % (sys.exc_info()[0],opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
575 cl.append('INPUT=%s.bam' % os.path.abspath(os.path.join(opts.outdir,sortedfile)))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
576 pic.delme.append(os.path.join(opts.outdir,sortedfile))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
577 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
578 cl.append('INPUT=%s' % os.path.abspath(opts.input))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
579 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
580
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
581
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
582 elif pic.picname == 'CollectGcBiasMetrics':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
583 assert os.path.isfile(ref_file_name),'PicardGC needs a reference sequence - cannot read %s' % ref_file_name
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
584 # sigh. Why do we do this fakefasta thing? Because we need NO fai to be available or picard barfs unless it has the same length as the input data.
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
585 # why? Dunno
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
586 fakefasta = os.path.join(opts.outdir,'%s_fake.fasta' % os.path.basename(ref_file_name))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
587 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
588 os.symlink(ref_file_name,fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
589 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
590 s = '## unable to symlink %s to %s - different devices? May need to replace with shutil.copy'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
591 info = s
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
592 shutil.copy(ref_file_name,fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
593 pic.delme.append(fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
594 x = 'rgPicardGCBiasMetrics'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
595 pdfname = '%s.pdf' % x
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
596 jpgname = '%s.jpg' % x
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
597 tempout = os.path.join(opts.outdir,'rgPicardGCBiasMetrics.out')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
598 temppdf = os.path.join(opts.outdir,pdfname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
599 cl.append('R=%s' % fakefasta)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
600 cl.append('WINDOW_SIZE=%s' % opts.windowsize)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
601 cl.append('MINIMUM_GENOME_FRACTION=%s' % opts.mingenomefrac)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
602 cl.append('INPUT=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
603 cl.append('OUTPUT=%s' % tempout)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
604 cl.append('TMP_DIR=%s' % opts.tmpdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
605 cl.append('CHART_OUTPUT=%s' % temppdf)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
606 cl.append('SUMMARY_OUTPUT=%s' % pic.metricsOut)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
607 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
608 if os.path.isfile(temppdf):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
609 cl2 = ['convert','-resize x400',temppdf,os.path.join(opts.outdir,jpgname)] # make the jpg for fixPicardOutputs to find
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
610 s,stdouts,rval = pic.runCL(cl=cl2,output_dir=opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
611 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
612 s='### runGC: Unable to find pdf %s - please check the log for the causal problem\n' % temppdf
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
613 lf = open(pic.log_filename,'a')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
614 lf.write(s)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
615 lf.write('\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
616 lf.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
617
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
618 elif pic.picname == 'CollectInsertSizeMetrics':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
619 """ <command interpreter="python">
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
620 picard_wrapper.py -i "$input_file" -n "$out_prefix" --tmpdir "${__new_file_path__}" --deviations "$deviations"
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
621 --histwidth "$histWidth" --minpct "$minPct" --malevel "$malevel"
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
622 -j "${GALAXY_DATA_INDEX_DIR}/shared/jars/picard/CollectInsertSizeMetrics.jar" -d "$html_file.files_path" -t "$html_file"
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
623 </command>
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
624 """
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
625 isPDF = 'InsertSizeHist.pdf'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
626 pdfpath = os.path.join(opts.outdir,isPDF)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
627 histpdf = 'InsertSizeHist.pdf'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
628 cl.append('I=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
629 cl.append('O=%s' % pic.metricsOut)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
630 cl.append('HISTOGRAM_FILE=%s' % histpdf)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
631 #if opts.taillimit <> '0': # this was deprecated although still mentioned in the docs at 1.56
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
632 # cl.append('TAIL_LIMIT=%s' % opts.taillimit)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
633 if opts.histwidth <> '0':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
634 cl.append('HISTOGRAM_WIDTH=%s' % opts.histwidth)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
635 if float( opts.minpct) > 0.0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
636 cl.append('MINIMUM_PCT=%s' % opts.minpct)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
637 if float(opts.deviations) > 0.0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
638 cl.append('DEVIATIONS=%s' % opts.deviations)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
639 if opts.malevel:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
640 malists = opts.malevel.split(',')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
641 malist = ['METRIC_ACCUMULATION_LEVEL=%s' % x for x in malists]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
642 cl += malist
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
643 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
644 if os.path.exists(pdfpath): # automake thumbnail - will be added to html
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
645 cl2 = ['mogrify', '-format jpg -resize x400 %s' % pdfpath]
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
646 pic.runCL(cl=cl2,output_dir=opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
647 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
648 s = 'Unable to find expected pdf file %s<br/>\n' % pdfpath
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
649 s += 'This <b>always happens if single ended data was provided</b> to this tool,\n'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
650 s += 'so please double check that your input data really is paired-end NGS data.<br/>\n'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
651 s += 'If your input was paired data this may be a bug worth reporting to the galaxy-bugs list\n<br/>'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
652 logging.info(s)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
653 if len(stdouts) > 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
654 logging.info(stdouts)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
655
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
656 elif pic.picname == 'MarkDuplicates':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
657 # assume sorted even if header says otherwise
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
658 cl.append('ASSUME_SORTED=%s' % (opts.assumesorted))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
659 # input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
660 cl.append('INPUT=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
661 # outputs
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
662 cl.append('OUTPUT=%s' % opts.output)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
663 cl.append('METRICS_FILE=%s' % pic.metricsOut )
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
664 # remove or mark duplicates
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
665 cl.append('REMOVE_DUPLICATES=%s' % opts.remdups)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
666 # the regular expression to be used to parse reads in incoming SAM file
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
667 cl.append('READ_NAME_REGEX="%s"' % opts.readregex)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
668 # maximum offset between two duplicate clusters
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
669 cl.append('OPTICAL_DUPLICATE_PIXEL_DISTANCE=%s' % opts.optdupdist)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
670 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
671
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
672 elif pic.picname == 'FixMateInformation':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
673 cl.append('I=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
674 cl.append('O=%s' % tempout)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
675 cl.append('SORT_ORDER=%s' % opts.sortorder)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
676 stdouts,rval = pic.runPic(opts.jar,cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
677 haveTempout = True
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
678
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
679 elif pic.picname == 'ReorderSam':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
680 # input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
681 cl.append('INPUT=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
682 # output
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
683 cl.append('OUTPUT=%s' % tempout)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
684 # reference
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
685 cl.append('REFERENCE=%s' % ref_file_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
686 # incomplete dict concordance
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
687 if opts.allow_inc_dict_concord == 'true':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
688 cl.append('ALLOW_INCOMPLETE_DICT_CONCORDANCE=true')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
689 # contig length discordance
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
690 if opts.allow_contig_len_discord == 'true':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
691 cl.append('ALLOW_CONTIG_LENGTH_DISCORDANCE=true')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
692 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
693 haveTempout = True
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
694
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
695 elif pic.picname == 'ReplaceSamHeader':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
696 cl.append('INPUT=%s' % opts.input)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
697 cl.append('OUTPUT=%s' % tempout)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
698 cl.append('HEADER=%s' % opts.header_file)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
699 stdouts,rval = pic.runPic(opts.jar, cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
700 haveTempout = True
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
701
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
702 elif pic.picname == 'CalculateHsMetrics':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
703 maxloglines = 100
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
704 baitfname = os.path.join(opts.outdir,'rgPicardHsMetrics.bait')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
705 targetfname = os.path.join(opts.outdir,'rgPicardHsMetrics.target')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
706 baitf = pic.makePicInterval(opts.baitbed,baitfname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
707 if opts.targetbed == opts.baitbed: # same file sometimes
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
708 targetf = baitf
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
709 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
710 targetf = pic.makePicInterval(opts.targetbed,targetfname)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
711 cl.append('BAIT_INTERVALS=%s' % baitf)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
712 cl.append('TARGET_INTERVALS=%s' % targetf)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
713 cl.append('INPUT=%s' % os.path.abspath(opts.input))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
714 cl.append('OUTPUT=%s' % pic.metricsOut)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
715 cl.append('TMP_DIR=%s' % opts.tmpdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
716 stdouts,rval = pic.runPic(opts.jar,cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
717
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
718 elif pic.picname == 'ValidateSamFile':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
719 import pysam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
720 doTranspose = False
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
721 sortedfile = os.path.join(opts.outdir,'rgValidate.sorted')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
722 stf = open(pic.log_filename,'w')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
723 tlog = None
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
724 if opts.datatype == 'sam': # need to work with a bam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
725 tlog,tempbam,rval = pic.samToBam(opts.input,opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
726 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
727 tlog = pic.sortSam(tempbam,sortedfile,opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
728 except:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
729 print '## exception on sorting sam file %s' % opts.input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
730 else: # is already bam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
731 try:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
732 tlog = pic.sortSam(opts.input,sortedfile,opts.outdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
733 except: # bug - [bam_sort_core] not being ignored - TODO fixme
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
734 print '## exception on sorting bam file %s' % opts.input
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
735 if tlog:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
736 print '##tlog=',tlog
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
737 stf.write(tlog)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
738 stf.write('\n')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
739 sortedfile = '%s.bam' % sortedfile # samtools does that
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
740 cl.append('O=%s' % pic.metricsOut)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
741 cl.append('TMP_DIR=%s' % opts.tmpdir)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
742 cl.append('I=%s' % sortedfile)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
743 opts.maxerrors = '99999999'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
744 cl.append('MAX_OUTPUT=%s' % opts.maxerrors)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
745 if opts.ignoreflags[0] <> 'None': # picard error values to ignore
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
746 igs = ['IGNORE=%s' % x for x in opts.ignoreflags if x <> 'None']
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
747 cl.append(' '.join(igs))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
748 if opts.bisulphite.lower() <> 'false':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
749 cl.append('IS_BISULFITE_SEQUENCED=true')
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
750 if opts.ref <> None or opts.ref_file <> None:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
751 cl.append('R=%s' % ref_file_name)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
752 stdouts,rval = pic.runPic(opts.jar,cl)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
753 if opts.datatype == 'sam':
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
754 pic.delme.append(tempbam)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
755 newsam = opts.output
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
756 outformat = 'bam'
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
757 pe = open(pic.metricsOut,'r').readlines()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
758 pic.cleanSam(insam=sortedfile, newsam=newsam, picardErrors=pe,outformat=outformat)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
759 pic.delme.append(sortedfile) # not wanted
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
760 stf.close()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
761 pic.cleanup()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
762 else:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
763 print >> sys.stderr,'picard.py got an unknown tool name - %s' % pic.picname
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
764 sys.exit(1)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
765 if haveTempout:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
766 # Some Picard tools produced a potentially intermediate bam file.
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
767 # Either just move to final location or create sam
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
768 if os.path.exists(tempout):
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
769 shutil.move(tempout, os.path.abspath(opts.output))
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
770 if opts.htmlout <> None or doFix: # return a pretty html page
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
771 pic.fixPicardOutputs(transpose=doTranspose,maxloglines=maxloglines)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
772 if rval <> 0:
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
773 print >> sys.stderr, '## exit code=%d; stdout=%s' % (rval,stdouts)
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
774 # signal failure
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
775 if __name__=="__main__": __main__()
ff4ec13e496e Uploaded tarball to repository
devteam
parents:
diff changeset
776