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