Skip to content

Commit 8849c30

Browse files
committed
feat: Add end property on SupplementaryAlignment
1 parent a41a565 commit 8849c30

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

fgpyo/sam/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153

154154
import enum
155155
import io
156+
from functools import cached_property
156157
from pathlib import Path
157158
from typing import IO
158159
from typing import Any
@@ -551,6 +552,11 @@ def __str__(self) -> str:
551552
)
552553
)
553554

555+
@cached_property
556+
def end(self) -> int:
557+
"""The 0-based exclusive end position of the alignment."""
558+
return self.start + self.cigar.length_on_target()
559+
554560
@staticmethod
555561
def parse(string: str) -> "SupplementaryAlignment":
556562
"""Returns a supplementary alignment parsed from the given string. The various fields

fgpyo/sam/tests/test_supplementary_alignments.py

+9
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ def test_from_read() -> None:
6969

7070
read = builder.add_single(attrs={"SA": f"{s1};{s2};"})
7171
assert SupplementaryAlignment.from_read(read) == [sa1, sa2]
72+
73+
74+
def test_end() -> None:
75+
"""Test that we can get the end of a SupplementaryAlignment."""
76+
77+
s1 = SupplementaryAlignment.parse("chr1,123,+,50S100M,60,0")
78+
79+
# NB: the SA tag is one-based, but SupplementaryAlignment is zero-based
80+
assert s1.end == 222

0 commit comments

Comments
 (0)