Skip to content

Conversion to and from DIA-NN .parquet - #418

Merged
mo-sameh merged 9 commits into
mainfrom
diann-parquet-translation
Aug 11, 2026
Merged

mo-sameh merged 9 commits into
mainfrom
diann-parquet-translation

Conversation

@mo-sameh

Copy link
Copy Markdown
Collaborator

Support DIA-NN parquet spectral libraries (read & write)

  • Read: LibraryReaderBase now loads .parquet and maps DIA-NN's dot-notation columns (via psm_reader.yaml).
  • Write: new translate_to_parquet / speclib_to_diann_df emit DIA-NN's schema ((UniMod:N) sequences, Flags, INT64/FLOAT dtypes, no Signature); TSV path unchanged.
  • Adds reader + round-trip tests and fixtures.

To-do list (outside contributers only)

@mschwoer mschwoer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very cool!

Comment thread alphabase/spectral_library/translate.py Resolved
Comment thread alphabase/spectral_library/translate.py Resolved
Comment thread alphabase/spectral_library/translate.py Outdated
nterm="",
cterm="",
)
df["Stripped.Sequence"] = precursor_df["sequence"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this creating a copy or a reference? :-)

Comment thread alphabase/spectral_library/translate.py Resolved
Comment thread tests/integration/test_psm_readers.py Resolved
Comment thread alphabase/spectral_library/reader.py Outdated
"""Load the spectral library from a csv file."""
"""Load the spectral library from a csv/tsv or parquet file.

DIA-NN 2.0 exports its spectral libraries as parquet (``report-lib.parquet``),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is our speclib class, right? can we read DIANN data into this so easily?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answered it myself: we can ..

this new capability should be added to docs/readme.

Comment thread alphabase/spectral_library/translate.py Outdated
)


# fragment column names passed to `merge_precursor_fragment_df`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this all be moved to a new file translate_diann.py ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to a new translate_diann.py; Although I think a larger refactor for 'exporters' can be done in a separate PR, for now it imports the shared helpers from translate.py

Comment thread alphabase/spectral_library/translate.py Outdated
return df[DIANN_PARQUET_COLUMN_ORDER]


def translate_to_parquet(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am missing some link to this cool functionality from the Readme and/or docs :-)

Comment thread alphabase/spectral_library/translate.py Outdated
return df[DIANN_PARQUET_COLUMN_ORDER]


def translate_to_parquet(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, could there be a thin wrapper in the SpecLibBase class that calls this method?

@GeorgWa
GeorgWa self-requested a review August 6, 2026 10:53

@GeorgWa GeorgWa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
If you have Ideas how to structure the high level code of writers more OOP,
(flattening, mod trasnlation, column mapping) etc. I think this would be worth it.

return default


def speclib_to_diann_df( # noqa: PLR0913, PLR0915

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Historically the writer has been a standalone function. I think this was not an active design decision but more of a quick hack. I would favour an OOP DIANNLibraryWriter(version='1.9'). THoughts @mschwoer ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(could be a TODO and/or a separate PR)

Comment thread alphabase/spectral_library/reader.py Outdated
Comment on lines +254 to +255
if isinstance(filename, (str, Path)) and str(filename).endswith(".parquet"):
return pd.read_parquet(filename)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we need the isinstance check here ..
downstream code would fail anyway if you passed int ..

either remove or make it a general check at the beginning of this method

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was guarding against a stream, not an int. _load_file is also called with an io.StringIO, which is a documented input of the base class (PSMReaderBase._load_file is annotated Union[str, Path, io.StringIO] and branches on it), used by 15 tests and several doc notebooks. Removing the check fails test_diann_speclib_reader and test_msfragger_speclib_reader.

I've kept the guard but switched it to check io.StringIO explicitly.

I suggest (in a separate PR): drop stream support altogether. Nothing in production uses a strea, it exists only because the tests embed 26 TEST_DATA_* TSV blobs as inline strings. Moving those into tests/integration/input_data/ as fixtures would let us delete this branch, the # for unit tests branch in _get_delimiter, and rename the poorly named filename argument (base + 9 overrides) in one go. Let me know what you think @mschwoer ?

return default


def speclib_to_diann_df( # noqa: PLR0913, PLR0915

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(could be a TODO and/or a separate PR)

writer = pq.ParquetWriter(parquet_path, schema)
try:
for i in tqdm.tqdm(range(0, len(precursor_df), batch_size)):
batch_speclib._precursor_df = precursor_df.iloc[i : i + batch_size] # noqa: SLF001

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we update only the _precursor_df of the batch_speclib, keeping ._fragment_intensity_df and ._fragment_mz_df from l. 339-340.
isn't batch_speclib in a weird state then? maybe worth adding a comment why this works

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(my guess is that the dangling entries of _fragment_intensity_df and _fragment_mz_df are just ignored downstream)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They aren't really ignored, but the full frag dfs are passed. merge_precursor_fragment_df indexes the fragment frames directly with each precursor's frag_start_idx/frag_stop_idx, and those are absolute offsets into the complete frames. We could of course slice the fragment dfs too and rebase the indices per batch, but that's extra bookkeeping for no gain, after all the point of batching here is to bound the memory needed of the flat output, not the library itself.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a brief comment explaining this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, already had one there 👍

@mo-sameh
mo-sameh merged commit 5a52a4e into main Aug 11, 2026
3 checks passed
@mo-sameh
mo-sameh deleted the diann-parquet-translation branch August 11, 2026 11:07
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.

3 participants