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