Skip to content

Commit

Permalink
feat: read from stdin and write to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Oct 25, 2023
1 parent 28ab1e3 commit aebe9f3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion fgpyo/sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@

import enum
import io
import sys
from pathlib import Path
from typing import IO
from typing import Any
Expand Down Expand Up @@ -187,6 +188,12 @@
_IOClasses = (io.TextIOBase, io.BufferedIOBase, io.RawIOBase, io.IOBase)
"""The classes that should be treated as file-like classes"""

_STDIN_PATHS = ["-", "stdin", "/dev/stdin"]
"""Paths that should be opened as stdin."""

_STDOUT_PATHS = ["-", "stdout", "/dev/stdout"]
"""Paths that should be opened as stdout."""


@enum.unique
class SamFileType(enum.Enum):
Expand Down Expand Up @@ -238,7 +245,12 @@ def _pysam_open(
:class:`~pysam.AlignmentFile`; may not include "mode".
"""

if isinstance(path, (str, Path)): # type: ignore
if isinstance(path, str) and open_for_reading and path in _STDIN_PATHS:
path = sys.stdin
elif isinstance(path, str) and not open_for_reading and path in _STDOUT_PATHS:
file_type = SamFileType.SAM if file_type is None else file_type
path = sys.stdout
elif isinstance(path, (str, Path)): # type: ignore
file_type = file_type or SamFileType.from_path(path)
path = str(path)
elif not isinstance(path, _IOClasses): # type: ignore
Expand Down

0 comments on commit aebe9f3

Please sign in to comment.