def get_theor_spectrum(peptide, acc_frag, types=('b', 'y'), maxcharge=None, **kwargs): """ Calculates theoretical spectra in two ways: usual one. and formatter in integer (mz / frag_acc). `peptide` -peptide sequence `acc_frag` - accuracy of matching. `types` - ion types. `maxcharge` - maximum charge. ---------- Returns spectra in two ways (usual, integer) """ peaks = {} theoretical_set = defaultdict(set) pl = len(peptide) - 1 if not maxcharge: maxcharge = 1 + int(ec.charge(peptide, pH=2)) for charge in range(1, maxcharge + 1): for ion_type in types: nterminal = ion_type[0] in 'abc' if nterminal: maxpart = peptide[:-1] maxmass = cmass.fast_mass(maxpart, ion_type=ion_type, charge=charge, **kwargs) marr = np.zeros((pl, ), dtype=float) marr[0] = maxmass for i in range(1, pl): marr[i] = marr[i - 1] - mass.fast_mass2( [maxpart[-i]]) / charge ### recalculate else: maxpart = peptide[1:] maxmass = cmass.fast_mass(maxpart, ion_type=ion_type, charge=charge, **kwargs) marr = np.zeros((pl, ), dtype=float) marr[pl - 1] = maxmass for i in range(pl - 2, -1, -1): marr[i] = marr[i + 1] - mass.fast_mass2( [maxpart[-(i + 2)]]) / charge ### recalculate tmp = marr / acc_frag tmp = tmp.astype(int) theoretical_set[ion_type].update(tmp) marr.sort() peaks[ion_type, charge] = marr return peaks, theoretical_set
def test_fast_mass(self): for pep in self.random_peptides: self.assertAlmostEqual( cmass.fast_mass(pep, aa_mass=self.test_aa_mass), sum(pep.count(aa) * m for aa, m in self.test_aa_mass.items()) + self.mass_H * 2.0 + self.mass_O)
def _get_theoretical_peptide_fragments( peptide: str, modifications: Optional[Dict[Union[float, str], float]] = None, types: str = 'by', max_charge: int = 1)\ -> List[PeptideFragmentAnnotation]: """ Get theoretical peptide fragments for the given peptide. Parameters ---------- peptide : str The peptide sequence for which the fragments will be generated. The peptide sequence should only exist of the 20 standard amino acids. modifications : Optional[Dict[Union[float, str], float]], optional Mapping of modification positions and mass differences. Valid positions are any amino acid index in the peptide (0-based), 'N-term', and 'C-term'. types : str, optional The fragment type. Can be any combination of 'a', 'b', 'c', 'x', 'y', and 'z' (the default is 'by', which means that b-ions and y-ions will be generated). max_charge : int, optional All fragments up to and including the given charge will be generated (the default is 1 to only generate singly-charged fragments). Returns ------- List[Tuple[PeptideFragmentAnnotation, float]] A list of all fragments as (`PeptideFragmentAnnotation`, m/z) tuples sorted in ascending m/z order. """ if modifications is not None: mods = modifications.copy() if 'N-term' in modifications: mods[-1] = mods['N-term'] del mods['N-term'] if 'C-term' in modifications: mods[len(peptide) + 1] = mods['C-term'] del mods['C-term'] else: mods = {} ions = [] for i in range(1, len(peptide)): for ion_type in types: if ion_type in 'abc': # N-terminal fragment. ion_index = i sequence = peptide[:i] mod_mass = sum([md for pos, md in mods.items() if pos < i]) else: # C-terminal fragment. ion_index = len(peptide) - i sequence = peptide[i:] mod_mass = sum([md for pos, md in mods.items() if pos >= i]) for charge in range(1, max_charge + 1): ions.append( PeptideFragmentAnnotation( ion_type, ion_index, charge, mass.fast_mass(sequence=sequence, ion_type=ion_type, charge=charge) + mod_mass / charge)) return sorted(ions, key=operator.attrgetter('calc_mz'))