void IsotopeModel::setSamples(const EmpiricalFormula & formula) { typedef std::vector<DoubleReal> ContainerType; ContainerType isotopes_exact; isotope_distribution_ = formula.getIsotopeDistribution(max_isotope_); isotope_distribution_.trimRight(trim_right_cutoff_); isotope_distribution_.renormalize(); // compute the average mass (-offset) CoordinateType isotopes_mean = 0; Int i = 0; for (IsotopeDistribution::iterator iter = isotope_distribution_.begin(); iter != isotope_distribution_.end(); ++iter, ++i) { isotopes_exact.push_back(iter->second); isotopes_mean += iter->second * i; } isotopes_mean *= isotope_distance_ / charge_; // (Need not divide by sum of probabilities, which is 1.) /// // "stretch" the averagine isotope distribution (so we can add datapoints between isotope peaks) /// size_t isotopes_exact_size = isotopes_exact.size(); isotopes_exact.resize(size_t((isotopes_exact_size - 1) * isotope_distance_ / interpolation_step_ + 1.6)); // round up a bit more for (Size i = isotopes_exact_size - 1; i; --i) { // we don't need to move the 0-th entry isotopes_exact[size_t(CoordinateType(i) * isotope_distance_ / interpolation_step_ / charge_ + 0.5)] = isotopes_exact[i]; isotopes_exact[i] = 0; } //// // compute the Gaussian/Cauchy distribution (to be added for widening the averagine isotope distribution) //// ContainerType peak_shape_values_y; // fill a container with CoordinateType points (x values) CoordinateType peak_width = 0.0; if (param_.getValue("isotope:mode:mode") == "Gaussian") { // Actual width for values in the smooth table for normal distribution peak_width = isotope_stdev_ * 4.0; // MAGIC alert, num stdev for smooth table for normal distribution ContainerType peak_shape_values_x; for (DoubleReal coord = -peak_width; coord <= peak_width; coord += interpolation_step_) { peak_shape_values_x.push_back(coord); } // compute normal approximation at these CoordinateType points (y values) Math::BasicStatistics<> normal_widening_model; normal_widening_model.setSum(1); normal_widening_model.setMean(0); normal_widening_model.setVariance(isotope_stdev_ * isotope_stdev_); normal_widening_model.normalApproximation(peak_shape_values_y, peak_shape_values_x); } else if (param_.getValue("isotope:mode:mode") == "Lorentzian") { peak_width = isotope_lorentz_fwhm_ * 8.0; // MAGIC alert: Lorentzian has infinite support, but we need to stop sampling at some point: 8*FWHM for (DoubleReal coord = -peak_width; coord <= peak_width; coord += interpolation_step_) { boost::math::cauchy_distribution<double> cauchy(0., isotope_lorentz_fwhm_ / 2.0); double x = boost::math::pdf(cauchy, coord); //double y = gsl_ran_cauchy_pdf(coord, isotope_lorentz_fwhm_/2.0); peak_shape_values_y.push_back(x); //cauchy is using HWHM not FWHM } } /// // fold the Gaussian/Lorentzian at each averagine peak, i.e. fill linear interpolation /// const ContainerType & left = isotopes_exact; const ContainerType & right = peak_shape_values_y; ContainerType & result = interpolation_.getData(); result.clear(); SignedSize r_max = std::min(SignedSize(left.size() + right.size() - 1), SignedSize(2 * peak_width / interpolation_step_ * max_isotope_ + 1)); result.resize(r_max, 0); // we loop backwards because then the small products tend to come first // (for better numerics) for (SignedSize i = left.size() - 1; i >= 0; --i) { if (left[i] == 0) continue; for (SignedSize j = std::min(r_max - i, SignedSize(right.size())) - 1; j >= 0; --j) { result[i + j] += left[i] * right[j]; } } monoisotopic_mz_ = mean_ - isotopes_mean; interpolation_.setMapping(interpolation_step_, peak_width / interpolation_step_, monoisotopic_mz_); //std::cerr << "mono now: " << monoisotopic_mz_ << " mono easy: " << formula.getMonoWeight()/formula.getCharge() << "\n"; // scale data so that integral over distribution equals one // multiply sum by interpolation_step_ -> rectangular approximation of integral IntensityType factor = scaling_ / (interpolation_step_ * std::accumulate(result.begin(), result.end(), IntensityType(0))); for (ContainerType::iterator iter = result.begin(); iter != result.end(); ++iter) { *iter *= factor; } }
void TheoreticalSpectrumGenerator::addPrecursorPeaks(RichPeakSpectrum & spec, const AASequence & peptide, Int charge) const { RichPeak1D p; // precursor peak double mono_pos = peptide.getMonoWeight(Residue::Full, charge) / double(charge); if (add_isotopes_) { IsotopeDistribution dist = peptide.getFormula(Residue::Full, charge).getIsotopeDistribution(max_isotope_); UInt j(0); for (IsotopeDistribution::ConstIterator it = dist.begin(); it != dist.end(); ++it, ++j) { p.setMZ((double)(mono_pos + j * Constants::NEUTRON_MASS_U) / (double)charge); p.setIntensity(pre_int_ * it->second); if (add_metainfo_) { String name("[M+H]+"); if (charge == 2) { name = "[M+2H]++"; } p.setMetaValue("IonName", name); } spec.push_back(p); } } else { p.setMZ(mono_pos); p.setIntensity(pre_int_); if (add_metainfo_) { String name("[M+H]+"); if (charge == 2) { name = "[M+2H]++"; } p.setMetaValue("IonName", name); } spec.push_back(p); } // loss peaks of the precursor //loss of water EmpiricalFormula ion = peptide.getFormula(Residue::Full, charge) - EmpiricalFormula("H2O"); mono_pos = ion.getMonoWeight() / double(charge); if (add_isotopes_) { IsotopeDistribution dist = ion.getIsotopeDistribution(max_isotope_); UInt j(0); for (IsotopeDistribution::ConstIterator it = dist.begin(); it != dist.end(); ++it, ++j) { p.setMZ((double)(mono_pos + j * Constants::NEUTRON_MASS_U) / (double)charge); p.setIntensity(pre_int_H2O_ * it->second); if (add_metainfo_) { String name("[M+H]-H2O+"); if (charge == 2) { name = "[M+2H]-H2O++"; } p.setMetaValue("IonName", name); } spec.push_back(p); } } else { p.setMZ(mono_pos); p.setIntensity(pre_int_H2O_); if (add_metainfo_) { String name("[M+H]-H2O+"); if (charge == 2) { name = "[M+2H]-H2O++"; } p.setMetaValue("IonName", name); } spec.push_back(p); } //loss of ammonia ion = peptide.getFormula(Residue::Full, charge) - EmpiricalFormula("NH3"); mono_pos = ion.getMonoWeight() / double(charge); if (add_isotopes_) { IsotopeDistribution dist = ion.getIsotopeDistribution(max_isotope_); UInt j(0); for (IsotopeDistribution::ConstIterator it = dist.begin(); it != dist.end(); ++it, ++j) { p.setMZ((double)(mono_pos + j * Constants::NEUTRON_MASS_U) / (double)charge); p.setIntensity(pre_int_NH3_ * it->second); if (add_metainfo_) { String name("[M+H]-NH3+"); if (charge == 2) { name = "[M+2H]-NH3++"; } p.setMetaValue("IonName", name); } spec.push_back(p); } } else { p.setMZ(mono_pos); p.setIntensity(pre_int_NH3_); if (add_metainfo_) { String name("[M+H]-NH3+"); if (charge == 2) { name = "[M+2H]-NH3++"; } p.setMetaValue("IonName", name); } spec.push_back(p); } spec.sortByPosition(); }
void TheoreticalSpectrumGenerator::addPrecursorPeaks(RichPeakSpectrum & spec, const AASequence & peptide, Int charge) { bool add_metainfo(param_.getValue("add_metainfo").toBool()); DoubleReal pre_int((DoubleReal)param_.getValue("precursor_intensity")); DoubleReal pre_int_H2O((DoubleReal)param_.getValue("precursor_H2O_intensity")); DoubleReal pre_int_NH3((DoubleReal)param_.getValue("precursor_NH3_intensity")); bool add_isotopes(param_.getValue("add_isotopes").toBool()); int max_isotope((int)param_.getValue("max_isotope")); // precursor peak DoubleReal mono_pos = peptide.getMonoWeight(Residue::Full, charge) / DoubleReal(charge); if (add_isotopes) { IsotopeDistribution dist = peptide.getFormula(Residue::Full, charge).getIsotopeDistribution(max_isotope); UInt j(0); for (IsotopeDistribution::ConstIterator it = dist.begin(); it != dist.end(); ++it, ++j) { p_.setMZ((DoubleReal)(mono_pos + j * Constants::NEUTRON_MASS_U) / (DoubleReal)charge); p_.setIntensity(pre_int * it->second); if (add_metainfo) { String name("[M+H]+"); if (charge == 2) { name = "[M+2H]++"; } p_.setMetaValue("IonName", name); } spec.push_back(p_); } } else { p_.setMZ(mono_pos); p_.setIntensity(pre_int); if (add_metainfo) { String name("[M+H]+"); if (charge == 2) { name = "[M+2H]++"; } p_.setMetaValue("IonName", name); } spec.push_back(p_); } // loss peaks of the precursor //loss of water EmpiricalFormula ion = peptide.getFormula(Residue::Full, charge) - EmpiricalFormula("H2O"); mono_pos = ion.getMonoWeight() / DoubleReal(charge); if (add_isotopes) { IsotopeDistribution dist = ion.getIsotopeDistribution(max_isotope); UInt j(0); for (IsotopeDistribution::ConstIterator it = dist.begin(); it != dist.end(); ++it, ++j) { p_.setMZ((DoubleReal)(mono_pos + j * Constants::NEUTRON_MASS_U) / (DoubleReal)charge); p_.setIntensity(pre_int_H2O * it->second); if (add_metainfo) { String name("[M+H]-H2O+"); if (charge == 2) { name = "[M+2H]-H2O++"; } p_.setMetaValue("IonName", name); } spec.push_back(p_); } } else { p_.setMZ(mono_pos); p_.setIntensity(pre_int_H2O); if (add_metainfo) { String name("[M+H]-H2O+"); if (charge == 2) { name = "[M+2H]-H2O++"; } p_.setMetaValue("IonName", name); } spec.push_back(p_); } //loss of ammonia ion = peptide.getFormula(Residue::Full, charge) - EmpiricalFormula("NH3"); mono_pos = ion.getMonoWeight() / DoubleReal(charge); if (add_isotopes) { IsotopeDistribution dist = ion.getIsotopeDistribution(max_isotope); UInt j(0); for (IsotopeDistribution::ConstIterator it = dist.begin(); it != dist.end(); ++it, ++j) { p_.setMZ((DoubleReal)(mono_pos + j * Constants::NEUTRON_MASS_U) / (DoubleReal)charge); p_.setIntensity(pre_int_NH3 * it->second); if (add_metainfo) { String name("[M+H]-NH3+"); if (charge == 2) { name = "[M+2H]-NH3++"; } p_.setMetaValue("IonName", name); } spec.push_back(p_); } } else { p_.setMZ(mono_pos); p_.setIntensity(pre_int_NH3); if (add_metainfo) { String name("[M+H]-NH3+"); if (charge == 2) { name = "[M+2H]-NH3++"; } p_.setMetaValue("IonName", name); } spec.push_back(p_); } spec.sortByPosition(); }