36#include <QMutexLocker>
39#include <QtConcurrent>
47 : m_timsDataDirectory(timsDataDirectory)
50 qDebug() <<
"Start of construction of TimsData";
55 QObject::tr(
"ERROR TIMS data directory %1 not found")
63 QObject::tr(
"ERROR TIMS data directory, %1 sqlite file not found")
72 if(!q.exec(
"select Key, Value from GlobalMetadata where "
73 "Key='TimsCompressionType';"))
78 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
79 "command %2:\n%3\n%4\n%5")
82 .arg(q.lastError().databaseText())
83 .arg(q.lastError().driverText())
84 .arg(q.lastError().nativeErrorCode()));
88 int compression_type = 0;
91 compression_type = q.value(1).toInt();
93 qDebug() <<
" compression_type=" << compression_type;
129 if(!q.exec(
"SELECT SUM(NumScans),COUNT(Id) FROM Frames"))
133 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
134 "command %2:\n%3\n%4\n%5")
137 .arg(qdb.lastError().databaseText())
138 .arg(qdb.lastError().driverText())
139 .arg(qdb.lastError().nativeErrorCode()));
148 if(!q.exec(
"select * from MzCalibration;"))
152 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
153 "command %2:\n%3\n%4\n%5")
156 .arg(q.lastError().databaseText())
157 .arg(q.lastError().driverText())
158 .arg(q.lastError().nativeErrorCode()));
163 QSqlRecord record = q.record();
165 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
170 if(!q.exec(
"select * from TimsCalibration;"))
174 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
175 "command %2:\n%3\n%4\n%5")
178 .arg(q.lastError().databaseText())
179 .arg(q.lastError().driverText())
180 .arg(q.lastError().nativeErrorCode()));
184 QSqlRecord record = q.record();
186 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
191 if(!q.exec(
"select Frames.TimsId, Frames.AccumulationTime, "
192 "Frames.MzCalibration, "
193 "Frames.T1, Frames.T2, "
194 "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, "
200 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
201 "command %2:\n%3\n%4\n%5")
204 .arg(q.lastError().databaseText())
205 .arg(q.lastError().driverText())
206 .arg(q.lastError().nativeErrorCode()));
212 QSqlRecord record = q.record();
216 frame_record.
frame_id = record.value(8).toULongLong();
217 frame_record.
tims_offset = record.value(0).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();
233 QString database_connection_name = QString(
"%1_%2")
235 .arg((quintptr)QThread::currentThread());
237 QSqlDatabase qdb = QSqlDatabase::database(database_connection_name);
240 qDebug() << database_connection_name;
241 qdb = QSqlDatabase::addDatabase(
"QSQLITE", database_connection_name);
250 QObject::tr(
"ERROR opening TIMS sqlite database file %1, database name "
253 .arg(database_connection_name)
254 .arg(qdb.lastError().databaseText())
255 .arg(qdb.lastError().driverText())
256 .arg(qdb.lastError().nativeErrorCode()));
294 qdb.exec(QString(
"SELECT Id, NumScans FROM "
295 "Frames ORDER BY Id"));
296 if(q.lastError().isValid())
300 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
301 "command %2:\n%3\n%4\n%5")
304 .arg(qdb.lastError().databaseText())
305 .arg(qdb.lastError().driverText())
306 .arg(qdb.lastError().nativeErrorCode()));
309 bool index_found =
false;
313 std::size_t numberScans;
314 std::size_t cumulScans = 0;
315 while(q.next() && (!index_found))
317 timsId = q.value(0).toULongLong();
318 numberScans = q.value(1).toULongLong();
323 std::pair<std::size_t, std::size_t>((cumulScans / 1000),
327 cumulScans += numberScans;
332std::pair<std::size_t, std::size_t>
338std::pair<std::size_t, std::size_t>
341 std::size_t fast_access = raw_index / 1000;
342 qDebug() <<
" fast_access=" << fast_access;
347 QObject::tr(
"ERROR raw index %1 not found (fast_access)")
350 std::size_t start_point_index = map_it->second;
351 while((start_point_index > 0) &&
362 return std::pair<std::size_t, std::size_t>(
369 QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
374 std::size_t index)
const
382 std::size_t index)
const
387 if(frameDescr.m_frameId == frame_id)
389 return frameDescr.m_globalScanIndex + index;
394 QObject::tr(
"ERROR raw index with frame_id=%1 scan_index=%2 not found")
413 qDebug() <<
" raw_index=" << raw_index;
422 QObject::tr(
"Error TimsData::getMassSpectrumCstSPtrByRawIndex "
423 "raw_index=%1 :\n%2")
425 .arg(error.
qwhat()));
433 qDebug() <<
" timsId=" << timsId;
439 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
444 tims_frame = std::make_shared<TimsFrameBase>(
452 double T1_frame = frame_record.
frame_t1;
453 double T2_frame = frame_record.
frame_t2;
456 tims_frame.get()->setMzCalibrationInterfaceSPtr(
458 T1_frame, T2_frame, it_map_record->second));
463 QObject::tr(
"ERROR MzCalibration database id %1 not found")
468 tims_frame.get()->setAcqDurationInMilliseconds(
471 tims_frame.get()->setRtInSeconds(frame_record.
frame_time);
472 tims_frame.get()->setMsMsType(frame_record.
msms_type);
475 auto it_map_record_tims_calibration =
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());
496 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
503std::vector<std::size_t>
507 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
510 std::vector<std::size_t> tims_frameid_list;
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 "
518 if(q.lastError().isValid())
522 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
524 "command %3:\n%4\n%5\n%6")
526 .arg(qdb.databaseName())
528 .arg(qdb.lastError().databaseText())
529 .arg(qdb.lastError().driverText())
530 .arg(qdb.lastError().nativeErrorCode()));
535 tims_frameid_list.push_back(q.value(0).toULongLong());
537 return tims_frameid_list;
540std::vector<std::size_t>
544 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
547 std::vector<std::size_t> tims_frameid_list;
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 "
556 if(q.lastError().isValid())
560 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
562 "command %3:\n%4\n%5\n%6")
564 .arg(qdb.databaseName())
566 .arg(qdb.lastError().databaseText())
567 .arg(qdb.lastError().driverText())
568 .arg(qdb.lastError().nativeErrorCode()));
573 tims_frameid_list.push_back(q.value(0).toULongLong());
575 return tims_frameid_list;
582 qDebug() <<
" timsId=" << timsId
597 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
614 double T1_frame = frame_record.
frame_t1;
615 double T2_frame = frame_record.
frame_t2;
618 tims_frame.get()->setMzCalibrationInterfaceSPtr(
620 T1_frame, T2_frame, it_map_record->second));
626 "ERROR MzCalibration database id %1 not found for frame_id=%2")
632 tims_frame.get()->setAcqDurationInMilliseconds(
635 tims_frame.get()->setRtInSeconds(frame_record.
frame_time);
636 tims_frame.get()->setMsMsType(frame_record.
msms_type);
639 auto it_map_record_tims_calibration =
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());
660 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
671 qDebug() <<
" timsId=" << timsId <<
" scanNum=" << scanNum;
674 return frame->getMassSpectrumCstSPtr(scanNum);
717 return tims_frame.get()->getMsLevel();
725 std::size_t global_scan_index,
726 bool want_binary_data)
730 msrun_id, mass_spectrum, global_scan_index, want_binary_data);
737 std::size_t global_scan_index,
738 bool want_binary_data)
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));
766 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
770 tims_frame.get()->getDriftTimeInMilliseconds(coordinate.second));
774 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
780 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
781 if(mass_spectrum.
size() > 0)
793 if(tims_frame.get()->getMsLevel() > 1)
798 auto spectrum_descr =
801 if(spectrum_descr.precursor_id > 0)
805 spectrum_descr.precursor_ion_data);
809 std::size_t prec_spectrum_index =
811 spectrum_descr.parent_frame, coordinate.second);
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));
822 spectrum_descr.isolationMz);
825 spectrum_descr.isolationWidth);
829 spectrum_descr.collisionEnergy);
832 (quint64)spectrum_descr.precursor_id);
840 QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex "
841 "spectrum_index=%1 :\n%2")
842 .arg(global_scan_index)
843 .arg(error.
qwhat()));
856 using Pair = std::pair<double, double>;
857 using Map = std::map<double, double>;
858 using Iterator = Map::iterator;
863 qdb.exec(QString(
"SELECT Time, SummedIntensities "
864 "FROM Frames WHERE MsMsType = 0 "
867 if(q.lastError().isValid())
871 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
873 "command %3:\n%4\n%5\n%6")
875 .arg(qdb.databaseName())
877 .arg(qdb.lastError().databaseText())
878 .arg(qdb.lastError().driverText())
879 .arg(qdb.lastError().nativeErrorCode()));
887 int cumulated_results = 2;
889 double rt = q.value(0).toDouble(&ok);
890 cumulated_results -= ok;
892 double sumY = q.value(1).toDouble(&ok);
893 cumulated_results -= ok;
895 if(cumulated_results)
899 "ERROR in TIMS sqlite database file: could not read either the "
900 "retention time or the summed intensities (%1, database name "
903 "command %3:\n%4\n%5\n%6")
905 .arg(qdb.databaseName())
907 .arg(qdb.lastError().databaseText())
908 .arg(qdb.lastError().driverText())
909 .arg(qdb.lastError().nativeErrorCode()));
913 std::pair<Iterator, bool>
res = rt_tic_map_trace.insert(Pair(
rt, sumY));
920 res.first->second += sumY;
927 return rt_tic_map_trace.
toTrace();
937 if(tims_frame.get()->getId() == timsId)
959 if(tims_frame.get()->getId() == timsId)
1040 std::vector<double> timeline;
1044 if(frame_record.mz_calibration_id != 0)
1046 timeline.push_back(frame_record.frame_time);
1061 qDebug() <<
" spectrum_index=" << index;
1067 raw_spectrum.
clear();
1068 tims_frame.get()->combineScansInTofIndexIntensityMap(
1069 raw_spectrum, coordinate.second, coordinate.second);
1070 return raw_spectrum;
1073const std::vector<FrameIdDescr> &
1079const std::vector<TimsFrameRecord> &
1118 if(mpa_timsDiaSlices ==
nullptr)
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)
static TimsDataFastMap & getTimsDataFastMapInstance()
std::size_t getGlobalScanIndexByScanCoordinates(std::size_t frame_id, std::size_t index) const
QSqlDatabase openDatabaseConnection() const
std::size_t getTotalScanCount() const
TimsDiaSlices * getTimsDiaSlicesPtr() const
TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId)
get a Tims frame with his database ID
TimsDdaPrecursors * mpa_timsDdaPrecursors
const std::vector< TimsFrameRecord > & getTimsFrameRecordList() const
std::vector< FrameIdDescr > m_frameIdDescrList
store every frame id and corresponding sizes
TimsFrameCstSPtr getTimsFrameCstSPtrCached(std::size_t timsId)
get a Tims frame with his database ID but look in the cache first
pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtrByRawIndex(std::size_t raw_index)
get a mass spectrum given its spectrum index
TimsDataFastMap & getScanByGlobalScanIndex(std::size_t index)
const std::vector< FrameIdDescr > & getFrameIdDescrList() const
std::size_t getTotalNumberOfFrames() const
Get total number of frames.
std::vector< std::size_t > getTimsMS1FrameIdsInRtRange(double rt_begin, double rt_end) const
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
TimsBinDec * getTimsBinDecPtr() const
TimsData(QDir timsDataDirectory)
build using the tims data directory
void getQualifiedMassSpectrumByRawIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t global_scan_index, bool want_binary_data)
std::vector< std::size_t > getTimsMS2FrameIdsInRtRange(double rt_begin, double rt_end) const
Trace getTicChromatogram() const
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId)
void getQualifiedMassSpectrumByGlobalScanIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t global_scan_index, bool want_binary_data)
std::deque< TimsFrameCstSPtr > m_timsFrameCache
std::pair< std::size_t, std::size_t > getScanCoordinateFromRawIndex(std::size_t spectrum_index) const
std::vector< TimsFrameRecord > m_mapFramesRecord
unsigned int getMsLevelBySpectrumIndex(std::size_t index)
std::size_t m_totalScanCount
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
std::map< int, QSqlRecord > m_mapTimsCalibrationRecord
void fillFrameIdDescrList()
private function to fill m_frameIdDescrList
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId)
get a Tims frame base (no binary data file access) with his database ID
unsigned int getMsLevelByGlobalScanIndex(std::size_t index)
MzCalibrationStore * mpa_mzCalibrationStore
pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtrByGlobalScanIndex(std::size_t index)
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
bool isDiaRun() const
tells if this MS run is a DIA run
std::deque< TimsFrameBaseCstSPtr > m_timsFrameBaseCache
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...
TimsBinDec * mpa_timsBinDec
bool isDdaRun() const
tells if this MS run is a DDA run
std::size_t getTotalNumberOfScans() const
get the total number of scans
TimsDdaPrecursors * getTimsDdaPrecursorsPtr() const
TimsDiaSlices * mpa_timsDiaSlices
std::size_t getRawIndexFromCoordinate(std::size_t frame_id, std::size_t scan_num) const
std::pair< std::size_t, std::size_t > getScanCoordinatesByGlobalScanIndex(std::size_t index) const
SpectrumDescr getSpectrumDescrWithScanCoordinates(const std::pair< std::size_t, std::size_t > &scan_coordinates)
A simple container of DataPoint instances.
process interrupted exception
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
std::shared_ptr< const TimsFrameBase > TimsFrameBaseCstSPtr
std::shared_ptr< TimsFrame > TimsFrameSPtr
std::shared_ptr< TimsFrameBase > TimsFrameBaseSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
@ IsolationMzWidth
m/z isolation window width (left + right)
@ CollisionEnergy
Bruker's timsTOF collision energy.
@ IsolationMz
Isolation m/z value.
@ BrukerPrecursorIndex
Bruker's timsTOF precursor index.
@ IonMobOneOverK0
1/kO value
std::shared_ptr< const TimsFrame > TimsFrameCstSPtr
std::size_t mz_calibration_id
std::size_t tims_calibration_id
handle specific data for DDA MS runs
handle specific data for DIA MS runs