/** @brief Similarity pairwise score This function return the similarity score of two spectra based on SteinScott. @param s1 const PeakSpectrum Spectrum 1 @param s2 const PeakSpectrum Spectrum 2 @see SteinScottImproveScore() */ double SteinScottImproveScore::operator()(const PeakSpectrum & s1, const PeakSpectrum & s2) const { const double epsilon = (double)param_.getValue("tolerance"); const double constant = epsilon / 10000; //const double c(0.0004); double score(0), sum(0), sum1(0), sum2(0), sum3(0), sum4(0); /* std::cout << s1 << std::endl; std::cout << std::endl; std::cout << s2 << std::endl;*/ for (PeakSpectrum::ConstIterator it1 = s1.begin(); it1 != s1.end(); ++it1) { double temp = it1->getIntensity(); sum1 += temp * temp; sum3 += temp; } for (PeakSpectrum::ConstIterator it1 = s2.begin(); it1 != s2.end(); ++it1) { double temp = it1->getIntensity(); sum2 += temp * temp; sum4 += temp; } double z = constant * (sum3 * sum4); Size j_left(0); for (Size i = 0; i != s1.size(); ++i) { for (Size j = j_left; j != s2.size(); ++j) { double pos1(s1[i].getMZ()), pos2(s2[j].getMZ()); if (std::abs(pos1 - pos2) <= 2 * epsilon) { sum += s1[i].getIntensity() * s2[j].getIntensity(); } else { if (pos2 > pos1) { break; } else { j_left = j; } } } } //std::cout<< sum << " Sum " << z << " z " << std::endl; score = (sum - z) / (std::sqrt((sum1 * sum2))); // std::cout<<score<< " score" << std::endl; if (score < (float)param_.getValue("threshold")) { score = 0; } return score; }
double SpectrumAlignmentScore::operator()(const PeakSpectrum & s1, const PeakSpectrum & s2) const { const double tolerance = (double)param_.getValue("tolerance"); bool is_relative_tolerance = param_.getValue("is_relative_tolerance").toBool(); bool use_linear_factor = param_.getValue("use_linear_factor").toBool(); bool use_gaussian_factor = param_.getValue("use_gaussian_factor").toBool(); if (use_linear_factor && use_gaussian_factor) { cerr << "Warning: SpectrumAlignmentScore, use either 'use_linear_factor' or 'use_gaussian_factor'!" << endl; } SpectrumAlignment aligner; Param p; p.setValue("tolerance", tolerance); p.setValue("is_relative_tolerance", (String)param_.getValue("is_relative_tolerance")); aligner.setParameters(p); vector<pair<Size, Size> > alignment; aligner.getSpectrumAlignment(alignment, s1, s2); double score(0), sum(0), sum1(0), sum2(0); for (PeakSpectrum::ConstIterator it1 = s1.begin(); it1 != s1.end(); ++it1) { sum1 += it1->getIntensity() * it1->getIntensity(); } for (PeakSpectrum::ConstIterator it1 = s2.begin(); it1 != s2.end(); ++it1) { sum2 += it1->getIntensity() * it1->getIntensity(); } for (vector<pair<Size, Size> >::const_iterator it = alignment.begin(); it != alignment.end(); ++it) { //double factor(0.0); //factor = (epsilon - fabs(s1[it->first].getPosition()[0] - s2[it->second].getPosition()[0])) / epsilon; double mz_tolerance(tolerance); if (is_relative_tolerance) { mz_tolerance = mz_tolerance * s1[it->first].getPosition()[0] / 1e6; } double mz_difference(fabs(s1[it->first].getPosition()[0] - s2[it->second].getPosition()[0])); double factor = 1.0; if (use_linear_factor || use_gaussian_factor) { factor = getFactor_(mz_tolerance, mz_difference, use_gaussian_factor); } sum += sqrt(s1[it->first].getIntensity() * s2[it->second].getIntensity() * factor); } score = sum / (sqrt(sum1 * sum2)); return score; }
std::vector<PeakSpectrum> AScore::peakPickingPerWindowsInSpectrum_(PeakSpectrum &real_spectrum) const { vector<PeakSpectrum> windows_top10; double spect_lower_bound = floor(real_spectrum.front().getMZ() / 100) * 100; double spect_upper_bound = ceil(real_spectrum.back().getMZ() / 100) * 100; Size number_of_windows = static_cast<Size>(ceil((spect_upper_bound - spect_lower_bound) / 100)); windows_top10.resize(number_of_windows); PeakSpectrum::Iterator it_current_peak = real_spectrum.begin(); Size window_upper_bound(spect_lower_bound + 100); for (Size current_window = 0; current_window < number_of_windows; ++current_window) { PeakSpectrum real_window; while (((*it_current_peak).getMZ() <= window_upper_bound) && (it_current_peak < real_spectrum.end())) { real_window.push_back(*it_current_peak); ++it_current_peak; } real_window.sortByIntensity(true); for (Size i = 0; (i < 10) & (i < real_window.size()); ++i) { windows_top10[current_window].push_back(real_window[i]); } window_upper_bound += 100; } return windows_top10; }
// s1 should be the original spectrum DoubleReal CompNovoIdentificationBase::compareSpectra_(const PeakSpectrum & s1, const PeakSpectrum & s2) { DoubleReal score(0.0); PeakSpectrum::ConstIterator it1 = s1.begin(); PeakSpectrum::ConstIterator it2 = s2.begin(); Size num_matches(0); while (it1 != s1.end() && it2 != s2.end()) { DoubleReal pos1(it1->getPosition()[0]), pos2(it2->getPosition()[0]); if (fabs(pos1 - pos2) < fragment_mass_tolerance_) { score += it1->getIntensity(); ++num_matches; } if (pos1 <= pos2) { ++it1; } else { ++it2; } } if (num_matches == 0) { return 0; } score /= sqrt((DoubleReal)num_matches); return score; }
void SuffixArrayPeptideFinder::getCandidates(vector<vector<pair<SuffixArrayPeptideFinder::FASTAEntry, String> > >& candidates, const String& DTA_file) { DTAFile dta_file; PeakSpectrum s; dta_file.load(DTA_file, s); s.sortByPosition(); PeakSpectrum::ConstIterator it(s.begin()); vector<double> spec; for (; it != s.end(); ++it) { spec.push_back(it->getPosition()[0]); } const vector<double> specc(spec); getCandidates(candidates, specc); return; }
SqrtMower copy(*e_ptr); TEST_EQUAL(*e_ptr == copy, true) END_SECTION START_SECTION((SqrtMower& operator=(const SqrtMower& source))) SqrtMower copy; copy = *e_ptr; TEST_EQUAL(*e_ptr == copy, true) END_SECTION START_SECTION((template<typename SpectrumType> void filterSpectrum(SpectrumType& spectrum))) DTAFile dta_file; PeakSpectrum spec; dta_file.load(OPENMS_GET_TEST_DATA_PATH("Transformers_tests.dta"), spec); TEST_REAL_SIMILAR((spec.begin() + 40)->getIntensity(), 37.5) e_ptr->filterSpectrum(spec); TEST_REAL_SIMILAR((spec.begin() + 40)->getIntensity(), sqrt(37.5)) END_SECTION START_SECTION((void filterPeakMap(PeakMap& exp))) DTAFile dta_file; PeakSpectrum spec; dta_file.load(OPENMS_GET_TEST_DATA_PATH("Transformers_tests.dta"), spec); PeakMap pm; pm.addSpectrum(spec); TEST_REAL_SIMILAR((pm.begin()->begin() + 40)->getIntensity(), 37.5)
double ZhangSimilarityScore::operator()(const PeakSpectrum & s1, const PeakSpectrum & s2) const { const double tolerance = (double)param_.getValue("tolerance"); bool use_linear_factor = param_.getValue("use_linear_factor").toBool(); bool use_gaussian_factor = param_.getValue("use_gaussian_factor").toBool(); double score(0), sum(0), sum1(0), sum2(0) /*, squared_sum1(0), squared_sum2(0)*/; // TODO remove parameter if (param_.getValue("is_relative_tolerance").toBool() ) { throw Exception::NotImplemented(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION); } for (PeakSpectrum::ConstIterator it1 = s1.begin(); it1 != s1.end(); ++it1) { sum1 += it1->getIntensity(); /* for (PeakSpectrum::ConstIterator it2 = s1.begin(); it2 != s1.end(); ++it2) { if (abs(it1->getPosition()[0] - it2->getPosition()[0]) <= 2 * tolerance) { squared_sum1 += it1->getIntensity() * it2->getIntensity(); } }*/ } /* UInt i_left(0); for (Size i = 0; i != s1.size(); ++i) { sum1 += s1[i].getIntensity(); for (Size j = i_left; j != s1.size(); ++j) { double pos1(s1[i].getPosition()[0]), pos2(s1[j].getPosition()[0]); if (abs(pos1 - pos2) <= 2 * tolerance) { squared_sum1 += s1[i].getIntensity() * s1[j].getIntensity(); } else { if (pos2 > pos1) { break; } else { i_left = i; } } } }*/ /* i_left = 0; for (Size i = 0; i != s2.size(); ++i) { sum2 += s2[i].getIntensity(); for (Size j = i_left; j != s2.size(); ++j) { double pos1(s2[i].getPosition()[0]), pos2(s2[j].getPosition()[0]); if (abs(pos1 - pos2) <= 2 * tolerance) { squared_sum1 += s2[i].getIntensity() * s2[j].getIntensity(); } else { if (pos2 > pos1) { break; } else { i_left = i; } } } }*/ for (PeakSpectrum::ConstIterator it1 = s2.begin(); it1 != s2.end(); ++it1) { sum2 += it1->getIntensity(); /* for (PeakSpectrum::ConstIterator it2 = s2.begin(); it2 != s2.end(); ++it2) { if (abs(it1->getPosition()[0] - it2->getPosition()[0]) <= 2 * tolerance) { squared_sum2 += it1->getIntensity() * it2->getIntensity(); } } */ } Size j_left(0); for (Size i = 0; i != s1.size(); ++i) { for (Size j = j_left; j != s2.size(); ++j) { double pos1(s1[i].getMZ()), pos2(s2[j].getMZ()); if (fabs(pos1 - pos2) < tolerance) { //double factor((tolerance - fabs(pos1 - pos2)) / tolerance); double factor = 1.0; if (use_linear_factor || use_gaussian_factor) { factor = getFactor_(tolerance, fabs(pos1 - pos2), use_gaussian_factor); } sum += sqrt(s1[i].getIntensity() * s2[j].getIntensity() * factor); } else { if (pos2 > pos1) { break; } else { j_left = j; } } } } /* for (PeakSpectrum::ConstIterator it1 = s1.begin(); it1 != s1.end(); ++it1) { for (PeakSpectrum::ConstIterator it2 = s2.begin(); it2 != s2.end(); ++it2) { if (abs(it1->getPosition()[0] - it2->getPosition()[0]) <= 2 * tolerance) { sum += sqrt(it1->getIntensity() * it2->getIntensity()); } } }*/ score = sum / (sqrt(sum1 * sum2)); return score; }
void CompNovoIonScoring::scoreSpectra(Map<double, IonScore> & ion_scores, PeakSpectrum & CID_spec, PeakSpectrum & ETD_spec, double precursor_weight, Size charge) { // adds single charged variants of putative single charged ions //addSingleChargedIons_(ion_scores, CID_spec); for (PeakSpectrum::ConstIterator it = CID_spec.begin(); it != CID_spec.end(); ++it) { double it_pos(it->getPosition()[0]); IonScore ion_score; ion_scores[it_pos] = ion_score; } for (PeakSpectrum::ConstIterator it = CID_spec.begin(); it != CID_spec.end(); ++it) { ion_scores[it->getPosition()[0]].s_isotope_pattern_1 = scoreIsotopes_(CID_spec, it, ion_scores, 1); if (it->getPosition()[0] < precursor_weight / 2.0) { ion_scores[it->getPosition()[0]].s_isotope_pattern_2 = scoreIsotopes_(CID_spec, it, ion_scores, 2); } else { ion_scores[it->getPosition()[0]].s_isotope_pattern_2 = -1; } } // find possible supporting ions from ETD spec to CID spec scoreETDFeatures_(charge, precursor_weight, ion_scores, CID_spec, ETD_spec); // combine the features and give b-ion scores scoreWitnessSet_(charge, precursor_weight, ion_scores, CID_spec); for (Map<double, IonScore>::iterator it = ion_scores.begin(); it != ion_scores.end(); ++it) { it->second.score = it->second.s_witness; } MassDecompositionAlgorithm decomp_algo; // check whether a PRMNode_ can be decomposed into amino acids // rescore the peaks that cannot be possible y-ion candidates double max_decomp_weight((double)param_.getValue("max_decomp_weight")); for (Map<double, IonScore>::iterator it = ion_scores.begin(); it != ion_scores.end(); ++it) { if (it->first > 19.0 && (it->first - 19.0) < max_decomp_weight) { vector<MassDecomposition> decomps; decomp_algo.getDecompositions(decomps, it->first - 19.0); #ifdef ION_SCORING_DEBUG cerr << "Decomps: " << it->first << " " << it->first - 19.0 << " " << decomps.size() << " " << it->second.score << endl; #endif if (decomps.empty()) { it->second.score = 0; } } if (it->first < precursor_weight && precursor_weight - it->first < max_decomp_weight) { vector<MassDecomposition> decomps; decomp_algo.getDecompositions(decomps, precursor_weight - it->first); #ifdef ION_SCORING_DEBUG cerr << "Decomps: " << it->first << " " << precursor_weight - it->first << " " << decomps.size() << " " << it->second.score << endl; #endif if (decomps.empty()) { it->second.score = 0; } } } ion_scores[CID_spec.begin()->getPosition()[0]].score = 1; ion_scores[(CID_spec.end() - 1)->getPosition()[0]].score = 1; }
void CompNovoIonScoring::scoreETDFeatures_(Size /*charge*/, double precursor_weight, Map<double, IonScore> & ion_scores, const PeakSpectrum & CID_spec, const PeakSpectrum & ETD_spec) { //double fragment_mass_tolerance((double)param_.getValue("fragment_mass_tolerance")); Size max_isotope_to_score(param_.getValue("max_isotope_to_score")); for (PeakSpectrum::ConstIterator it1 = CID_spec.begin(); it1 != CID_spec.end(); ++it1) { double pos1(it1->getPosition()[0]); double b_sum(0.0), y_sum(0.0); // score a-ions for (PeakSpectrum::ConstIterator it2 = CID_spec.begin(); it2 != CID_spec.end(); ++it2) { double pos2(it2->getPosition()[0]); if (fabs(pos1 - pos2 - 28.0) < fragment_mass_tolerance_) { double factor((fragment_mass_tolerance_ - fabs(pos1 - pos2 - 28.0)) / fragment_mass_tolerance_); #ifdef SCORE_ETDFEATURES_DEBUG cerr << "scoreETDFeatures: found a-ion " << pos1 << " (" << pos2 << ") (factor=" << factor << ") " << b_sum << " -> "; #endif b_sum += it2->getIntensity() * factor; #ifdef SCORE_ETDFEATURES_DEBUG cerr << endl; #endif } } for (PeakSpectrum::ConstIterator it2 = ETD_spec.begin(); it2 != ETD_spec.end(); ++it2) { double pos2(it2->getPosition()[0]); // check if pos2 is precursor doubly charged, which has not fragmented double pre_diff_lower = (precursor_weight + Constants::PROTON_MASS_U) / 2.0 - fragment_mass_tolerance_; double pre_diff_upper = (precursor_weight + 4.0 * Constants::PROTON_MASS_U) / 2.0 + fragment_mass_tolerance_; if (pos2 > pre_diff_lower && pos2 < pre_diff_upper) { #ifdef SCORE_ETDFEATURES_DEBUG cerr << "scoreETDFeatures: pre-range: " << pos2 << " is in precursor peak range: " << pre_diff_lower << " <-> " << pre_diff_upper << endl; #endif continue; } //double diff(pos2 - pos1); // pos1 is CID ion; pos2 is ETD ion // pos1 b-ion, pos2 c-ion if (fabs(pos1 + 17.0 - pos2) < fragment_mass_tolerance_) { // now test if the ETD peak has "isotope" pattern double factor((fragment_mass_tolerance_ - fabs(pos1 + 17.0 - pos2)) / fragment_mass_tolerance_); #ifdef SCORE_ETDFEATURES_DEBUG cerr << "scoreETDFeatures: is b-ion: " << pos1 << " (" << pos2 << ") (factor=" << factor << ") " << b_sum << " -> "; #endif vector<double> iso_pattern; iso_pattern.push_back(it1->getIntensity()); double actual_pos = it1->getPosition()[0]; for (PeakSpectrum::ConstIterator it3 = it2; it3 != ETD_spec.end(); ++it3) { double it3_pos(it3->getPosition()[0]); if (fabs(fabs(actual_pos - it3_pos) - Constants::NEUTRON_MASS_U) < fragment_mass_tolerance_) { iso_pattern.push_back(it3->getIntensity()); actual_pos = it3_pos; } if (iso_pattern.size() == max_isotope_to_score) { break; } } if (ion_scores[it1->getPosition()[0]].is_isotope_1_mono != -1) { b_sum += it2->getIntensity() * iso_pattern.size() * factor; } #ifdef SCORE_ETDFEATURES_DEBUG cerr << b_sum << endl; #endif } // pos1 z-ion, pos2 y-ion if (fabs(pos2 + 16.0 - pos1) < fragment_mass_tolerance_) { double factor((fragment_mass_tolerance_ - fabs(pos2 + 16.0 - pos1)) / fragment_mass_tolerance_); // now test if the ETD peak has "isotope" pattern #ifdef SCORE_ETDFEATURES_DEBUG cerr << "scoreETDFeatures: is y-ion: " << pos1 << " (" << pos2 << ") (factor=" << factor << ") " << y_sum << " -> "; #endif vector<double> iso_pattern; iso_pattern.push_back(it1->getIntensity()); double actual_pos = it1->getPosition()[0]; for (PeakSpectrum::ConstIterator it3 = it2; it3 != ETD_spec.end(); ++it3) { double it3_pos(it3->getPosition()[0]); if (fabs(fabs(actual_pos - it3_pos) - Constants::NEUTRON_MASS_U) < fragment_mass_tolerance_) { iso_pattern.push_back(it3->getIntensity()); actual_pos = it3_pos; } if (iso_pattern.size() == max_isotope_to_score) { break; } } #ifdef SCORE_ETDFEATURES_DEBUG cerr << ion_scores[it1->getPosition()[0]].is_isotope_1_mono << " "; #endif if (ion_scores[it1->getPosition()[0]].is_isotope_1_mono != -1) { y_sum += it2->getIntensity() * iso_pattern.size() * factor; } #ifdef SCORE_ETDFEATURES_DEBUG cerr << y_sum << endl; #endif } } ion_scores[it1->getPosition()[0]].s_bion = b_sum; ion_scores[it1->getPosition()[0]].s_yion = y_sum; } return; }
void CompNovoIonScoring::scoreWitnessSet_(Size charge, double precursor_weight, Map<double, IonScore> & ion_scores, const PeakSpectrum & CID_spec) { vector<double> diffs; //diffs.push_back(28.0); diffs.push_back(17.0); diffs.push_back(18.0); // witnesses of CID spec (diffs) for (PeakSpectrum::ConstIterator it1 = CID_spec.begin(); it1 != CID_spec.end(); ++it1) { //Size num_wit(0); double wit_score(0.0); double pos1(it1->getPosition()[0]); wit_score += it1->getIntensity(); for (PeakSpectrum::ConstIterator it2 = CID_spec.begin(); it2 != CID_spec.end(); ++it2) { double pos2(it2->getPosition()[0]); // direct ++ if (charge > 1) { if (fabs(pos2 * 2 - Constants::PROTON_MASS_U - pos1) < fragment_mass_tolerance_) { double factor((fragment_mass_tolerance_ - fabs(pos2 * 2 - Constants::PROTON_MASS_U - pos1)) / fragment_mass_tolerance_); // pos1 is ion, pos2 is ++ion #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: ++ion " << pos1 << " " << pos2 << " (factor=" << factor << ") " << wit_score << " -> "; #endif if (ion_scores[it2->getPosition()[0]].s_isotope_pattern_2 < 0.2) { wit_score += it2->getIntensity() * /* 0.2 */ factor; } else { wit_score += it2->getIntensity() * ion_scores[it2->getPosition()[0]].s_isotope_pattern_2 * factor; } #ifdef SCORE_WITNESSSET_DEBUG cerr << wit_score << endl; #endif } } // diffs? for (vector<double>::const_iterator it = diffs.begin(); it != diffs.end(); ++it) { // pos1 is ion, pos2 loss peak if (fabs(pos1 - pos2 - *it) < fragment_mass_tolerance_) { double factor((fragment_mass_tolerance_ - fabs(pos1 - pos2 - *it)) / fragment_mass_tolerance_); #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: diff " << pos1 << " (" << pos2 << ") " << *it << " (factor=" << factor << ") " << wit_score << " -> "; #endif wit_score += it2->getIntensity() /* / 5.0*/ * factor; #ifdef SCORE_WITNESSSET_DEBUG cerr << wit_score << endl; #endif } } // is there a b-ion?; pos1 is ion, pos2 complementary ion if (fabs(pos1 + pos2 - 1 * Constants::PROTON_MASS_U - precursor_weight) < fragment_mass_tolerance_) { double factor((fragment_mass_tolerance_ - fabs(pos1 + pos2 - Constants::PROTON_MASS_U - precursor_weight)) / fragment_mass_tolerance_); /*factor *= 0.2;*/ #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: complementary " << pos1 << " (" << pos2 << ") (factor=" << factor << ") " << wit_score << " -> "; #endif // found complementary ion if (ion_scores[it2->getPosition()[0]].s_isotope_pattern_1 < 0.5 || ion_scores[it2->getPosition()[0]].is_isotope_1_mono != 1) { wit_score += it2->getIntensity() /* * 0.5*/ * factor; } else { wit_score += it2->getIntensity() * ion_scores[it2->getPosition()[0]].s_isotope_pattern_1 * factor; } #ifdef SCORE_WITNESSSET_DEBUG cerr << wit_score << endl; #endif if (ion_scores[it2->getPosition()[0]].s_bion != 0) { #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: complementary is b-ion " << pos1 << "(" << pos2 << ")" << wit_score << " -> "; #endif wit_score += ion_scores[it2->getPosition()[0]].s_bion * factor; #ifdef SCORE_WITNESSSET_DEBUG cerr << wit_score << endl; #endif } } } // isotope pattern ok? if (ion_scores[it1->getPosition()[0]].s_isotope_pattern_1 > 0 && ion_scores[it1->getPosition()[0]].is_isotope_1_mono == 1) { #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: isotope pattern: " << pos1 << " " << wit_score << " -> "; #endif wit_score += ion_scores[it1->getPosition()[0]].s_isotope_pattern_1 * wit_score; #ifdef SCORE_WITNESSSET_DEBUG cerr << wit_score << endl; #endif } if (ion_scores[it1->getPosition()[0]].s_yion > 0) { #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: is y-ion: " << pos1 << " " << wit_score << " -> "; #endif wit_score += ion_scores[it1->getPosition()[0]].s_yion; #ifdef SCORE_WITNESSSET_DEBUG cerr << wit_score << endl; #endif } if (ion_scores[it1->getPosition()[0]].s_bion > 0) { #ifdef SCORE_WITNESSSET_DEBUG cerr << "scoreWitnessSet: is b-ion: " << pos1 << " " << wit_score << " -> "; #endif if (ion_scores[it1->getPosition()[0]].s_bion < wit_score) { wit_score -= ion_scores[it1->getPosition()[0]].s_bion; } else { wit_score = 0; } } ion_scores[it1->getPosition()[0]].s_witness = wit_score; } return; }