class AveragineCache(object):
    def __init__(self, averagine, backend=None, cache_truncation=1.0):
        if backend is None:
            backend = {}
        self.backend = backend
        self.averagine = Averagine(averagine)
        self.cache_truncation = cache_truncation

    def has_mz_charge_pair(self, mz, charge=1, charge_carrier=PROTON, truncate_after=0.95, ignore_below=0.0):
        if self.cache_truncation == 0.0:
            key_mz = mz
        else:
            key_mz = round(mz / self.cache_truncation) * self.cache_truncation
        if (key_mz, charge, charge_carrier) in self.backend:
            # return shift_isotopic_pattern(
            #     mz, [p.clone() for p in self.backend[key_mz, charge, charge_carrier]])
            return self.backend[key_mz, charge, charge_carrier].clone().shift(mz)
        else:
            tid = self.averagine.isotopic_cluster(
                mz, charge, charge_carrier, truncate_after, ignore_below)
            self.backend[key_mz, charge, charge_carrier] = tid.clone()
            return tid

    isotopic_cluster = has_mz_charge_pair

    def __repr__(self):
        return "AveragineCache(%r)" % self.averagine

    def clear(self):
        self.backend.clear()
Пример #2
0
class AveragineCache(object):
    def __init__(self, averagine, backend=None, cache_truncation=1.0):
        if backend is None:
            backend = {}
        self.backend = backend
        self.averagine = Averagine(averagine)
        self.cache_truncation = cache_truncation

    def has_mz_charge_pair(self,
                           mz,
                           charge=1,
                           charge_carrier=PROTON,
                           truncate_after=0.95,
                           ignore_below=0.0):
        if self.cache_truncation == 0.0:
            key_mz = mz
        else:
            key_mz = round(mz / self.cache_truncation) * self.cache_truncation
        if (key_mz, charge, charge_carrier) in self.backend:
            # return shift_isotopic_pattern(
            #     mz, [p.clone() for p in self.backend[key_mz, charge, charge_carrier]])
            return self.backend[key_mz, charge,
                                charge_carrier].clone().shift(mz)
        else:
            tid = self.averagine.isotopic_cluster(mz, charge, charge_carrier,
                                                  truncate_after, ignore_below)
            self.backend[key_mz, charge, charge_carrier] = tid.clone()
            return tid

    isotopic_cluster = has_mz_charge_pair

    def __repr__(self):
        return "AveragineCache(%r)" % self.averagine

    def clear(self):
        self.backend.clear()
 def __init__(self, averagine, backend=None, cache_truncation=1.0):
     if backend is None:
         backend = {}
     self.backend = backend
     self.averagine = Averagine(averagine)
     self.cache_truncation = cache_truncation
Пример #4
0
 def __init__(self, averagine, backend=None, cache_truncation=1.0):
     if backend is None:
         backend = {}
     self.backend = backend
     self.averagine = Averagine(averagine)
     self.cache_truncation = cache_truncation
Пример #5
0
class AveragineCache(object):
    """A wrapper around a :class:`Averagine` instance which will cache isotopic patterns
    produced for new (m/z, charge) pairs and reuses it for nearby m/z values

    Attributes
    ----------
    averagine : :class:`~Averagine`
        The averagine to use to generate new isotopic patterns
    cache_truncation : float
        Number of decimal places to round off the m/z for caching purposes
    """
    def __init__(self, averagine, backend=None, cache_truncation=1.0):
        if backend is None:
            backend = {}
        self.backend = backend
        self.averagine = Averagine(averagine)
        self.cache_truncation = cache_truncation

    def has_mz_charge_pair(self,
                           mz,
                           charge=1,
                           charge_carrier=PROTON,
                           truncate_after=0.95,
                           ignore_below=0.0):
        if self.cache_truncation == 0.0:
            key_mz = mz
        else:
            key_mz = round(mz / self.cache_truncation) * self.cache_truncation
        if (key_mz, charge, charge_carrier) in self.backend:
            return self.backend[key_mz, charge,
                                charge_carrier].clone().shift(mz)
        else:
            tid = self.averagine.isotopic_cluster(mz, charge, charge_carrier,
                                                  truncate_after, ignore_below)
            self.backend[key_mz, charge, charge_carrier] = tid.clone()
            return tid

    def isotopic_cluster(self,
                         mz,
                         charge=1,
                         charge_carrier=PROTON,
                         truncate_after=0.95,
                         ignore_below=0.0):
        """Generate a theoretical isotopic pattern for the given m/z and charge state, thresholded
        by theoretical peak height and density.

        Mimics :meth:`.Averagine.isotopic_cluster` but uses the object's cache through
        :meth:`has_mz_charge_pair`.

        Parameters
        ----------
        mz : float
            The reference m/z to calculate the neutral mass to interpolate from
        charge : int, optional
            The reference charge state to calculate the neutral mass. Defaults to 1
        charge_carrier : float, optional
            The mass of the charge carrier. Defaults to the mass of a proton.
        truncate_after : float, optional
            The percentage of the signal in the theoretical isotopic pattern to include.
            Defaults to 0.95, including the first 95% of the signal in the generated pattern
        ignore_below : float, optional
            Omit theoretical peaks whose intensity is below this number.
            Defaults to 0.0

        Returns
        -------
        :class:`.TheoreticalIsotopicPattern`
            The generated and thresholded pattern
        """
        return self.has_mz_charge_pair(mz, charge, charge_carrier,
                                       truncate_after, ignore_below)

    def __repr__(self):
        return "AveragineCache(%r)" % self.averagine

    def clear(self):
        self.backend.clear()
