Repository navigation
Conversion to and from DIA-NN .parquet - #418
Conversation
| nterm="", | ||
| cterm="", | ||
| ) | ||
| df["Stripped.Sequence"] = precursor_df["sequence"] |
There was a problem hiding this comment.
is this creating a copy or a reference? :-)
| """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``), |
There was a problem hiding this comment.
but this is our speclib class, right? can we read DIANN data into this so easily?
There was a problem hiding this comment.
answered it myself: we can ..
this new capability should be added to docs/readme.
| ) | ||
|
|
||
|
|
||
| # fragment column names passed to `merge_precursor_fragment_df` |
There was a problem hiding this comment.
could this all be moved to a new file translate_diann.py ?
There was a problem hiding this comment.
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
| return df[DIANN_PARQUET_COLUMN_ORDER] | ||
|
|
||
|
|
||
| def translate_to_parquet( |
There was a problem hiding this comment.
I am missing some link to this cool functionality from the Readme and/or docs :-)
| return df[DIANN_PARQUET_COLUMN_ORDER] | ||
|
|
||
|
|
||
| def translate_to_parquet( |
There was a problem hiding this comment.
also, could there be a thin wrapper in the SpecLibBase class that calls this method?
GeorgWa
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
(could be a TODO and/or a separate PR)
| if isinstance(filename, (str, Path)) and str(filename).endswith(".parquet"): | ||
| return pd.read_parquet(filename) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
(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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
(my guess is that the dangling entries of _fragment_intensity_df and _fragment_mz_df are just ignored downstream)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
maybe add a brief comment explaining this
There was a problem hiding this comment.
Yep, already had one there 👍
Support DIA-NN parquet spectral libraries (read & write)
LibraryReaderBasenow loads .parquet and maps DIA-NN's dot-notation columns (viapsm_reader.yaml).translate_to_parquet/speclib_to_diann_dfemit DIA-NN's schema ((UniMod:N) sequences, Flags, INT64/FLOAT dtypes, no Signature); TSV path unchanged.To-do list (outside contributers only)