libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
cosinesimilarity.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/compartraces/cosinesimilarity.cpp
3 * \date 12/06/2023
4 * \author Olivier Langella
5 * \brief computes cosine similarity of 2 traces vector
6 * https://en.wikipedia.org/wiki/Cosine_similarity
7 */
8
9/*******************************************************************************
10 * Copyright (c) 2023 Olivier Langella <Olivier.Langella@u-psud.fr>.
11 *
12 * This file is part of PAPPSOms-tools.
13 *
14 * PAPPSOms-tools is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * PAPPSOms-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with PAPPSOms-tools. If not, see <http://www.gnu.org/licenses/>.
26 *
27 ******************************************************************************/
28
29
30#include "cosinesimilarity.h"
31#include <cmath>
32
33
34using namespace pappso;
35
37{
38 mp_precision = precision;
39
40 msp_filterExclusion = std::make_shared<FilterMzExclusion>(precision);
41}
42
48
52
53double
55 pappso::Trace trace_b) const
56{
57 // get inner peaks
58 // we need a filter to get pairs of common peaks between a & b
59 // or at least compute quickly intensity square of common pairs
60
61 msp_filterExclusion.get()->filter(trace_a);
62 msp_filterExclusion.get()->filter(trace_b);
63
64 auto itb = trace_b.begin();
65 double inner_intensity_product = 0;
66 for(const auto &peak_a : trace_a)
67 {
68 MzRange range(peak_a.x, mp_precision);
69 double low = range.lower();
70 double up = range.upper();
71
72 while((itb != trace_b.end()) && (itb->x < low))
73 {
74 itb++;
75 }
76 if(itb->x < up)
77 {
78 inner_intensity_product += peak_a.y * itb->y;
79 }
80 }
81
82 double tracea_product = 0;
83 // a intensity sum of intensity square
84 for(const auto &peak_a : trace_a)
85 {
86 tracea_product += peak_a.y * peak_a.y;
87 }
88
89
90 double traceb_product = 0;
91 // a intensity sum of intensity square
92 for(const auto &peak_a : trace_b)
93 {
94 traceb_product += peak_a.y * peak_a.y;
95 }
96
97 return (inner_intensity_product /
98 (sqrt(tracea_product) * sqrt(traceb_product)));
99}
double similarity(pappso::Trace trace_a, pappso::Trace trace_b) const
CosineSimilarity(PrecisionPtr precision)
FilterInterfaceSPtr msp_filterExclusion
virtual Trace & filter(Trace &data_points) const =0
pappso_double lower() const
Definition mzrange.h:71
pappso_double upper() const
Definition mzrange.h:77
A simple container of DataPoint instances.
Definition trace.h:148
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
Definition trace.cpp:1210
computes cosine similarity of 2 traces vector https://en.wikipedia.org/wiki/Cosine_similarity
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39