From aebe9f36a18d9f5a8e2b00c7d7e13a548263aeff Mon Sep 17 00:00:00 2001 From: Matt Stone Date: Tue, 24 Oct 2023 20:13:20 -0400 Subject: [PATCH] feat: read from stdin and write to stdout --- fgpyo/sam/__init__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/fgpyo/sam/__init__.py b/fgpyo/sam/__init__.py index e054350a..4d3a0979 100644 --- a/fgpyo/sam/__init__.py +++ b/fgpyo/sam/__init__.py @@ -152,6 +152,7 @@ import enum import io +import sys from pathlib import Path from typing import IO from typing import Any @@ -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): @@ -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