Skip to content

Initialize AlignedSegment in makeAlignedSegmentPairtoolized and handle missing SEQ - #288

Open
rpinho wants to merge 1 commit into
open2c:masterfrom
rpinho:fix-281-parse2-seq
Open

rpinho wants to merge 1 commit into
open2c:masterfrom
rpinho:fix-281-parse2-seq

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown

Summary

pairtools parse2 --add-columns seq (and pairtools parse --add-columns seq) fails on pysam >= 0.23.2 — outputting <class 'NotImplementedType'> on Linux/GCC 14 Python 3.12 (#281) or crashing with SIGBUS on macOS arm64 + pysam 0.24.1 — and also breaks --add-columns mismatches and --drop-seq.

Root cause: makeAlignedSegmentPairtoolized in pairtools/lib/parse_pysam.pyx constructed AlignedSegmentPairtoolized via __new__ without calling AlignedSegment.__init__, leaving AlignedSegment.cache (_AlignedSegment_Cache, added in pysam >= 0.23.2) as Py_None instead of an initialized cache object. Accessing cached sequence/quality properties (query_sequence, seq, query_qualities, query_alignment_sequence) or mutating them (--drop-seq) then dereferenced Py_None as _AlignedSegment_Cache.

In addition, parse_pysam_entry in pairtools/lib/parse.py read the deprecated sam.seq alias without guarding None for records whose SEQ field is * (l_qseq == 0), which serialized missing sequences as literal "None" instead of "*".

Who hits this: pairtools parse2 and pairtools parse users extracting read sequences (--add-columns seq), mismatches (--add-columns mismatches), or stripping sequences (--drop-seq) on pysam >= 0.23.2 (#281).

Changes

  • pairtools/lib/parse_pysam.pyx:
    • Instantiate AlignedSegmentPairtoolized(header) in makeAlignedSegmentPairtoolized so AlignedSegment.__init__ initializes its delegate and _AlignedSegment_Cache, then copy src via bam_copy1(dest._delegate, src) with a NULL check raising MemoryError.
  • pairtools/lib/parse.py:
    • In parse_pysam_entry, use sam.query_sequence and emit "*" when None.
  • tests/test_parse.py & tests/test_parse2.py:
    • Add regression tests covering AlignmentFilePairtoolized cached properties (query_sequence, seq, query_alignment_sequence, query_qualities), pairtools parse and pairtools parse2 with --add-columns seq on both SAM and BAM inputs (matching --flip/--no-flip settings, reverse-strand reference-orientation sequences), omitted SEQ == "*" on unmapped and supplementary walk alignments, all EXTRA_COLUMNS including mismatches with MD tags, and --keep-sam --drop-seq.

Verification

  • pytest tests/: 40 passed, 0 failed (up from 36 passed on 677b9085; all 4 new tests fail on 677b9085).
  • CI lint (flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics): 0 errors.
  • Memory/throughput check across five 200,000-record BAM runs: flat RSS after 25k records (93.62 MiB peak, ~3.94M records/s single-threaded) with clean bam1_t ownership across SAM, BAM, and reference-free CRAM.
  • Found and written with AI agents; a separate cross-family AI referee independently audited pysam 0.24.1 libcalignedsegment.pyx / htslib ownership, 102 CLI runs, throughput/RSS, and the full test/lint suites before opening this PR.

Fixes #281

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pairtools parse2 returns <class 'NotImplementedType'> when using --add-columns seq

1 participant