Skip to content

Commit

Permalink
Suppress stderr printing when sam indices are older than sam file
Browse files Browse the repository at this point in the history
  • Loading branch information
TedBrookings committed Jul 27, 2023
1 parent 22b579c commit 195b4b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
25 changes: 25 additions & 0 deletions fgpyo/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
import gzip
import io
import os
import sys
from contextlib import contextmanager
from pathlib import Path
from typing import IO
from typing import Any
from typing import Generator
from typing import Iterable
from typing import Iterator
from typing import Set
Expand Down Expand Up @@ -196,3 +199,25 @@ def write_lines(path: Path, lines_to_write: Iterable[Any]) -> None:
for line in lines_to_write:
writer.write(str(line))
writer.write("\n")


@contextmanager
def redirect_dev_null(
file_num: int = sys.stderr.fileno(),
) -> Generator[None, None, None]:
"""A context manager that redirects output of file handle to /dev/null
Args:
file_num: number of filehandle to redirect. Uses stderr by default
"""
# open /dev/null for writing
f_devnull = os.open(os.devnull, os.O_RDWR)
# save old file descriptor and redirect stderr to /dev/null
save_stderr = os.dup(file_num)
os.dup2(f_devnull, file_num)

yield

# restore file descriptor and close devnull
os.dup2(save_stderr, file_num)
os.close(f_devnull)
8 changes: 6 additions & 2 deletions fgpyo/sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
from pysam import AlignmentFile as SamFile
from pysam import AlignmentHeader as SamHeader

import fgpyo.io
from fgpyo.collections import PeekableIterator

SamPath = Union[IO[Any], Path, str]
Expand Down Expand Up @@ -259,8 +260,11 @@ def _pysam_open(
if unmapped and open_for_reading:
kwargs["check_sq"] = False

# Open it!
return pysam.AlignmentFile(path, **kwargs)
# Open it alignment file, suppressing stderr in case index files are older than SAM file
with fgpyo.io.redirect_dev_null():
alignment_file = pysam.AlignmentFile(path, **kwargs)
# now restore stderr and return the alignment file
return alignment_file


def reader(
Expand Down
25 changes: 2 additions & 23 deletions fgpyo/vcf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
order via the :func:`~variantbuilder.VariantBuilder.to_sorted_list()` method.
"""
import os
import sys
from abc import ABC
from abc import abstractmethod
from contextlib import contextmanager
Expand All @@ -77,6 +75,7 @@
from pysam import VariantFile as VcfWriter
from pysam import VariantHeader

import fgpyo.io
from fgpyo.sam.builder import SamBuilder

Variant = TypeVar("Variant")
Expand Down Expand Up @@ -143,26 +142,6 @@ class VcfFieldNumber(Enum):
UNKNOWN = "."


@contextmanager
def redirect_dev_null(file_num: int = sys.stderr.fileno()) -> Generator[None, None, None]:
"""A context manager that redirects output of file handle to /dev/null
Args:
file_num: number of filehandle to redirect. Uses stderr by default
"""
# open /dev/null for writing
f_devnull = os.open(os.devnull, os.O_RDWR)
# save old file descriptor and redirect stderr to /dev/null
save_stderr = os.dup(file_num)
os.dup2(f_devnull, file_num)

yield

# restore file descriptor and close devnull
os.dup2(save_stderr, file_num)
os.close(f_devnull)


@contextmanager
def reader(path: VcfPath) -> Generator[VcfReader, None, None]:
"""Opens the given path for VCF reading
Expand All @@ -171,7 +150,7 @@ def reader(path: VcfPath) -> Generator[VcfReader, None, None]:
path: the path to a VCF, or an open file handle
"""
if isinstance(path, (str, Path, TextIO)):
with redirect_dev_null():
with fgpyo.io.redirect_dev_null():
# to avoid spamming log about index older than vcf, redirect stderr to /dev/null: only
# when first opening the file
_reader = VariantFile(path, mode="r") # type: ignore
Expand Down

0 comments on commit 195b4b3

Please sign in to comment.