libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
timsframe.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/vendors/tims/timsframe.cpp
3 * \date 23/08/2019
4 * \author Olivier Langella
5 * \brief handle a single Bruker's TimsTof frame
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
28#include "timsframe.h"
29#include "../../../pappsomspp/pappsoexception.h"
30#include <QDebug>
31#include <QObject>
32#include <QtEndian>
33#include <memory>
34
35namespace pappso
36{
37
39 const TimsFrame *fram_p, const XicCoordTims &xic_struct)
40{
41 xic_ptr = xic_struct.xicSptr.get();
42
44 mobilityIndexEnd = xic_struct.scanNumEnd;
46 fram_p->getMzCalibrationInterfaceSPtr().get()->getTofIndexFromMz(
47 xic_struct.mzRange.lower()); // convert mz to raw digitizer value
49 fram_p->getMzCalibrationInterfaceSPtr().get()->getTofIndexFromMz(
50 xic_struct.mzRange.upper());
51 tmpIntensity = 0;
52}
53
54TimsFrame::TimsFrame(std::size_t timsId, quint32 scanNum)
55 : TimsFrameBase(timsId, scanNum)
56{
57 // m_timsDataFrame.resize(10);
58}
59
60TimsFrame::TimsFrame(std::size_t timsId,
61 quint32 scanNum,
62 char *p_bytes,
63 std::size_t len)
64 : TimsFrameBase(timsId, scanNum)
65{
66 // langella@themis:~/developpement/git/bruker/cbuild$
67 // ./src/sample/timsdataSamplePappso
68 // /gorgone/pappso/fichiers_fabricants/Bruker/Demo_TimsTOF_juin2019/Samples/1922001/1922001-1_S-415_Pep_Pur-1ul_Slot1-10_1_2088.d/
69 qDebug() << timsId;
70
71 m_timsDataFrame.resize(len);
72
73 if(p_bytes != nullptr)
74 {
75 unshufflePacket(p_bytes);
76 }
77 else
78 {
79 if(m_scanNumber == 0)
80 {
81
83 QObject::tr("TimsFrame::TimsFrame(%1,%2,nullptr,%3) FAILED")
84 .arg(m_timsId)
85 .arg(m_scanNumber)
86 .arg(len));
87 }
88 }
89}
90
92{
93}
94
98
99
100void
102{
103 qDebug();
104 quint64 len = m_timsDataFrame.size();
105 if(len % 4 != 0)
106 {
108 QObject::tr("TimsFrame::unshufflePacket error: len % 4 != 0"));
109 }
110
111 quint64 nb_uint4 = len / 4;
112
113 char *dest = m_timsDataFrame.data();
114 quint64 src_offset = 0;
115
116 for(quint64 j = 0; j < 4; j++)
117 {
118 for(quint64 i = 0; i < nb_uint4; i++)
119 {
120 dest[(i * 4) + j] = src[src_offset];
121 src_offset++;
122 }
123 }
124 qDebug();
125}
126
127std::size_t
128TimsFrame::getNbrPeaks(std::size_t scanNum) const
129{
130 if(m_timsDataFrame.size() == 0)
131 return 0;
132 /*
133 if(scanNum == 0)
134 {
135 quint32 res = (*(quint32 *)(m_timsDataFrame.constData() + 4)) -
136 (*(quint32 *)(m_timsDataFrame.constData()-4));
137 return res / 2;
138 }*/
139 if(scanNum == (m_scanNumber - 1))
140 {
141 auto nb_uint4 = m_timsDataFrame.size() / 4;
142
143 std::size_t cumul = 0;
144 for(quint32 i = 0; i < m_scanNumber; i++)
145 {
146 cumul += (*(quint32 *)(m_timsDataFrame.constData() + (i * 4)));
147 }
148 return (nb_uint4 - cumul) / 2;
149 }
150 checkScanNum(scanNum);
151
152 // quint32 *res = (quint32 *)(m_timsDataFrame.constData() + (scanNum * 4));
153 // qDebug() << " res=" << *res;
154 return (*(quint32 *)(m_timsDataFrame.constData() + ((scanNum + 1) * 4))) / 2;
155}
156
157std::size_t
158TimsFrame::getScanOffset(std::size_t scanNum) const
159{
160 std::size_t offset = 0;
161 for(std::size_t i = 0; i < (scanNum + 1); i++)
162 {
163 offset += (*(quint32 *)(m_timsDataFrame.constData() + (i * 4)));
164 }
165 return offset;
166}
167
168
169std::vector<quint32>
170TimsFrame::getScanIndexList(std::size_t scanNum) const
171{
172 qDebug();
173 checkScanNum(scanNum);
174 std::vector<quint32> scan_tof;
175
176 if(m_timsDataFrame.size() == 0)
177 return scan_tof;
178 scan_tof.resize(getNbrPeaks(scanNum));
179
180 std::size_t offset = getScanOffset(scanNum);
181
182 qint32 previous = -1;
183 for(std::size_t i = 0; i < scan_tof.size(); i++)
184 {
185 scan_tof[i] =
186 (*(quint32 *)(m_timsDataFrame.constData() + (offset * 4) + (i * 8))) +
187 previous;
188 previous = scan_tof[i];
189 }
190 qDebug();
191 return scan_tof;
192}
193
194std::vector<quint32>
195TimsFrame::getScanIntensities(std::size_t scanNum) const
196{
197 qDebug();
198 checkScanNum(scanNum);
199 std::vector<quint32> scan_intensities;
200
201 if(m_timsDataFrame.size() == 0)
202 return scan_intensities;
203
204 scan_intensities.resize(getNbrPeaks(scanNum));
205
206 std::size_t offset = getScanOffset(scanNum);
207
208 for(std::size_t i = 0; i < scan_intensities.size(); i++)
209 {
210 scan_intensities[i] = (*(quint32 *)(m_timsDataFrame.constData() +
211 (offset * 4) + (i * 8) + 4));
212 }
213 qDebug();
214 return scan_intensities;
215}
216
217
218quint64
220{
221 qDebug();
222
223 quint64 summed_intensities = 0;
224
225 if(m_timsDataFrame.size() == 0)
226 return summed_intensities;
227 // checkScanNum(scanNum);
228
229 std::size_t size = getNbrPeaks(scanNum);
230
231 std::size_t offset = getScanOffset(scanNum);
232
233 qint32 previous = -1;
234
235 for(std::size_t i = 0; i < size; i++)
236 {
237 quint32 x =
238 (*(quint32 *)((m_timsDataFrame.constData() + (offset * 4) + (i * 8))) +
239 previous);
240
241 quint32 y = (*(quint32 *)(m_timsDataFrame.constData() + (offset * 4) +
242 (i * 8) + 4));
243
244 previous = x;
245
246 summed_intensities += y;
247 }
248
249 // Normalization over the accumulation time for this frame.
250 summed_intensities *= ((double)100.0 / m_accumulationTime);
251
252 qDebug();
253
254 return summed_intensities;
255}
256
257
258/**
259 * @brief ...
260 *
261 * @param mobility_scan_begin p_mobility_scan_begin:...
262 * @param mobility_scan_end p_mobility_scan_end:...
263 * @return quint64
264 */
265quint64
266TimsFrame::cumulateScansIntensities(std::size_t mobility_scan_begin,
267 std::size_t mobility_scan_end) const
268{
269 quint64 summed_intensities = 0;
270
271 // qDebug() << "begin mobility_scan_begin =" << mobility_scan_begin
272 //<< "mobility_scan_end =" << mobility_scan_end;
273
274 if(m_timsDataFrame.size() == 0)
275 return summed_intensities;
276
277 try
278 {
279 std::size_t mobility_scan_max = mobility_scan_end + 1;
280
281 for(std::size_t i = mobility_scan_begin; i < mobility_scan_max; i++)
282 {
283 qDebug() << i;
284 summed_intensities += cumulateSingleScanIntensities(i);
285 qDebug() << i;
286 }
287 }
288 catch(std::exception &error)
289 {
290 qDebug() << QString("Failure in %1 %2 to %3 :\n %4")
291 .arg(__FUNCTION__)
292 .arg(mobility_scan_begin)
293 .arg(mobility_scan_end)
294 .arg(error.what());
295 }
296
297 // qDebug() << "end";
298
299 return summed_intensities;
300}
301
302
303void
304TimsFrame::cumulateScan(std::size_t scanNum,
305 TimsDataFastMap &accumulate_into) const
306{
307 // qDebug();
308
309 if(m_timsDataFrame.size() == 0)
310 return;
311
312 // checkScanNum(scanNum);
313
314 std::size_t scan_size = getNbrPeaks(scanNum);
315
316 std::size_t scan_offset = getScanOffset(scanNum);
317
318 qint32 previous = -1;
319 for(std::size_t i = 0; i < scan_size; i++)
320 {
321 quint32 x = (*(quint32 *)((m_timsDataFrame.constData() +
322 (scan_offset * 4) + (i * 8))) +
323 previous);
324 quint32 y = (*(quint32 *)(m_timsDataFrame.constData() +
325 (scan_offset * 4) + (i * 8) + 4));
326
327 previous = x;
328
329 accumulate_into.accumulateIntensity(x, y);
330 }
331
332 // qDebug();
333}
334
335// Proof of concept m/z range-conditions for accumulations
336void
337TimsFrame::cumulateScan2(std::size_t scanNum,
338 TimsDataFastMap &accumulate_into,
339 quint32 accepted_tof_index_range_begin,
340 quint32 accepted_tof_index_range_end) const
341{
342 // qDebug();
343
344 if(m_timsDataFrame.size() == 0)
345 return;
346
347 // checkScanNum(scanNum);
348
349 std::size_t scan_size = getNbrPeaks(scanNum);
350 std::size_t scan_offset = getScanOffset(scanNum);
351 qint32 previous = -1;
352
353 for(std::size_t i = 0; i < scan_size; i++)
354 {
355 quint32 x = (*(quint32 *)((m_timsDataFrame.constData() +
356 (scan_offset * 4) + (i * 8))) +
357 previous);
358
359 if(x < accepted_tof_index_range_begin)
360 {
361 // qDebug() << "TOF index still not in range, x:" << x;
362 continue;
363 }
364 if(x > accepted_tof_index_range_end)
365 {
366 // qDebug() << "TOF index already out of range, x:" << x;
367 break;
368 }
369
370 // qDebug() << "TOF index in range, x:" << x;
371
372 quint32 y = (*(quint32 *)(m_timsDataFrame.constData() +
373 (scan_offset * 4) + (i * 8) + 4));
374
375 previous = x;
376
377 accumulate_into.accumulateIntensity(x, y);
378 }
379
380 // qDebug();
381}
382
383
384Trace
385TimsFrame::cumulateScansToTrace(std::size_t mobility_scan_begin,
386 std::size_t mobility_scan_end) const
387{
388 // qDebug();
389
390 Trace new_trace;
391
392 try
393 {
394 if(m_timsDataFrame.size() == 0)
395 return new_trace;
396 TimsDataFastMap &raw_spectrum =
398 raw_spectrum.tofIndexList.clear();
399 // double local_accumulationTime = 0;
400
401 std::size_t mobility_scan_max = mobility_scan_end + 1;
402 qDebug();
403 for(std::size_t i = mobility_scan_begin; i < mobility_scan_max; i++)
404 {
405 // qDebug() << i;
406 cumulateScan(i, raw_spectrum);
407 // qDebug() << i;
408
409 // local_accumulationTime += m_accumulationTime;
410 }
411
412 // qDebug();
413
414 pappso::DataPoint data_point_cumul;
415
416
417 MzCalibrationInterface *mz_calibration_p =
419
420
421 for(quint32 tof_index : raw_spectrum.tofIndexList)
422 {
423 data_point_cumul.x = mz_calibration_p->getMzFromTofIndex(tof_index);
424 // normalization
425 data_point_cumul.y = raw_spectrum.readIntensity(tof_index) *
426 ((double)100.0 / m_accumulationTime);
427 new_trace.push_back(data_point_cumul);
428 }
429 new_trace.sortX();
430
431 // qDebug();
432 }
433
434 catch(std::exception &error)
435 {
436 qDebug() << QString(
437 "Failure in TimsFrame::cumulateScanToTrace %1 to %2 :\n %3")
438 .arg(mobility_scan_begin, mobility_scan_end)
439 .arg(error.what());
440 }
441 return new_trace;
442}
443
444Trace
446 std::size_t mz_index_merge_window,
447 std::size_t mobility_scan_begin,
448 std::size_t mobility_scan_end,
449 quint32 &mz_minimum_index_out,
450 quint32 &mz_maximum_index_out) const
451{
452 // qDebug();
453
454 Trace new_trace;
455
456 try
457 {
458 if(m_timsDataFrame.size() == 0)
459 {
460 qDebug() << "The frame is empty, returning empty trace.";
461 return new_trace;
462 }
463
464 // Allocate a map for (TOF,intensity) pairs to
465 // accumulate ion mobility scans.
466
467 TimsDataFastMap &raw_spectrum =
469 raw_spectrum.tofIndexList.clear();
470 // double local_accumulationTime = 0;
471
472 std::size_t mobility_scan_max = mobility_scan_end + 1;
473
474 for(std::size_t i = mobility_scan_begin; i < mobility_scan_max; i++)
475 {
476 // qDebug() << "Going to cumulate currently iterated mobility scan:"
477 // << i;
478 cumulateScan(i, raw_spectrum);
479 // qDebug() << "Done cumulating currently iterated mobility scan:" <<
480 // i;
481
482 // local_accumulationTime += m_accumulationTime;
483 }
484
485 // qDebug();
486
487 pappso::DataPoint data_point_cumul;
488
489 MzCalibrationInterface *mz_calibration_p =
491
492 // If the caller asks that m/z values be binned larger than they are,
493 // ask that the m/z raw map be reduced in resolution.
494 if(mz_index_merge_window > 0)
495 {
496 raw_spectrum.downsizeMzRawMap(mz_index_merge_window);
497 }
498
499 // Store the first mz index and the last mz index of the current spectrum.
500 // The values are set to the out parameters.
501 mz_minimum_index_out = std::numeric_limits<quint32>::max();
502 mz_maximum_index_out = 0;
503
504 for(quint32 tof_index : raw_spectrum.tofIndexList)
505 {
506 if(tof_index > mz_maximum_index_out)
507 mz_maximum_index_out = tof_index;
508 if(tof_index < mz_minimum_index_out)
509 mz_minimum_index_out = tof_index;
510
511 // Convert the TOF index to m/z
512 data_point_cumul.x = mz_calibration_p->getMzFromTofIndex(tof_index);
513
514 // Normalization
515 data_point_cumul.y = raw_spectrum.readIntensity(tof_index) *
516 ((double)100.0 / m_accumulationTime);
517
518 // Finally make the data point a new Trace point.
519 new_trace.push_back(data_point_cumul);
520 }
521
522 // qDebug() << "At this point we have mz_minimum_index_out:"
523 // << mz_minimum_index_out
524 // << "and mz_maximum_index_out:" << mz_maximum_index_out;
525
526 // FIXME: this does not seem to be necessary since raw_spectrum is a map
527 // with auto-sorting on the keys which are quint32.
528 // new_trace.sortX();
529
530 // qDebug();
531 }
532 catch(std::exception &error)
533 {
534 qDebug() << QString(
535 "Failure in TimsFrame::cumulateScanToTrace %1 to %2 :\n %3")
536 .arg(mobility_scan_begin, mobility_scan_end)
537 .arg(error.what());
538 }
539
540 // qDebug() << "Returning new trace of size:" << new_trace.size();
541
542 return new_trace;
543}
544
545
546Trace
548 std::size_t mz_index_merge_window,
549 double mz_range_begin,
550 double mz_range_end,
551 std::size_t mobility_scan_begin,
552 std::size_t mobility_scan_end,
553 quint32 &mz_minimum_index_out,
554 quint32 &mz_maximum_index_out) const
555{
556 qDebug() << "Calling cumulateScansToTraceMzDownResolution2 for both mz and "
557 "im ranges accounting.";
558
559 Trace new_trace;
560
561 try
562 {
563 if(m_timsDataFrame.size() == 0)
564 {
565 qDebug() << "The frame is empty, returning empty trace.";
566 return new_trace;
567 }
568
569 // Allocate a map for (TOF index,intensity) pairs to
570 // accumulate ion mobility scans.
571
572 TimsDataFastMap &raw_spectrum =
574 raw_spectrum.tofIndexList.clear();
575 // double local_accumulationTime = 0;
576
577 std::size_t mobility_scan_max = mobility_scan_end + 1;
578
579 quint32 tof_index_for_mz_range_begin =
580 getMzCalibrationInterfaceSPtr().get()->getTofIndexFromMz(
581 mz_range_begin);
582 quint32 tof_index_for_mz_range_end =
583 getMzCalibrationInterfaceSPtr().get()->getTofIndexFromMz(mz_range_end);
584
585 qDebug() << "20240529 TOF index for mz range begin:"
586 << tof_index_for_mz_range_begin;
587 qDebug() << "20240529 TOF index for mz range end:"
588 << tof_index_for_mz_range_end;
589
590 for(std::size_t iter = mobility_scan_begin; iter < mobility_scan_max;
591 iter++)
592 {
593 // qDebug() << "Going to cumulate currently iterated mobility scan:"
594 // << iter;
595 cumulateScan2(iter,
596 raw_spectrum,
597 tof_index_for_mz_range_begin,
598 tof_index_for_mz_range_end);
599 // qDebug() << "Done cumulating currently iterated mobility scan:" <<
600 // i;
601
602 // local_accumulationTime += m_accumulationTime;
603 }
604
605 // qDebug();
606
607 pappso::DataPoint data_point_cumul;
608
609 MzCalibrationInterface *mz_calibration_p =
611
612 // If the caller asks that m/z values be binned larger than they are,
613 // ask that the m/z raw map be reduced in resolution.
614 if(mz_index_merge_window > 0)
615 {
616 raw_spectrum.downsizeMzRawMap(mz_index_merge_window);
617 }
618
619 // Store the first mz index and the last mz index of the current spectrum.
620 // The values are set to the out parameters.
621 mz_minimum_index_out = std::numeric_limits<quint32>::max();
622 mz_maximum_index_out = 0;
623
624 // for(std::pair<quint32, quint32> pair_tof_intensity : raw_spectrum)
625 for(quint32 tof_index : raw_spectrum.tofIndexList)
626 {
627 std::size_t intensity = raw_spectrum.readIntensity(tof_index);
628 if(tof_index > mz_maximum_index_out)
629 mz_maximum_index_out = tof_index;
630 if(tof_index < mz_minimum_index_out)
631 mz_minimum_index_out = tof_index;
632
633 // Convert the TOF index to m/z
634 data_point_cumul.x = mz_calibration_p->getMzFromTofIndex(tof_index);
635
636 // Normalization
637 data_point_cumul.y = intensity * ((double)100.0 / m_accumulationTime);
638
639 // Finally make the data point a new Trace point.
640 new_trace.push_back(data_point_cumul);
641 }
642
643 // qDebug() << "At this point we have mz_minimum_index_out:"
644 // << mz_minimum_index_out
645 // << "and mz_maximum_index_out:" << mz_maximum_index_out;
646
647 // FIXME: this does not seem to be necessary since raw_spectrum is a map
648 // with auto-sorting on the keys which are quint32.
649 // new_trace.sortX();
650
651 // qDebug();
652 }
653 catch(std::exception &error)
654 {
655 qDebug() << QString(
656 "Failure in TimsFrame::cumulateScanToTrace %1 to %2 :\n %3")
657 .arg(mobility_scan_begin, mobility_scan_end)
658 .arg(error.what());
659 }
660
661 // qDebug() << "Returning new trace of size:" << new_trace.size();
662
663 return new_trace;
664}
665
666
667void
669 std::size_t mobility_scan_begin,
670 std::size_t mobility_scan_end) const
671{
672 // qDebug() << "begin mobility_scan_begin=" << mobility_scan_begin
673 //<< " mobility_scan_end=" << mobility_scan_end;
674
675 if(m_timsDataFrame.size() == 0)
676 return;
677 try
678 {
679
680 std::size_t mobility_scan_max = mobility_scan_end + 1;
681 qDebug();
682 for(std::size_t i = mobility_scan_begin; i < mobility_scan_max; i++)
683 {
684 qDebug() << i;
685 cumulateScan(i, rawSpectrum);
686 qDebug() << i;
687 // local_accumulationTime += m_accumulationTime;
688 }
689 }
690
691 catch(std::exception &error)
692 {
693 qDebug() << QString("Failure in %1 %2 to %3 :\n %4")
694 .arg(__FUNCTION__)
695 .arg(mobility_scan_begin)
696 .arg(mobility_scan_end)
697 .arg(error.what());
698 }
699
700 // qDebug() << "end";
701}
702
703
705TimsFrame::getMassSpectrumSPtr(std::size_t scanNum) const
706{
707
708 // qDebug() << " scanNum=" << scanNum;
709
710 checkScanNum(scanNum);
711
712 // qDebug();
713
714 pappso::MassSpectrumSPtr mass_spectrum_sptr =
715 std::make_shared<pappso::MassSpectrum>();
716 // std::vector<DataPoint>
717
718 if(m_timsDataFrame.size() == 0)
719 return mass_spectrum_sptr;
720
721 // qDebug();
722
723 std::size_t size = getNbrPeaks(scanNum);
724
725 std::size_t offset = getScanOffset(scanNum);
726
727 MzCalibrationInterface *mz_calibration_p =
729
730
731 qint32 previous = -1;
732 qint32 tof_index;
733 // std::vector<quint32> index_list;
734 DataPoint data_point;
735 for(std::size_t i = 0; i < size; i++)
736 {
737 tof_index =
738 (*(quint32 *)((m_timsDataFrame.constData() + (offset * 4) + (i * 8))) +
739 previous);
740 data_point.y = (*(quint32 *)(m_timsDataFrame.constData() + (offset * 4) +
741 (i * 8) + 4));
742
743 // intensity normalization
744 data_point.y *= 100.0 / m_accumulationTime;
745
746 previous = tof_index;
747
748
749 // mz calibration
750 data_point.x = mz_calibration_p->getMzFromTofIndex(tof_index);
751 mass_spectrum_sptr.get()->push_back(data_point);
752 }
753
754 // qDebug();
755
756 return mass_spectrum_sptr;
757}
758
759
760Trace
761TimsFrame::getMobilityScan(std::size_t scanNum,
762 std::size_t mz_index_merge_window,
763 double mz_range_begin,
764 double mz_range_end,
765 quint32 &mz_minimum_index_out,
766 quint32 &mz_maximum_index_out) const
767{
768 qDebug() << "mz_range_begin:" << mz_range_begin
769 << "mz_range_end:" << mz_range_end
770 << "mz_index_merge_window:" << mz_index_merge_window;
771
772 Trace spectrum;
773
774 quint32 mz_index_begin = 0;
775 quint32 mz_index_end = std::numeric_limits<quint32>::max();
776
777 if(mz_range_end > 0)
778 {
779 qDebug() << "m/z range is requested.";
780
781 mz_index_begin =
782 msp_mzCalibration.get()->getTofIndexFromMz(mz_range_begin);
783 mz_index_end = msp_mzCalibration.get()->getTofIndexFromMz(mz_range_end);
784 }
785
786 qDebug() << "After conversion of mz indices, mz_index_begin:"
787 << mz_index_begin << "mz_index_end;" << mz_index_end;
788
789 auto raw_spectrum =
790 getRawValuePairList(scanNum, mz_index_begin, mz_index_end);
791
792 qDebug() << " raw_spectrum.size();" << raw_spectrum.size();
793
794 if(mz_index_merge_window > 0)
795 {
796 qDebug() << "mz_index_merge_window;=" << mz_index_merge_window
797 << " raw_spectrum.size()=" << raw_spectrum.size();
798 raw_spectrum =
799 downsizeMzRawValuePairList(mz_index_merge_window, raw_spectrum);
800 }
801
802 if(raw_spectrum.size() > 0)
803 {
804 mz_minimum_index_out = raw_spectrum.front().mz_tof_index;
805 mz_maximum_index_out = raw_spectrum.back().mz_tof_index;
806
807 for(auto &&element : raw_spectrum)
808 {
809 spectrum.push_back(DataPoint(
810 msp_mzCalibration.get()->getMzFromTofIndex(element.mz_tof_index),
811 static_cast<double>(element.intensity_index)));
812
813 // intensity normalization
814 spectrum.back().y *= 100.0 / m_accumulationTime;
815 }
816 }
817
818 return spectrum;
819}
820
821void
823 std::vector<XicCoordTims *>::iterator &itXicListbegin,
824 std::vector<XicCoordTims *>::iterator &itXicListend,
825 XicExtractMethod method) const
826{
827 qDebug() << std::distance(itXicListbegin, itXicListend);
828
829 std::vector<TimsFrame::XicComputeStructure> tmp_xic_list;
830
831 for(auto it = itXicListbegin; it != itXicListend; it++)
832 {
833 tmp_xic_list.push_back(TimsFrame::XicComputeStructure(this, **it));
834
835 qDebug() << " tmp_xic_struct.mobilityIndexBegin="
836 << tmp_xic_list.back().mobilityIndexBegin
837 << " tmp_xic_struct.mobilityIndexEnd="
838 << tmp_xic_list.back().mobilityIndexEnd;
839
840 qDebug() << " tmp_xic_struct.mzIndexLowerBound="
841 << tmp_xic_list.back().mzIndexLowerBound
842 << " tmp_xic_struct.mzIndexUpperBound="
843 << tmp_xic_list.back().mzIndexUpperBound;
844 }
845 if(tmp_xic_list.size() == 0)
846 return;
847 /*
848 std::sort(tmp_xic_list.begin(), tmp_xic_list.end(), [](const
849 TimsXicStructure &a, const TimsXicStructure &b) { return
850 a.mobilityIndexBegin < b.mobilityIndexBegin;
851 });
852 */
853 std::vector<std::size_t> unique_scan_num_list;
854 for(auto &&struct_xic : tmp_xic_list)
855 {
856 for(std::size_t scan = struct_xic.mobilityIndexBegin;
857 (scan <= struct_xic.mobilityIndexEnd) && (scan < m_scanNumber);
858 scan++)
859 {
860 unique_scan_num_list.push_back(scan);
861 }
862 }
863 std::sort(unique_scan_num_list.begin(), unique_scan_num_list.end());
864 auto it_scan_num_end =
865 std::unique(unique_scan_num_list.begin(), unique_scan_num_list.end());
866 auto it_scan_num = unique_scan_num_list.begin();
867
868 while(it_scan_num != it_scan_num_end)
869 {
870 TraceSPtr ms_spectrum = getRawTraceSPtr(*it_scan_num);
871 // qDebug() << ms_spectrum.get()->toString();
872 for(auto &&tmp_xic_struct : tmp_xic_list)
873 {
874 if(((*it_scan_num) >= tmp_xic_struct.mobilityIndexBegin) &&
875 ((*it_scan_num) <= tmp_xic_struct.mobilityIndexEnd))
876 {
877 if(method == XicExtractMethod::max)
878 {
879 tmp_xic_struct.tmpIntensity +=
880 ms_spectrum.get()->maxY(tmp_xic_struct.mzIndexLowerBound,
881 tmp_xic_struct.mzIndexUpperBound);
882
883 qDebug() << "tmp_xic_struct.tmpIntensity="
884 << tmp_xic_struct.tmpIntensity;
885 }
886 else
887 {
888 // sum
889 tmp_xic_struct.tmpIntensity +=
890 ms_spectrum.get()->sumY(tmp_xic_struct.mzIndexLowerBound,
891 tmp_xic_struct.mzIndexUpperBound);
892 qDebug() << "tmp_xic_struct.tmpIntensity="
893 << tmp_xic_struct.tmpIntensity;
894 }
895 }
896 }
897 it_scan_num++;
898 }
899
900 for(auto &&tmp_xic_struct : tmp_xic_list)
901 {
902 if(tmp_xic_struct.tmpIntensity != 0)
903 {
904 qDebug() << tmp_xic_struct.xic_ptr;
905 tmp_xic_struct.xic_ptr->push_back(
906 {m_time, tmp_xic_struct.tmpIntensity});
907 }
908 }
909
910 qDebug();
911}
912
913
915TimsFrame::getRawTraceSPtr(std::size_t scanNum) const
916{
917
918 // qDebug();
919
920 pappso::TraceSPtr trace_sptr = std::make_shared<pappso::Trace>();
921 // std::vector<DataPoint>
922
923 if(m_timsDataFrame.size() == 0)
924 return trace_sptr;
925 // qDebug();
926
927 std::size_t size = getNbrPeaks(scanNum);
928
929 std::size_t offset = getScanOffset(scanNum);
930
931 qint32 previous = -1;
932 std::vector<quint32> index_list;
933 for(std::size_t i = 0; i < size; i++)
934 {
935 DataPoint data_point(
936 (*(quint32 *)((m_timsDataFrame.constData() + (offset * 4) + (i * 8))) +
937 previous),
938 (*(quint32 *)(m_timsDataFrame.constData() + (offset * 4) + (i * 8) +
939 4)));
940
941 // intensity normalization
942 data_point.y *= 100.0 / m_accumulationTime;
943
944 previous = data_point.x;
945 trace_sptr.get()->push_back(data_point);
946 }
947 // qDebug();
948 return trace_sptr;
949}
950
951
952std::vector<TimsFrame::RawValuePair>
954 quint32 accepted_tof_index_range_begin,
955 quint32 accepted_tof_index_range_end) const
956{
957 // qDebug() << accepted_tof_index_range_begin;
958
959 std::vector<TimsFrame::RawValuePair> raw_value_pairs;
960 // std::vector<DataPoint>
961
962 if(m_timsDataFrame.size() == 0)
963 return raw_value_pairs;
964 // qDebug();
965
966 std::size_t size = getNbrPeaks(scanNum);
967
968 std::size_t offset = getScanOffset(scanNum);
969
970 // qDebug() << size;
971 qint32 previous = -1;
972 std::vector<quint32> index_list;
973 for(std::size_t i = 0; i < size; i++)
974 {
975
976 // qDebug() << i;
977 TimsFrame::RawValuePair raw_value_pair(
978 {(*(quint32 *)((m_timsDataFrame.constData() + (offset * 4) + (i * 8))) +
979 previous),
980 (*(quint32 *)(m_timsDataFrame.constData() + (offset * 4) + (i * 8) +
981 4))});
982
983 previous = raw_value_pair.mz_tof_index;
984 if(raw_value_pair.mz_tof_index < accepted_tof_index_range_begin)
985 {
986 // qDebug() << "TOF index still not in range, x:" << x;
987 continue;
988 }
989 if(raw_value_pair.mz_tof_index > accepted_tof_index_range_end)
990 {
991 // qDebug() << "TOF index already out of range, x:" << x;
992 break;
993 }
994
995 raw_value_pairs.push_back(raw_value_pair);
996 }
997 qDebug() << raw_value_pairs.size();
998 return raw_value_pairs;
999}
1000
1001} // namespace pappso
virtual double getMzFromTofIndex(quint32 tof_index)=0
get m/z from time of flight raw index
pappso_double lower() const
Definition mzrange.h:71
pappso_double upper() const
Definition mzrange.h:77
double m_accumulationTime
accumulation time in milliseconds
virtual std::vector< RawValuePair > & downsizeMzRawValuePairList(std::size_t mzindex_merge_window, std::vector< RawValuePair > &spectrum) const
downsize mz resolution to lower the number of real mz computations
MzCalibrationInterfaceSPtr msp_mzCalibration
double m_time
retention time
quint32 m_scanNumber
total number of scans contained in this frame
std::size_t m_timsId
Tims frame database id (the SQL identifier of this frame)
virtual const MzCalibrationInterfaceSPtr & getMzCalibrationInterfaceSPtr() const final
get the MzCalibration model to compute mz and TOF for this frame
bool checkScanNum(std::size_t scanNum) const
check that this scan number exists
QByteArray m_timsDataFrame
Definition timsframe.h:279
virtual std::vector< quint32 > getScanIndexList(std::size_t scanNum) const override
get raw index list for one given scan index are not TOF nor m/z, just index on digitizer
virtual std::size_t getNbrPeaks(std::size_t scanNum) const override
get the number of peaks in this spectrum need the binary file
virtual Trace cumulateScansToTraceMzDownResolution(std::size_t mzindex_merge_window, std::size_t scanNumBegin, std::size_t scanNumEnd, quint32 &mz_minimum_index, quint32 &mz_maximum_index) const override
cumulate spectrum given a scan number range need the binary file The intensities are normalized with ...
virtual ~TimsFrame()
Definition timsframe.cpp:95
virtual quint64 cumulateScansIntensities(std::size_t scanNumBegin, std::size_t scanNumEnd) const override
...
virtual Trace cumulateScansToTraceMzDownResolution2(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 override
cumulate spectrum given a scan number range need the binary file The intensities are normalized with ...
virtual Trace cumulateScansToTrace(std::size_t scanNumBegin, std::size_t scanNumEnd) const override
cumulate scan list into a trace
TimsFrame(std::size_t timsId, quint32 scanNum, char *p_bytes, std::size_t len)
Definition timsframe.cpp:60
virtual std::vector< RawValuePair > getRawValuePairList(std::size_t scanNum, quint32 accepted_tof_index_range_begin, quint32 accepted_tof_index_range_end) const
get the raw index tof_index and intensities (normalized)
virtual pappso::MassSpectrumSPtr getMassSpectrumSPtr(std::size_t scanNum) const override
get Mass spectrum with peaks for this scan number need the binary file
virtual quint64 cumulateSingleScanIntensities(std::size_t scanNum) const override
virtual std::vector< quint32 > getScanIntensities(std::size_t scanNum) const override
get raw intensities without transformation from one scan it needs intensity normalization
void unshufflePacket(const char *src)
unshuffle data packet of tims compression type 2
virtual Trace getMobilityScan(std::size_t scanNum, std::size_t mz_index_merge_window, double mz_range_begin, double mz_range_end, quint32 &mz_minimum_index_out, quint32 &mz_maximum_index_out) const override
get a single mobility scan m/z + intensities
virtual void cumulateScan(std::size_t scanNum, TimsDataFastMap &accumulate_into) const
cumulate a scan into a map
virtual void cumulateScan2(std::size_t scanNum, TimsDataFastMap &accumulate_into, quint32 accepted_tof_index_range_begin, quint32 accepted_tof_index_range_end) const
std::size_t getScanOffset(std::size_t scanNum) const
get offset for this spectrum in the binary file
virtual void cumulateScansInRawMap(TimsDataFastMap &rawSpectrum, std::size_t scanNumBegin, std::size_t scanNumEnd) const override
cumulate scan list into a trace into a raw spectrum map
void extractTimsXicListInRtRange(std::vector< XicCoordTims * >::iterator &itXicListbegin, std::vector< XicCoordTims * >::iterator &itXicListend, XicExtractMethod method) const
virtual pappso::TraceSPtr getRawTraceSPtr(std::size_t scanNum) const
get the raw index tof_index and intensities (normalized)
A simple container of DataPoint instances.
Definition trace.h:148
void sortX(SortOrder sort_order=SortOrder::ascending)
Definition trace.cpp:1086
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< Trace > TraceSPtr
Definition trace.h:135
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
XicExtractMethod
Definition types.h:244
@ max
maximum of intensities
pappso_double x
Definition datapoint.h:23
pappso_double y
Definition datapoint.h:24
replacement for std::map
std::size_t accumulateIntensity(quint32 tofIndex, std::size_t intensity)
accumulates intesity for the given tof index
std::size_t readIntensity(quint32)
reads intensity for a tof_index
std::vector< quint32 > tofIndexList
void downsizeMzRawMap(std::size_t mzindex_merge_window)
downsize mz resolution to lower the number of real mz computations
static TimsDataFastMap & getTimsDataFastMapInstance()
XicComputeStructure(const TimsFrame *fram_p, const XicCoordTims &xic_struct)
Definition timsframe.cpp:38
coordinates of the XIC to extract and the resulting XIC after extraction
std::size_t scanNumEnd
mobility index end
std::size_t scanNumBegin
mobility index begin
XicSPtr xicSptr
extracted xic
Definition xiccoord.h:130
MzRange mzRange
the mass to extract
Definition xiccoord.h:120
handle a single Bruker's TimsTof frame