From 8849c309c0c3d8a28cd39b5d39932735399d38ba Mon Sep 17 00:00:00 2001 From: Matt Stone Date: Wed, 17 Apr 2024 14:08:32 -0400 Subject: [PATCH] feat: Add end property on SupplementaryAlignment --- fgpyo/sam/__init__.py | 6 ++++++ fgpyo/sam/tests/test_supplementary_alignments.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/fgpyo/sam/__init__.py b/fgpyo/sam/__init__.py index e67d7e91..71a8cfbb 100644 --- a/fgpyo/sam/__init__.py +++ b/fgpyo/sam/__init__.py @@ -153,6 +153,7 @@ import enum import io +from functools import cached_property from pathlib import Path from typing import IO from typing import Any @@ -551,6 +552,11 @@ def __str__(self) -> str: ) ) + @cached_property + def end(self) -> int: + """The 0-based exclusive end position of the alignment.""" + return self.start + self.cigar.length_on_target() + @staticmethod def parse(string: str) -> "SupplementaryAlignment": """Returns a supplementary alignment parsed from the given string. The various fields diff --git a/fgpyo/sam/tests/test_supplementary_alignments.py b/fgpyo/sam/tests/test_supplementary_alignments.py index 42f4ce6d..1102048f 100644 --- a/fgpyo/sam/tests/test_supplementary_alignments.py +++ b/fgpyo/sam/tests/test_supplementary_alignments.py @@ -69,3 +69,12 @@ def test_from_read() -> None: read = builder.add_single(attrs={"SA": f"{s1};{s2};"}) assert SupplementaryAlignment.from_read(read) == [sa1, sa2] + + +def test_end() -> None: + """Test that we can get the end of a SupplementaryAlignment.""" + + s1 = SupplementaryAlignment.parse("chr1,123,+,50S100M,60,0") + + # NB: the SA tag is one-based, but SupplementaryAlignment is zero-based + assert s1.end == 222