Skip to content

Commit dafd8e3

Browse files
authored
add unit test for TestBwamemIdxstats (#93)
add unit test for TestBwamemIdxstats
1 parent fb0ebd2 commit dafd8e3

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

read_utils.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,11 @@ def minimap2_idxstats(inBam, refFasta, outBam=None, outStats=None,
13701370

13711371
assert outBam or outStats, "Either outBam or outStats must be specified"
13721372

1373+
bam_aligned = util.file.mkstempfname('.aligned.bam')
13731374
if outBam is None:
1374-
bam_aligned = mkstempfname('.aligned.bam')
1375+
bam_filtered = mkstempfname('.filtered.bam')
13751376
else:
1376-
bam_aligned = outBam
1377+
bam_filtered = outBam
13771378

13781379
samtools = tools.samtools.SamtoolsTool()
13791380
mm2 = tools.minimap2.Minimap2()
@@ -1382,21 +1383,24 @@ def minimap2_idxstats(inBam, refFasta, outBam=None, outStats=None,
13821383
shutil.copyfile(refFasta, ref_indexed)
13831384

13841385
mm2.align_bam(inBam, ref_indexed, bam_aligned)
1385-
1386+
13861387
if filterReadsAfterAlignment:
13871388
samtools.filter_to_proper_primary_mapped_reads(bam_aligned,
13881389
bam_filtered,
13891390
require_pairs_to_be_proper=not doNotRequirePairsToBeProper,
13901391
reject_singletons=not keepSingletons)
1392+
os.unlink(bam_aligned)
13911393
else:
1392-
bam_filtered = bam_aligned
1394+
shutil.move(bam_aligned, bam_filtered)
13931395

13941396
if outStats is not None:
13951397
samtools.idxstats(bam_filtered, outStats)
13961398

13971399
if outBam is None:
13981400
os.unlink(bam_filtered)
13991401

1402+
1403+
14001404
def parser_minimap2_idxstats(parser=argparse.ArgumentParser()):
14011405
parser.add_argument('inBam', help='Input unaligned reads, BAM format.')
14021406
parser.add_argument('refFasta', help='Reference genome, FASTA format, pre-indexed by Picard and Novoalign.')
@@ -1435,10 +1439,11 @@ def bwamem_idxstats(inBam, refFasta, outBam=None, outStats=None,
14351439

14361440
assert outBam or outStats, "Either outBam or outStats must be specified"
14371441

1442+
bam_aligned = util.file.mkstempfname('.aligned.bam')
14381443
if outBam is None:
1439-
bam_aligned = mkstempfname('.aligned.bam')
1444+
bam_filtered = mkstempfname('.filtered.bam')
14401445
else:
1441-
bam_aligned = outBam
1446+
bam_filtered = outBam
14421447

14431448
samtools = tools.samtools.SamtoolsTool()
14441449
bwa = tools.bwa.Bwa()
@@ -1456,8 +1461,9 @@ def bwamem_idxstats(inBam, refFasta, outBam=None, outStats=None,
14561461
bam_filtered,
14571462
require_pairs_to_be_proper=not doNotRequirePairsToBeProper,
14581463
reject_singletons=not keepSingletons)
1464+
os.unlink(bam_aligned)
14591465
else:
1460-
bam_filtered = bam_aligned
1466+
shutil.move(bam_aligned, bam_filtered)
14611467

14621468
if outStats is not None:
14631469
samtools.idxstats(bam_filtered, outStats)

test/unit/test_read_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ def test_bwamem_idxstats(self):
4848
self.assertEqual(actual_count, self.samtools.count(outBam, opts=['-F', '4']))
4949
self.assertGreater(actual_count, 18000)
5050

51+
def test_bwamem_idxstats_with_filtering(self):
52+
inBam = os.path.join(util.file.get_test_input_path(), 'G5012.3.testreads.bam')
53+
outBam = util.file.mkstempfname('.bam', directory=self.tempDir)
54+
outStats = util.file.mkstempfname('.stats.txt', directory=self.tempDir)
55+
read_utils.bwamem_idxstats(inBam, self.ebolaRef, outBam, outStats, filterReadsAfterAlignment=True)
56+
with open(outStats, 'rt') as inf:
57+
actual_count = int(inf.readline().strip().split('\t')[2])
58+
self.assertEqual(actual_count, self.samtools.count(outBam, opts=['-F', '4']))
59+
self.assertLess(actual_count, 18000)
60+
61+
outBamNoFiltering = util.file.mkstempfname('.bam', directory=self.tempDir)
62+
outStatsNoFiltering = util.file.mkstempfname('.stats.txt', directory=self.tempDir)
63+
read_utils.bwamem_idxstats(inBam, self.ebolaRef, outBamNoFiltering, outStatsNoFiltering, filterReadsAfterAlignment=False)
64+
with open(outStatsNoFiltering, 'rt') as inf:
65+
count_without_filtering = int(inf.readline().strip().split('\t')[2])
66+
67+
# the filtered count should be less than the count without filtering
68+
self.assertLess(actual_count, count_without_filtering)
69+
5170
def test_bwamem_idxstats_no_bam_output(self):
5271
inBam = os.path.join(util.file.get_test_input_path(), 'G5012.3.testreads.bam')
5372
outStats = util.file.mkstempfname('.stats.txt', directory=self.tempDir)

0 commit comments

Comments
 (0)