Skip to content

Commit

Permalink
refactor bwa.py to remove the sam->bam conversion added earlier in th…
Browse files Browse the repository at this point in the history
…is branch
  • Loading branch information
tomkinsc committed Jul 14, 2016
1 parent a07cd3e commit bfdcccc
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions tools/bwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import os.path
import subprocess
import shutil

import tools
import tools.samtools
Expand Down Expand Up @@ -56,20 +55,18 @@ def mem(self, inReads, refDb, outAlign, opts=None, threads=None):
fq1 = util.file.mkstempfname('.1.fastq')
fq2 = util.file.mkstempfname('.2.fastq')
aln_sam = util.file.mkstempfname('.sam')
aln_sam_sorted = util.file.mkstempfname('sorted.sam')
samtools.bam2fq(inReads, fq1, fq2)
self.execute('mem', opts + ['-t', str(threads), refDb, fq1, fq2], stdout=aln_sam)

if '-t' not in opts:
threads = threads or utils.misc.available_cpu_count()
opts.extend( ('-t', str(threads)) )

self.execute('mem', opts + [refDb, fq1, fq2], stdout=aln_sam)

os.unlink(fq1)
os.unlink(fq2)
samtools.sort(aln_sam, aln_sam_sorted)
samtools.sort(aln_sam, outAlign)
os.unlink(aln_sam)
# cannot index sam files; only do so if a bam is desired
# cannot index sam files; only do so if a bam/cram is desired
if outAlign.endswith(".bam") or outAlign.endswith(".cram"):
# convert sam -> bam
samtools.view(["-b"], aln_sam_sorted, outAlign)
samtools.index(outAlign)
elif outAlign.endswith(".sam"):
shutil.copyfile(aln_sam_sorted, outAlign)
os.unlink(aln_sam_sorted)


0 comments on commit bfdcccc

Please sign in to comment.