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