libpappsomspp
Library for mass spectrometry
Toggle main menu visibility
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
#include "
pappsomspp/core/mzrange.h
"
33
#include "
pappsomspp/core/processing/filters/filterexclusionmz.h
"
34
35
36
using namespace
pappso
;
37
38
CosineSimilarity::CosineSimilarity
(
PrecisionPtr
precision)
39
{
40
mp_precision
= precision;
41
42
msp_filterExclusion
= std::make_shared<FilterMzExclusion>(precision);
43
}
44
45
CosineSimilarity::CosineSimilarity
(
const
CosineSimilarity
&other)
46
{
47
mp_precision
= other.
mp_precision
;
48
msp_filterExclusion
= other.
msp_filterExclusion
;
49
}
50
51
CosineSimilarity::~CosineSimilarity
()
52
{
53
}
54
55
double
56
pappso::CosineSimilarity::similarity
(
pappso::Trace
trace_a,
pappso::Trace
trace_b)
const
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
}
pappso::CosineSimilarity::similarity
double similarity(pappso::Trace trace_a, pappso::Trace trace_b) const
Definition
cosinesimilarity.cpp:56
pappso::CosineSimilarity::mp_precision
PrecisionPtr mp_precision
Definition
cosinesimilarity.h:64
pappso::CosineSimilarity::CosineSimilarity
CosineSimilarity(PrecisionPtr precision)
Definition
cosinesimilarity.cpp:38
pappso::CosineSimilarity::~CosineSimilarity
~CosineSimilarity()
Definition
cosinesimilarity.cpp:51
pappso::CosineSimilarity::msp_filterExclusion
FilterInterfaceSPtr msp_filterExclusion
Definition
cosinesimilarity.h:65
pappso::MzRange
Definition
mzrange.h:46
pappso::MzRange::lower
pappso_double lower() const
Definition
mzrange.h:71
pappso::MzRange::upper
pappso_double upper() const
Definition
mzrange.h:77
pappso::Trace
A simple container of DataPoint instances.
Definition
trace.h:152
cosinesimilarity.h
filterexclusionmz.h
mzrange.h
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition
aa.cpp:39
pappso::PrecisionPtr
const PrecisionBase * PrecisionPtr
Definition
precision.h:122
pappsomspp
core
processing
compartraces
cosinesimilarity.cpp
Generated on
for libpappsomspp by
1.17.0