libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
timsframebase.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/vendors/tims/timsframebase.cpp
3 * \date 16/12/2019
4 * \author Olivier Langella
5 * \brief handle a single Bruker's TimsTof frame without binary data
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2019 Olivier Langella <Olivier.Langella@u-psud.fr>.
10 *
11 * This file is part of the PAPPSOms++ library.
12 *
13 * PAPPSOms++ is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * PAPPSOms++ is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25 *
26 ******************************************************************************/
27#include "timsframebase.h"
31#include <QDebug>
32#include <QObject>
33#include <cmath>
34#include <algorithm>
35
36namespace pappso
37{
38
39TimsFrameBase::TimsFrameBase(std::size_t frame_id, quint32 scanCount)
40{
41 qDebug() << frame_id;
42 m_frameId = frame_id;
43
44 m_scanCount = scanCount;
45}
46
47TimsFrameBase::TimsFrameBase([[maybe_unused]] const TimsFrameBase &other)
48{
49}
50
54
55void
56TimsFrameBase::setAcqDurationInMilliseconds(double acquisition_duration_ms)
57{
58 m_acqDurationInMilliseconds = acquisition_duration_ms;
59}
60
61
62void
64 double T2_frame,
65 double digitizerTimebase,
66 double digitizerDelay,
67 double C0,
68 double C1,
69 double C2,
70 double C3,
71 double C4,
72 double T1_ref,
73 double T2_ref,
74 double dC1,
75 double dC2)
76{
77
78 /* MzCalibrationModel1 mzCalibration(temperature_correction,
79 digitizerTimebase,
80 digitizerDelay,
81 C0,
82 C1,
83 C2,
84 C3,
85 C4);
86 */
87 msp_mzCalibration = std::make_shared<MzCalibrationModel1>(T1_frame,
88 T2_frame,
89 digitizerTimebase,
90 digitizerDelay,
91 C0,
92 C1,
93 C2,
94 C3,
95 C4,
96 T1_ref,
97 T2_ref,
98 dC1,
99 dC2);
100}
101
102bool
103TimsFrameBase::checkScanNum(std::size_t scanNum) const
104{
105 if(scanNum >= m_scanCount)
106 {
108 QObject::tr("Invalid scan number : scanNum %1 > m_scanNumber %2")
109 .arg(scanNum)
110 .arg(m_scanCount));
111 }
112
113 return true;
114}
115
116std::size_t
117TimsFrameBase::getScanPeakCount(std::size_t scanNum) const
118{
119 throw PappsoException(
120 QObject::tr("ERROR unable to get number of peaks in TimsFrameBase for scan number %1")
121 .arg(scanNum));
122}
123
124std::size_t
129
131TimsFrameBase::getMassSpectrumSPtr(std::size_t scanNum) const
132{
133 throw PappsoException(
134 QObject::tr("ERROR unable to getMassSpectrumSPtr in TimsFrameBase for scan number %1")
135 .arg(scanNum));
136}
137
138
141{
142 // qDebug();
143
144 return getMassSpectrumSPtr(scanNum);
145}
146Trace
147TimsFrameBase::cumulateScansToTrace(std::size_t scanNumBegin, std::size_t scanNumEnd) const
148{
149 throw PappsoException(QObject::tr("ERROR unable to cumulateScanToTrace in TimsFrameBase for scan "
150 "number begin %1 end %2")
151 .arg(scanNumBegin)
152 .arg(scanNumEnd));
153}
154
155Trace
157 std::size_t mzindex_merge_window [[maybe_unused]],
158 std::size_t scanNumBegin [[maybe_unused]],
159 std::size_t scanNumEnd [[maybe_unused]],
160 quint32 &minimum_tof_index_out [[maybe_unused]],
161 quint32 &maximum_tof_index_out [[maybe_unused]]) const
162{
163 throw PappsoException(
164 QObject::tr("Non implemented function %1 %2 %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
165}
166
167Trace
169 std::size_t mz_index_merge_window [[maybe_unused]],
170 double mz_range_begin [[maybe_unused]],
171 double mz_range_end [[maybe_unused]],
172 std::size_t mobility_scan_begin [[maybe_unused]],
173 std::size_t mobility_scan_end [[maybe_unused]],
174 quint32 &mz_minimum_index_out [[maybe_unused]],
175 quint32 &mz_maximum_index_out [[maybe_unused]]) const
176{
177 throw PappsoException(
178 QObject::tr("Non implemented function %1 %2 %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
179}
180
181Trace
182TimsFrameBase::getMobilityScan(std::size_t scanNum [[maybe_unused]],
183 std::size_t mz_index_merge_window [[maybe_unused]],
184 double mz_range_begin [[maybe_unused]],
185 double mz_range_end [[maybe_unused]],
186 quint32 &mz_minimum_index_out [[maybe_unused]],
187 quint32 &mz_maximum_index_out [[maybe_unused]]) const
188{
189 throw PappsoException(
190 QObject::tr("Non implemented function %1 %2 %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
191}
192
193void
195 std::size_t scanNumBegin,
196 std::size_t scanNumEnd) const
197{
198 throw PappsoException(
199 QObject::tr("ERROR unable to cumulateScansInRawMap in TimsFrameBase for scan "
200 "number begin %1 end %2")
201 .arg(scanNumBegin)
202 .arg(scanNumEnd));
203}
204
205
206void
208 std::size_t scanNumBegin,
209 std::size_t scanNumEnd,
210 quint32 tof_index_begin,
211 quint32 tof_index_end) const
212{
213 throw PappsoException(
214 QObject::tr("ERROR unable to cumulateScansInRawMap in TimsFrameBase for scan "
215 "number begin %1 end %2, tof index begin %3, tof index end %4")
216 .arg(scanNumBegin)
217 .arg(scanNumEnd)
218 .arg(tof_index_begin)
219 .arg(tof_index_end));
220}
221
222quint64
224{
225 throw PappsoException(
226 QObject::tr("ERROR unable to cumulateSingleScanIntensities in TimsFrameBase for scan "
227 "number %1.")
228 .arg(scanNum));
229
230 return 0;
231}
232
233
234quint64
235TimsFrameBase::cumulateScanRangeIntensities(std::size_t scanNumBegin, std::size_t scanNumEnd) const
236{
237 throw PappsoException(
238 QObject::tr("ERROR unable to cumulateScansInRawMap in TimsFrameBase for scan "
239 "number begin %1 end %2")
240 .arg(scanNumBegin)
241 .arg(scanNumEnd));
242
243 return 0;
244}
245
246void
248{
249 m_rtInSeconds = time;
250}
251
252void
254{
255
256 qDebug() << " m_msMsType=" << type;
257 m_msMsType = type;
258}
259
260unsigned int
262{
263 if(m_msMsType == 0)
264 return 1;
265 return 2;
266}
267
268double
270{
271 return m_rtInSeconds;
272}
273
274std::size_t
276{
277 return m_frameId;
278}
279void
281 double C0,
282 double C1,
283 double C2,
284 double C3,
285 double C4,
286 [[maybe_unused]] double C5,
287 double C6,
288 double C7,
289 double C8,
290 double C9)
291{
292 if(tims_model_type != 2)
293 {
295 QObject::tr("ERROR in TimsFrame::setTimsCalibration tims_model_type != 2"));
296 }
297 m_timsDvStart = C2; // C2 from TimsCalibration
298 m_timsTtrans = C4; // C4 from TimsCalibration
299 m_timsNdelay = C0; // C0 from TimsCalibration
300 m_timsVmin = C8; // C8 from TimsCalibration
301 m_timsVmax = C9; // C9 from TimsCalibration
302 m_timsC6 = C6;
303 m_timsC7 = C7;
304
305
306 m_timsSlope = (C3 - m_timsDvStart) / C1; // //C3 from TimsCalibration // C2 from
307 // TimsCalibration // C1 from TimsCalibration
308}
309double
311{
312 double v = m_timsDvStart + m_timsSlope * ((double)scanNum - m_timsTtrans - m_timsNdelay);
313
314 if(v < m_timsVmin)
315 {
317 QObject::tr("ERROR in TimsFrame::getVoltageTransformation invalid tims "
318 "calibration, v < m_timsVmin %1 < %2")
319 .arg(v)
320 .arg(m_timsVmin));
321 }
322
323
324 if(v > m_timsVmax)
325 {
327 QObject::tr("ERROR in TimsFrame::getVoltageTransformation invalid tims "
328 "calibration, v > m_timsVmax %1 > %2")
329 .arg(v)
330 .arg(m_timsVmax));
331 }
332 return v;
333}
334double
336{
337 return (m_acqDurationInMilliseconds / (double)m_scanCount) * ((double)scanNum);
338}
339
340double
342{
343 return 1 / (m_timsC6 + (m_timsC7 / getVoltageTransformation(scanNum)));
344}
345
346
347std::size_t
349{
350 double temp = 1 / one_over_k0;
351 temp = temp - m_timsC6;
352 temp = temp / m_timsC7;
353 temp = 1 / temp;
354 temp = temp - m_timsDvStart;
355 temp = temp / m_timsSlope + m_timsTtrans + m_timsNdelay;
356 return (std::size_t)std::round(temp);
357}
358
359bool
361{
362 if((m_timsDvStart == other.m_timsDvStart) && (m_timsTtrans == other.m_timsTtrans) &&
363 (m_timsNdelay == other.m_timsNdelay) && (m_timsVmin == other.m_timsVmin) &&
364 (m_timsVmax == other.m_timsVmax) && (m_timsC6 == other.m_timsC6) &&
365 (m_timsC7 == other.m_timsC7) && (m_timsSlope == other.m_timsSlope))
366 {
367 return true;
368 }
369 return false;
370}
371
372
375{
376 qDebug();
377 // qDebug();
378 // add flanking peaks
379 pappso::Trace local_trace;
380
381 MzCalibrationInterface *mz_calibration_p = getMzCalibrationInterfaceSPtr().get();
382
383
384 DataPoint element;
385 for(quint32 tof_index : accumulated_scans.getTofIndexList())
386 {
387 // intensity normalization
388 element.y =
389 ((double)accumulated_scans.readIntensity(tof_index)) * 100.0 / m_acqDurationInMilliseconds;
390
391 // mz calibration
392 element.x = mz_calibration_p->getMzFromTofIndex(tof_index);
393
394 local_trace.push_back(element);
395 }
396 local_trace.sortX();
397
398 qDebug();
399 // qDebug();
400 return local_trace;
401}
402
405{
406 if(msp_mzCalibration == nullptr)
407 {
408
409 throw pappso::PappsoException(QObject::tr("ERROR in %1, %2, %3 msp_mzCalibration is null")
410 .arg(__FILE__)
411 .arg(__FUNCTION__)
412 .arg(__LINE__));
413 }
414 return msp_mzCalibration;
415}
416
417void
419{
420
421 if(mzCalibration == nullptr)
422 {
423
424 throw pappso::PappsoException(QObject::tr("ERROR in %1, %2, %3 msp_mzCalibration is null")
425 .arg(__FILE__)
426 .arg(__FUNCTION__)
427 .arg(__LINE__));
428 }
429 msp_mzCalibration = mzCalibration;
430}
431
432
433quint32
435{
436 quint32 max_value = 0;
437 for(quint32 i = 0; i < m_scanCount; i++)
438 {
439 qDebug() << "m_scanNumber=" << m_scanCount << " i=" << i;
440 std::vector<quint32> index_list = getScanTofIndexList(i);
441 auto it = std::max_element(index_list.begin(), index_list.end());
442 if(it != index_list.end())
443 {
444 max_value = std::max(max_value, *it);
445 }
446 }
447 return max_value;
448}
449
450std::vector<quint32>
451TimsFrameBase::getScanTofIndexList(std::size_t scanNum) const
452{
453 throw PappsoException(
454 QObject::tr("ERROR unable to getScanIndexList in TimsFrameBase for scan number %1")
455 .arg(scanNum));
456}
457
458
459std::vector<quint32>
460TimsFrameBase::getScanIntensityList(std::size_t scanNum) const
461{
462 throw PappsoException(
463 QObject::tr("ERROR unable to getScanIntensities in TimsFrameBase for scan number %1")
464 .arg(scanNum));
465}
466
467Trace
469 std::size_t tof_index_end,
471 std::size_t scan_index_begin,
472 std::size_t scan_index_end) const
473{
474 Trace im_trace;
475 DataPoint data_point;
476 if(scan_index_end > m_scanCount)
477 scan_index_end = m_scanCount;
478 for(quint32 i = scan_index_begin; i < (scan_index_end + 1); i++)
479 {
480 data_point.x = i;
481 data_point.y = 0;
482 qDebug() << "m_scanNumber=" << m_scanCount << " i=" << i;
483 std::vector<quint32> index_list = getScanTofIndexList(i);
484 auto it_lower =
485 std::find_if(index_list.begin(), index_list.end(), [tof_index_begin](quint32 to_compare) {
486 if(to_compare < tof_index_begin)
487 {
488 return false;
489 }
490 return true;
491 });
492
493
494 if(it_lower == index_list.end())
495 {
496 }
497 else
498 {
499
500
501 auto it_upper =
502 std::find_if(index_list.begin(), index_list.end(), [tof_index_end](quint32 to_compare) {
503 if(tof_index_end >= to_compare)
504 {
505 return false;
506 }
507 return true;
508 });
509 std::vector<quint32> intensity_list = getScanIntensityList(i);
510 for(int j = std::distance(index_list.begin(), it_lower);
511 j < std::distance(index_list.begin(), it_upper);
512 j++)
513 {
514 if(method == Enums::XicExtractMethod::sum)
515 {
516 data_point.y += intensity_list[j];
517 }
518 else
519 {
520 data_point.y = std::max((double)intensity_list[j], data_point.y);
521 }
522 }
523 }
524 im_trace.push_back(data_point);
525 }
526 qDebug();
527 return im_trace;
528}
529
530
531std::vector<TimsFrameBase::TofIndexIntensityPair> &
533 std::size_t mzindex_merge_window,
534 std::vector<TimsFrameBase::TofIndexIntensityPair> &rawSpectrum) const
535{
536
537 qDebug() << rawSpectrum.size();
538 std::vector<TimsFrameBase::TofIndexIntensityPair> new_spectrum;
539
541 current_point.intensity_index = 0;
542 current_point.tof_index = 0;
543 for(auto &pair_mz_intensity : rawSpectrum)
544 {
545 quint32 mzkey = (pair_mz_intensity.tof_index / mzindex_merge_window);
546 mzkey = (mzkey * mzindex_merge_window) + (mzindex_merge_window / 2);
547
548 if(current_point.tof_index != mzkey)
549 {
550 if(current_point.tof_index > 0)
551 {
552 new_spectrum.push_back(current_point);
553 }
554
555 current_point.intensity_index = pair_mz_intensity.intensity_index;
556 current_point.tof_index = mzkey;
557 }
558 else
559 {
560 current_point.intensity_index += pair_mz_intensity.intensity_index;
561 }
562 }
563
564 if(current_point.tof_index > 0)
565 {
566 new_spectrum.push_back(current_point);
567 }
568 rawSpectrum = new_spectrum;
569 qDebug() << rawSpectrum.size();
570 return rawSpectrum;
571}
572// namespace pappso
573} // namespace pappso
virtual double getMzFromTofIndex(quint32 tof_index)=0
get m/z from time of flight raw index
replacement for std::map
const std::vector< quint32 > & getTofIndexList() const
std::size_t readIntensity(quint32)
reads intensity for a tof_index
double m_rtInSeconds
retention time
double getVoltageTransformation(std::size_t scanNum) const
get voltage for a given scan number
virtual std::size_t getTotalNumberOfScans() const
get the number of scans contained in this frame each scan represents an ion mobility slice
virtual pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtr(std::size_t scan_index) const final
get the mass spectrum corresponding to a scan index
MzCalibrationInterfaceSPtr msp_mzCalibration
virtual std::size_t getScanPeakCount(std::size_t scanIndex) const
get the number of peaks in this spectrum need the binary file
double getDriftTimeInMilliseconds(std::size_t scan_index) const
get drift time of a scan number in milliseconds
virtual Trace cumulateScansToTrace(std::size_t scanIndexBegin, std::size_t scanIndexEnd) const
cumulate spectrum given a scan number range need the binary file The intensities are normalized with ...
virtual std::vector< TofIndexIntensityPair > & downgradeResolutionOfTofIndexIntensityPairList(std::size_t tof_index_merge_window, std::vector< TofIndexIntensityPair > &spectrum) const
Downgrade the TOF index resolution to lower the number of real m/z computations.
std::size_t getScanIndexFromOneOverK0(double one_over_k0) const
get the scan number from a given 1/Ko mobility value
virtual bool hasSameCalibrationData(const TimsFrameBase &other) const
tells if 2 tims frame has the same calibration data Usefull to know if raw data can be handled betwee...
virtual Trace combineScansToTraceWithDowngradedMzResolution(std::size_t tof_index_merge_window, std::size_t scanIndexBegin, std::size_t scanIndexEnd, quint32 &minimum_tof_index_out, quint32 &maximum_tof_index_out) const
cumulate spectrum given a scan index range need the binary file The intensities are normalized with r...
double m_acqDurationInMilliseconds
acquisition duration in milliseconds
double getRtInSeconds() const
virtual quint32 getMaximumRawMassIndex() const
get the maximum raw mass index contained in this frame
virtual Trace getIonMobilityTraceByTofIndexRange(std::size_t tof_index_begin, std::size_t tof_index_end, Enums::XicExtractMethod method, std::size_t scan_index_begin, std::size_t scan_index_end) const
get a mobility trace cumulating intensities inside the given mass index range
TimsFrameBase(std::size_t frameId, quint32 scanCount)
constructor for binary independant tims frame
pappso::Trace getTraceFromTofIndexIntensityMap(TimsDataFastMap &accumulated_scans) const
transform accumulation of raw scans into a real mass spectrum
virtual Trace combineScansToTraceWithDowngradedMzResolution2(std::size_t mz_index_merge_window, double mz_range_begin, double mz_range_end, std::size_t mobility_scan_begin, std::size_t mobility_scan_end, quint32 &mz_minimum_index_out, quint32 &mz_maximum_index_out) const
cumulate spectrum given a scan number range need the binary file The intensities are normalized with ...
unsigned int getMsLevel() const
void setTimsCalibration(int tims_model_type, double C0, double C1, double C2, double C3, double C4, double C5, double C6, double C7, double C8, double C9)
virtual MassSpectrumSPtr getMassSpectrumSPtr(std::size_t scan_index) const
get Mass spectrum with peaks for this scan index need the binary file
virtual const MzCalibrationInterfaceSPtr & getMzCalibrationInterfaceSPtr() const final
get the MzCalibration model to compute mz and TOF for this frame
virtual quint64 cumulateScanRangeIntensities(std::size_t scan_index_begin, std::size_t scan_index_end) const
void setRtInSeconds(double time)
double getOneOverK0Transformation(std::size_t scan_index) const
get 1/K0 value of a given scan (mobility value)
void setMsMsType(quint8 type)
void setMzCalibration(double T1_frame, double T2_frame, double digitizerTimebase, double digitizerDelay, double C0, double C1, double C2, double C3, double C4, double T1_ref, double T2_ref, double dC1, double dC2)
quint32 m_scanCount
total number of scans contained in this frame
std::size_t m_frameId
Tims frame database id (the SQL identifier of this frame).
void setAcqDurationInMilliseconds(double acquisition_duration_ms)
virtual Trace getMobilityScan(std::size_t scan_index, std::size_t tof_index_merge_window, double mz_range_begin, double mz_range_end, quint32 &mz_minimum_index_out, quint32 &mz_maximum_index_out) const
get a single mobility scan m/z + intensities
bool checkScanNum(std::size_t scanNum) const
check that this scan number exists
virtual void combineScansInTofIndexIntensityMap(TimsDataFastMap &tof_index_intensity_map, std::size_t scan_index_begin, std::size_t scan_index_end) const
cumulate scan list into a trace into a raw spectrum map The intensities are NOT normalized with respe...
void setMzCalibrationInterfaceSPtr(MzCalibrationInterfaceSPtr mzCalibration)
virtual std::vector< quint32 > getScanTofIndexList(std::size_t scan_index) const
get raw index list for one given scan index are not TOF nor m/z, just index on digitizer
virtual std::vector< quint32 > getScanIntensityList(std::size_t scan_index) const
get raw intensities without transformation from one scan it needs intensity normalization
std::size_t getId() const
virtual quint64 cumulateScanIntensities(std::size_t scan_index) const
A simple container of DataPoint instances.
Definition trace.h:152
void sortX(Enums::SortOrder sort_order=Enums::SortOrder::ascending)
Definition trace.cpp:1071
@ sum
sum of intensities
Definition types.h:279
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< MzCalibrationInterface > MzCalibrationInterfaceSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
pappso_double x
Definition datapoint.h:24
pappso_double y
Definition datapoint.h:25