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"
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_binaryData.resize(len);
72
73 if(p_bytes != nullptr)
74 {
75 unshufflePacket(p_bytes);
76 }
77 else
78 {
79 if(m_scanCount == 0)
80 {
81
83 QObject::tr("TimsFrame::TimsFrame(%1,%2,nullptr,%3) FAILED")
84 .arg(m_frameId)
85 .arg(m_scanCount)
86 .arg(len));
87 }
88 }
89}
90
92{
93}
94
98
99
100void
102{
103 qDebug();
104 quint64 len = m_binaryData.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_binaryData.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::getScanPeakCount(std::size_t scanNum) const
129{
130 if(m_binaryData.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_scanCount - 1))
140 {
141 auto nb_uint4 = m_binaryData.size() / 4;
142
143 std::size_t cumul = 0;
144 for(quint32 i = 0; i < m_scanCount; i++)
145 {
146 cumul += (*(quint32 *)(m_binaryData.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_binaryData.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_binaryData.constData() + (i * 4)));
164 }
165 return offset;
166}
167
168
169std::vector<quint32>
170TimsFrame::getScanTofIndexList(std::size_t scanNum) const
171{
172 // qDebug();
173 checkScanNum(scanNum);
174 std::vector<quint32> scan_tof;
175
176 if(m_binaryData.size() == 0)
177 return scan_tof;
178 scan_tof.resize(getScanPeakCount(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_binaryData.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::getScanIntensityList(std::size_t scanNum) const
196{
197 // qDebug();
198 checkScanNum(scanNum);
199 std::vector<quint32> scan_intensities;
200
201 if(m_binaryData.size() == 0)
202 return scan_intensities;
203
204 scan_intensities.resize(getScanPeakCount(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] =
211 (*(quint32 *)(m_binaryData.constData() + (offset * 4) + (i * 8) + 4));
212 }
213 // qDebug();
214 return scan_intensities;
215}
216
217
218quint64
219TimsFrame::cumulateScanIntensities(std::size_t scanNum) const
220{
221 qDebug();
222
223 quint64 summed_intensities = 0;
224
225 if(m_binaryData.size() == 0)
226 return summed_intensities;
227 // checkScanNum(scanNum);
228
229 std::size_t size = getScanPeakCount(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_binaryData.constData() + (offset * 4) + (i * 8))) +
239 previous);
240
241 quint32 y =
242 (*(quint32 *)(m_binaryData.constData() + (offset * 4) + (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_acqDurationInMilliseconds);
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::cumulateScanRangeIntensities(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_binaryData.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 += cumulateScanIntensities(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_binaryData.size() == 0)
310 return;
311 qDebug();
312 // checkScanNum(scanNum);
313
314 std::size_t scan_size = getScanPeakCount(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_binaryData.constData() + (scan_offset * 4) +
322 (i * 8))) +
323 previous);
324 quint32 y = (*(quint32 *)(m_binaryData.constData() + (scan_offset * 4) +
325 (i * 8) + 4));
326
327 previous = x;
328 qDebug() << "x=" << x << " y=" << y;
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_binaryData.size() == 0)
345 return;
346
347 // checkScanNum(scanNum);
348
349 std::size_t scan_size = getScanPeakCount(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_binaryData.constData() + (scan_offset * 4) +
356 (i * 8))) +
357 previous);
358
359 previous = x;
360
361 if(x < accepted_tof_index_range_begin)
362 {
363
364 qDebug() << "TOF index still not in range, x:" << x;
365 continue;
366 }
367 if(x > accepted_tof_index_range_end)
368 {
369 qDebug() << "TOF index already out of range, x:" << x;
370 break;
371 }
372
373 qDebug() << "TOF index in range, x:" << x;
374
375 quint32 y = (*(quint32 *)(m_binaryData.constData() + (scan_offset * 4) +
376 (i * 8) + 4));
377
378 accumulate_into.accumulateIntensity(x, y);
379 }
380
381 // qDebug();
382}
383
384
385Trace
386TimsFrame::cumulateScansToTrace(std::size_t mobility_scan_begin,
387 std::size_t mobility_scan_end) const
388{
389 // qDebug();
390
391 Trace new_trace;
392
393 try
394 {
395 if(m_binaryData.size() == 0)
396 return new_trace;
397 TimsDataFastMap &raw_spectrum =
399 raw_spectrum.clear();
400 // double local_accumulationTime = 0;
401
402 std::size_t mobility_scan_max = mobility_scan_end + 1;
403 qDebug();
404 for(std::size_t i = mobility_scan_begin; i < mobility_scan_max; i++)
405 {
406 // qDebug() << i;
407 cumulateScan(i, raw_spectrum);
408 // qDebug() << i;
409
410 // local_accumulationTime += m_accumulationTime;
411 }
412
413 // qDebug();
414
415 pappso::DataPoint data_point_cumul;
416
417
418 MzCalibrationInterface *mz_calibration_p =
420
421
422 for(quint32 tof_index : raw_spectrum.getTofIndexList())
423 {
424 data_point_cumul.x = mz_calibration_p->getMzFromTofIndex(tof_index);
425 // normalization
426 data_point_cumul.y = raw_spectrum.readIntensity(tof_index) *
427 ((double)100.0 / m_acqDurationInMilliseconds);
428 new_trace.push_back(data_point_cumul);
429 }
430 new_trace.sortX();
431
432 // qDebug();
433 }
434
435 catch(std::exception &error)
436 {
437 qDebug() << QString(
438 "Failure in TimsFrame::cumulateScanToTrace %1 to %2 :\n %3")
439 .arg(mobility_scan_begin, mobility_scan_end)
440 .arg(error.what());
441 }
442 return new_trace;
443}
444
445Trace
447 std::size_t mz_index_merge_window,
448 std::size_t mobility_scan_begin,
449 std::size_t mobility_scan_end,
450 quint32 &mz_minimum_index_out,
451 quint32 &mz_maximum_index_out) const
452{
453 // qDebug();
454
455 Trace new_trace;
456
457 try
458 {
459 if(m_binaryData.size() == 0)
460 {
461 qDebug() << "The frame is empty, returning empty trace.";
462 return new_trace;
463 }
464
465 // Allocate a map for (TOF,intensity) pairs to
466 // accumulate ion mobility scans.
467
468 TimsDataFastMap &raw_spectrum =
470 raw_spectrum.clear();
471 // double local_accumulationTime = 0;
472
473 std::size_t mobility_scan_max = mobility_scan_end + 1;
474
475 for(std::size_t i = mobility_scan_begin; i < mobility_scan_max; i++)
476 {
477 // qDebug() << "Going to cumulate currently iterated mobility scan:"
478 // << i;
479 cumulateScan(i, raw_spectrum);
480 // qDebug() << "Done cumulating currently iterated mobility scan:" <<
481 // i;
482
483 // local_accumulationTime += m_accumulationTime;
484 }
485
486 // qDebug();
487
488 pappso::DataPoint data_point_cumul;
489
490 MzCalibrationInterface *mz_calibration_p =
492
493 // If the caller asks that m/z values be binned larger than they are,
494 // ask that the m/z raw map be reduced in resolution.
495 if(mz_index_merge_window > 0)
496 {
497 raw_spectrum.downsizeMzRawMap(mz_index_merge_window);
498 }
499
500 // Store the first mz index and the last mz index of the current spectrum.
501 // The values are set to the out parameters.
502 mz_minimum_index_out = std::numeric_limits<quint32>::max();
503 mz_maximum_index_out = 0;
504
505 // FIXME: donc je comprends que les index sont contigus
506 // puisqu'on utilise at(key) ?
507 for(quint32 tof_index : raw_spectrum.getTofIndexList())
508 {
509 if(tof_index > mz_maximum_index_out)
510 mz_maximum_index_out = tof_index;
511 if(tof_index < mz_minimum_index_out)
512 mz_minimum_index_out = tof_index;
513
514 // Convert the TOF index to m/z
515 data_point_cumul.x = mz_calibration_p->getMzFromTofIndex(tof_index);
516
517 // Normalization
518 data_point_cumul.y = raw_spectrum.readIntensity(tof_index) *
519 ((double)100.0 / m_acqDurationInMilliseconds);
520
521 // Finally make the data point a new Trace point.
522 new_trace.push_back(data_point_cumul);
523 }
524
525 // qDebug() << "At this point we have mz_minimum_index_out:"
526 // << mz_minimum_index_out
527 // << "and mz_maximum_index_out:" << mz_maximum_index_out;
528
529 // FIXME: this does not seem to be necessary since raw_spectrum is a map
530 // with auto-sorting on the keys which are quint32.
531 // new_trace.sortX();
532
533 // qDebug();
534 }
535 catch(std::exception &error)
536 {
537 qDebug() << QString(
538 "Failure in TimsFrame::cumulateScanToTrace %1 to %2 :\n %3")
539 .arg(mobility_scan_begin, mobility_scan_end)
540 .arg(error.what());
541 }
542
543 // qDebug() << "Returning new trace of size:" << new_trace.size();
544
545 return new_trace;
546}
547
548
549Trace
551 std::size_t mz_index_merge_window,
552 double mz_range_begin,
553 double mz_range_end,
554 std::size_t mobility_scan_begin,
555 std::size_t mobility_scan_end,
556 quint32 &mz_minimum_index_out,
557 quint32 &mz_maximum_index_out) const
558{
559 qDebug() << "Calling cumulateScansToTraceMzDownResolution2 for both mz and "
560 "im ranges accounting.";
561
562 Trace new_trace;
563
564 try
565 {
566 if(m_binaryData.size() == 0)
567 {
568 qDebug() << "The frame is empty, returning empty trace.";
569 return new_trace;
570 }
571
572 // Allocate a map for (TOF index,intensity) pairs to
573 // accumulate ion mobility scans.
574
575 TimsDataFastMap &raw_spectrum =
577 raw_spectrum.clear();
578 // double local_accumulationTime = 0;
579
580 std::size_t mobility_scan_max = mobility_scan_end + 1;
581
582 quint32 tof_index_for_mz_range_begin =
583 getMzCalibrationInterfaceSPtr().get()->getTofIndexFromMz(
584 mz_range_begin);
585 quint32 tof_index_for_mz_range_end =
586 getMzCalibrationInterfaceSPtr().get()->getTofIndexFromMz(mz_range_end);
587
588 qDebug() << "20240529 TOF index for mz range begin:"
589 << tof_index_for_mz_range_begin;
590 qDebug() << "20240529 TOF index for mz range end:"
591 << tof_index_for_mz_range_end;
592
593 for(std::size_t iter = mobility_scan_begin; iter < mobility_scan_max;
594 iter++)
595 {
596 // qDebug() << "Going to cumulate currently iterated mobility scan:"
597 // << iter;
598 cumulateScan2(iter,
599 raw_spectrum,
600 tof_index_for_mz_range_begin,
601 tof_index_for_mz_range_end);
602 // qDebug() << "Done cumulating currently iterated mobility scan:" <<
603 // i;
604
605 // local_accumulationTime += m_accumulationTime;
606 }
607
608 // qDebug();
609
610 pappso::DataPoint data_point_cumul;
611
612 MzCalibrationInterface *mz_calibration_p =
614
615 // If the caller asks that m/z values be binned larger than they are,
616 // ask that the m/z raw map be reduced in resolution.
617 if(mz_index_merge_window > 0)
618 {
619 raw_spectrum.downsizeMzRawMap(mz_index_merge_window);
620 }
621
622 // Store the first mz index and the last mz index of the current spectrum.
623 // The values are set to the out parameters.
624 mz_minimum_index_out = std::numeric_limits<quint32>::max();
625 mz_maximum_index_out = 0;
626
627 // for(std::pair<quint32, quint32> pair_tof_intensity : raw_spectrum)
628 for(quint32 tof_index : raw_spectrum.getTofIndexList())
629 {
630 std::size_t intensity = raw_spectrum.readIntensity(tof_index);
631 if(tof_index > mz_maximum_index_out)
632 mz_maximum_index_out = tof_index;
633 if(tof_index < mz_minimum_index_out)
634 mz_minimum_index_out = tof_index;
635
636 // Convert the TOF index to m/z
637 data_point_cumul.x = mz_calibration_p->getMzFromTofIndex(tof_index);
638
639 // Normalization
640 data_point_cumul.y =
641 intensity * ((double)100.0 / m_acqDurationInMilliseconds);
642
643 // Finally make the data point a new Trace point.
644 new_trace.push_back(data_point_cumul);
645 }
646
647 // qDebug() << "At this point we have mz_minimum_index_out:"
648 // << mz_minimum_index_out
649 // << "and mz_maximum_index_out:" << mz_maximum_index_out;
650
651 // FIXME: this does not seem to be necessary since raw_spectrum is a map
652 // with auto-sorting on the keys which are quint32.
653 // new_trace.sortX();
654
655 // qDebug();
656 }
657 catch(std::exception &error)
658 {
659 qDebug() << QString(
660 "Failure in TimsFrame::cumulateScanToTrace %1 to %2 :\n %3")
661 .arg(mobility_scan_begin, mobility_scan_end)
662 .arg(error.what());
663 }
664
665 // qDebug() << "Returning new trace of size:" << new_trace.size();
666
667 return new_trace;
668}
669
670
671void
673 std::size_t scan_index_begin,
674 std::size_t scan_index_end) const
675{
676 // qDebug() << "begin mobility_scan_begin=" << mobility_scan_begin
677 //<< " mobility_scan_end=" << mobility_scan_end;
678
679 if(m_binaryData.size() == 0)
680 return;
681 try
682 {
683
684 std::size_t mobility_scan_max = scan_index_end + 1;
685 // if (mobility_scan_max > m_scanCount) mobility_scan_max = m_scanCount;
686 qDebug();
687 for(std::size_t i = scan_index_begin; i < mobility_scan_max; i++)
688 {
689 qDebug() << i;
690 cumulateScan(i, rawSpectrum);
691 qDebug() << i;
692 // local_accumulationTime += m_accumulationTime;
693 }
694 }
695
696 catch(std::exception &error)
697 {
698 qDebug() << QString("Failure in %1 %2 to %3 :\n %4")
699 .arg(__FUNCTION__)
700 .arg(scan_index_begin)
701 .arg(scan_index_end)
702 .arg(error.what());
703 }
704
705 // qDebug() << "end";
706}
707
708
709void
711 std::size_t scan_index_begin,
712 std::size_t scan_index_end,
713 quint32 tof_index_begin,
714 quint32 tof_index_end) const
715{
716 qDebug() << "tof_index_begin=" << tof_index_begin
717 << " tof_index_end=" << tof_index_end;
718 //<< " mobility_scan_end=" << mobility_scan_end;
719
720 if(m_binaryData.size() == 0)
721 return;
722 try
723 {
724
725 std::size_t mobility_scan_max = scan_index_end + 1;
726 // if (mobility_scan_max > m_scanCount) mobility_scan_max = m_scanCount;
727 qDebug();
728 for(std::size_t i = scan_index_begin; i < mobility_scan_max; i++)
729 {
730 qDebug() << i;
731 cumulateScan2(i, rawSpectrum, tof_index_begin, tof_index_end);
732 qDebug() << i << rawSpectrum.getTofIndexList().size();
733 // local_accumulationTime += m_accumulationTime;
734 }
735 }
736
737 catch(std::exception &error)
738 {
739 qDebug() << QString("Failure in %1 %2 to %3 :\n %4")
740 .arg(__FUNCTION__)
741 .arg(scan_index_begin)
742 .arg(scan_index_end)
743 .arg(error.what());
744 }
745
746 // qDebug() << "end";
747}
748
750TimsFrame::getMassSpectrumSPtr(std::size_t scanNum) const
751{
752
753 // qDebug() << " scanNum=" << scanNum;
754
755 checkScanNum(scanNum);
756
757 // qDebug();
758
759 pappso::MassSpectrumSPtr mass_spectrum_sptr =
760 std::make_shared<pappso::MassSpectrum>();
761 // std::vector<DataPoint>
762
763 if(m_binaryData.size() == 0)
764 return mass_spectrum_sptr;
765
766 // qDebug();
767
768 std::size_t size = getScanPeakCount(scanNum);
769
770 std::size_t offset = getScanOffset(scanNum);
771
772 MzCalibrationInterface *mz_calibration_p =
774
775
776 qint32 previous = -1;
777 qint32 tof_index;
778 // std::vector<quint32> index_list;
779 DataPoint data_point;
780 for(std::size_t i = 0; i < size; i++)
781 {
782 tof_index =
783 (*(quint32 *)((m_binaryData.constData() + (offset * 4) + (i * 8))) +
784 previous);
785 data_point.y =
786 (*(quint32 *)(m_binaryData.constData() + (offset * 4) + (i * 8) + 4));
787
788 // intensity normalization
789 data_point.y *= 100.0 / m_acqDurationInMilliseconds;
790
791 previous = tof_index;
792
793
794 // mz calibration
795 data_point.x = mz_calibration_p->getMzFromTofIndex(tof_index);
796 mass_spectrum_sptr.get()->push_back(data_point);
797 }
798
799 // qDebug();
800
801 return mass_spectrum_sptr;
802}
803
804
805Trace
806TimsFrame::getMobilityScan(std::size_t scanNum,
807 std::size_t mz_index_merge_window,
808 double mz_range_begin,
809 double mz_range_end,
810 quint32 &mz_minimum_index_out,
811 quint32 &mz_maximum_index_out) const
812{
813 qDebug() << "mz_range_begin:" << mz_range_begin
814 << "mz_range_end:" << mz_range_end
815 << "mz_index_merge_window:" << mz_index_merge_window;
816
817 Trace spectrum;
818
819 quint32 mz_index_begin = 0;
820 quint32 mz_index_end = std::numeric_limits<quint32>::max();
821
822 if(mz_range_end > 0)
823 {
824 qDebug() << "m/z range is requested.";
825
826 mz_index_begin =
827 msp_mzCalibration.get()->getTofIndexFromMz(mz_range_begin);
828 mz_index_end = msp_mzCalibration.get()->getTofIndexFromMz(mz_range_end);
829 }
830
831 qDebug() << "After conversion of mz indices, mz_index_begin:"
832 << mz_index_begin << "mz_index_end;" << mz_index_end;
833
834 auto raw_spectrum =
835 getRawValuePairList(scanNum, mz_index_begin, mz_index_end);
836
837 qDebug() << " raw_spectrum.size();" << raw_spectrum.size();
838
839 if(mz_index_merge_window > 0)
840 {
841 qDebug() << "mz_index_merge_window;=" << mz_index_merge_window
842 << " raw_spectrum.size()=" << raw_spectrum.size();
844 mz_index_merge_window, raw_spectrum);
845 }
846
847 if(raw_spectrum.size() > 0)
848 {
849 mz_minimum_index_out = raw_spectrum.front().tof_index;
850 mz_maximum_index_out = raw_spectrum.back().tof_index;
851
852 for(auto &&element : raw_spectrum)
853 {
854 spectrum.push_back(DataPoint(
855 msp_mzCalibration.get()->getMzFromTofIndex(element.tof_index),
856 static_cast<double>(element.intensity_index)));
857
858 // intensity normalization
859 spectrum.back().y *= 100.0 / m_acqDurationInMilliseconds;
860 }
861 }
862
863 return spectrum;
864}
865
866void
868 std::vector<XicCoordTims *>::iterator &itXicListbegin,
869 std::vector<XicCoordTims *>::iterator &itXicListend,
870 XicExtractMethod method) const
871{
872 qDebug() << std::distance(itXicListbegin, itXicListend);
873
874 std::vector<TimsFrame::XicComputeStructure> tmp_xic_list;
875
876 for(auto it = itXicListbegin; it != itXicListend; it++)
877 {
878 tmp_xic_list.push_back(TimsFrame::XicComputeStructure(this, **it));
879
880 qDebug() << " tmp_xic_struct.mobilityIndexBegin="
881 << tmp_xic_list.back().mobilityIndexBegin
882 << " tmp_xic_struct.mobilityIndexEnd="
883 << tmp_xic_list.back().mobilityIndexEnd;
884
885 qDebug() << " tmp_xic_struct.mzIndexLowerBound="
886 << tmp_xic_list.back().mzIndexLowerBound
887 << " tmp_xic_struct.mzIndexUpperBound="
888 << tmp_xic_list.back().mzIndexUpperBound;
889 }
890 if(tmp_xic_list.size() == 0)
891 return;
892 /*
893 std::sort(tmp_xic_list.begin(), tmp_xic_list.end(), [](const
894 TimsXicStructure &a, const TimsXicStructure &b) { return
895 a.mobilityIndexBegin < b.mobilityIndexBegin;
896 });
897 */
898 std::vector<std::size_t> unique_scan_num_list;
899 for(auto &&struct_xic : tmp_xic_list)
900 {
901 for(std::size_t scan = struct_xic.mobilityIndexBegin;
902 (scan <= struct_xic.mobilityIndexEnd) && (scan < m_scanCount);
903 scan++)
904 {
905 unique_scan_num_list.push_back(scan);
906 }
907 }
908 std::sort(unique_scan_num_list.begin(), unique_scan_num_list.end());
909 auto it_scan_num_end =
910 std::unique(unique_scan_num_list.begin(), unique_scan_num_list.end());
911 auto it_scan_num = unique_scan_num_list.begin();
912
913 while(it_scan_num != it_scan_num_end)
914 {
915 TraceSPtr ms_spectrum = getRawTraceSPtr(*it_scan_num);
916 // qDebug() << ms_spectrum.get()->toString();
917 for(auto &&tmp_xic_struct : tmp_xic_list)
918 {
919 if(((*it_scan_num) >= tmp_xic_struct.mobilityIndexBegin) &&
920 ((*it_scan_num) <= tmp_xic_struct.mobilityIndexEnd))
921 {
922 if(method == XicExtractMethod::max)
923 {
924 tmp_xic_struct.tmpIntensity +=
925 ms_spectrum.get()->maxY(tmp_xic_struct.mzIndexLowerBound,
926 tmp_xic_struct.mzIndexUpperBound);
927
928 qDebug() << "tmp_xic_struct.tmpIntensity="
929 << tmp_xic_struct.tmpIntensity;
930 }
931 else
932 {
933 // sum
934 tmp_xic_struct.tmpIntensity +=
935 ms_spectrum.get()->sumY(tmp_xic_struct.mzIndexLowerBound,
936 tmp_xic_struct.mzIndexUpperBound);
937 qDebug() << "tmp_xic_struct.tmpIntensity="
938 << tmp_xic_struct.tmpIntensity;
939 }
940 }
941 }
942 it_scan_num++;
943 }
944
945 for(auto &&tmp_xic_struct : tmp_xic_list)
946 {
947 if(tmp_xic_struct.tmpIntensity != 0)
948 {
949 qDebug() << tmp_xic_struct.xic_ptr;
950 tmp_xic_struct.xic_ptr->push_back(
951 {m_rtInSeconds, tmp_xic_struct.tmpIntensity});
952 }
953 }
954
955 qDebug();
956}
957
958
960TimsFrame::getRawTraceSPtr(std::size_t scanNum) const
961{
962
963 // qDebug();
964
965 pappso::TraceSPtr trace_sptr = std::make_shared<pappso::Trace>();
966 // std::vector<DataPoint>
967
968 if(m_binaryData.size() == 0)
969 return trace_sptr;
970 // qDebug();
971
972 std::size_t size = getScanPeakCount(scanNum);
973
974 std::size_t offset = getScanOffset(scanNum);
975
976 qint32 previous = -1;
977 std::vector<quint32> index_list;
978 for(std::size_t i = 0; i < size; i++)
979 {
980 DataPoint data_point(
981 (*(quint32 *)((m_binaryData.constData() + (offset * 4) + (i * 8))) +
982 previous),
983 (*(quint32 *)(m_binaryData.constData() + (offset * 4) + (i * 8) + 4)));
984
985 // intensity normalization
986 data_point.y *= 100.0 / m_acqDurationInMilliseconds;
987
988 previous = data_point.x;
989 trace_sptr.get()->push_back(data_point);
990 }
991 // qDebug();
992 return trace_sptr;
993}
994
995
996std::vector<TimsFrame::TofIndexIntensityPair>
998 quint32 accepted_tof_index_range_begin,
999 quint32 accepted_tof_index_range_end) const
1000{
1001 // qDebug() << accepted_tof_index_range_begin;
1002
1003 std::vector<TimsFrame::TofIndexIntensityPair> raw_value_pairs;
1004 // std::vector<DataPoint>
1005
1006 if(m_binaryData.size() == 0)
1007 return raw_value_pairs;
1008 // qDebug();
1009
1010 std::size_t size = getScanPeakCount(scanNum);
1011
1012 std::size_t offset = getScanOffset(scanNum);
1013
1014 // qDebug() << size;
1015 qint32 previous = -1;
1016 std::vector<quint32> index_list;
1017 for(std::size_t i = 0; i < size; i++)
1018 {
1019
1020 // qDebug() << i;
1021 TimsFrame::TofIndexIntensityPair raw_value_pair(
1022 {(*(quint32 *)((m_binaryData.constData() + (offset * 4) + (i * 8))) +
1023 previous),
1024 (*(quint32 *)(m_binaryData.constData() + (offset * 4) + (i * 8) +
1025 4))});
1026
1027 previous = raw_value_pair.tof_index;
1028 if(raw_value_pair.tof_index < accepted_tof_index_range_begin)
1029 {
1030 // qDebug() << "TOF index still not in range, x:" << x;
1031 continue;
1032 }
1033 if(raw_value_pair.tof_index > accepted_tof_index_range_end)
1034 {
1035 // qDebug() << "TOF index already out of range, x:" << x;
1036 break;
1037 }
1038
1039 raw_value_pairs.push_back(raw_value_pair);
1040 }
1041 qDebug() << raw_value_pairs.size();
1042 return raw_value_pairs;
1043}
1044
1045} // 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
replacement for std::map
std::size_t accumulateIntensity(quint32 tofIndex, std::size_t intensity)
accumulates intesity for the given tof index
const std::vector< quint32 > & getTofIndexList() const
std::size_t readIntensity(quint32)
reads intensity for a tof_index
void downsizeMzRawMap(std::size_t mzindex_merge_window)
downsize mz resolution to lower the number of real mz computations
static TimsDataFastMap & getTimsDataFastMapInstance()
double m_rtInSeconds
retention time
MzCalibrationInterfaceSPtr msp_mzCalibration
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.
double m_acqDurationInMilliseconds
acquisition duration in milliseconds
virtual const MzCalibrationInterfaceSPtr & getMzCalibrationInterfaceSPtr() const final
get the MzCalibration model to compute mz and TOF for this frame
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)
bool checkScanNum(std::size_t scanNum) const
check that this scan number exists
virtual Trace combineScansToTraceWithDowngradedMzResolution(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 quint64 cumulateScanRangeIntensities(std::size_t scanNumBegin, std::size_t scanNumEnd) const override
...
virtual quint64 cumulateScanIntensities(std::size_t scanNum) const override
virtual ~TimsFrame()
Definition timsframe.cpp:95
TimsFrame(std::size_t timsId, quint32 scanNum, char *p_bytes, std::size_t len)
Definition timsframe.cpp:60
QByteArray m_binaryData
Definition timsframe.h:287
virtual pappso::MassSpectrumSPtr getMassSpectrumSPtr(std::size_t scanNum) const override
get Mass spectrum with peaks for this scan index need the binary file
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 override
cumulate spectrum given a scan number range need the binary file The intensities are normalized with ...
virtual Trace cumulateScansToTrace(std::size_t scanIndexBegin, std::size_t scanIndexEnd) const override
cumulate scan list into a trace
virtual std::vector< quint32 > getScanIntensityList(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 std::vector< TofIndexIntensityPair > 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 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
virtual std::size_t getScanPeakCount(std::size_t scanIndex) const override
get the number of peaks in this spectrum need the binary file
std::size_t getScanOffset(std::size_t scanNum) const
get offset for this spectrum in the binary file
virtual std::vector< quint32 > getScanTofIndexList(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
void extractTimsXicListInRtRange(std::vector< XicCoordTims * >::iterator &itXicListbegin, std::vector< XicCoordTims * >::iterator &itXicListend, XicExtractMethod method) const
void combineScansInTofIndexIntensityMap(TimsDataFastMap &rawSpectrum, std::size_t scan_index_begin, std::size_t scan_index_end) const override
cumulate scan list into a trace into a raw spectrum map
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:247
@ max
maximum of intensities
pappso_double x
Definition datapoint.h:23
pappso_double y
Definition datapoint.h:24
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