Skip to content

HTSlib warnings can't be suppressed? #1303

Open
@ohickl

Description

@ohickl

Description:

Hi,

I'm encountering a persistent warning message when working with SAM/BAM files using pysam, and I'm seeking your advice on how to suppress or resolve it.

Issue Description

When processing my SAM/BAM files with pysam, I frequently receive the following warning message:

    [W::sam_hrecs_update_hashes] PG line with multiple ID tags. The first encountered was preferred ...

I understand that this warning is generated when pysam (or the underlying htslib library) encounters multiple PG (Program) lines in the file header with the same ID tag, and that the software defaults to using the first ID it encounters.
I know the reason for this in this instance and want to ignore it.

Steps I’ve Tried to Suppress the Warning

I’ve attempted several common methods to suppress or redirect this warning, including:

  • Using Python's warnings.filterwarnings:
    import warnings
    warnings.filterwarnings("ignore", message=".*PG line with multiple ID tags.*", category=UserWarning, module="pysam")

This approach did not suppress the warning, possibly because it's not using Python’s standard warning system.

  • Redirecting sys.stderr:
    import sys
    import os
    from contextlib import redirect_stderr

    with open(os.devnull, 'w') as fnull:
        with redirect_stderr(fnull):
            # Code that triggers the warning

This also did not work, which leads me to believe the message is being printed by the underlying C library in a way that bypasses Python’s standard output streams.

My Questions

  • Is there a recommended method to suppress or redirect this specific warning message?
  • If this is a common issue, would it be possible to add an option in pysam to suppress such warnings?

Any advice or guidance you can provide would be greatly appreciated. Thank you for your time and for maintaining this incredibly useful library!

Environment:

pysam version: 0.22.1
Python version: 3.12.4
Operating System: Nobara Linux 40 (KDE Plasma)

Best,

Oskar

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions