Skip to content

NEW: annotating real spectra vs calculated one - #314

Merged
boopthesnoot merged 9 commits into
mainfrom
annotate_migration
Dec 4, 2025
Merged

boopthesnoot merged 9 commits into
mainfrom
annotate_migration

Conversation

@boopthesnoot

@boopthesnoot boopthesnoot commented Apr 9, 2025 •

Copy link
Copy Markdown
Contributor

@GeorgWa added functionality to compare experimental spectra with calculated ones, so all credit to him.

taken from https://github.com/MannLabs/metaptcm/blob/cbf579b52041d96636dd04c29d4855e5391bd136/metaptcm/utils/annotation.py so @mschwoer already reviewed this

Feel free to suggest a different placement within the package, also added some noqas.

@boopthesnoot
boopthesnoot requested review from GeorgWa and mschwoer April 9, 2025 16:01
@boopthesnoot
boopthesnoot marked this pull request as draft April 10, 2025 07:26

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

This module currently contains the conversion of a flat (annotated) library to a dense library. In the meantime we have implemented a more optimized and clean version in alphabase.

See here: create_dense_matrices

# Loss type mappings - these map loss type numeric codes to their string representations
# Used in annotate.py and flat.py

LOSS_NUMBER_TO_TYPE = {0: "", 17: "_NH3", 18: "_H2O", 98: "_modloss"}

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.

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.

How much redundency is there between these two modules?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i'll try to minimize redundancy, thank you for pointing it out

Comment thread alphabase/spectral_library/annotate.py Outdated
----------
speclib_flat : SpecLibFlat
A spectral library flat object containing precursor information.
raw_data : MzMLReader | ThermoRawData

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.

I think the most accurate thig would be to use a MsDataBase from alpharaw here as annotation type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

can't use alpharaw, otherwise we get circular dependencies

Comment thread alphabase/spectral_library/annotate.py Outdated
)


def _get_dense_column( # noqa: PLR0913

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.

Part of flat -> dense

Comment thread alphabase/spectral_library/annotate.py Outdated
return "_".join(items)


def _add_frag_column_annotation(

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.

Part of flat -> dense

Comment thread alphabase/spectral_library/annotate.py Outdated
)


def _assign_to_dense(

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.

Part of flat -> dense

@GeorgWa
GeorgWa self-requested a review April 11, 2025 11: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.

It's a bit integration work but then we should be good :)

Comment thread alphabase/spectral_library/annotate.py Outdated
)
from alphabase.spectral_library.flat import SpecLibFlat

UNANNOTATED_TYPE = 255

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.

can you move this to alphabase/peptide/fragment.py?

Comment thread alphabase/spectral_library/annotate.py Outdated
return outlib_flat


def calculate_pif(spectrum_peak_df: pd.DataFrame) -> float:

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.

What do you think about moving them into a metrics.py module? similar for gini etc.

Comment thread alphabase/spectral_library/annotate.py Outdated
)


def _sequence_coverage_metric(flatlib: SpecLibFlat) -> None:

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.

move to metrics.py

Comment thread alphabase/spectral_library/annotate.py Outdated
)


def _sequence_gini_metric(flatlib: SpecLibFlat) -> None:

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.

move to metrics.py

Comment thread alphabase/spectral_library/annotate.py Outdated
)


def _normalized_count_metric(flatlib: SpecLibFlat) -> None:

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.

move to metrics.py

Comment thread alphabase/spectral_library/annotate.py Outdated
return np.median(valid_values)


def _mass_accuracy_metric(flatlib: SpecLibFlat) -> None:

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.

move to metrics.py

Comment thread alphabase/spectral_library/annotate.py Outdated
mass_error_ppm=mass_error_ppm,
)

matched_precursor_df.loc[i, "pif"] = calculate_pif(spectrum_peak_df)

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.

Sorry, that I'm asking you to fix my mistakes here :D

could you change the calulate_pif() function so it's structured and applied in the same way as the other metrics?
So it's called after annotation and operates on the precursor df.

)


def add_dense_lib(

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.

what do you think about adding this function directly to the speclib flat object?
so we can call SpecLibFlat.add_dense_lib().
I think this fits the ab pattern better.

# Now if we have a fragment type that is a,b,c we should have the corresponding x,y,z

corresponding = {"a": "x", "b": "y", "c": "z", "x": "a", "y": "b", "z": "c"}
loss_number_to_type = {0: "", 18: "_H2O", 17: "_NH3", 98: "_modloss"}

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.

get_full_charged_types() is deprecated, so there is no need to do any changes here.

# Loss type mappings - these map loss type numeric codes to their string representations
# Used in annotate.py and flat.py

LOSS_NUMBER_TO_TYPE = {0: "", 17: "_NH3", 18: "_H2O", 98: "_modloss"}

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.

I think this can be removed as it's not needed by the new AB based implementation :).

@boopthesnoot
boopthesnoot marked this pull request as ready for review October 9, 2025 12:49
@boopthesnoot
boopthesnoot merged commit 66dd599 into main Dec 4, 2025
3 checks passed
@boopthesnoot
boopthesnoot deleted the annotate_migration branch December 4, 2025 13:09
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