Пример #6
0
    for k, v in b.items():
        a[k] += v
    return dict(a)


try:
    _Averagine = Averagine
    _TheoreticalIsotopicPattern = TheoreticalIsotopicPattern
    from ms_deisotope._c.averagine import Averagine, TheoreticalIsotopicPattern
except ImportError as e:
    print(e, "averagine")

peptide = Averagine({
    "C": 4.9384,
    "H": 7.7583,
    "N": 1.3577,
    "O": 1.4773,
    "S": 0.0417
})
glycopeptide = Averagine({
    "C": 10.93,
    "H": 15.75,
    "N": 1.6577,
    "O": 6.4773,
    "S": 0.02054
})
glycan = Averagine({'C': 7.0, 'H': 11.8333, 'N': 0.5, 'O': 5.16666})
permethylated_glycan = Averagine({
    'C': 12.0,
    'H': 21.8333,
    'N': 0.5,
Пример #7
0
class AveragineCache(object):
    """A wrapper around a :class:`Averagine` instance which will cache isotopic patterns
    produced for new (m/z, charge) pairs and reuses it for nearby m/z values

    Attributes
    ----------
    averagine : :class:`~Averagine`
        The averagine to use to generate new isotopic patterns
    cache_truncation : float
        Number of decimal places to round off the m/z for caching purposes
    """

    def __init__(self, averagine, backend=None, cache_truncation=1.0):
        if backend is None:
            backend = {}
        self.backend = backend
        self.averagine = Averagine(averagine)
        self.cache_truncation = cache_truncation

    def __call__(self, mz, charge=1, charge_carrier=PROTON, truncate_after=TRUNCATE_AFTER, ignore_below=IGNORE_BELOW):
        return self.isotopic_cluster(mz, charge, charge_carrier, truncate_after, ignore_below)

    def has_mz_charge_pair(self, mz, charge=1, charge_carrier=PROTON, truncate_after=TRUNCATE_AFTER,
                           ignore_below=IGNORE_BELOW):
        if self.cache_truncation == 0.0:
            key_mz = mz
        else:
            key_mz = round(mz / self.cache_truncation) * self.cache_truncation
        if (key_mz, charge, charge_carrier) in self.backend:
            return self.backend[key_mz, charge, charge_carrier].clone().shift(mz)
        else:
            tid = self.averagine.isotopic_cluster(
                mz, charge, charge_carrier, truncate_after, ignore_below)
            self.backend[key_mz, charge, charge_carrier] = tid.clone()
            return tid

    def isotopic_cluster(self, mz, charge=1, charge_carrier=PROTON, truncate_after=TRUNCATE_AFTER,
                         ignore_below=IGNORE_BELOW):
        """Generate a theoretical isotopic pattern for the given m/z and charge state, thresholded
        by theoretical peak height and density.

        Mimics :meth:`.Averagine.isotopic_cluster` but uses the object's cache through
        :meth:`has_mz_charge_pair`.

        Parameters
        ----------
        mz : float
            The reference m/z to calculate the neutral mass to interpolate from
        charge : int, optional
            The reference charge state to calculate the neutral mass. Defaults to 1
        charge_carrier : float, optional
            The mass of the charge carrier. Defaults to the mass of a proton.
        truncate_after : float, optional
            The percentage of the signal in the theoretical isotopic pattern to include.
            Defaults to TRUNCATE_AFTER, including the first 95% of the signal in the generated pattern
        ignore_below : float, optional
            Omit theoretical peaks whose intensity is below this number.
            Defaults to 0.0

        Returns
        -------
        :class:`.TheoreticalIsotopicPattern`
            The generated and thresholded pattern
        """
        return self.has_mz_charge_pair(mz, charge, charge_carrier, truncate_after, ignore_below)

    def __repr__(self):
        return "AveragineCache(%r)" % self.averagine

    def clear(self):
        self.backend.clear()

    def populate(self, min_mz=10, max_mz=3000, min_charge=1, max_charge=8, charge_carrier=PROTON,
                 truncate_after=TRUNCATE_AFTER, ignore_below=IGNORE_BELOW):
        for i in range(int(min_mz), int(max_mz)):
            for j in range(min(max_charge, min_charge), max(min_charge, max_charge)):
                self.isotopic_cluster(i, j, PROTON, TRUNCATE_AFTER, IGNORE_BELOW)
        return self