skills/pysam/references/migration_to_0_24.md
Use this checklist when moving an existing environment or pipeline to pysam 0.24.0.
For a reproducible environment:
uv pip install "pysam==0.24.0"
If maintaining Python/Cython extensions that cimport pysam declarations, rebuild them against 0.24. Do not reuse binaries built against a different pysam ABI without verification.
New CRAM output defaults to CRAM 3.1. If downstream software only supports 3.0:
pysam.AlignmentFile(
"output.cram",
"wc",
header=header,
reference_filename="reference.fa",
format_options=["version=3.0"],
)
Upgrade downstream tooling instead of forcing 3.0 when possible.
Implicit EBI reference fetching is no longer enabled by default. Existing pipelines that relied on hidden network retrieval can now fail.
Preferred fix:
pysam.AlignmentFile(
"input.cram",
"rc",
reference_filename="reference.fa",
)
Only configure REF_PATH and REF_CACHE when managed checksum-based lookup is
intentional. Do not restore remote fetching without documenting network,
cache, and provenance behavior.
format_optionsPysam 0.24 fixes Python 3 handling so AlignmentFile(format_options=...) and
HTSFile.add_hts_options() accept documented str values rather than
requiring bytes:
format_options=["version=3.0"]
Remove workarounds that encode these strings to bytes.
Pysam 0.23.2 restored top-level aliases such as pysam.CMATCH after a Cython
behavior change, but the release notes warn that a future release will remove
them.
Migrate new and maintained code:
# Compatibility alias
pysam.CMATCH
# Preferred
pysam.CIGAR_OPS.CMATCH
Audit all CIGAR operators, not only CMATCH.
Pysam 0.24:
AlignedSegment.modified_basesRetest MM/ML parsing with:
modified_bases_forwardpysam.array_to_qualitystring() was optimized substantially in 0.24. Remove
custom micro-optimizations only after confirming identical handling of missing
or invalid values.
The 0.24 release fixes coverage functionality for Python 3.13 and later.
Re-run count_coverage() regression cases when upgrading both Python and
pysam:
Pysam 0.24 adds documentation for many VariantHeader and VariantRecord
internal field wrappers. Prefer documented public mappings and methods over
introspection into Cython implementation details.
Recent 0.23.x releases also improved AlignmentFile and VariantFile I/O
exception handling. Catch specific OSError/ValueError conditions and do
not depend on old message text.
Pysam 0.23.1 was yanked because it broke binary compatibility for Cython
projects by changing AlignedSegment size. Pysam 0.23.2 restored
compatibility. Pure Python users were not affected in the same way.
When migrating from 0.23:
Keep wrapped command output out of memory for bulk or binary operations:
import pysam.samtools
pysam.samtools.sort(
"-o",
"sorted.bam",
"input.bam",
catch_stdout=False,
)
This is not new to 0.24, but it is important when updating older examples that
omit catch_stdout=False.
pysam.__version__ and pysam.__samtools_version__.count_coverage() thresholds.