File size: 11,237 Bytes
98c5587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fe50a9
98c5587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import annotations

import logging
import os
import sys
from typing import Any

import numpy as np

current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(current_dir, '..', 'lib'))

from pythoms.molecule import IPMolecule

try:
    import IsoSpecPy as isospec

    ISOSPEC_AVAILABLE = True
except ImportError:
    ISOSPEC_AVAILABLE = False

ISOTOPE_LIBRARY = 'isospec' if ISOSPEC_AVAILABLE else 'pythoms'

_isotope_pattern_cache: dict[tuple[str, int, int], dict[str, Any]] = {}
_ISOTOPE_CACHE_MAX_SIZE = 1000

logger = logging.getLogger(__name__)


class IsotopeMixin:
    """Mixin for isotope pattern generation (IsoSpecPy and PythoMS backends)."""

    def generate_isotope_pattern(self, formula: str, charge: int = 1, resolution: int = 20000) -> dict:
        """
        Generate isotope pattern for a given formula.
        Dispatches to either IsoSpecPy (faster) or PythoMS based on ISOTOPE_LIBRARY setting.
        Returns both bar pattern and Gaussian pattern.

        Uses global cache for speed optimization.

        Parameters:
            formula: Chemical formula string
            charge: Charge state
            resolution: MS resolution (default 20000 is fallback when webapp cannot parse from uploaded data)
        """
        global _isotope_pattern_cache, _ISOTOPE_CACHE_MAX_SIZE, ISOTOPE_LIBRARY

        # Check cache first
        cache_key = (formula, charge, resolution)
        if cache_key in _isotope_pattern_cache:
            logger.debug(f'[generate_isotope_pattern] CACHE HIT for {formula[:30]}... (z={charge})')
            return _isotope_pattern_cache[cache_key]

        logger.debug(f'[generate_isotope_pattern] CACHE MISS for {formula[:30]}... (z={charge}) - computing...')
        # Dispatch to appropriate library
        if ISOTOPE_LIBRARY == 'isospec' and ISOSPEC_AVAILABLE:
            result = self._generate_isotope_pattern_isospec(formula, charge, resolution)
        else:
            result = self._generate_isotope_pattern_pythoms(formula, charge, resolution)

        # Cache the result (with size limit) if successful
        if 'error' not in result:
            if len(_isotope_pattern_cache) >= _ISOTOPE_CACHE_MAX_SIZE:
                # Remove oldest entry (first key)
                oldest_key = next(iter(_isotope_pattern_cache))
                del _isotope_pattern_cache[oldest_key]
            _isotope_pattern_cache[cache_key] = result

        return result

    def _consolidate_formula(self, formula: str) -> str:
        """
        Consolidate a formula with duplicate elements into standard form.
        E.g., 'C304H368N128O184P30Ag28N2H8' -> 'C304H376N130O184P30Ag28'

        IsoSpecPy requires each element to appear only once.
        """
        import re

        # Parse formula: find all element-count pairs
        # Matches element symbols (1-2 letters, first uppercase) followed by optional count
        pattern = r'([A-Z][a-z]?)(\d*)'
        matches = re.findall(pattern, formula)

        # Consolidate counts for each element
        element_counts: dict[str, int] = {}
        for element, count in matches:
            if element:  # Skip empty matches
                count = int(count) if count else 1
                element_counts[element] = element_counts.get(element, 0) + count

        # Rebuild formula in a standard order (C, H, N, O, P, S, then others alphabetically)
        priority_order = ['C', 'H', 'N', 'O', 'P', 'S']
        result = []

        # Add priority elements first
        for elem in priority_order:
            if elem in element_counts:
                count = element_counts.pop(elem)
                result.append(f'{elem}{count}' if count > 1 else elem)

        # Add remaining elements alphabetically
        for elem in sorted(element_counts.keys()):
            count = element_counts[elem]
            result.append(f'{elem}{count}' if count > 1 else elem)

        return ''.join(result)

    def _generate_isotope_pattern_isospec(self, formula: str, charge: int = 1, resolution: int = 20000) -> dict:
        """
        Generate isotope pattern using IsoSpecPy (faster than PythoMS for large molecules).
        """
        try:
            # Consolidate formula to handle duplicate elements (e.g., from adducts)
            # IsoSpecPy requires each element to appear only once
            consolidated_formula = self._consolidate_formula(formula)

            # prob_to_cover=0.9999 captures 99.99% of the isotope distribution
            iso_result = isospec.IsoTotalProb(formula=consolidated_formula, prob_to_cover=0.9999)

            # IsoSpecPy returns CFFI objects - must convert to list first
            masses = np.array(list(iso_result.masses))
            probs = np.array(list(iso_result.probs))

            if len(masses) == 0:
                return {'error': 'IsoSpecPy returned empty pattern'}

            # Sort by mass first
            sort_idx = np.argsort(masses)
            masses = masses[sort_idx]
            probs = probs[sort_idx]

            # Get monoisotopic mass (first peak after sorting, before any filtering)
            monoisotopic_mass = masses[0]

            # Calculate molecular weight (weighted average of ALL peaks)
            molecular_weight = np.average(masses, weights=probs)

            # Convert to m/z (apply charge)
            # The formula passed is already the ION formula (protons already removed)
            # So we just divide by charge, same as PythoMS does
            mz_values = masses / abs(charge)

            # Bin peaks FIRST to match PythoMS behavior (combine peaks at similar m/z)
            # IsoSpecPy returns fine-grained peaks, PythoMS aggregates by nominal mass
            # Use 0.2 Da bins to match typical isotope spacing
            bin_width = 0.2 / abs(charge)
            min_mz = mz_values.min()
            max_mz = mz_values.max()
            bins = np.arange(min_mz - bin_width / 2, max_mz + bin_width, bin_width)

            # Digitize: assign each peak to a bin
            bin_indices = np.digitize(mz_values, bins)

            # Aggregate peaks in each bin
            binned_mz = []
            binned_int = []
            for i in range(1, len(bins)):
                mask = bin_indices == i
                if np.any(mask):
                    # Weighted average for m/z, sum for intensity
                    bin_probs = probs[mask]
                    bin_mzs = mz_values[mask]
                    binned_mz.append(np.average(bin_mzs, weights=bin_probs))
                    binned_int.append(np.sum(bin_probs))

            if not binned_mz:
                return {'error': 'IsoSpecPy: no peaks after binning'}

            mz_values = np.array(binned_mz)
            probs = np.array(binned_int)

            # Normalize AFTER binning to max = 1.0 (like PythoMS)
            probs = probs / np.max(probs)

            # Filter out low intensity peaks (threshold=0.01 like PythoMS)
            threshold = 0.01
            mask = probs >= threshold
            mz_values = mz_values[mask]
            probs = probs[mask]

            if len(mz_values) == 0:
                return {'error': 'IsoSpecPy: all peaks below threshold after filtering'}

            # Create bar isotope pattern in PythoMS format
            barip = [mz_values.tolist(), probs.tolist()]

            # Calculate FWHM for Gaussian smoothing
            if len(mz_values) > 0:
                theoretical_mz = mz_values[0]
            else:
                theoretical_mz = monoisotopic_mass / abs(charge)

            fwhm = theoretical_mz / resolution

            # Generate Gaussian pattern using the same smooth function
            gaussian_pattern = self.smooth_gaussian_pattern(barip, fwhm, num_points_per_fwhm=100)

            # Sort Gaussian pattern by m/z
            if gaussian_pattern and len(gaussian_pattern[0]) > 0:
                gaussian_mz = np.array(gaussian_pattern[0])
                gaussian_int = np.array(gaussian_pattern[1])
                sort_idx = np.argsort(gaussian_mz)
                gaussian_mz_sorted = gaussian_mz[sort_idx].tolist()
                gaussian_int_sorted = gaussian_int[sort_idx].tolist()
            else:
                gaussian_mz_sorted = []
                gaussian_int_sorted = []

            return {
                'mz': barip[0],
                'intensity': barip[1],
                'gaussian_mz': gaussian_mz_sorted,
                'gaussian_intensity': gaussian_int_sorted,
                'monoisotopic_mass': monoisotopic_mass,
                'molecular_weight': molecular_weight,
            }
        except Exception as e:
            # Fall back to PythoMS if IsoSpecPy fails
            logger.warning(f'IsoSpecPy failed for {formula}: {e}, falling back to PythoMS')
            return self._generate_isotope_pattern_pythoms(formula, charge, resolution)

    def _generate_isotope_pattern_pythoms(self, formula: str, charge: int = 1, resolution: int = 20000) -> dict:
        """
        Generate isotope pattern using PythoMS (original implementation).
        """
        try:
            mol = IPMolecule(
                formula,
                charge=charge,
                resolution=resolution,
                verbose=False,
                ipmethod='hybrid',
                dropmethod='threshold',
                threshold=0.01,
            )

            # Get bar isotope pattern
            barip = mol.bar_isotope_pattern

            # Calculate theoretical m/z for FWHM calculation
            # Use the first m/z value from bar pattern (monoisotopic peak)
            if len(barip[0]) > 0:
                theoretical_mz = barip[0][0]  # First m/z value = monoisotopic peak
            else:
                # Fallback to old method if bar pattern is empty
                theoretical_mass = mol.monoisotopic_mass
                theoretical_mz = (theoretical_mass - charge * self.m_p) / charge

            # Generate Gaussian pattern using custom smooth function
            fwhm = theoretical_mz / resolution

            # Use custom smooth Gaussian generation instead of PythoMS version
            gaussian_pattern = self.smooth_gaussian_pattern(barip, fwhm, num_points_per_fwhm=100)

            # Sort Gaussian pattern by m/z to prevent zigzag plotting
            if gaussian_pattern and len(gaussian_pattern[0]) > 0:
                gaussian_mz = np.array(gaussian_pattern[0])
                gaussian_int = np.array(gaussian_pattern[1])

                # Sort by m/z
                sort_idx = np.argsort(gaussian_mz)
                gaussian_mz_sorted = gaussian_mz[sort_idx].tolist()
                gaussian_int_sorted = gaussian_int[sort_idx].tolist()
            else:
                gaussian_mz_sorted = []
                gaussian_int_sorted = []

            return {
                'mz': barip[0],
                'intensity': barip[1],
                'gaussian_mz': gaussian_mz_sorted,
                'gaussian_intensity': gaussian_int_sorted,
                'monoisotopic_mass': mol.monoisotopic_mass,
                'molecular_weight': mol.molecular_weight,
            }
        except Exception as e:
            return {'error': str(e)}