libpappsomspp
Library for mass spectrometry
pappso::MassSpectrumPlusCombiner Class Reference

#include <massspectrumpluscombiner.h>

Inheritance diagram for pappso::MassSpectrumPlusCombiner:
pappso::MassSpectrumCombiner pappso::MassDataCombinerInterface

Public Member Functions

 MassSpectrumPlusCombiner ()
 Construct an uninitialized instance. More...
 
 MassSpectrumPlusCombiner (int decimal_places)
 
 MassSpectrumPlusCombiner (const MassSpectrumPlusCombiner &other)
 
 MassSpectrumPlusCombiner (MassSpectrumPlusCombinerCstSPtr other)
 
virtual ~MassSpectrumPlusCombiner ()
 Destruct the instance. More...
 
- Public Member Functions inherited from pappso::MassSpectrumCombiner
 MassSpectrumCombiner ()
 Construct an uninitialized instance. More...
 
 MassSpectrumCombiner (std::vector< pappso_double > bins, int decimalPlaces=-1)
 
 MassSpectrumCombiner (int decimal_places)
 
 MassSpectrumCombiner (MassSpectrumCombinerCstSPtr other)
 
 MassSpectrumCombiner (const MassSpectrumCombiner &other)
 
 MassSpectrumCombiner (const MassSpectrumCombiner &&other)
 
virtual ~MassSpectrumCombiner ()
 Destruct the instance. More...
 
std::vector< pappso_double >::const_iterator begin () const
 
std::vector< pappso_double >::const_iterator end () const
 
std::vector< pappso_double >::iterator begin ()
 
std::vector< pappso_double >::iterator end ()
 
void setBins (std::vector< pappso_double > bins)
 
const std::vector< pappso_double > & getBins () const
 
std::size_t binCount () const
 
virtual MapTracecombine (MapTrace &map_trace, const Trace &trace) const
 
virtual MapTracecombine (MapTrace &map_trace_out, const MapTrace &map_trace_in) const
 
- Public Member Functions inherited from pappso::MassDataCombinerInterface
 MassDataCombinerInterface (int decimal_places=-1)
 
virtual ~MassDataCombinerInterface ()
 
void setDecimalPlaces (int value)
 
int getDecimalPlaces () const
 
void setFilterResampleKeepXRange (const FilterResampleKeepXRange &range)
 
virtual MapTracecombine (MapTrace &map_trace, Iterator begin, Iterator end)
 

Private Member Functions

virtual MapTracecombineNoFilteringStep (MapTrace &map_trace, const Trace &trace) const
 

Additional Inherited Members

- Public Types inherited from pappso::MassDataCombinerInterface
using Iterator = std::vector< const Trace * >::const_iterator
 
- Protected Member Functions inherited from pappso::MassSpectrumCombiner
std::vector< pappso_double >::iterator findBin (pappso_double mz)
 Find the bin that will contain mz. More...
 
- Protected Attributes inherited from pappso::MassSpectrumCombiner
std::vector< pappso_doublem_bins
 
- Protected Attributes inherited from pappso::MassDataCombinerInterface
int m_decimalPlaces = -1
 Number of decimals to use for the keys (x values) More...
 
bool m_isApplyXRangeFilter = false
 
FilterResampleKeepXRange m_filterXRange
 

Detailed Description

Definition at line 24 of file massspectrumpluscombiner.h.

Constructor & Destructor Documentation

◆ MassSpectrumPlusCombiner() [1/4]

pappso::MassSpectrumPlusCombiner::MassSpectrumPlusCombiner ( )

Construct an uninitialized instance.

Definition at line 35 of file massspectrumpluscombiner.cpp.

36 {
37 }

◆ MassSpectrumPlusCombiner() [2/4]

pappso::MassSpectrumPlusCombiner::MassSpectrumPlusCombiner ( int  decimal_places)

Definition at line 40 of file massspectrumpluscombiner.cpp.

41  : MassSpectrumCombiner(decimal_places)
42 {
43 }

◆ MassSpectrumPlusCombiner() [3/4]

pappso::MassSpectrumPlusCombiner::MassSpectrumPlusCombiner ( const MassSpectrumPlusCombiner other)

Definition at line 46 of file massspectrumpluscombiner.cpp.

48  : MassSpectrumCombiner(other)
49 
50 {
51  // qDebug() << __FILE__ << " @ " << __LINE__ << __FUNCTION__ << "()";
52 }

◆ MassSpectrumPlusCombiner() [4/4]

pappso::MassSpectrumPlusCombiner::MassSpectrumPlusCombiner ( MassSpectrumPlusCombinerCstSPtr  other)

Definition at line 55 of file massspectrumpluscombiner.cpp.

57  : MassSpectrumCombiner(other)
58 
59 {
60  // qDebug() << __FILE__ << " @ " << __LINE__ << __FUNCTION__ << "()";
61 }

◆ ~MassSpectrumPlusCombiner()

pappso::MassSpectrumPlusCombiner::~MassSpectrumPlusCombiner ( )
virtual

Destruct the instance.

Definition at line 65 of file massspectrumpluscombiner.cpp.

66 {
67 }

Member Function Documentation

◆ combineNoFilteringStep()

MapTrace & pappso::MassSpectrumPlusCombiner::combineNoFilteringStep ( MapTrace map_trace,
const Trace trace 
) const
privatevirtual

Implements pappso::MassSpectrumCombiner.

Definition at line 71 of file massspectrumpluscombiner.cpp.

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 }

References pappso::DataPoint::isValid(), pappso::MassSpectrumCombiner::m_bins, pappso::MassDataCombinerInterface::m_decimalPlaces, pappso::Utils::roundToDecimals(), pappso::DataPoint::x, and pappso::DataPoint::y.


The documentation for this class was generated from the following files:
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:69
pappso::MassDataCombinerInterface::m_decimalPlaces
int m_decimalPlaces
Number of decimals to use for the keys (x values)
Definition: massdatacombinerinterface.h:44
pappso::MassSpectrumCombiner::MassSpectrumCombiner
MassSpectrumCombiner()
Construct an uninitialized instance.
Definition: massspectrumcombiner.cpp:28
pappso::Utils::roundToDecimals
static pappso_double roundToDecimals(pappso_double value, int decimal_places)
Definition: utils.cpp:125
pappso::MassSpectrumCombiner::m_bins
std::vector< pappso_double > m_bins
Definition: massspectrumcombiner.h:58