libpappsomspp
Library for mass spectrometry
massspectrumpluscombiner.cpp
Go to the documentation of this file.
1 /////////////////////// StdLib includes
2 #include <numeric>
3 #include <limits>
4 #include <vector>
5 #include <map>
6 #include <cmath>
7 #include <iostream>
8 #include <iomanip>
9 
10 
11 /////////////////////// Qt includes
12 #include <QDebug>
13 #include <QFile>
14 #include <QThread>
15 #if 0
16 // For debugging purposes.
17 #include <QFile>
18 #endif
19 
20 
21 /////////////////////// Local includes
23 #include "../../types.h"
24 #include "../../utils.h"
25 #include "../../pappsoexception.h"
26 #include "../../exception/exceptionoutofrange.h"
27 #include "../../exception/exceptionnotpossible.h"
28 
29 
30 namespace pappso
31 {
32 
33 
34 //! Construct an uninitialized instance.
36 {
37 }
38 
39 
41  : MassSpectrumCombiner(decimal_places)
42 {
43 }
44 
45 
47  const MassSpectrumPlusCombiner &other)
48  : MassSpectrumCombiner(other)
49 
50 {
51  // qDebug() << __FILE__ << " @ " << __LINE__ << __FUNCTION__ << "()";
52 }
53 
54 
57  : MassSpectrumCombiner(other)
58 
59 {
60  // qDebug() << __FILE__ << " @ " << __LINE__ << __FUNCTION__ << "()";
61 }
62 
63 
64 //! Destruct the instance.
66 {
67 }
68 
69 
70 MapTrace &
72  const Trace &trace) const
73 {
74  if(!trace.size())
75  {
76  // qDebug() << "Thread:" << QThread::currentThreadId()
77  //<< "Returning right away because trace is empty.";
78  return map_trace;
79  }
80 
81  // We will need to only use these iterator variables if we do not want to
82  // loose consistency.
83 
84  using TraceIter = std::vector<DataPoint>::const_iterator;
85  TraceIter trace_iter_begin = trace.begin();
86  TraceIter trace_iter = trace_iter_begin;
87  TraceIter trace_iter_end = trace.end();
88 
89  // The destination map trace will be filled-in with the result of the
90  // combination.
91 
92  // Sanity check:
93  if(!m_bins.size())
94  throw(ExceptionNotPossible("The bin vector cannot be empty."));
95 
96  using BinIter = std::vector<pappso_double>::const_iterator;
97 
98  BinIter bin_iter = m_bins.begin();
99  BinIter bin_end_iter = m_bins.end();
100 
101  // qDebug() << "initial bins iter at a distance of:"
102  //<< std::distance(m_bins.begin(), bin_iter)
103  //<< "bins distance:" << std::distance(m_bins.begin(), m_bins.end())
104  //<< "bins size:" << m_bins.size() << "first bin:" << m_bins.front()
105  //<< "last bin:" << m_bins.back();
106 
107  // Iterate in the vector of bins and for each bin check if there are matching
108  // data points in the trace.
109 
110  pappso_double current_bin = 0;
111 
112  pappso_double trace_x = 0;
113  pappso_double trace_y = 0;
114 
115  // Lower bound returns an iterator pointing to the first element in the
116  // range [first, last) that is not less than (i.e. greater or equal to)
117  // value, or last if no such element is found.
118 
119  auto bin_iter_for_mz = lower_bound(bin_iter, bin_end_iter, trace_iter->x);
120 
121  if(bin_iter_for_mz != bin_end_iter)
122  {
123  if(bin_iter_for_mz != m_bins.begin())
124  bin_iter = --bin_iter_for_mz;
125  }
126  else
127  throw(ExceptionNotPossible("The bin vector must match the mz value."));
128 
129  while(bin_iter != bin_end_iter)
130  {
131  current_bin = *bin_iter;
132 
133  // qDebug() << "Current bin:" << QString("%1").arg(current_bin, 0, 'f',
134  // 15)
135  //<< "at a distance of:"
136  //<< std::distance(m_bins.begin(), bin_iter);
137 
138  // For the current bin, we start by instantiating a new DataPoint. By
139  // essence, each bin will have at most one corresponding DataPoint.
140 
141  DataPoint new_data_point;
142 
143  // Do not set the y value to 0 so that we can actually test if the
144  // data point is valid later on (try not to push back y=0 data
145  // points).
146  new_data_point.x = current_bin;
147 
148  // Now perform a loop over the data points in the mass spectrum.
149 
150  // qDebug() << "trace_iter:" << trace_iter->toString()
151  //<< "data point distance:"
152  //<< std::distance(trace_iter_begin, trace_iter);
153 
154  while(trace_iter != trace_iter_end)
155  {
156 
157  bool trace_matched = false;
158 
159  // If trace is not to the end and the y value is not 0
160  // apply the shift, perform the rounding and check if the obtained
161  // x value is in the current bin, that is if it is less or equal
162  // to the current bin.
163 
164  // qDebug() << "Thread:" << QThread::currentThreadId();
165  // qDebug() << "trace_iter:" << trace_iter->toString()
166  //<< "data point distance:"
167  //<< std::distance(trace_iter_begin, trace_iter);
168 
169  // if(!Utils::almostEqual(trace_iter->y, 0.0, 10))
170  if(trace_iter->y)
171  {
172  trace_x = trace_iter->x;
173  trace_y = trace_iter->y;
174 
175  // trace_x is the m/z value that we need to combine,
176  // so make sure we check if there is a mz shift to apply.
177 
178  // if(m_mzIntegrationParams.m_applyMzShift)
179  // trace_x += m_mzIntegrationParams.m_mzShift;
180 
181  // Now apply the rounding (if any).
182  if(m_decimalPlaces != -1)
183  trace_x = Utils::roundToDecimals(trace_x, m_decimalPlaces);
184 
185  if(trace_x <= current_bin)
186  {
187 
188  // qDebug() << "Matched, increment trace_iter";
189  new_data_point.y += trace_y;
190 
191  // Let's record that we matched.
192  trace_matched = true;
193 
194  // Because we matched, we can step-up with the
195  // iterator.
196  ++trace_iter;
197  }
198  // else
199  //{
200  // We did have a non-0 y value, but that did not
201  // match. So we do not step-up with the iterator.
202  //}
203  }
204  // End of
205  // if(trace_iter->y)
206  else
207  {
208  // We iterated into a y=0 data point, so just skip it. Let
209  // the below code think that we have matched the point and
210  // iterate one step up.
211 
212  // qDebug() << "The y value is almost equal to 0, increment the "
213  //"trace iter but do nothing else.";
214 
215  trace_matched = true;
216  ++trace_iter;
217  }
218  // At this point, check if none of them matched.
219 
220  if(!trace_matched)
221  {
222  // None of the first and trace mass spectra data
223  // points were found to match the current bin. All we
224  // have to do is go to the next bin. We break and the
225  // bin vector iterator will be incremented.
226 
227  // However, if we had a valid new data point, that
228  // data point needs to be pushed back in the new mass
229  // spectrum.
230 
231  if(new_data_point.isValid())
232  {
233 
234  // We need to check if that bin value is present already in
235  // the map_trace object passed as parameter.
236 
237  std::pair<std::map<pappso_double, pappso_double>::iterator,
238  bool>
239  result =
240  map_trace.insert(std::pair<pappso_double, pappso_double>(
241  new_data_point.x, new_data_point.y));
242 
243  if(!result.second)
244  {
245  // The key already existed! The item was not inserted. We
246  // need to update the value.
247 
248  result.first->second += new_data_point.y;
249 
250  // qDebug() << "Incremented the data point in the map
251  // trace.";
252  }
253  // else
254  //{
255  // qDebug()
256  //<< "Inserted a new data point into the map trace.";
257  //}
258  }
259 
260  // We need to break this loop! That will increment the
261  // bin iterator.
262 
263  break;
264  }
265  }
266  // End of
267  // while(1)
268 
269  // Each time we get here, that means that we have consumed all
270  // the mass spectra data points that matched the current bin.
271  // So go to the next bin.
272 
273  if(trace_iter == trace_iter_end)
274  {
275 
276  // Make sure a last check is done in case one data point was
277  // cooking...
278 
279  if(new_data_point.isValid())
280  {
281 
282  std::pair<std::map<pappso_double, pappso_double>::iterator, bool>
283  result =
284  map_trace.insert(std::pair<pappso_double, pappso_double>(
285  new_data_point.x, new_data_point.y));
286 
287  if(!result.second)
288  {
289  result.first->second += new_data_point.y;
290  }
291  }
292 
293  // Now truly exit the loops...
294  break;
295  }
296 
297  ++bin_iter;
298  }
299  // End of
300  // while(bin_iter != bin_end_iter)
301 
302  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()"
303  //<< "The combination result mass spectrum being returned has TIC:"
304  //<< new_trace.sumY();
305 
306  return map_trace;
307 }
308 
309 
310 } // namespace pappso
pappso::MassSpectrumPlusCombinerCstSPtr
std::shared_ptr< const MassSpectrumPlusCombiner > MassSpectrumPlusCombinerCstSPtr
Definition: massspectrumpluscombiner.h:16
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:48
pappso::MassSpectrumPlusCombiner::combineNoFilteringStep
virtual MapTrace & combineNoFilteringStep(MapTrace &map_trace, const Trace &trace) const
Definition: massspectrumpluscombiner.cpp:71
pappso::MassSpectrumCombiner
Definition: massspectrumcombiner.h:30
pappso::MassDataCombinerInterface::m_decimalPlaces
int m_decimalPlaces
Number of decimals to use for the keys (x values)
Definition: massdatacombinerinterface.h:44
pappso::DataPoint::y
pappso_double y
Definition: datapoint.h:23
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks
Definition: aa.cpp:39
pappso::DataPoint
Definition: datapoint.h:21
pappso::MapTrace
Definition: maptrace.h:33
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:32
pappso::MassSpectrumPlusCombiner
Definition: massspectrumpluscombiner.h:25
pappso::MassSpectrumPlusCombiner::~MassSpectrumPlusCombiner
virtual ~MassSpectrumPlusCombiner()
Destruct the instance.
Definition: massspectrumpluscombiner.cpp:65
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:132
pappso::DataPoint::isValid
bool isValid() const
Definition: datapoint.cpp:132
pappso::DataPoint::x
pappso_double x
Definition: datapoint.h:22
massspectrumpluscombiner.h
pappso::Utils::roundToDecimals
static pappso_double roundToDecimals(pappso_double value, int decimal_places)
Definition: utils.cpp:104
pappso::MassSpectrumPlusCombiner::MassSpectrumPlusCombiner
MassSpectrumPlusCombiner()
Construct an uninitialized instance.
Definition: massspectrumpluscombiner.cpp:35
pappso::MassSpectrumCombiner::m_bins
std::vector< pappso_double > m_bins
Definition: massspectrumcombiner.h:58