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