36#include <QMutexLocker>
39#include <QtConcurrent>
49 qDebug() <<
"Start of construction of TimsData";
54 throw PappsoException(QObject::tr(
"ERROR TIMS data directory %1 not found")
61 throw PappsoException(QObject::tr(
"ERROR TIMS data directory, %1 sqlite file not found")
69 QSqlQuery sql_query(qdb);
70 if(!sql_query.exec(
"select Key, Value from GlobalMetadata;"))
74 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
75 "command %2:\n%3\n%4\n%5")
77 .arg(sql_query.lastQuery())
78 .arg(sql_query.lastError().databaseText())
79 .arg(sql_query.lastError().driverText())
80 .arg(sql_query.lastError().nativeErrorCode()));
83 while(sql_query.next())
85 QSqlRecord record = sql_query.record();
87 std::pair<QString, QVariant>(record.value(0).toString(), record.value(1)));
91 qDebug() <<
" compression_type=" << compression_type;
126 if(!sql_query.exec(
"SELECT SUM(NumScans),COUNT(Id) FROM Frames"))
129 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
130 "command %2:\n%3\n%4\n%5")
132 .arg(sql_query.lastQuery())
133 .arg(qdb.lastError().databaseText())
134 .arg(qdb.lastError().driverText())
135 .arg(qdb.lastError().nativeErrorCode()));
144 if(!sql_query.exec(
"select * from MzCalibration;"))
147 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
148 "command %2:\n%3\n%4\n%5")
150 .arg(sql_query.lastQuery())
151 .arg(sql_query.lastError().databaseText())
152 .arg(sql_query.lastError().driverText())
153 .arg(sql_query.lastError().nativeErrorCode()));
156 while(sql_query.next())
158 QSqlRecord record = sql_query.record();
164 if(!sql_query.exec(
"select * from TimsCalibration;"))
167 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
168 "command %2:\n%3\n%4\n%5")
170 .arg(sql_query.lastQuery())
171 .arg(sql_query.lastError().databaseText())
172 .arg(sql_query.lastError().driverText())
173 .arg(sql_query.lastError().nativeErrorCode()));
175 while(sql_query.next())
177 QSqlRecord record = sql_query.record();
179 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
184 if(!sql_query.exec(
"select Frames.TimsId, Frames.AccumulationTime, "
185 "Frames.MzCalibration, "
186 "Frames.T1, Frames.T2, "
187 "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, "
192 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
193 "command %2:\n%3\n%4\n%5")
195 .arg(sql_query.lastQuery())
196 .arg(sql_query.lastError().databaseText())
197 .arg(sql_query.lastError().driverText())
198 .arg(sql_query.lastError().nativeErrorCode()));
202 while(sql_query.next())
204 QSqlRecord record = sql_query.record();
207 frame_record.
frame_id = record.value(8).toULongLong();
208 frame_record.
tims_offset = record.value(0).toULongLong();
211 frame_record.
frame_t1 = record.value(3).toDouble();
212 frame_record.
frame_t2 = record.value(4).toDouble();
213 frame_record.
frame_time = record.value(5).toDouble();
214 frame_record.
msms_type = record.value(6).toInt();
224 QString database_connection_name = QString(
"%1_%2")
226 .arg((quintptr)QThread::currentThread());
228 QSqlDatabase qdb = QSqlDatabase::database(database_connection_name);
231 qDebug() << database_connection_name;
232 qdb = QSqlDatabase::addDatabase(
"QSQLITE", database_connection_name);
240 throw PappsoException(QObject::tr(
"ERROR opening TIMS sqlite database file %1, database name "
243 .arg(database_connection_name)
244 .arg(qdb.lastError().databaseText())
245 .arg(qdb.lastError().driverText())
246 .arg(qdb.lastError().nativeErrorCode()));
285 QString(
"SELECT Id, NumScans FROM "
286 "Frames ORDER BY Id"));
288 if(q.lastError().isValid())
291 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
292 "command %2:\n%3\n%4\n%5")
295 .arg(qdb.lastError().databaseText())
296 .arg(qdb.lastError().driverText())
297 .arg(qdb.lastError().nativeErrorCode()));
300 bool index_found =
false;
304 std::size_t numberScans;
305 std::size_t cumulScans = 0;
306 while(q.next() && (!index_found))
308 timsId = q.value(0).toULongLong();
309 numberScans = q.value(1).toULongLong();
317 cumulScans += numberScans;
322std::pair<std::size_t, std::size_t>
328std::pair<std::size_t, std::size_t>
331 std::size_t fast_access = raw_index / 1000;
332 qDebug() <<
" fast_access=" << fast_access;
337 QObject::tr(
"ERROR raw index %1 not found (fast_access)").arg(raw_index));
339 std::size_t start_point_index = map_it->second;
340 while((start_point_index > 0) &&
350 return std::pair<std::size_t, std::size_t>(
355 throw ExceptionNotFound(QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
371 if(frameDescr.m_frameId == frame_id)
373 return frameDescr.m_globalScanIndex + index;
377 throw ExceptionNotFound(QObject::tr(
"ERROR raw index with frame_id=%1 scan_index=%2 not found")
396 qDebug() <<
" raw_index=" << raw_index;
404 throw PappsoException(QObject::tr(
"Error TimsData::getMassSpectrumCstSPtrByRawIndex "
405 "raw_index=%1 :\n%2")
407 .arg(error.
qwhat()));
415 qDebug() <<
" timsId=" << timsId;
420 throw ExceptionNotFound(QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
431 double T1_frame = frame_record.
frame_t1;
432 double T2_frame = frame_record.
frame_t2;
435 tims_frame.get()->setMzCalibrationInterfaceSPtr(
440 throw ExceptionNotFound(QObject::tr(
"ERROR MzCalibration database id %1 not found")
447 tims_frame.get()->setRtInSeconds(frame_record.
frame_time);
448 tims_frame.get()->setMsMsType(frame_record.
msms_type);
451 auto it_map_record_tims_calibration =
456 tims_frame.get()->setTimsCalibration(
457 it_map_record_tims_calibration->second.value(1).toInt(),
458 it_map_record_tims_calibration->second.value(2).toDouble(),
459 it_map_record_tims_calibration->second.value(3).toDouble(),
460 it_map_record_tims_calibration->second.value(4).toDouble(),
461 it_map_record_tims_calibration->second.value(5).toDouble(),
462 it_map_record_tims_calibration->second.value(6).toDouble(),
463 it_map_record_tims_calibration->second.value(7).toDouble(),
464 it_map_record_tims_calibration->second.value(8).toDouble(),
465 it_map_record_tims_calibration->second.value(9).toDouble(),
466 it_map_record_tims_calibration->second.value(10).toDouble(),
467 it_map_record_tims_calibration->second.value(11).toDouble());
471 throw ExceptionNotFound(QObject::tr(
"ERROR TimsCalibration database id %1 not found")
478std::vector<std::size_t>
482 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
485 std::vector<std::size_t> tims_frameid_list;
488 q.prepare(QString(
"SELECT Frames.Id FROM Frames WHERE "
489 "Frames.MsMsType=0 AND (Frames.Time>=%1) "
490 "AND (Frames.Time<=%2) ORDER BY "
495 if(q.lastError().isValid())
498 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
500 "command %3:\n%4\n%5\n%6\nrtbegin=%7 rtend=%8")
502 .arg(qdb.databaseName())
504 .arg(qdb.lastError().databaseText())
505 .arg(qdb.lastError().driverText())
506 .arg(qdb.lastError().nativeErrorCode())
513 tims_frameid_list.push_back(q.value(0).toULongLong());
515 return tims_frameid_list;
518std::vector<std::size_t>
522 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
525 std::vector<std::size_t> tims_frameid_list;
529 q.prepare(QString(
"SELECT Frames.Id FROM Frames WHERE "
530 "Frames.MsMsType=8 AND "
531 "(Frames.Time>=%1) AND (Frames.Time<=%2) ORDER BY "
536 if(q.lastError().isValid())
539 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
541 "command %3:\n%4\n%5\n%6")
543 .arg(qdb.databaseName())
545 .arg(qdb.lastError().databaseText())
546 .arg(qdb.lastError().driverText())
547 .arg(qdb.lastError().nativeErrorCode()));
552 tims_frameid_list.push_back(q.value(0).toULongLong());
554 return tims_frameid_list;
561 qDebug() <<
" timsId=" << timsId <<
" m_mapFramesRecord.size()=" <<
m_mapFramesRecord.size();
574 throw ExceptionNotFound(QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
589 double T1_frame = frame_record.
frame_t1;
590 double T2_frame = frame_record.
frame_t2;
593 tims_frame.get()->setMzCalibrationInterfaceSPtr(
599 QObject::tr(
"ERROR MzCalibration database id %1 not found for frame_id=%2")
607 tims_frame.get()->setRtInSeconds(frame_record.
frame_time);
608 tims_frame.get()->setMsMsType(frame_record.
msms_type);
611 auto it_map_record_tims_calibration =
616 tims_frame.get()->setTimsCalibration(
617 it_map_record_tims_calibration->second.value(1).toInt(),
618 it_map_record_tims_calibration->second.value(2).toDouble(),
619 it_map_record_tims_calibration->second.value(3).toDouble(),
620 it_map_record_tims_calibration->second.value(4).toDouble(),
621 it_map_record_tims_calibration->second.value(5).toDouble(),
622 it_map_record_tims_calibration->second.value(6).toDouble(),
623 it_map_record_tims_calibration->second.value(7).toDouble(),
624 it_map_record_tims_calibration->second.value(8).toDouble(),
625 it_map_record_tims_calibration->second.value(9).toDouble(),
626 it_map_record_tims_calibration->second.value(10).toDouble(),
627 it_map_record_tims_calibration->second.value(11).toDouble());
631 throw ExceptionNotFound(QObject::tr(
"ERROR TimsCalibration database id %1 not found")
642 qDebug() <<
" timsId=" << timsId <<
" scanNum=" << scanNum;
645 return frame->getMassSpectrumCstSPtr(scanNum);
688 return tims_frame.get()->getMsLevel();
695 std::size_t global_scan_index,
696 bool want_binary_data)
700 msrun_id, mass_spectrum, global_scan_index, want_binary_data);
706 std::size_t global_scan_index,
707 bool want_binary_data)
727 spectrum_id.
setNativeId(QString(
"frame_id=%1 scan_index=%2 global_scan_index=%3")
728 .arg(coordinate.first)
729 .arg(coordinate.second)
730 .arg(global_scan_index));
734 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
738 tims_frame.get()->getDriftTimeInMilliseconds(coordinate.second));
742 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
748 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
749 if(mass_spectrum.
size() > 0)
761 if(tims_frame.get()->getMsLevel() > 1)
766 auto spectrum_descr =
768 if(spectrum_descr.precursor_id > 0)
776 spectrum_descr.parent_frame, coordinate.second);
780 QString(
"frame_id=%1 scan_index=%2 global_scan_index=%3")
781 .arg(spectrum_descr.parent_frame)
782 .arg(coordinate.second)
783 .arg(prec_spectrum_index));
786 spectrum_descr.isolationMz);
788 spectrum_descr.isolationWidth);
791 spectrum_descr.collisionEnergy);
794 (quint64)spectrum_descr.precursor_id);
801 throw PappsoException(QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex "
802 "spectrum_index=%1 :\n%2")
803 .arg(global_scan_index)
804 .arg(error.
qwhat()));
817 using Pair = std::pair<double, double>;
818 using Map = std::map<double, double>;
819 using Iterator = Map::iterator;
826 QString(
"SELECT Time, SummedIntensities "
827 "FROM Frames WHERE MsMsType = 0 "
831 if(q.lastError().isValid())
834 throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
836 "command %3:\n%4\n%5\n%6")
838 .arg(qdb.databaseName())
840 .arg(qdb.lastError().databaseText())
841 .arg(qdb.lastError().driverText())
842 .arg(qdb.lastError().nativeErrorCode()));
850 int cumulated_results = 2;
852 double rt = q.value(0).toDouble(&ok);
853 cumulated_results -= ok;
855 double sumY = q.value(1).toDouble(&ok);
856 cumulated_results -= ok;
858 if(cumulated_results)
861 QObject::tr(
"ERROR in TIMS sqlite database file: could not read either the "
862 "retention time or the summed intensities (%1, database name "
865 "command %3:\n%4\n%5\n%6")
867 .arg(qdb.databaseName())
869 .arg(qdb.lastError().databaseText())
870 .arg(qdb.lastError().driverText())
871 .arg(qdb.lastError().nativeErrorCode()));
875 std::pair<Iterator, bool> res = rt_tic_map_trace.insert(Pair(rt, sumY));
882 res.first->second += sumY;
889 return rt_tic_map_trace.
toTrace();
899 if(tims_frame.get()->getId() == timsId)
921 if(tims_frame.get()->getId() == timsId)
1002 std::vector<double> timeline;
1006 if(frame_record.mz_calibration_id != 0)
1008 timeline.push_back(frame_record.frame_time);
1023 qDebug() <<
" spectrum_index=" << index;
1029 raw_spectrum.
clear();
1030 tims_frame.get()->combineScansInTofIndexIntensityMap(
1031 raw_spectrum, coordinate.second, coordinate.second);
1032 return raw_spectrum;
1035const std::vector<FrameIdDescr> &
1041const std::vector<TimsFrameRecord> &
1080 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
std::map< QString, QVariant > m_mapGlobalMetadaTable
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
const QVariant & getGlobalMetadataValue(const QString &key) const
SpectrumDescr getSpectrumDescrWithScanCoordinates(const std::pair< std::size_t, std::size_t > &scan_coordinates)
A simple container of DataPoint instances.
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