Skip to content

Commit

Permalink
Adding initial batch of samtools type hinted wrapper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Roach authored and TedBrookings committed Nov 14, 2023
1 parent ca4a572 commit 21f6540
Show file tree
Hide file tree
Showing 5 changed files with 1,055 additions and 15 deletions.
9 changes: 5 additions & 4 deletions fgpyo/sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@ class SamFileType(enum.Enum):
ext (str): The standard file extension for this file type.
"""

def __init__(self, mode: str, ext: str) -> None:
def __init__(self, mode: str, ext: str, index_ext: Optional[str]) -> None:
self.mode = mode
self.extension = ext
self.index_extension = index_ext

SAM = ("", ".sam")
BAM = ("b", ".bam")
CRAM = ("c", ".cram")
SAM = ("", ".sam", None)
BAM = ("b", ".bam", ".bai")
CRAM = ("c", ".cram", ".crai")

@classmethod
def from_path(cls, path: Union[Path, str]) -> "SamFileType":
Expand Down
19 changes: 8 additions & 11 deletions fgpyo/sam/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pysam import AlignmentHeader

from fgpyo import sam
from fgpyo import samtools
from fgpyo.sam import SamOrder


Expand Down Expand Up @@ -588,18 +589,14 @@ def to_path(
if pred(rec):
writer.write(rec)

samtools_sort_args = ["-o", str(path), fp.name]

file_handle.close()
if self._sort_order == SamOrder.QueryName:
# Ignore type hints for now until we have wrappers to use here.
pysam.sort("-n", *samtools_sort_args) # type: ignore
elif self._sort_order == SamOrder.Coordinate:
# Ignore type hints for now until we have wrappers to use here.
if index:
samtools_sort_args.insert(0, "--write-index")
pysam.sort(*samtools_sort_args) # type: ignore

if self._sort_order not in {SamOrder.Unsorted, SamOrder.Unknown}:
samtools.sort(
input=Path(fp.name),
output=path,
index_output=index,
sort_order=self._sort_order,
)
return path

def __len__(self) -> int:
Expand Down
Loading

0 comments on commit 21f6540

Please sign in to comment.