From da7b61932c95346068c03c841e03f4b6cbe706b0 Mon Sep 17 00:00:00 2001 From: Michael Strobel Date: Fri, 20 Feb 2026 15:00:01 -0800 Subject: [PATCH] Fix charge=0 breaking things. --- modifinder/classes/ModiFinder.py | 14 ++++++++++---- .../engines/alignment/CosineAlignmentEngine.py | 8 +++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/modifinder/classes/ModiFinder.py b/modifinder/classes/ModiFinder.py index 38b5080..7d912ea 100644 --- a/modifinder/classes/ModiFinder.py +++ b/modifinder/classes/ModiFinder.py @@ -136,18 +136,20 @@ def __init__( -------- """ - self.network = None self.unknowns = None self.ppm_tolerance = ppm_tolerance + + # Add ppm_tolerance to kwargs + kwargs["ppm_tolerance"] = ppm_tolerance if alignmentEngine is None: - self.alignmentEngine = CosineAlignmentEngine(ppm_tolerance = self.ppm_tolerance, **kwargs) + self.alignmentEngine = CosineAlignmentEngine(**kwargs) else: self.alignmentEngine = alignmentEngine if annotationEngine is None: - self.annotationEngine = MAGMaAnnotationEngine(ppm_tolerance = self.ppm_tolerance, **kwargs) + self.annotationEngine = MAGMaAnnotationEngine(**kwargs) else: self.annotationEngine = annotationEngine @@ -230,7 +232,11 @@ def add_adjusted_edge(self, u, v, edgeDetail: EdgeDetail = None, **kwargs): smaller = u if self.network.nodes[u]["compound"].spectrum.precursor_mz <= self.network.nodes[v]["compound"].spectrum.precursor_mz else v larger = u if self.network.nodes[u]["compound"].spectrum.precursor_mz > self.network.nodes[v]["compound"].spectrum.precursor_mz else v if edgeDetail is None: - edgeDetail = self.alignmentEngine.align_single(self.network.nodes[smaller]["compound"].spectrum, self.network.nodes[larger]["compound"].spectrum, **kwargs) + + edgeDetail = self.alignmentEngine.align_single( + self.network.nodes[smaller]["compound"].spectrum, + self.network.nodes[larger]["compound"].spectrum, + **kwargs) self.update_edge(smaller, larger, edgeDetail, **kwargs) diff --git a/modifinder/engines/alignment/CosineAlignmentEngine.py b/modifinder/engines/alignment/CosineAlignmentEngine.py index d49524d..c02e839 100644 --- a/modifinder/engines/alignment/CosineAlignmentEngine.py +++ b/modifinder/engines/alignment/CosineAlignmentEngine.py @@ -26,16 +26,17 @@ def _cosine_fast( spec_other (Spectrum): Second spectrum mz_tolerance (float): Tolerance in Da for fragment m/z values, if None, it is not used. ppm_tolerance (float): Tolerance in ppm for fragment m/z values, if None, it is not used. - allow_shift (bool): _description_ + allow_shift (bool): Shift for m/z values. If not 0, hybrid search is performed. shift = prec_mz(qry) - prec_mz(ref) Returns: Tuple[float, List[Tuple[int, int]]]: _description_ """ - + if mz_tolerance is None and ppm_tolerance is None: raise ModiFinderError("At least one of mz_tolerance or ppm_tolerance must be provided.") - precursor_charge = spec.precursor_charge if spec.precursor_charge is not None else 1 + if precursor_charge == 0: + precursor_charge = 1 precursor_mass_diff = ( spec.precursor_mz - spec_other.precursor_mz ) * precursor_charge @@ -169,6 +170,7 @@ def align_single( Returns: EdgeDetail: the edge detail object """ + cosine, matched_peaks = _cosine_fast( SpectrumTuple1, SpectrumTuple2,