From bfdcccc623d92ef2e7399fda3141b14fa7a2a456 Mon Sep 17 00:00:00 2001 From: Christopher Tomkins-Tinch Date: Thu, 14 Jul 2016 17:41:17 -0400 Subject: [PATCH] refactor bwa.py to remove the sam->bam conversion added earlier in this branch --- tools/bwa.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tools/bwa.py b/tools/bwa.py index 37a870aeb..3fe19af20 100644 --- a/tools/bwa.py +++ b/tools/bwa.py @@ -7,7 +7,6 @@ import os import os.path import subprocess -import shutil import tools import tools.samtools @@ -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) - -