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>
34
35
36using namespace pappso;
37
39{
40 mp_precision = precision;
41
42 msp_filterExclusion = std::make_shared<FilterMzExclusion>(precision);
43}
44
50
54
55double
57{
58 // get inner peaks
59 // we need a filter to get pairs of common peaks between a & b
60 // or at least compute quickly intensity square of common pairs
61
62 msp_filterExclusion.get()->filter(trace_a);
63 msp_filterExclusion.get()->filter(trace_b);
64
65 auto itb = trace_b.begin();
66 double inner_intensity_product = 0;
67 for(const auto &peak_a : trace_a)
68 {
69 MzRange range(peak_a.x, mp_precision);
70 double low = range.lower();
71 double up = range.upper();
72
73 while((itb != trace_b.end()) && (itb->x < low))
74 {
75 itb++;
76 }
77 if(itb->x < up)
78 {
79 inner_intensity_product += peak_a.y * itb->y;
80 }
81 }
82
83 double tracea_product = 0;
84 // a intensity sum of intensity square
85 for(const auto &peak_a : trace_a)
86 {
87 tracea_product += peak_a.y * peak_a.y;
88 }
89
90
91 double traceb_product = 0;
92 // a intensity sum of intensity square
93 for(const auto &peak_a : trace_b)
94 {
95 traceb_product += peak_a.y * peak_a.y;
96 }
97
98 return (inner_intensity_product / (sqrt(tracea_product) * sqrt(traceb_product)));
99}
double similarity(pappso::Trace trace_a, pappso::Trace trace_b) const
CosineSimilarity(PrecisionPtr precision)
FilterInterfaceSPtr msp_filterExclusion
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:152
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
const PrecisionBase * PrecisionPtr
Definition precision.h:122