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