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