libpappsomspp
Library for mass spectrometry
maptrace.cpp
Go to the documentation of this file.
1#include <numeric>
2#include <limits>
3#include <vector>
4#include <map>
5#include <cmath>
6#include <algorithm>
7#include <iostream>
8#include <iomanip>
9
10/////////////////////// Qt includes
11#include <QDebug>
12
13
14#include "../exception/exceptionnotpossible.h"
15#include "maptrace.h"
16#include "../processing/combiners/tracepluscombiner.h"
17#include "../processing/combiners/traceminuscombiner.h"
18#include "../types.h"
19
20
22 qRegisterMetaType<pappso::MapTrace>("pappso::MapTrace");
24 qRegisterMetaType<pappso::MapTrace *>("pappso::MapTrace *");
25
26
27namespace pappso
28{
29
31{
32}
33
34
36 const std::vector<std::pair<pappso_double, pappso_double>> &dataPoints)
37{
38 for(auto &dataPoint : dataPoints)
39 {
40 insert(dataPoint);
41 }
42}
43
44
45MapTrace::MapTrace(const std::vector<DataPoint> &dataPoints)
46{
47 for(auto &dataPoint : dataPoints)
48 {
49 insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
50 }
51}
52
53
55 : std::map<pappso_double, pappso_double>(other)
56{
57}
58
59
61{
62 // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()";
63
64 for(auto &dataPoint : trace)
65 {
66 // std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << "
67 // () "
68 //<< std::setprecision(15)
69 //<< "Current data point: " << dataPoint.toString().toStdString() <<
70 // std::endl;
71
72 insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
73 }
74
75 // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()"
76 //<< "After construction, map size: " << size();
77}
78
79
81{
82 // Calls the destructor for each DataPoint object in the vector.
83 clear();
84}
85
86
87size_t
88MapTrace::initialize(const std::vector<pappso_double> &xVector,
89 const std::vector<pappso_double> &yVector)
90{
91 // Clear *this, because initialize supposes that *this will contain only the
92 // data in the vectors.
93
94 clear();
95
96 // Sanity check
97 if(xVector.size() != yVector.size())
99 QObject::tr("Fatal error at msrundatasettreenode.cpp "
100 "-- ERROR xVector and yVector must have the same size."
101 "Program aborted."));
102
103 for(std::size_t iter = 0; iter < xVector.size(); ++iter)
104 {
105 insert(std::pair<pappso_double, pappso_double>(xVector.at(iter),
106 yVector.at(iter)));
107 }
108
109 return size();
110}
111
112
113size_t
114MapTrace::initialize(const std::map<pappso_double, pappso_double> &map)
115{
116
117 // Clear *this, because initialize supposes that *this will be identical to
118 // map.
119
120 clear();
121
122 for(auto &&pair : map)
123 {
124 insert(pair);
125 }
126
127 return size();
128}
129
130
131MapTrace &
133{
134
135 if(&other == this)
136 return *this;
137
138 // Clear *this, because initialize supposes that *this will be identical to
139 // other.
140
141 clear();
142
143 for(auto &pair : other)
144 {
145 insert(pair);
146 }
147
148 return *this;
149}
150
151
154{
155 return std::make_shared<MapTrace>(*this);
156}
157
158
161{
162 return std::make_shared<const MapTrace>(*this);
163}
164
165
166std::vector<pappso_double>
168{
169 std::vector<pappso_double> vector;
170
171 for(auto &&pair : *this)
172 vector.push_back(pair.first);
173
174 return vector;
175}
176
177
178std::vector<pappso_double>
180{
181 std::vector<pappso_double> vector;
182
183 for(auto &&pair : *this)
184 vector.push_back(pair.second);
185
186 return vector;
187}
188
189
190void
192{
193
194 // Try to insert the data point into the map. Check if that was done or
195 // not.
196 std::pair<std::map<double, double>::iterator, bool> res =
197 insert(std::pair<double, double>(data_point.x, data_point.y));
198
199 if(!res.second)
200 {
201 // One other same (x,y) value pair was seen already. Only increment the y
202 // value.
203
204 res.first->second += data_point.y;
205 }
206}
207
208
209void
211{
212 for(const DataPoint &data_point : trace)
213 insertOrUpdate(data_point);
214}
215
216
217Trace
219{
220 Trace trace;
221
222 for(auto &&pair : *this)
223 trace.push_back(DataPoint(pair.first, pair.second));
224
225 return trace;
226}
227
228
229QString
231{
232 // Even if the spectrum is empty, we should return an empty string.
233 QString text;
234
235 for(auto &&pair : *this)
236 {
237// For debugging
238#if 0
239
240 QString new_data_point_text = QString("%1 %2\n")
241 .arg(pair.first, 0, 'f', 10)
242 .arg(pair.second, 0, 'f', 10);
243
244 qDebug() << "new data point text:" << new_data_point_text;
245 text.append(new_data_point_text);
246#endif
247
248 text.append(QString("%1 %2\n")
249 .arg(pair.first, 0, 'f', 10)
250 .arg(pair.second, 0, 'f', 10));
251 }
252
253 return text;
254}
255
256
257} // namespace pappso
QString toString() const
Definition: maptrace.cpp:230
MapTraceSPtr makeMapTraceSPtr() const
Definition: maptrace.cpp:153
virtual ~MapTrace()
Definition: maptrace.cpp:80
virtual MapTrace & operator=(const MapTrace &other)
Definition: maptrace.cpp:132
Trace toTrace() const
Definition: maptrace.cpp:218
std::vector< pappso_double > xValues()
Definition: maptrace.cpp:167
MapTraceCstSPtr makeMapTraceCstSPtr() const
Definition: maptrace.cpp:160
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition: maptrace.cpp:88
void insertOrUpdate(const DataPoint &data_point)
Definition: maptrace.cpp:191
std::vector< pappso_double > yValues()
Definition: maptrace.cpp:179
A simple container of DataPoint instances.
Definition: trace.h:147
int mapTracePtrMetaTypeId
Definition: maptrace.cpp:23
int mapTraceMetaTypeId
Definition: maptrace.cpp:21
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< MapTrace > MapTraceSPtr
Definition: maptrace.h:25
double pappso_double
A type definition for doubles.
Definition: types.h:48
std::shared_ptr< const MapTrace > MapTraceCstSPtr
Definition: maptrace.h:26
pappso_double x
Definition: datapoint.h:22
pappso_double y
Definition: datapoint.h:23