Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ samtools typehinted wrappers #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ if [[ "$1" == "--check" ]]; then
fi

banner "Executing in conda environment ${CONDA_DEFAULT_ENV} in directory ${root}"
run "Checking poetry" "poetry check"
run "Unit Tests" "python -m pytest -vv -r sx fgpyo"
run "Import Sorting" "isort --force-single-line-imports --profile black fgpyo"
run "Style Checking" "black --line-length 99 $black_extra_args fgpyo"
Expand Down
8 changes: 4 additions & 4 deletions fgpyo/io/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def test_assert_path_is_writeable_pass() -> None:
@pytest.mark.parametrize(
"suffix, expected",
[
(".gz", io._io.TextIOWrapper),
(".fa", io._io.TextIOWrapper),
(".gz", io.TextIOWrapper),
(".fa", io.TextIOWrapper),
],
)
def test_reader(
Expand All @@ -103,8 +103,8 @@ def test_reader(
@pytest.mark.parametrize(
"suffix, expected",
[
(".gz", io._io.TextIOWrapper),
(".fa", io._io.TextIOWrapper),
(".gz", io.TextIOWrapper),
(".fa", io.TextIOWrapper),
],
)
def test_writer(
Expand Down
11 changes: 7 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add index_ext to attributes docs.

"""

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer keeping the parameter and attribute names the same as it makes things more obvious/readable:

Suggested change
self.index_extension = index_ext
self.index_extension = index_extension

Although I see the convention has already broken for ext/extension. 🤔


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 Expand Up @@ -911,3 +912,5 @@ class SamOrder(enum.Enum):
Coordinate = "coordinate" #: coordinate sorted
QueryName = "queryname" #: queryname sorted
Unknown = "unknown" # Unknown SAM / BAM / CRAM sort order
# Sort by template-coordinate, SO is unsorted, GO is query, SS is template-coordinate:
TemplateCoordinate = "unsorted"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move/upgrade comments to become attributes docs in the main class docstring for auto documentation?

24 changes: 13 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 @@ -136,6 +137,11 @@ def __init__(
"SQ": (sd if sd is not None else SamBuilder.default_sd()),
"RG": [(rg if rg is not None else SamBuilder.default_rg())],
}
if sort_order == SamOrder.TemplateCoordinate:
self._header["HD"] = {
**self._header["HD"],
**{"GO": "query", "SS": "template-coordinate"},
}
if extra_header is not None:
self._header = {**self._header, **extra_header}
self._samheader = AlignmentHeader.from_dict(self._header)
Expand Down Expand Up @@ -588,18 +594,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(
alignment_file=Path(fp.name),
output=path,
index_output=index,
sort_order=self._sort_order,
)
return path

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