From f8c479ddd9b7cd0dfcdab920f92d20a7c0f13139 Mon Sep 17 00:00:00 2001 From: clintval Date: Thu, 26 Dec 2024 20:00:42 -0500 Subject: [PATCH] Add methods to fix mate info on non-primaries and templates --- tests/fgpyo/sam/test_sam.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/fgpyo/sam/test_sam.py b/tests/fgpyo/sam/test_sam.py index 31a2f75..5252514 100755 --- a/tests/fgpyo/sam/test_sam.py +++ b/tests/fgpyo/sam/test_sam.py @@ -496,7 +496,8 @@ def test_not_is_proper_pair_if_too_far_apart() -> None: assert not is_proper_pair(r1, r2) -def test_isize() -> None: +def test_isize_when_r2_defined() -> None: + """Tests that an insert size can be calculated when both input records are defined.""" builder = SamBuilder() r1, r2 = builder.add_pair(chrom="chr1", start1=100, cigar1="115M", start2=250, cigar2="40M") assert sam.isize(r1, r2) == 190 @@ -506,6 +507,40 @@ def test_isize() -> None: assert sam.isize(r1, r2) == 0 +def test_isize_when_r2_undefined() -> None: + """Tests that an insert size can be calculated when R1 is provided only.""" + builder = SamBuilder() + r1, r2 = builder.add_pair(chrom="chr1", start1=100, cigar1="115M", start2=250, cigar2="40M") + assert sam.isize(r1) == 190 + assert sam.isize(r2) == -190 + + r1, r2 = builder.add_pair(chrom="chr1", start1=100, cigar1="115M") + assert sam.isize(r1) == 0 + assert sam.isize(r2) == 0 + + +def test_isize_when_r2_undefined_indels_in_r2_cigar() -> None: + """Tests that an insert size can be derived without R2 by using R2's cigar.""" + builder = SamBuilder() + r1, _ = builder.add_pair( + chrom="chr1", + start1=100, + cigar1="115M", + start2=250, + cigar2="10S5M1D1M1D2I2D30M", # only 40bp reference-consuming operators + ) + assert sam.isize(r1) == 190 + + +def test_isize_raises_when_r2_not_provided_and_no_mate_cigar_tag() -> None: + """Tests that an insert size can be calculated when both input records are defined.""" + builder = SamBuilder() + r1, _ = builder.add_pair(chrom="chr1", start1=100, cigar1="115M", start2=250, cigar2="40M") + r1.set_tag("MC", None) + with pytest.raises(ValueError, match="Cannot determine proper pair status without R2's cigar"): + sam.isize(r1) + + def test_calc_edit_info_no_edits() -> None: chrom = "ACGCTAGACTGCTAGCAGCATCTCATAGCACTTCGCGCTATAGCGATATAAATATCGCGATCTAGCG" builder = SamBuilder(r1_len=30)