libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
timsdata.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/vendors/tims/timsdata.cpp
3 * \date 27/08/2019
4 * \author Olivier Langella
5 * \brief main Tims data handler
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 "timsdata.h"
32#include <QDebug>
33#include <QSqlError>
34#include <QSqlQuery>
35#include <QSqlRecord>
36#include <QMutexLocker>
37#include <QThread>
38#include <set>
39#include <QtConcurrent>
40#include "timsddaprecursors.h"
41#include "timsdiaslices.h"
42
43namespace pappso
44{
45
46TimsData::TimsData(QDir timsDataDirectory)
47 : m_timsDataDirectory(timsDataDirectory)
48{
49
50 qDebug() << "Start of construction of TimsData";
52 if(!m_timsDataDirectory.exists())
53 {
54 throw PappsoException(
55 QObject::tr("ERROR TIMS data directory %1 not found")
56 .arg(m_timsDataDirectory.absolutePath()));
57 }
58
59 if(!QFileInfo(m_timsDataDirectory.absoluteFilePath("analysis.tdf")).exists())
60 {
61
62 throw PappsoException(
63 QObject::tr("ERROR TIMS data directory, %1 sqlite file not found")
64 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf")));
65 }
66
67 // Open the database
68 QSqlDatabase qdb = openDatabaseConnection();
69
70
71 QSqlQuery q(qdb);
72 if(!q.exec("select Key, Value from GlobalMetadata where "
73 "Key='TimsCompressionType';"))
74 {
75
76 qDebug();
77 throw PappsoException(
78 QObject::tr("ERROR in TIMS sqlite database file %1, executing SQL "
79 "command %2:\n%3\n%4\n%5")
80 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
81 .arg(q.lastQuery())
82 .arg(q.lastError().databaseText())
83 .arg(q.lastError().driverText())
84 .arg(q.lastError().nativeErrorCode()));
85 }
86
87
88 int compression_type = 0;
89 if(q.next())
90 {
91 compression_type = q.value(1).toInt();
92 }
93 qDebug() << " compression_type=" << compression_type;
95 QFileInfo(m_timsDataDirectory.absoluteFilePath("analysis.tdf_bin")),
96 compression_type);
97
98 qDebug();
99
100 try
101 {
102
103 qDebug();
105 }
106 catch(pappso::ExceptionNotFound &not_found)
107 {
108
109 qDebug();
110 mpa_timsDdaPrecursors = nullptr;
111 }
112
113
114 qDebug();
115 try
116 {
117 qDebug();
118 mpa_timsDiaSlices = new TimsDiaSlices(q, this);
119 }
120 catch(pappso::ExceptionNotFound &not_found)
121 {
122 qDebug();
123 mpa_timsDiaSlices = nullptr;
124 }
125
127
128 // get number of scans
129 if(!q.exec("SELECT SUM(NumScans),COUNT(Id) FROM Frames"))
130 {
131 qDebug();
132 throw PappsoException(
133 QObject::tr("ERROR in TIMS sqlite database file %1, executing SQL "
134 "command %2:\n%3\n%4\n%5")
135 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
136 .arg(q.lastQuery())
137 .arg(qdb.lastError().databaseText())
138 .arg(qdb.lastError().driverText())
139 .arg(qdb.lastError().nativeErrorCode()));
140 }
141 if(q.next())
142 {
143 qDebug();
144 m_totalScanCount = q.value(0).toLongLong();
145 m_frameCount = q.value(1).toLongLong();
146 }
147
148 if(!q.exec("select * from MzCalibration;"))
149 {
150 qDebug();
151 throw PappsoException(
152 QObject::tr("ERROR in TIMS sqlite database file %1, executing SQL "
153 "command %2:\n%3\n%4\n%5")
154 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
155 .arg(q.lastQuery())
156 .arg(q.lastError().databaseText())
157 .arg(q.lastError().driverText())
158 .arg(q.lastError().nativeErrorCode()));
159 }
160
161 while(q.next())
162 {
163 QSqlRecord record = q.record();
165 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
166 }
167
168 // m_mapTimsCalibrationRecord
169
170 if(!q.exec("select * from TimsCalibration;"))
171 {
172 qDebug();
173 throw PappsoException(
174 QObject::tr("ERROR in TIMS sqlite database file %1, executing SQL "
175 "command %2:\n%3\n%4\n%5")
176 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
177 .arg(q.lastQuery())
178 .arg(q.lastError().databaseText())
179 .arg(q.lastError().driverText())
180 .arg(q.lastError().nativeErrorCode()));
181 }
182 while(q.next())
183 {
184 QSqlRecord record = q.record();
186 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
187 }
188
189
190 // store frames
191 if(!q.exec("select Frames.TimsId, Frames.AccumulationTime, " // 1
192 "Frames.MzCalibration, " // 2
193 "Frames.T1, Frames.T2, " // 4
194 "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, " // 7
195 "Frames.Id " // 8
196 " FROM Frames;"))
197 {
198 qDebug();
199 throw PappsoException(
200 QObject::tr("ERROR in TIMS sqlite database file %1, executing SQL "
201 "command %2:\n%3\n%4\n%5")
202 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
203 .arg(q.lastQuery())
204 .arg(q.lastError().databaseText())
205 .arg(q.lastError().driverText())
206 .arg(q.lastError().nativeErrorCode()));
207 }
208
210 while(q.next())
211 {
212 QSqlRecord record = q.record();
213 TimsFrameRecord &frame_record =
214 m_mapFramesRecord[record.value(8).toULongLong()];
215
216 frame_record.frame_id = record.value(8).toULongLong();
217 frame_record.tims_offset = record.value(0).toULongLong();
218 frame_record.accumulation_time = record.value(1).toDouble();
219 frame_record.mz_calibration_id = record.value(2).toULongLong();
220 frame_record.frame_t1 = record.value(3).toDouble();
221 frame_record.frame_t2 = record.value(4).toDouble();
222 frame_record.frame_time = record.value(5).toDouble();
223 frame_record.msms_type = record.value(6).toInt();
224 frame_record.tims_calibration_id = record.value(7).toULongLong();
225 }
226
227 qDebug();
228}
229
230QSqlDatabase
232{
233 QString database_connection_name = QString("%1_%2")
234 .arg(m_timsDataDirectory.absolutePath())
235 .arg((quintptr)QThread::currentThread());
236 // Open the database
237 QSqlDatabase qdb = QSqlDatabase::database(database_connection_name);
238 if(!qdb.isValid())
239 {
240 qDebug() << database_connection_name;
241 qdb = QSqlDatabase::addDatabase("QSQLITE", database_connection_name);
242 qdb.setDatabaseName(m_timsDataDirectory.absoluteFilePath("analysis.tdf"));
243 }
244
245
246 if(!qdb.open())
247 {
248 qDebug();
249 throw PappsoException(
250 QObject::tr("ERROR opening TIMS sqlite database file %1, database name "
251 "%2 :\n%3\n%4\n%5")
252 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
253 .arg(database_connection_name)
254 .arg(qdb.lastError().databaseText())
255 .arg(qdb.lastError().driverText())
256 .arg(qdb.lastError().nativeErrorCode()));
257 }
258 return qdb;
259}
260
261TimsData::TimsData([[maybe_unused]] const TimsData &other)
262{
263 qDebug();
264}
265
267{
268 // m_qdb.close();
269 if(mpa_timsBinDec != nullptr)
270 {
271 delete mpa_timsBinDec;
272 }
273 if(mpa_mzCalibrationStore != nullptr)
274 {
276 }
277 if(mpa_timsDdaPrecursors != nullptr)
278 {
280 }
281 if(mpa_timsDiaSlices != nullptr)
282 {
283 delete mpa_timsDiaSlices;
284 }
285}
286
287void
289{
290 qDebug();
291 QSqlDatabase qdb = openDatabaseConnection();
292
293 QSqlQuery q =
294 qdb.exec(QString("SELECT Id, NumScans FROM "
295 "Frames ORDER BY Id"));
296 if(q.lastError().isValid())
297 {
298
299 throw PappsoException(
300 QObject::tr("ERROR in TIMS sqlite database file %1, executing SQL "
301 "command %2:\n%3\n%4\n%5")
302 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
303 .arg(q.lastQuery())
304 .arg(qdb.lastError().databaseText())
305 .arg(qdb.lastError().driverText())
306 .arg(qdb.lastError().nativeErrorCode()));
307 }
308 TimsFrameSPtr tims_frame;
309 bool index_found = false;
310 std::size_t timsId;
311 /** @brief number of scans in mobility dimension (number of TOF scans)
312 */
313 std::size_t numberScans;
314 std::size_t cumulScans = 0;
315 while(q.next() && (!index_found))
316 {
317 timsId = q.value(0).toULongLong();
318 numberScans = q.value(1).toULongLong();
319
320 // qDebug() << timsId;
321
323 std::pair<std::size_t, std::size_t>((cumulScans / 1000),
324 m_frameIdDescrList.size()));
325
326 m_frameIdDescrList.push_back({timsId, numberScans, cumulScans});
327 cumulScans += numberScans;
328 }
329 qDebug();
330}
331
332std::pair<std::size_t, std::size_t>
333TimsData::getScanCoordinateFromRawIndex(std::size_t raw_index) const
334{
335 return getScanCoordinatesByGlobalScanIndex(raw_index);
336}
337
338std::pair<std::size_t, std::size_t>
340{
341 std::size_t fast_access = raw_index / 1000;
342 qDebug() << " fast_access=" << fast_access;
343 auto map_it = m_thousandIndexToFrameIdDescrListIndex.find(fast_access);
345 {
346 throw ExceptionNotFound(
347 QObject::tr("ERROR raw index %1 not found (fast_access)")
348 .arg(raw_index));
349 }
350 std::size_t start_point_index = map_it->second;
351 while((start_point_index > 0) &&
352 (m_frameIdDescrList[start_point_index].m_globalScanIndex > raw_index))
353 {
354 start_point_index--;
355 }
356 for(std::size_t i = start_point_index; i < m_frameIdDescrList.size(); i++)
357 {
358
359 if(raw_index < (m_frameIdDescrList[i].m_globalScanIndex +
360 m_frameIdDescrList[i].m_scanCount))
361 {
362 return std::pair<std::size_t, std::size_t>(
363 m_frameIdDescrList[i].m_frameId,
364 raw_index - m_frameIdDescrList[i].m_globalScanIndex);
365 }
366 }
367
368 throw ExceptionNotFound(
369 QObject::tr("ERROR raw index %1 not found").arg(raw_index));
370}
371
372std::size_t
374 std::size_t index) const
375{
376
377 return getGlobalScanIndexByScanCoordinates(frame_id, index);
378}
379
380std::size_t
382 std::size_t index) const
383{
384
385 for(auto frameDescr : m_frameIdDescrList)
386 {
387 if(frameDescr.m_frameId == frame_id)
388 {
389 return frameDescr.m_globalScanIndex + index;
390 }
391 }
392
393 throw ExceptionNotFound(
394 QObject::tr("ERROR raw index with frame_id=%1 scan_index=%2 not found")
395 .arg(frame_id)
396 .arg(index));
397}
398
399/** @brief get a mass spectrum given its spectrum index
400 * @param raw_index a number begining at 0, corresponding to a Tims Scan in
401 * the order they lies in the binary data file
402 */
405{
407}
408
411{
412
413 qDebug() << " raw_index=" << raw_index;
414 try
415 {
416 auto coordinate = getScanCoordinatesByGlobalScanIndex(raw_index);
417 return getMassSpectrumCstSPtr(coordinate.first, coordinate.second);
418 }
419 catch(PappsoException &error)
420 {
421 throw PappsoException(
422 QObject::tr("Error TimsData::getMassSpectrumCstSPtrByRawIndex "
423 "raw_index=%1 :\n%2")
424 .arg(raw_index)
425 .arg(error.qwhat()));
426 }
427}
428
431{
432
433 qDebug() << " timsId=" << timsId;
434
435 const TimsFrameRecord &frame_record = m_mapFramesRecord[timsId];
436 if(timsId > m_totalScanCount)
437 {
438 throw ExceptionNotFound(
439 QObject::tr("ERROR Frames database id %1 not found").arg(timsId));
440 }
441 TimsFrameBaseSPtr tims_frame;
442
443
444 tims_frame = std::make_shared<TimsFrameBase>(
445 TimsFrameBase(timsId, frame_record.tims_offset));
446
447 auto it_map_record =
449 if(it_map_record != m_mapMzCalibrationRecord.end())
450 {
451
452 double T1_frame = frame_record.frame_t1; // Frames.T1
453 double T2_frame = frame_record.frame_t2; // Frames.T2
454
455
456 tims_frame.get()->setMzCalibrationInterfaceSPtr(
458 T1_frame, T2_frame, it_map_record->second));
459 }
460 else
461 {
462 throw ExceptionNotFound(
463 QObject::tr("ERROR MzCalibration database id %1 not found")
464 .arg(frame_record.mz_calibration_id));
465 }
466
467
468 tims_frame.get()->setAcqDurationInMilliseconds(
469 frame_record.accumulation_time);
470
471 tims_frame.get()->setRtInSeconds(frame_record.frame_time);
472 tims_frame.get()->setMsMsType(frame_record.msms_type);
473
474
475 auto it_map_record_tims_calibration =
477 if(it_map_record_tims_calibration != m_mapTimsCalibrationRecord.end())
478 {
479
480 tims_frame.get()->setTimsCalibration(
481 it_map_record_tims_calibration->second.value(1).toInt(),
482 it_map_record_tims_calibration->second.value(2).toDouble(),
483 it_map_record_tims_calibration->second.value(3).toDouble(),
484 it_map_record_tims_calibration->second.value(4).toDouble(),
485 it_map_record_tims_calibration->second.value(5).toDouble(),
486 it_map_record_tims_calibration->second.value(6).toDouble(),
487 it_map_record_tims_calibration->second.value(7).toDouble(),
488 it_map_record_tims_calibration->second.value(8).toDouble(),
489 it_map_record_tims_calibration->second.value(9).toDouble(),
490 it_map_record_tims_calibration->second.value(10).toDouble(),
491 it_map_record_tims_calibration->second.value(11).toDouble());
492 }
493 else
494 {
495 throw ExceptionNotFound(
496 QObject::tr("ERROR TimsCalibration database id %1 not found")
497 .arg(frame_record.tims_calibration_id));
498 }
499
500 return tims_frame;
501}
502
503std::vector<std::size_t>
504TimsData::getTimsMS1FrameIdsInRtRange(double rt_begin, double rt_end) const
505{
506
507 qDebug() << " rt_begin=" << rt_begin << " rt_end=" << rt_end;
508 if(rt_begin < 0)
509 rt_begin = 0;
510 std::vector<std::size_t> tims_frameid_list;
511 QSqlDatabase qdb = openDatabaseConnection();
512 QSqlQuery q = qdb.exec(QString("SELECT Frames.Id FROM Frames WHERE "
513 "Frames.MsMsType=0 AND (Frames.Time>=%1) "
514 "AND (Frames.Time<=%2) ORDER BY "
515 "Frames.Time;")
516 .arg(rt_begin)
517 .arg(rt_end));
518 if(q.lastError().isValid())
519 {
520
521 throw PappsoException(
522 QObject::tr("ERROR in TIMS sqlite database file %1, database name %2, "
523 "executing SQL "
524 "command %3:\n%4\n%5\n%6")
525 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
526 .arg(qdb.databaseName())
527 .arg(q.lastQuery())
528 .arg(qdb.lastError().databaseText())
529 .arg(qdb.lastError().driverText())
530 .arg(qdb.lastError().nativeErrorCode()));
531 }
532 while(q.next())
533 {
534
535 tims_frameid_list.push_back(q.value(0).toULongLong());
536 }
537 return tims_frameid_list;
538}
539
540std::vector<std::size_t>
541TimsData::getTimsMS2FrameIdsInRtRange(double rt_begin, double rt_end) const
542{
543
544 qDebug() << " rt_begin=" << rt_begin << " rt_end=" << rt_end;
545 if(rt_begin < 0)
546 rt_begin = 0;
547 std::vector<std::size_t> tims_frameid_list;
548 QSqlDatabase qdb = openDatabaseConnection();
549 QSqlQuery q =
550 qdb.exec(QString("SELECT Frames.Id FROM Frames WHERE "
551 "Frames.MsMsType=8 AND "
552 "(Frames.Time>=%1) AND (Frames.Time<=%2) ORDER BY "
553 "Frames.Time;")
554 .arg(rt_begin)
555 .arg(rt_end));
556 if(q.lastError().isValid())
557 {
558
559 throw PappsoException(
560 QObject::tr("ERROR in TIMS sqlite database file %1, database name %2, "
561 "executing SQL "
562 "command %3:\n%4\n%5\n%6")
563 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
564 .arg(qdb.databaseName())
565 .arg(q.lastQuery())
566 .arg(qdb.lastError().databaseText())
567 .arg(qdb.lastError().driverText())
568 .arg(qdb.lastError().nativeErrorCode()));
569 }
570 while(q.next())
571 {
572
573 tims_frameid_list.push_back(q.value(0).toULongLong());
574 }
575 return tims_frameid_list;
576}
577
580{
581
582 qDebug() << " timsId=" << timsId
583 << " m_mapFramesRecord.size()=" << m_mapFramesRecord.size();
584
585 /*
586 for(auto pair_i : m_mapFramesRecord)
587 {
588
589 qDebug() << " pair_i=" << pair_i.first;
590 }
591 */
592
593 const TimsFrameRecord &frame_record = m_mapFramesRecord[timsId];
594 if(timsId > m_totalScanCount)
595 {
596 throw ExceptionNotFound(
597 QObject::tr("ERROR Frames database id %1 not found").arg(timsId));
598 }
599
600 TimsFrameSPtr tims_frame;
601
602
603 // QMutexLocker lock(&m_mutex);
604 tims_frame =
606 // lock.unlock();
607
608 qDebug();
609 auto it_map_record =
611 if(it_map_record != m_mapMzCalibrationRecord.end())
612 {
613
614 double T1_frame = frame_record.frame_t1; // Frames.T1
615 double T2_frame = frame_record.frame_t2; // Frames.T2
616
617
618 tims_frame.get()->setMzCalibrationInterfaceSPtr(
620 T1_frame, T2_frame, it_map_record->second));
621 }
622 else
623 {
624 throw ExceptionNotFound(
625 QObject::tr(
626 "ERROR MzCalibration database id %1 not found for frame_id=%2")
627 .arg(frame_record.mz_calibration_id)
628 .arg(timsId));
629 }
630
631
632 tims_frame.get()->setAcqDurationInMilliseconds(
633 frame_record.accumulation_time);
634
635 tims_frame.get()->setRtInSeconds(frame_record.frame_time);
636 tims_frame.get()->setMsMsType(frame_record.msms_type);
637
638 qDebug();
639 auto it_map_record_tims_calibration =
641 if(it_map_record_tims_calibration != m_mapTimsCalibrationRecord.end())
642 {
643
644 tims_frame.get()->setTimsCalibration(
645 it_map_record_tims_calibration->second.value(1).toInt(),
646 it_map_record_tims_calibration->second.value(2).toDouble(),
647 it_map_record_tims_calibration->second.value(3).toDouble(),
648 it_map_record_tims_calibration->second.value(4).toDouble(),
649 it_map_record_tims_calibration->second.value(5).toDouble(),
650 it_map_record_tims_calibration->second.value(6).toDouble(),
651 it_map_record_tims_calibration->second.value(7).toDouble(),
652 it_map_record_tims_calibration->second.value(8).toDouble(),
653 it_map_record_tims_calibration->second.value(9).toDouble(),
654 it_map_record_tims_calibration->second.value(10).toDouble(),
655 it_map_record_tims_calibration->second.value(11).toDouble());
656 }
657 else
658 {
659 throw ExceptionNotFound(
660 QObject::tr("ERROR TimsCalibration database id %1 not found")
661 .arg(frame_record.tims_calibration_id));
662 }
663 qDebug();
664 return tims_frame;
665}
666
667
669TimsData::getMassSpectrumCstSPtr(std::size_t timsId, std::size_t scanNum)
670{
671 qDebug() << " timsId=" << timsId << " scanNum=" << scanNum;
673
674 return frame->getMassSpectrumCstSPtr(scanNum);
675}
676
677
678std::size_t
680{
681 return m_frameCount;
682}
683
684
685std::size_t
687{
688 return m_frameCount;
689}
690
691
692std::size_t
697
698
699std::size_t
701{
702 return m_totalScanCount;
703}
704
705unsigned int
707{
708 return getMsLevelByGlobalScanIndex(index);
709}
710
711
712unsigned int
714{
715 auto coordinate = getScanCoordinatesByGlobalScanIndex(index);
716 auto tims_frame = getTimsFrameCstSPtrCached(coordinate.first);
717 return tims_frame.get()->getMsLevel();
718}
719
720
721void
723 const MsRunIdCstSPtr &msrun_id,
724 QualifiedMassSpectrum &mass_spectrum,
725 std::size_t global_scan_index,
726 bool want_binary_data)
727{
728
730 msrun_id, mass_spectrum, global_scan_index, want_binary_data);
731}
732
733void
735 const MsRunIdCstSPtr &msrun_id,
736 QualifiedMassSpectrum &mass_spectrum,
737 std::size_t global_scan_index,
738 bool want_binary_data)
739{
740
741
742 try
743 {
744 auto coordinate = getScanCoordinatesByGlobalScanIndex(global_scan_index);
745 TimsFrameBaseCstSPtr tims_frame;
746 if(want_binary_data)
747 {
748 tims_frame = getTimsFrameCstSPtrCached(coordinate.first);
749 }
750 else
751 {
752 tims_frame = getTimsFrameBaseCstSPtrCached(coordinate.first);
753 }
754 MassSpectrumId spectrum_id;
755
756 spectrum_id.setSpectrumIndex(global_scan_index);
757 spectrum_id.setMsRunId(msrun_id);
758 spectrum_id.setNativeId(
759 QString("frame_id=%1 scan_index=%2 global_scan_index=%3")
760 .arg(coordinate.first)
761 .arg(coordinate.second)
762 .arg(global_scan_index));
763
764 mass_spectrum.setMassSpectrumId(spectrum_id);
765
766 mass_spectrum.setMsLevel(tims_frame.get()->getMsLevel());
767 mass_spectrum.setRtInSeconds(tims_frame.get()->getRtInSeconds());
768
769 mass_spectrum.setDtInMilliSeconds(
770 tims_frame.get()->getDriftTimeInMilliseconds(coordinate.second));
771 // 1/K0
772 mass_spectrum.setParameterValue(
774 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
775
776 mass_spectrum.setEmptyMassSpectrum(true);
777 if(want_binary_data)
778 {
779 mass_spectrum.setMassSpectrumSPtr(
780 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
781 if(mass_spectrum.size() > 0)
782 {
783 mass_spectrum.setEmptyMassSpectrum(false);
784 }
785 }
786 else
787 {
788 // if(tims_frame.get()->getNbrPeaks(coordinate.second) > 0)
789 //{
790 mass_spectrum.setEmptyMassSpectrum(false);
791 // }
792 }
793 if(tims_frame.get()->getMsLevel() > 1)
794 {
795
796 if(mpa_timsDdaPrecursors != nullptr)
797 {
798 auto spectrum_descr =
800 coordinate);
801 if(spectrum_descr.precursor_id > 0)
802 {
803
804 mass_spectrum.appendPrecursorIonData(
805 spectrum_descr.precursor_ion_data);
806
807
808 MassSpectrumId spectrum_id;
809 std::size_t prec_spectrum_index =
811 spectrum_descr.parent_frame, coordinate.second);
812
813 mass_spectrum.setPrecursorSpectrumIndex(prec_spectrum_index);
814 mass_spectrum.setPrecursorNativeId(
815 QString("frame_id=%1 scan_index=%2 global_scan_index=%3")
816 .arg(spectrum_descr.parent_frame)
817 .arg(coordinate.second)
818 .arg(prec_spectrum_index));
819
820 mass_spectrum.setParameterValue(
822 spectrum_descr.isolationMz);
823 mass_spectrum.setParameterValue(
825 spectrum_descr.isolationWidth);
826
827 mass_spectrum.setParameterValue(
829 spectrum_descr.collisionEnergy);
830 mass_spectrum.setParameterValue(
832 (quint64)spectrum_descr.precursor_id);
833 }
834 }
835 }
836 }
837 catch(PappsoException &error)
838 {
839 throw PappsoException(
840 QObject::tr("Error TimsData::getQualifiedMassSpectrumByRawIndex "
841 "spectrum_index=%1 :\n%2")
842 .arg(global_scan_index)
843 .arg(error.qwhat()));
844 }
845}
846
847Trace
849{
850 // In the Frames table, each frame has a record describing the
851 // SummedIntensities for all the mobility spectra.
852
853
854 MapTrace rt_tic_map_trace;
855
856 using Pair = std::pair<double, double>;
857 using Map = std::map<double, double>;
858 using Iterator = Map::iterator;
859
860
861 QSqlDatabase qdb = openDatabaseConnection();
862 QSqlQuery q =
863 qdb.exec(QString("SELECT Time, SummedIntensities "
864 "FROM Frames WHERE MsMsType = 0 "
865 "ORDER BY Time;"));
866
867 if(q.lastError().isValid())
868 {
869
870 throw PappsoException(
871 QObject::tr("ERROR in TIMS sqlite database file %1, database name %2, "
872 "executing SQL "
873 "command %3:\n%4\n%5\n%6")
874 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
875 .arg(qdb.databaseName())
876 .arg(q.lastQuery())
877 .arg(qdb.lastError().databaseText())
878 .arg(qdb.lastError().driverText())
879 .arg(qdb.lastError().nativeErrorCode()));
880 }
881
882 while(q.next())
883 {
884
885 bool ok = false;
886
887 int cumulated_results = 2;
888
889 double rt = q.value(0).toDouble(&ok);
890 cumulated_results -= ok;
891
892 double sumY = q.value(1).toDouble(&ok);
893 cumulated_results -= ok;
894
895 if(cumulated_results)
896 {
897 throw PappsoException(
898 QObject::tr(
899 "ERROR in TIMS sqlite database file: could not read either the "
900 "retention time or the summed intensities (%1, database name "
901 "%2, "
902 "executing SQL "
903 "command %3:\n%4\n%5\n%6")
904 .arg(m_timsDataDirectory.absoluteFilePath("analysis.tdf"))
905 .arg(qdb.databaseName())
906 .arg(q.lastQuery())
907 .arg(qdb.lastError().databaseText())
908 .arg(qdb.lastError().driverText())
909 .arg(qdb.lastError().nativeErrorCode()));
910 }
911
912 // Try to insert value sumY at key rt.
913 std::pair<Iterator, bool> res = rt_tic_map_trace.insert(Pair(rt, sumY));
914
915 if(!res.second)
916 {
917 // One other same rt value was seen already (like in ion mobility
918 // mass spectrometry, for example). Only increment the y value.
919
920 res.first->second += sumY;
921 }
922 }
923
924 // qDebug().noquote() << "The TIC chromatogram:\n"
925 //<< rt_tic_map_trace.toTrace().toString();
926
927 return rt_tic_map_trace.toTrace();
928}
929
930
933{
934 QMutexLocker locker(&m_mutex);
935 for(auto &tims_frame : m_timsFrameBaseCache)
936 {
937 if(tims_frame.get()->getId() == timsId)
938 {
939 m_timsFrameBaseCache.push_back(tims_frame);
941 m_timsFrameBaseCache.pop_front();
942 return tims_frame;
943 }
944 }
945
948 m_timsFrameBaseCache.pop_front();
949 return m_timsFrameBaseCache.back();
950}
951
954{
955 qDebug();
956 QMutexLocker locker(&m_mutex);
957 for(auto &tims_frame : m_timsFrameCache)
958 {
959 if(tims_frame.get()->getId() == timsId)
960 {
961 m_timsFrameCache.push_back(tims_frame);
962 if(m_timsFrameCache.size() > m_cacheSize)
963 m_timsFrameCache.pop_front();
964 return tims_frame;
965 }
966 }
967 TimsFrameCstSPtr frame_sptr = getTimsFrameCstSPtr(timsId);
968
969 // locker.relock();
970 qDebug();
971
972 m_timsFrameCache.push_back(frame_sptr);
973 if(m_timsFrameCache.size() > m_cacheSize)
974 m_timsFrameCache.pop_front();
975 qDebug();
976 return m_timsFrameCache.back();
977
978
979 /*
980// the frame is not in the cache
981if(std::find(m_someoneIsLoadingFrameId.begin(),
982 m_someoneIsLoadingFrameId.end(),
983 timsId) == m_someoneIsLoadingFrameId.end())
984 {
985 // not found, we are alone on this frame
986 m_someoneIsLoadingFrameId.push_back(timsId);
987 qDebug();
988 //locker.unlock();
989 TimsFrameCstSPtr frame_sptr = getTimsFrameCstSPtr(timsId);
990
991 // locker.relock();
992 qDebug();
993 m_someoneIsLoadingFrameId.erase(
994 std::find(m_someoneIsLoadingFrameId.begin(),
995 m_someoneIsLoadingFrameId.end(),
996 timsId));
997
998 m_timsFrameCache.push_back(frame_sptr);
999 if(m_timsFrameCache.size() > m_cacheSize)
1000 m_timsFrameCache.pop_front();
1001 qDebug();
1002 return m_timsFrameCache.back();
1003 }
1004else
1005 {
1006 // this frame is loading by someone else, we have to wait
1007 qDebug();
1008 // locker.unlock();
1009 // std::size_t another_frame_id = timsId;
1010 while(true)
1011 {
1012 QThread::usleep(1);
1013 // locker.relock();
1014
1015 for(auto &tims_frame : m_timsFrameCache)
1016 {
1017 if(tims_frame.get()->getId() == timsId)
1018 {
1019 m_timsFrameCache.push_back(tims_frame);
1020 return tims_frame;
1021 }
1022 }
1023 // locker.unlock();
1024}
1025} // namespace pappso
1026*/
1027}
1028
1029
1030std::vector<double>
1035
1036std::vector<double>
1038{
1039
1040 std::vector<double> timeline;
1041 timeline.reserve(m_mapFramesRecord.size());
1042 for(const TimsFrameRecord &frame_record : m_mapFramesRecord)
1043 {
1044 if(frame_record.mz_calibration_id != 0)
1045 {
1046 timeline.push_back(frame_record.frame_time);
1047 }
1048 }
1049 return timeline;
1050}
1051
1054{
1055 return getScanByGlobalScanIndex(index);
1056}
1057
1060{
1061 qDebug() << " spectrum_index=" << index;
1062 auto coordinate = getScanCoordinatesByGlobalScanIndex(index);
1063 TimsFrameBaseCstSPtr tims_frame;
1064 tims_frame = getTimsFrameCstSPtrCached(coordinate.first);
1065
1067 raw_spectrum.clear();
1068 tims_frame.get()->combineScansInTofIndexIntensityMap(
1069 raw_spectrum, coordinate.second, coordinate.second);
1070 return raw_spectrum;
1071}
1072
1073const std::vector<FrameIdDescr> &
1075{
1076 return m_frameIdDescrList;
1077}
1078
1079const std::vector<TimsFrameRecord> &
1084
1085const QDir &
1090
1093{
1094 if(mpa_timsDdaPrecursors == nullptr)
1095 {
1096 throw pappso::PappsoException("TimsData does not contain DDA precursors");
1097 }
1098 return mpa_timsDdaPrecursors;
1099}
1100
1101TimsBinDec *
1103{
1104 return mpa_timsBinDec;
1105}
1106
1107bool
1109{
1110 if(mpa_timsDdaPrecursors == nullptr)
1111 return false;
1112 return true;
1113}
1114
1115bool
1117{
1118 if(mpa_timsDiaSlices == nullptr)
1119 return false;
1120 return true;
1121}
1122
1125{
1126 qDebug();
1127 if(mpa_timsDiaSlices == nullptr)
1128 {
1129 throw pappso::PappsoException("TimsData does not contain DIA slices");
1130 }
1131 return mpa_timsDiaSlices;
1132}
1133
1134} // namespace pappso
Trace toTrace() const
Definition maptrace.cpp:219
void setNativeId(const QString &native_id)
void setMsRunId(MsRunIdCstSPtr other)
void setSpectrumIndex(std::size_t index)
MzCalibrationInterfaceSPtr getInstance(double T1_frame, double T2_frame, const QSqlRecord &mzcalibration_record)
virtual const QString & qwhat() const
Class representing a fully specified mass spectrum.
void setPrecursorNativeId(const QString &native_id)
Set the scan native id of the precursor ion.
void setDtInMilliSeconds(pappso_double rt)
Set the drift time in milliseconds.
void appendPrecursorIonData(const PrecursorIonData &precursor_ion_data)
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
void setMsLevel(uint ms_level)
Set the mass spectrum level.
void setPrecursorSpectrumIndex(std::size_t precursor_scan_num)
Set the scan number of the precursor ion.
void setParameterValue(QualifiedMassSpectrumParameter parameter, const QVariant &value)
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
void setEmptyMassSpectrum(bool is_empty_mass_spectrum)
TimsFrameSPtr getTimsFrameSPtrByOffset(std::size_t frameId, const std::vector< pappso::TimsFrameRecord > &frame_record_list)
replacement for std::map
static TimsDataFastMap & getTimsDataFastMapInstance()
std::size_t getGlobalScanIndexByScanCoordinates(std::size_t frame_id, std::size_t index) const
Definition timsdata.cpp:381
QSqlDatabase openDatabaseConnection() const
Definition timsdata.cpp:231
std::size_t getTotalScanCount() const
Definition timsdata.cpp:700
TimsDiaSlices * getTimsDiaSlicesPtr() const
TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId)
get a Tims frame with his database ID
Definition timsdata.cpp:579
TimsDdaPrecursors * mpa_timsDdaPrecursors
Definition timsdata.h:255
const std::vector< TimsFrameRecord > & getTimsFrameRecordList() const
std::vector< FrameIdDescr > m_frameIdDescrList
store every frame id and corresponding sizes
Definition timsdata.h:274
TimsFrameCstSPtr getTimsFrameCstSPtrCached(std::size_t timsId)
get a Tims frame with his database ID but look in the cache first
Definition timsdata.cpp:953
pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtrByRawIndex(std::size_t raw_index)
get a mass spectrum given its spectrum index
Definition timsdata.cpp:404
TimsDataFastMap & getScanByGlobalScanIndex(std::size_t index)
friend TimsDdaPrecursors
Definition timsdata.h:71
virtual ~TimsData()
Definition timsdata.cpp:266
const std::vector< FrameIdDescr > & getFrameIdDescrList() const
std::size_t getTotalNumberOfFrames() const
Get total number of frames.
Definition timsdata.cpp:679
std::vector< std::size_t > getTimsMS1FrameIdsInRtRange(double rt_begin, double rt_end) const
Definition timsdata.cpp:504
pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtr(std::size_t timsId, std::size_t scanNum)
get a mass spectrum given the tims frame database id and scan number within tims frame
Definition timsdata.cpp:669
TimsBinDec * getTimsBinDecPtr() const
TimsData(QDir timsDataDirectory)
build using the tims data directory
Definition timsdata.cpp:46
void getQualifiedMassSpectrumByRawIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t global_scan_index, bool want_binary_data)
Definition timsdata.cpp:722
std::vector< std::size_t > getTimsMS2FrameIdsInRtRange(double rt_begin, double rt_end) const
Definition timsdata.cpp:541
Trace getTicChromatogram() const
Definition timsdata.cpp:848
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId)
Definition timsdata.cpp:932
void getQualifiedMassSpectrumByGlobalScanIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t global_scan_index, bool want_binary_data)
Definition timsdata.cpp:734
std::deque< TimsFrameCstSPtr > m_timsFrameCache
Definition timsdata.h:261
std::size_t m_cacheSize
Definition timsdata.h:260
std::pair< std::size_t, std::size_t > getScanCoordinateFromRawIndex(std::size_t spectrum_index) const
Definition timsdata.cpp:333
std::vector< TimsFrameRecord > m_mapFramesRecord
Definition timsdata.h:267
unsigned int getMsLevelBySpectrumIndex(std::size_t index)
Definition timsdata.cpp:706
std::size_t m_totalScanCount
Definition timsdata.h:258
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
Definition timsdata.h:265
std::map< int, QSqlRecord > m_mapTimsCalibrationRecord
Definition timsdata.h:266
void fillFrameIdDescrList()
private function to fill m_frameIdDescrList
Definition timsdata.cpp:288
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId)
get a Tims frame base (no binary data file access) with his database ID
Definition timsdata.cpp:430
unsigned int getMsLevelByGlobalScanIndex(std::size_t index)
Definition timsdata.cpp:713
friend TimsDiaSlices
Definition timsdata.h:72
QDir m_timsDataDirectory
Definition timsdata.h:252
MzCalibrationStore * mpa_mzCalibrationStore
Definition timsdata.h:269
pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtrByGlobalScanIndex(std::size_t index)
Definition timsdata.cpp:410
virtual std::vector< double > getRetentionTimeLineInSeconds() const
virtual std::vector< double > getRetentionTimeLine() const
retention timeline get retention times along the MSrun in seconds
const QDir & getTimsDataDirectory() const
TimsDataFastMap & getRawMsBySpectrumIndex(std::size_t index)
get raw signal for a spectrum index only to use to see the raw signal
std::size_t getFrameCount() const
Definition timsdata.cpp:686
bool isDiaRun() const
tells if this MS run is a DIA run
std::deque< TimsFrameBaseCstSPtr > m_timsFrameBaseCache
Definition timsdata.h:262
std::map< std::size_t, std::size_t > m_thousandIndexToFrameIdDescrListIndex
index to find quickly a frameId in the description list with the raw index of spectrum modulo 1000 @k...
Definition timsdata.h:281
TimsBinDec * mpa_timsBinDec
Definition timsdata.h:253
bool isDdaRun() const
tells if this MS run is a DDA run
std::size_t getTotalNumberOfScans() const
get the total number of scans
Definition timsdata.cpp:693
TimsDdaPrecursors * getTimsDdaPrecursorsPtr() const
TimsDiaSlices * mpa_timsDiaSlices
Definition timsdata.h:256
std::size_t m_frameCount
Definition timsdata.h:259
std::size_t getRawIndexFromCoordinate(std::size_t frame_id, std::size_t scan_num) const
Definition timsdata.cpp:373
std::pair< std::size_t, std::size_t > getScanCoordinatesByGlobalScanIndex(std::size_t index) const
Definition timsdata.cpp:339
SpectrumDescr getSpectrumDescrWithScanCoordinates(const std::pair< std::size_t, std::size_t > &scan_coordinates)
A simple container of DataPoint instances.
Definition trace.h:148
process interrupted exception
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< const TimsFrameBase > TimsFrameBaseCstSPtr
std::shared_ptr< TimsFrame > TimsFrameSPtr
Definition timsframe.h:42
std::shared_ptr< TimsFrameBase > TimsFrameBaseSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition msrunid.h:46
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
@ IsolationMzWidth
m/z isolation window width (left + right)
@ CollisionEnergy
Bruker's timsTOF collision energy.
@ BrukerPrecursorIndex
Bruker's timsTOF precursor index.
@ rt
Retention time.
std::shared_ptr< const TimsFrame > TimsFrameCstSPtr
Definition timsframe.h:43
main Tims data handler
handle specific data for DDA MS runs
handle specific data for DIA MS runs