Skip to content

Fix LibraryReaderBase silently dropping precursors with partial None/NaN metadata - #479

Open
rpinho wants to merge 1 commit into
MannLabs:mainfrom
rpinho:fix-library-reader-partial-nulls
Open

rpinho wants to merge 1 commit into
MannLabs:mainfrom
rpinho:fix-library-reader-partial-nulls

Conversation

@rpinho

@rpinho rpinho commented Nov 27, 2026

Copy link
Copy Markdown

Who hits this: Anyone importing DIA-NN .parquet or Spectronaut/OpenSWATH TSV spectral libraries with LibraryReaderBase where any non-fragment metadata column (such as Genes, Protein.Names, or IonMobility/IM) has missing values for a subset of precursors (for example contaminant entries CON__*, unannotated UniProt entries, or non-TIMS precursors). #418

Root cause

In alphabase/spectral_library/reader.py:LibraryReaderBase._get_fragment_intensity():

  1. lib_df.dropna(axis=1, how="all") drops only completely empty columns, leaving partially populated metadata columns in non_fragment_columns.
  2. Unlike PSMReaderBase._load_file() (which calls _sanitize_missing_values()), LibraryReaderBase._load_file() leaves None/NaN in partially missing string columns (from Parquet or NA/NaN tokens in TSV).
  3. lib_df.groupby(non_fragment_columns) uses pandas' default dropna=True, which silently discards every precursor whose grouping key contains None or NaN in any non-fragment column. If every precursor has a missing value in at least one partially populated metadata column, groupby yields zero groups and np.concatenate(frag_intens_list) raises ValueError: need at least one array to concatenate.
  4. Additionally, alphabase/utils.py:_sanitize_missing_values() called df[column].fillna("") on string-like Categorical columns (for which pd.api.types.is_string_dtype returns True), which raises TypeError: Cannot setitem on a Categorical with a new category () whenever "" is not already in cat.categories.

Changes

  • alphabase/spectral_library/reader.py:
    • Sanitize partially missing string metadata via _sanitize_missing_values(lib_df) immediately after lib_df.dropna(axis=1, how="all") (preserving existing removal of 100%-empty optional columns).
    • Pass dropna=False, observed=True to lib_df.groupby(non_fragment_columns, ...) so precursors with partially missing numeric metadata (e.g. IM=NaN, which mobility_to_ccs_for_df() maps to ccs=0.0) are preserved without FutureWarning on pandas 2.x.
  • alphabase/utils.py:
    • Handle pd.CategoricalDtype columns in _sanitize_missing_values(): for string categoricals containing nulls, add "" to categories before fillna(""); leave non-null string categoricals and numeric categoricals untouched.
  • tests/unit/spectral_library/test_reader.py & tests/unit/test_utils.py:
    • Add unit regression tests covering partial None/NaN in Parquet, partial NA/NaN in TSV, all-precursors-partial-nulls (no ValueError), 100%-NaN optional columns still dropped, complete and partially missing Categorical Genes columns, and _sanitize_missing_values categorical handling.

Verification

  • Reproduced on both pandas 2.2.3 and pandas 3.1.1:
    • 3-precursor Parquet & TSV libraries (PEPTIDEK with Genes=None/NA, MELODYR with IM=NaN): baseline silently dropped 2 of 3 precursors (1 precursor / 7 dense fragment rows); patched keeps all 3 precursors / 20 dense fragment rows with genes == "" for PEPTIDEK, ccs == 0.0 for MELODYR, and theoretical fragment m/z values matching alphabase.constants to < 1e-4 Da.
    • 2-precursor Parquet where both precursors have a missing value in a different metadata column: baseline raised ValueError: need at least one array to concatenate; patched returns both precursors and 13 dense fragment rows.
    • Bundled 223-row diann_speclib_head.parquet with Genes re-encoded as category: imports all 21 precursors / 375 dense rows with exact parity to string Genes and zero TypeError/FutureWarning.
    • Downstream SpecLibBase (mobility_to_ccs_for_df, calc_precursor_mz, refine_df, save_hdf/load_hdf) verified on pandas 2.2.3.
  • Full test suites:
    • pandas 2.2.3: pytest tests/unit 314 passed; pytest tests/integration/test_psm_readers.py 21 passed (including test_diann_speclib_reader and test_diann_parquet_speclib_reader).
    • pandas 3.1.1: pytest tests/unit 314 passed.
  • Linting: ruff check, ruff format --check, and scoped ruff --config ruff-lint-psm-readers.toml all pass with 0 errors.

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.

1 participant