Mercurial > repos > juanperin > bwa_wrapper
comparison bwa_long/bwa_wrapper.py @ 0:fb4844b6a98e default tip
Migrated tool version 1.0.3 from old tool shed archive to new tool shed repository
author | juanperin |
---|---|
date | Tue, 07 Jun 2011 17:28:32 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fb4844b6a98e |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 Runs BWA on single-end or paired-end data. | |
5 Produces a SAM file containing the mappings. | |
6 Works with BWA version 0.5.3-0.5.5. | |
7 | |
8 usage: bwa_wrapper.py [options] | |
9 -t, --threads=t: The number of threads to use | |
10 -r, --ref=r: The reference genome to use or index | |
11 -f, --fastq=f: The (forward) fastq file to use for the mapping | |
12 -F, --rfastq=F: The reverse fastq file to use for mapping if paired-end data | |
13 -u, --output=u: The file to save the output (SAM format) | |
14 -g, --genAlignType=g: The type of pairing (single or paired) | |
15 -p, --params=p: Parameter setting to use (pre_set or full) | |
16 -s, --fileSource=s: Whether to use a previously indexed reference sequence or one from history (indexed or history) | |
17 -n, --maxEditDist=n: Maximum edit distance if integer | |
18 -m, --fracMissingAligns=m: Fraction of missing alignments given 2% uniform base error rate if fraction | |
19 -o, --maxGapOpens=o: Maximum number of gap opens | |
20 -e, --maxGapExtens=e: Maximum number of gap extensions | |
21 -d, --disallowLongDel=d: Disallow a long deletion within specified bps | |
22 -i, --disallowIndel=i: Disallow indel within specified bps | |
23 -l, --seed=l: Take the first specified subsequences | |
24 -k, --maxEditDistSeed=k: Maximum edit distance to the seed | |
25 -M, --mismatchPenalty=M: Mismatch penalty | |
26 -O, --gapOpenPenalty=O: Gap open penalty | |
27 -E, --gapExtensPenalty=E: Gap extension penalty | |
28 -R, --suboptAlign=R: Proceed with suboptimal alignments even if the top hit is a repeat | |
29 -N, --noIterSearch=N: Disable iterative search | |
30 -T, --outputTopN=T: Output top specified hits | |
31 -S, --maxInsertSize=S: Maximum insert size for a read pair to be considered mapped good | |
32 -P, --maxOccurPairing=P: Maximum occurrences of a read for pairings | |
33 -D, --dbkey=D: Dbkey for reference genome | |
34 -H, --suppressHeader=h: Suppress header | |
35 """ | |
36 | |
37 import optparse, os, shutil, subprocess, sys, tempfile | |
38 | |
39 def stop_err( msg ): | |
40 sys.stderr.write( '%s\n' % msg ) | |
41 sys.exit() | |
42 | |
43 def __main__(): | |
44 #Parse Command Line | |
45 parser = optparse.OptionParser() | |
46 parser.add_option( '-t', '--threads', dest='threads', help='The number of threads to use' ) | |
47 parser.add_option( '-r', '--ref', dest='ref', help='The reference genome to use or index' ) | |
48 parser.add_option( '-f', '--fastq', dest='fastq', help='The (forward) fastq file to use for the mapping' ) | |
49 parser.add_option( '-F', '--rfastq', dest='rfastq', help='The reverse fastq file to use for mapping if paired-end data' ) | |
50 parser.add_option( '-u', '--output', dest='output', help='The file to save the output (SAM format)' ) | |
51 parser.add_option( '-g', '--genAlignType', dest='genAlignType', help='The type of pairing (single or paired)' ) | |
52 parser.add_option( '-p', '--params', dest='params', help='Parameter setting to use (pre_set or full)' ) | |
53 parser.add_option( '-s', '--fileSource', dest='fileSource', help='Whether to use a previously indexed reference sequence or one form history (indexed or history)' ) | |
54 parser.add_option( '-n', '--maxEditDist', dest='maxEditDist', help='Maximum edit distance if integer' ) | |
55 parser.add_option( '-m', '--fracMissingAligns', dest='fracMissingAligns', help='Fraction of missing alignments given 2% uniform base error rate if fraction' ) | |
56 parser.add_option( '-o', '--maxGapOpens', dest='maxGapOpens', help='Maximum number of gap opens' ) | |
57 parser.add_option( '-e', '--maxGapExtens', dest='maxGapExtens', help='Maximum number of gap extensions' ) | |
58 parser.add_option( '-d', '--disallowLongDel', dest='disallowLongDel', help='Disallow a long deletion within specified bps' ) | |
59 parser.add_option( '-i', '--disallowIndel', dest='disallowIndel', help='Disallow indel within specified bps' ) | |
60 parser.add_option( '-l', '--seed', dest='seed', help='Take the first specified subsequences' ) | |
61 parser.add_option( '-k', '--maxEditDistSeed', dest='maxEditDistSeed', help='Maximum edit distance to the seed' ) | |
62 parser.add_option( '-M', '--mismatchPenalty', dest='mismatchPenalty', help='Mismatch penalty' ) | |
63 parser.add_option( '-O', '--gapOpenPenalty', dest='gapOpenPenalty', help='Gap open penalty' ) | |
64 parser.add_option( '-E', '--gapExtensPenalty', dest='gapExtensPenalty', help='Gap extension penalty' ) | |
65 parser.add_option( '-R', '--suboptAlign', dest='suboptAlign', help='Proceed with suboptimal alignments even if the top hit is a repeat' ) | |
66 parser.add_option( '-N', '--noIterSearch', dest='noIterSearch', help='Disable iterative search' ) | |
67 parser.add_option( '-T', '--outputTopN', dest='outputTopN', help='Output top specified hits' ) | |
68 parser.add_option( '-S', '--maxInsertSize', dest='maxInsertSize', help='Maximum insert size for a read pair to be considered mapped good' ) | |
69 parser.add_option( '-P', '--maxOccurPairing', dest='maxOccurPairing', help='Maximum occurrences of a read for pairings' ) | |
70 parser.add_option( '-D', '--dbkey', dest='dbkey', help='Dbkey for reference genome' ) | |
71 parser.add_option( '-H', '--suppressHeader', dest='suppressHeader', help='Suppress header' ) | |
72 (options, args) = parser.parse_args() | |
73 # make temp directory for placement of indices | |
74 tmp_index_dir = tempfile.mkdtemp() | |
75 tmp_dir = tempfile.mkdtemp() | |
76 # index if necessary | |
77 if options.fileSource == 'history': | |
78 ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir ) | |
79 ref_file_name = ref_file.name | |
80 ref_file.close() | |
81 os.symlink( options.ref, ref_file_name ) | |
82 # determine which indexing algorithm to use, based on size | |
83 try: | |
84 size = os.stat( options.ref ).st_size | |
85 if size <= 2**30: | |
86 indexingAlg = 'is' | |
87 else: | |
88 indexingAlg = 'bwtsw' | |
89 except: | |
90 indexingAlg = 'is' | |
91 indexing_cmds = '-a %s' % indexingAlg | |
92 cmd1 = 'bwa index %s %s' % ( indexing_cmds, ref_file_name ) | |
93 try: | |
94 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name | |
95 tmp_stderr = open( tmp, 'wb' ) | |
96 proc = subprocess.Popen( args=cmd1, shell=True, cwd=tmp_index_dir, stderr=tmp_stderr.fileno() ) | |
97 returncode = proc.wait() | |
98 tmp_stderr.close() | |
99 # get stderr, allowing for case where it's very large | |
100 tmp_stderr = open( tmp, 'rb' ) | |
101 stderr = '' | |
102 buffsize = 1048576 | |
103 try: | |
104 while True: | |
105 stderr += tmp_stderr.read( buffsize ) | |
106 if not stderr or len( stderr ) % buffsize != 0: | |
107 break | |
108 except OverflowError: | |
109 pass | |
110 tmp_stderr.close() | |
111 if returncode != 0: | |
112 raise Exception, stderr | |
113 except Exception, e: | |
114 # clean up temp dirs | |
115 if os.path.exists( tmp_index_dir ): | |
116 shutil.rmtree( tmp_index_dir ) | |
117 if os.path.exists( tmp_dir ): | |
118 shutil.rmtree( tmp_dir ) | |
119 stop_err( 'Error indexing reference sequence. ' + str( e ) ) | |
120 else: | |
121 ref_file_name = options.ref | |
122 # set up aligning and generate aligning command options | |
123 if options.params == 'pre_set': | |
124 aligning_cmds = '-t %s' % options.threads | |
125 gen_alignment_cmds = '' | |
126 else: | |
127 if options.maxEditDist != '0': | |
128 editDist = options.maxEditDist | |
129 else: | |
130 editDist = options.fracMissingAligns | |
131 if options.seed != '-1': | |
132 seed = '-l %s' % options.seed | |
133 else: | |
134 seed = '' | |
135 if options.suboptAlign == 'true': | |
136 suboptAlign = '-R' | |
137 else: | |
138 suboptAlign = '' | |
139 if options.noIterSearch == 'true': | |
140 noIterSearch = '-N' | |
141 else: | |
142 noIterSearch = '' | |
143 aligning_cmds = '-n %s -o %s -e %s -d %s -i %s %s -k %s -t %s -M %s -O %s -E %s %s %s' % \ | |
144 ( editDist, options.maxGapOpens, options.maxGapExtens, options.disallowLongDel, | |
145 options.disallowIndel, seed, options.maxEditDistSeed, options.threads, | |
146 options.mismatchPenalty, options.gapOpenPenalty, options.gapExtensPenalty, | |
147 suboptAlign, noIterSearch ) | |
148 if options.genAlignType == 'single': | |
149 gen_alignment_cmds = '-n %s' % options.outputTopN | |
150 elif options.genAlignType == 'paired': | |
151 gen_alignment_cmds = '-a %s -o %s' % ( options.maxInsertSize, options.maxOccurPairing ) | |
152 else: | |
153 gen_alignment_cmds = '-n %s' % options.outputTopN | |
154 # set up output files | |
155 tmp_align_out = tempfile.NamedTemporaryFile( dir=tmp_dir ) | |
156 tmp_align_out_name = tmp_align_out.name | |
157 tmp_align_out.close() | |
158 tmp_align_out2 = tempfile.NamedTemporaryFile( dir=tmp_dir ) | |
159 tmp_align_out2_name = tmp_align_out2.name | |
160 tmp_align_out2.close() | |
161 # prepare actual aligning and generate aligning commands | |
162 cmd2 = 'bwa aln %s %s %s > %s' % ( aligning_cmds, ref_file_name, options.fastq, tmp_align_out_name ) | |
163 cmd2b = '' | |
164 if options.genAlignType == 'paired': | |
165 cmd2b = 'bwa aln %s %s %s > %s' % ( aligning_cmds, ref_file_name, options.rfastq, tmp_align_out2_name ) | |
166 cmd3 = 'bwa sampe %s %s %s %s %s %s >> %s' % ( gen_alignment_cmds, ref_file_name, tmp_align_out_name, tmp_align_out2_name, options.fastq, options.rfastq, options.output ) | |
167 elif options.genAlignType == 'single': | |
168 cmd3 = 'bwa samse %s %s %s %s >> %s' % ( gen_alignment_cmds, ref_file_name, tmp_align_out_name, options.fastq, options.output ) | |
169 else: | |
170 cmd2 = 'sleep 1' | |
171 cmd2b = 'sleep 1' | |
172 cmd3 = 'bwa bwasw %s %s %s >> %s' % ( gen_alignment_cmds, ref_file_name, options.fastq, options.output ) | |
173 # perform alignments | |
174 buffsize = 1048576 | |
175 try: | |
176 # need to nest try-except in try-finally to handle 2.4 | |
177 try: | |
178 # align | |
179 try: | |
180 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name | |
181 tmp_stderr = open( tmp, 'wb' ) | |
182 proc = subprocess.Popen( args=cmd2, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() ) | |
183 returncode = proc.wait() | |
184 tmp_stderr.close() | |
185 # get stderr, allowing for case where it's very large | |
186 tmp_stderr = open( tmp, 'rb' ) | |
187 stderr = '' | |
188 try: | |
189 while True: | |
190 stderr += tmp_stderr.read( buffsize ) | |
191 if not stderr or len( stderr ) % buffsize != 0: | |
192 break | |
193 except OverflowError: | |
194 pass | |
195 tmp_stderr.close() | |
196 if returncode != 0: | |
197 raise Exception, stderr | |
198 except Exception, e: | |
199 raise Exception, 'Error aligning sequence. ' + str( e ) | |
200 # and again if paired data | |
201 try: | |
202 if cmd2b: | |
203 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name | |
204 tmp_stderr = open( tmp, 'wb' ) | |
205 proc = subprocess.Popen( args=cmd2b, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() ) | |
206 returncode = proc.wait() | |
207 tmp_stderr.close() | |
208 # get stderr, allowing for case where it's very large | |
209 tmp_stderr = open( tmp, 'rb' ) | |
210 stderr = '' | |
211 try: | |
212 while True: | |
213 stderr += tmp_stderr.read( buffsize ) | |
214 if not stderr or len( stderr ) % buffsize != 0: | |
215 break | |
216 except OverflowError: | |
217 pass | |
218 tmp_stderr.close() | |
219 if returncode != 0: | |
220 raise Exception, stderr | |
221 except Exception, e: | |
222 raise Exception, 'Error aligning second sequence. ' + str( e ) | |
223 # generate align | |
224 try: | |
225 tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name | |
226 tmp_stderr = open( tmp, 'wb' ) | |
227 proc = subprocess.Popen( args=cmd3, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() ) | |
228 returncode = proc.wait() | |
229 tmp_stderr.close() | |
230 # get stderr, allowing for case where it's very large | |
231 tmp_stderr = open( tmp, 'rb' ) | |
232 stderr = '' | |
233 try: | |
234 while True: | |
235 stderr += tmp_stderr.read( buffsize ) | |
236 if not stderr or len( stderr ) % buffsize != 0: | |
237 break | |
238 except OverflowError: | |
239 pass | |
240 tmp_stderr.close() | |
241 if returncode != 0: | |
242 raise Exception, stderr | |
243 except Exception, e: | |
244 raise Exception, 'Error generating alignments. ' + str( e ) | |
245 # remove header if necessary | |
246 if options.suppressHeader == 'true': | |
247 tmp_out = tempfile.NamedTemporaryFile( dir=tmp_dir) | |
248 tmp_out_name = tmp_out.name | |
249 tmp_out.close() | |
250 try: | |
251 shutil.move( options.output, tmp_out_name ) | |
252 except Exception, e: | |
253 raise Exception, 'Error moving output file before removing headers. ' + str( e ) | |
254 fout = file( options.output, 'w' ) | |
255 for line in file( tmp_out.name, 'r' ): | |
256 if not ( line.startswith( '@HD' ) or line.startswith( '@SQ' ) or line.startswith( '@RG' ) or line.startswith( '@PG' ) or line.startswith( '@CO' ) ): | |
257 fout.write( line ) | |
258 fout.close() | |
259 # check that there are results in the output file | |
260 if os.path.getsize( options.output ) > 0: | |
261 sys.stdout.write( 'BWA run on %s-end data' % options.genAlignType ) | |
262 else: | |
263 raise Exception, 'The output file is empty. You may simply have no matches, or there may be an error with your input file or settings.' | |
264 except Exception, e: | |
265 stop_err( 'The alignment failed.\n' + str( e ) ) | |
266 finally: | |
267 # clean up temp dir | |
268 if os.path.exists( tmp_index_dir ): | |
269 shutil.rmtree( tmp_index_dir ) | |
270 if os.path.exists( tmp_dir ): | |
271 shutil.rmtree( tmp_dir ) | |
272 | |
273 if __name__=="__main__": __main__() |