libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
massspectrumpluscombiner.cpp
Go to the documentation of this file.
1/////////////////////// StdLib includes
2#include <vector>
3#include <map>
4#include <cmath>
5
6
7/////////////////////// Qt includes
8#include <QDebug>
9#include <QFile>
10#include <QThread>
11#if 0
12// For debugging purposes.
13#include <QFile>
14#endif
15
16
17/////////////////////// Local includes
21
22namespace pappso
23{
24
25
26//! Construct an uninitialized instance.
31
33 QObject *parent_p)
34 : MassSpectrumCombiner(decimal_places, parent_p)
35{
36}
37
39 const MassSpectrumPlusCombiner &other, QObject *parent_p)
40 : MassSpectrumCombiner(other, parent_p)
41
42{
43}
44
51
52//! Destruct the instance.
56
59{
60 if(this == &other)
61 return *this;
62
64
65 m_bins.assign(other.m_bins.begin(), other.m_bins.end());
66
67 return *this;
68}
69
70// We get a trace as parameter that this combiner needs to combine into the
71// map_trace we also get as parameter. This combiner is for a mass spectrum, and
72// thus it needs to have been configured to hold a vector of m/z values that are
73// bins into which DataPoint_s from trace are combined into map_trace. It is the
74// bins that drive the traversal of trace. For each bin, check if there is
75// (or are, if bins are big) data points in the trace that fit in it.
76
77// One important consideration is that the bins are formed before the call to
78// this combine() function and thus the distance between one m/z value and the
79// next is already determined. This means that we do not need to bother with the
80// conversion of m/z values to new decimal_places-defined values.
81
82// Another idea is that, upon the very first combination of the first spectrum
83// of the the thousands of spectra that are going to potentially be combined
84// into map_trace object, that map_trace object is *empty*. This means that we
85// can set the bins in that map_trace object and then iterated directly in the
86// destination map_trace and do the intensity incrementation calculation right
87// into the .second member of the pair that is being iterated into in the
88// map_trace object.
90MassSpectrumPlusCombiner::combine(MapTrace &map_trace, const Trace &trace) const
91{
92 // qDebug();
93
94 // If there is no single point in the trace we need to combine into the
95 // map_trace, then there is nothing to do.
96 if(!trace.size())
97 {
98 // qDebug() << "Thread:" << QThread::currentThreadId()
99 //<< "Returning right away because trace is empty.";
100 return map_trace;
101 }
102
103 // The destination map trace will be filled-in with the result of the
104 // combination.
105
106 // Sanity check:
107 if(!m_bins.size())
108 throw(ExceptionNotPossible("The bin vector cannot be empty."));
109
110 // qDebug() << qSetRealNumberPrecision(6) << "There are " << m_bins.size() <<
111 // "bins running from"
112 // << m_bins.front() << "to" << m_bins.back();
113
114 // Since the map_trace argument is used all along the combination of
115 // potentially thousands of mass spectra, we need to be careful. But
116 // clearly, if that map_trace object is empty, then, one way to accelerate the
117 // whole process is to copy the bins in there and later assume that
118 // the bin is already in it. So that we need not test each time if we have
119 // inserted the new data point or if we simply need to increment its
120 // intensity.
121
122 if(!map_trace.size())
123 {
124 std::transform(m_bins.begin(),
125 m_bins.end(),
126 std::inserter(map_trace, map_trace.end()),
127 [](double key) {
128 return std::make_pair(key, 0.0);
129 });
130 }
131
132 // We will need to only use these iterator variables if we do not want to
133 // loose consistency.
134
135 using TraceIter = std::vector<DataPoint>::const_iterator;
136 TraceIter trace_iter_begin = trace.begin();
137 TraceIter trace_iter = trace_iter_begin;
138 TraceIter trace_iter_end = trace.end();
139
140 // qDebug() << qSetRealNumberPrecision(6) << "The trace to combine has " <<
141 // trace.size()
142 // << "data points, with first a m/z" << trace.front().x << "and last
143 // at m/z"
144 // << trace.back().x;
145
146 // We will iterate in the destination map_trace that has been filled with
147 // the bins initially computed for this combiner.
148 using BinIter = std::map<double, double>::iterator;
149 BinIter map_trace_bin_iter = map_trace.begin();
150 BinIter map_trace_bin_end_iter = map_trace.end();
151
152 // We do not want to iterate in too many map_trace items before arriving
153 // to the map_trace item that matches the m/z value of the first data
154 // point in trace.
155
156 // Get the first map_trace bin that would match the very first data point in
157 // the trace that we need to combine into the map_trace. We want the first
158 // map_trace element having key >= trace_iter->x.
159 BinIter map_trace_bin_iter_for_mz = map_trace.upper_bound(trace_iter->x);
160
161 // If it's not the beginning, the previous element is what we want
162 if(map_trace_bin_iter_for_mz != map_trace.begin())
163 {
164 map_trace_bin_iter =
165 --map_trace_bin_iter_for_mz; // This is the greatest map_trace key <
166 // trace_iter->x
167
168 // qDebug() << "Found map_trace key=" << map_trace_bin_iter_for_mz->first
169 // << ", value=" << map_trace_bin_iter_for_mz->second;
170 }
171 else
172 {
173 // qDebug() << "The first bin found to match the first data point in trace
174 // is:"
175 // << map_trace.begin()->first;
176 }
177
178 // qDebug() << "The first bin found to match the first m/z value in the trace
179 // to combine ("
180 // << trace_iter->x
181 // << ") is at index:" << std::distance(map_trace.begin(),
182 // map_trace_bin_iter);
183
184 while(map_trace_bin_iter != map_trace_bin_end_iter)
185 {
186 // qDebug() << qSetRealNumberPrecision(6)
187 // << "Now iterating in new bin with mz:" <<
188 // map_trace_bin_iter->first
189 // << "at a distance of the first bin:"
190 // << std::distance(map_trace.begin(), map_trace_bin_iter);
191
192 // Now perform a loop over the data points in the mass spectrum.
193
194 // qDebug() << "Start looping in the trace to check if its DataPoint.x "
195 //"value matches that of the current bin."
196 //<< "trace_iter:" << trace_iter->toString()
197 //<< "at data point distance from first point in trace:"
198
199 while(trace_iter != trace_iter_end)
200 {
201 // qDebug() << qSetRealNumberPrecision(6)
202 // << "trace data point being iterated into: " <<
203 // trace_iter->x << " - "
204 // << trace_iter->y;
205
206 // If trace is not to the end and the y value is not 0, perform the
207 // rounding and check if the obtained x value is in the current bin,
208 // that is if it is less or equal to the current bin.
209
210 // qDebug() << "Thread:" << QThread::currentThreadId();
211 // qDebug() << "trace_iter:" << trace_iter->toString()
212 //<< "data point distance:"
213 //<< std::distance(trace_iter_begin, trace_iter);
214
215
216 if(trace_iter->x <= map_trace_bin_iter->first)
217 {
218 // qDebug() << qSetRealNumberPrecision(6) << "Matched because
219 // trace x" << trace_iter->x
220 // << "is <= current bin m/z" <<
221 // map_trace_bin_iter->first;
222
223 // qDebug() << "Incrementing the y value by " << trace_iter->y;
224
225 map_trace_bin_iter->second += trace_iter->y;
226
227 // qDebug() << qSetRealNumberPrecision(6)
228 // << "After incrementation of the intensity: " <<
229 // map_trace_bin_iter->second;
230
231 // Because we matched, we can step-up with the
232 // trace iterator. The idea is that, if the bins are large, then,
233 // multiple data points from a given mass spec acquisition' trace
234 // may fall inside that bin.
235
236 // qDebug() << "Now increment the trace iterator.";
237
238 ++trace_iter;
239 }
240 else
241 {
242 // The trace data point's x did not match the current map_trace
243 // bin m/z value because the former was greater than the latter.
244
245 // Because the DataPoint instances in the trace are sorted in
246 // increasing x order, we know we won't have to add any data point
247 // to this bin. So we break this loop, which will increment the
248 // map_trace bin iterator.
249
250 break;
251 }
252 }
253 // End of
254 // while(trace_iter != trace_iter_end)
255
256 // Each time we get here, that means that we have consumed all
257 // the mass spectra data points that matched the current bin.
258 // So go to the next bin.
259
260 if(trace_iter == trace_iter_end)
261 {
262 // Well, in fact we truly need to exit the loops...
263 break;
264 }
265
266 // qDebug() << "Now increment the bin iterator.";
267 ++map_trace_bin_iter;
268 }
269 // End of
270 // while(bin_iter != bin_end_iter)
271
272 return map_trace;
273}
274
275MapTrace &
277 const MapTrace &map_trace_in) const
278{
279 // qDebug();
280
281 if(!map_trace_in.size())
282 {
283 // qDebug() << "Thread:" << QThread::currentThreadId()
284 //<< "Returning right away because map_trace_in is empty.";
285 return map_trace_out;
286 }
287
288 Trace trace(map_trace_in);
289
290 return combine(map_trace_out, trace);
291}
292
293void
295{
296 // qDebug() << "registerJsConstructor for MassSpectrumPlusCombiner to
297 // QJSEngine.";
298
299 if(engine == nullptr)
300 {
301 qFatal() << "Cannot register class: engine is null";
302 }
303
304 // Register the meta object as a constructor
305 QJSValue jsMetaObject =
306 engine->newQMetaObject(&MassSpectrumPlusCombiner::staticMetaObject);
307 engine->globalObject().setProperty("MassSpectrumPlusCombiner", jsMetaObject);
308}
309
310} // namespace pappso
int m_decimalPlaces
Number of decimals to use for the keys (x values).
MassSpectrumCombiner(QObject *parent_p=nullptr)
Construct an uninitialized instance.
std::vector< pappso_double > m_bins
static void registerJsConstructor(QJSEngine *engine)
virtual ~MassSpectrumPlusCombiner()
Destruct the instance.
virtual Q_INVOKABLE MapTrace & combine(MapTrace &map_trace, const Trace &trace) const override
MassSpectrumPlusCombiner & operator=(const MassSpectrumPlusCombiner &other)
Q_INVOKABLE MassSpectrumPlusCombiner(QObject *parent_p=nullptr)
Construct an uninitialized instance.
A simple container of DataPoint instances.
Definition trace.h:152
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< const MassSpectrumPlusCombiner > MassSpectrumPlusCombinerCstSPtr