libpappsomspp
Library for mass spectrometry
Toggle main menu visibility
Loading...
Searching...
No Matches
datapoint.cpp
Go to the documentation of this file.
1
// Copyright 2019, 2020 Filippo Rusconi
2
// License: GPLv3+
3
4
/////////////////////// StdLib includes
5
#include <vector>
6
#include <cmath>
7
8
9
/////////////////////// Qt includes
10
#include <QDebug>
11
#include <QDataStream>
12
#include <QRegularExpressionMatch>
13
14
15
/////////////////////// Local includes
16
#include "
datapoint.h
"
17
#include "
pappsomspp/core/types.h
"
18
#include "
pappsomspp/core/utils.h
"
19
#include "
pappsomspp/core/pappsoexception.h
"
20
#include "
pappsomspp/core/exception/exceptionoutofrange.h
"
21
#include "
pappsomspp/core/exception/exceptionnotpossible.h
"
22
23
24
int
dataPointMetaTypeId
= qRegisterMetaType<pappso::DataPoint>(
"pappso::DataPoint"
);
25
26
27
int
dataPointCstSPtrMetaTypeId
=
28
qRegisterMetaType<pappso::DataPointCstSPtr>(
"pappso::DataPointCstSPtr"
);
29
30
namespace
pappso
31
{
32
33
34
DataPoint::DataPoint
(
pappso_double
x
,
pappso_double
y
) :
x
(
x
),
y
(
y
)
35
{
36
}
37
38
39
DataPoint::DataPoint
(std::pair<pappso_double, pappso_double> pair) :
x
(pair.first),
y
(pair.second)
40
{
41
}
42
43
44
DataPoint::DataPoint
(
const
QString &text)
45
{
46
if
(!
initialize
(text))
47
throw
ExceptionNotPossible
(
48
"Failed to initialize the DataPoint object using the provided string."
);
49
}
50
51
DataPoint::DataPoint
(
const
DataPoint
&other) :
x
(other.
x
),
y
(other.
y
)
52
{
53
}
54
55
56
// For debugging purposes.
57
// DataPoint::~DataPoint()
58
//{
59
////qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
60
////<< "Calling destructor for DataPoint.";
61
//}
62
63
64
DataPointCstSPtr
65
DataPoint::makeDataPointCstSPtr
()
const
66
{
67
return
std::make_shared<const DataPoint>(*
this
);
68
}
69
70
71
void
72
DataPoint::initialize
(
pappso_double
x
,
pappso_double
y
)
73
{
74
this->x =
x
;
75
this->y =
y
;
76
}
77
78
79
void
80
DataPoint::initialize
(
const
DataPoint
&other)
81
{
82
x
= other.
x
;
83
y
= other.
y
;
84
}
85
86
87
bool
88
DataPoint::initialize
(
const
QString &text)
89
{
90
91
QRegularExpressionMatch regExpMatch;
92
93
regExpMatch =
Utils::xyMassDataFormatRegExp
.match(text);
94
95
if
(!regExpMatch.hasMatch())
96
return
false
;
97
98
bool
ok =
false
;
99
100
double
key = regExpMatch.captured(1).toDouble(&ok);
101
102
if
(!ok)
103
return
false
;
104
105
// Note that group 2 is the separator group.
106
107
double
val = regExpMatch.captured(3).toDouble(&ok);
108
109
if
(!ok)
110
return
false
;
111
112
x
= key;
113
y
= val;
114
115
return
true
;
116
}
117
118
119
void
120
DataPoint::reset
()
121
{
122
x
= -1;
123
y
= 0;
124
}
125
126
127
bool
128
DataPoint::isValid
()
const
129
{
130
return
(
x
>= 0);
131
}
132
133
134
QString
135
DataPoint::toString
()
const
136
{
137
return
QString(
"%1 %2"
).arg(
x
, 0,
'f'
, 15).arg(
y
, 0,
'f'
, 15);
138
}
139
140
141
QString
142
DataPoint::toString
(
int
decimals)
const
143
{
144
return
QString(
"%1 %2"
).arg(
x
, 0,
'f'
, decimals).arg(
y
, 0,
'f'
, decimals);
145
}
146
147
148
QDataStream &
149
operator<<
(QDataStream &out,
const
DataPoint
&dataPoint)
150
{
151
out << dataPoint.
x
;
152
out << dataPoint.
y
;
153
154
return
out;
155
}
156
157
158
QDataStream &
159
operator>>
(QDataStream &in,
DataPoint
&dataPoint)
160
{
161
162
if
(in.atEnd())
163
{
164
throw
PappsoException
(QString(
"error in QDataStream unserialize operator>> of massSpectrum "
165
"dataPoint:\nread datastream failed status=%1"
)
166
.arg(in.status()));
167
}
168
in >> dataPoint.
x
;
169
in >> dataPoint.
y
;
170
171
return
in;
172
}
173
174
175
void
176
DataPoint::incrementX
(
pappso_double
value)
177
{
178
x
+= value;
179
}
180
181
182
void
183
DataPoint::incrementY
(
pappso_double
value)
184
{
185
y
+= value;
186
}
187
188
bool
189
DataPoint::operator==
(
const
DataPoint
&other)
const
190
{
191
return
((
x
== other.
x
) && (
y
== other.
y
));
192
}
193
194
195
DataPoint
&
196
DataPoint::operator=
(
const
DataPoint
&other)
197
{
198
x
= other.
x
;
199
y
= other.
y
;
200
201
return
*
this
;
202
}
203
204
205
}
// namespace pappso
pappso::ExceptionNotPossible
Definition
exceptionnotpossible.h:29
pappso::PappsoException
Definition
pappsoexception.h:41
pappso::Utils::xyMassDataFormatRegExp
static QRegularExpression xyMassDataFormatRegExp
Regular expression matching <numerical value><non-numerical*><numericalvalue>.
Definition
utils.h:60
dataPointCstSPtrMetaTypeId
int dataPointCstSPtrMetaTypeId
Definition
datapoint.cpp:27
dataPointMetaTypeId
int dataPointMetaTypeId
Definition
datapoint.cpp:24
datapoint.h
exceptionnotpossible.h
exceptionoutofrange.h
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition
aa.cpp:39
pappso::operator<<
QDataStream & operator<<(QDataStream &outstream, const MassSpectrum &massSpectrum)
Definition
massspectrum.cpp:339
pappso::operator>>
QDataStream & operator>>(QDataStream &instream, MassSpectrum &massSpectrum)
Definition
massspectrum.cpp:353
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition
types.h:60
pappso::DataPointCstSPtr
std::shared_ptr< const DataPoint > DataPointCstSPtr
Definition
datapoint.h:18
pappsoexception.h
pappso::DataPoint
Definition
datapoint.h:22
pappso::DataPoint::incrementY
void incrementY(pappso_double value)
Definition
datapoint.cpp:183
pappso::DataPoint::DataPoint
DataPoint()=default
pappso::DataPoint::x
pappso_double x
Definition
datapoint.h:24
pappso::DataPoint::isValid
bool isValid() const
Definition
datapoint.cpp:128
pappso::DataPoint::toString
QString toString() const
Definition
datapoint.cpp:135
pappso::DataPoint::initialize
void initialize(pappso_double x, pappso_double y)
Definition
datapoint.cpp:72
pappso::DataPoint::operator==
bool operator==(const DataPoint &other) const
Definition
datapoint.cpp:189
pappso::DataPoint::incrementX
void incrementX(pappso_double value)
Definition
datapoint.cpp:176
pappso::DataPoint::operator=
DataPoint & operator=(const DataPoint &other)
Definition
datapoint.cpp:196
pappso::DataPoint::reset
void reset()
Definition
datapoint.cpp:120
pappso::DataPoint::y
pappso_double y
Definition
datapoint.h:25
pappso::DataPoint::makeDataPointCstSPtr
DataPointCstSPtr makeDataPointCstSPtr() const
Definition
datapoint.cpp:65
types.h
utils.h
pappsomspp
core
trace
datapoint.cpp
Generated on
for libpappsomspp by
1.17.0