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