Skip to content

Fix pairtools select column indexing with --remove-columns and AST-based condition binding - #289

Open
rpinho wants to merge 1 commit into
open2c:masterfrom
rpinho:fix-select-remove-columns
Open

rpinho wants to merge 1 commit into
open2c:masterfrom
rpinho:fix-select-remove-columns

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown

Fixes #279

Who hits this: Any pairtools select user combining --remove-columns with a filter condition (for example dropping read_len1,read_len2 while filtering on mapq1/mapq2), or calling evaluate_stream / evaluate_df with custom type_cast or conditions containing column names inside string literals or f-strings. #279

Root cause

  1. Shifted/missing column indexing (pairtools/cli/select.py): select_py() removed --remove-columns from the header via headerops.set_columns(...) before calling headerops.extract_column_names(header) to build column_names for evaluate_stream(). Because evaluate_stream() reads the unmodified input rows, every column after a removed column was evaluated at the wrong index (e.g., mapq1/mapq2 silently read read_len1/read_len2 values), and filtering on a removed column raised NameError.
  2. Global TYPES mutation (pairtools/lib/select.py): evaluate_stream() called TYPES.update(dict(type_cast)) in place, leaking custom casts into later evaluate_stream() / evaluate_df() calls in the same Python process.
  3. Regex column-name substitution (pairtools/lib/select.py): re.sub(r"\b%s\b" % col, ...) rewrote column names appearing inside string literals (e.g. readID == "mapq1"), attribute/keyword names, f-string debug expressions, and comprehension/lambda parameters.

Changes

  • pairtools/cli/select.py:
    • Extract input_columns (and column_names) from the unmodified header before applying --remove-columns, so conditions always index the input row layout and can filter on columns being removed from the output.
    • Deduplicate --remove-columns entries, compute column_scheme once from input_columns, and avoid splitting/rejoining dropped rows when --output-rest is not set.
  • pairtools/lib/select.py:
    • Copy TYPES (_types = TYPES.copy()) in evaluate_stream() instead of mutating the module-level default mapping.
    • Replace regex substitution with _ColumnNameBinder (ast.NodeTransformer) + _compile_condition(), rewriting only unshadowed ast.Name nodes in ast.Load context so string literals, attribute names, keyword argument names, f-string expressions (f"{mapq1=}" across Python 3.11/3.12+), lambda parameters, and comprehension targets are preserved.
  • tests/test_select.py:
    • Add unit and CLI regression tests covering --remove-columns with downstream and removed-column filters (both selected and --output-rest streams), TYPES isolation across calls, and AST scoping / string-literal / f-string conditions.

Verification

  • Reproduced issue bug in "select" when filtering and --remove-columns at the same time #279: with --remove-columns read_len1,read_len2 and (mapq1>=30) and (mapq2>=30), baseline kept all 4 rows (0 in --output-rest) by comparing read_len1/read_len2; patched matches the unprojected control (r1/r4 selected, r2/r3 in --output-rest, 12 output columns in both streams).
  • Verified filtering on a removed column ((read_len1>=80) and (mapq1>=30) with --remove-columns read_len1): baseline raises NameError; patched selects the matching rows and writes the 13-column projected output.
  • Ran full pytest (46 passed) and flake8 (0 errors) on both Python 3.11 and Python 3.12, plus an independent CLI matrix across 21 filter/projection partitions.

Found and written with AI agents; a separate AI referee reproduced the problem before this PR.

🤖 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.

bug in "select" when filtering and --remove-columns at the same time

1 participant