36#include <QMutexLocker> 
   39#include <QtConcurrent> 
   49  qDebug() << 
"Start of construction of TimsData";
 
   53      throw PappsoException(QObject::tr(
"ERROR TIMS data directory %1 not found")
 
   60      throw PappsoException(QObject::tr(
"ERROR TIMS data directory, %1 sqlite file not found")
 
   68  QSqlQuery sql_query(qdb);
 
   69  if(!sql_query.exec(
"select Key, Value from GlobalMetadata;"))
 
   73      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL " 
   74                                        "command %2:\n%3\n%4\n%5")
 
   76                              .arg(sql_query.lastQuery())
 
   77                              .arg(sql_query.lastError().databaseText())
 
   78                              .arg(sql_query.lastError().driverText())
 
   79                              .arg(sql_query.lastError().nativeErrorCode()));
 
   82  while(sql_query.next())
 
   84      QSqlRecord record = sql_query.record();
 
   86        std::pair<QString, QVariant>(record.value(0).toString(), record.value(1)));
 
   90  qDebug() << 
" compression_type=" << compression_type;
 
  125  if(!sql_query.exec(
"SELECT SUM(NumScans),COUNT(Id) FROM Frames"))
 
  128      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL " 
  129                                        "command %2:\n%3\n%4\n%5")
 
  131                              .arg(sql_query.lastQuery())
 
  132                              .arg(qdb.lastError().databaseText())
 
  133                              .arg(qdb.lastError().driverText())
 
  134                              .arg(qdb.lastError().nativeErrorCode()));
 
  143  if(!sql_query.exec(
"select * from MzCalibration;"))
 
  146      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL " 
  147                                        "command %2:\n%3\n%4\n%5")
 
  149                              .arg(sql_query.lastQuery())
 
  150                              .arg(sql_query.lastError().databaseText())
 
  151                              .arg(sql_query.lastError().driverText())
 
  152                              .arg(sql_query.lastError().nativeErrorCode()));
 
  155  while(sql_query.next())
 
  157      QSqlRecord record = sql_query.record();
 
  163  if(!sql_query.exec(
"select * from TimsCalibration;"))
 
  166      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL " 
  167                                        "command %2:\n%3\n%4\n%5")
 
  169                              .arg(sql_query.lastQuery())
 
  170                              .arg(sql_query.lastError().databaseText())
 
  171                              .arg(sql_query.lastError().driverText())
 
  172                              .arg(sql_query.lastError().nativeErrorCode()));
 
  174  while(sql_query.next())
 
  176      QSqlRecord record = sql_query.record();
 
  178        std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
 
  183  if(!sql_query.exec(
"select Frames.TimsId, Frames.AccumulationTime, "         
  184                     "Frames.MzCalibration, "                                  
  185                     "Frames.T1, Frames.T2, "                                  
  186                     "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, "  
  191      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL " 
  192                                        "command %2:\n%3\n%4\n%5")
 
  194                              .arg(sql_query.lastQuery())
 
  195                              .arg(sql_query.lastError().databaseText())
 
  196                              .arg(sql_query.lastError().driverText())
 
  197                              .arg(sql_query.lastError().nativeErrorCode()));
 
  201  while(sql_query.next())
 
  203      QSqlRecord record             = sql_query.record();
 
  206      frame_record.
frame_id            = record.value(8).toULongLong();
 
  207      frame_record.
tims_offset         = record.value(0).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();
 
 
  223  QString database_connection_name = QString(
"%1_%2")
 
  225                                       .arg((quintptr)QThread::currentThread());
 
  227  QSqlDatabase qdb = QSqlDatabase::database(database_connection_name);
 
  230      qDebug() << database_connection_name;
 
  231      qdb = QSqlDatabase::addDatabase(
"QSQLITE", database_connection_name);
 
  239      throw PappsoException(QObject::tr(
"ERROR opening TIMS sqlite database file %1, database name " 
  242                              .arg(database_connection_name)
 
  243                              .arg(qdb.lastError().databaseText())
 
  244                              .arg(qdb.lastError().driverText())
 
  245                              .arg(qdb.lastError().nativeErrorCode()));
 
 
  283    qdb.exec(QString(
"SELECT Id, NumScans FROM " 
  284                     "Frames ORDER BY Id"));
 
  285  if(q.lastError().isValid())
 
  288      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL " 
  289                                        "command %2:\n%3\n%4\n%5")
 
  292                              .arg(qdb.lastError().databaseText())
 
  293                              .arg(qdb.lastError().driverText())
 
  294                              .arg(qdb.lastError().nativeErrorCode()));
 
  297  bool index_found = 
false;
 
  301  std::size_t numberScans;
 
  302  std::size_t cumulScans = 0;
 
  303  while(q.next() && (!index_found))
 
  305      timsId      = q.value(0).toULongLong();
 
  306      numberScans = q.value(1).toULongLong();
 
  314      cumulScans += numberScans;
 
 
  319std::pair<std::size_t, std::size_t>
 
  325std::pair<std::size_t, std::size_t>
 
  328  std::size_t fast_access = raw_index / 1000;
 
  329  qDebug() << 
" fast_access=" << fast_access;
 
  334        QObject::tr(
"ERROR raw index %1 not found (fast_access)").arg(raw_index));
 
  336  std::size_t start_point_index = map_it->second;
 
  337  while((start_point_index > 0) &&
 
  347          return std::pair<std::size_t, std::size_t>(
 
  352  throw ExceptionNotFound(QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
 
 
  368      if(frameDescr.m_frameId == frame_id)
 
  370          return frameDescr.m_globalScanIndex + index;
 
  374  throw ExceptionNotFound(QObject::tr(
"ERROR raw index with frame_id=%1 scan_index=%2 not found")
 
 
  393  qDebug() << 
" raw_index=" << raw_index;
 
  401      throw PappsoException(QObject::tr(
"Error TimsData::getMassSpectrumCstSPtrByRawIndex " 
  402                                        "raw_index=%1 :\n%2")
 
  404                              .arg(error.
qwhat()));
 
 
  412  qDebug() << 
" timsId=" << timsId;
 
  417      throw ExceptionNotFound(QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
 
  428      double T1_frame = frame_record.
frame_t1; 
 
  429      double T2_frame = frame_record.
frame_t2; 
 
  432      tims_frame.get()->setMzCalibrationInterfaceSPtr(
 
  437      throw ExceptionNotFound(QObject::tr(
"ERROR MzCalibration database id %1 not found")
 
  444  tims_frame.get()->setRtInSeconds(frame_record.
frame_time);
 
  445  tims_frame.get()->setMsMsType(frame_record.
msms_type);
 
  448  auto it_map_record_tims_calibration =
 
  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());
 
  468      throw ExceptionNotFound(QObject::tr(
"ERROR TimsCalibration database id %1 not found")
 
 
  475std::vector<std::size_t>
 
  479  qDebug() << 
" rt_begin=" << rt_begin << 
" rt_end=" << rt_end;
 
  482  std::vector<std::size_t> tims_frameid_list;
 
  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 " 
  490  if(q.lastError().isValid())
 
  493      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, " 
  495                                        "command %3:\n%4\n%5\n%6\nrtbegin=%7 rtend=%8")
 
  497                              .arg(qdb.databaseName())
 
  499                              .arg(qdb.lastError().databaseText())
 
  500                              .arg(qdb.lastError().driverText())
 
  501                              .arg(qdb.lastError().nativeErrorCode())
 
  508      tims_frameid_list.push_back(q.value(0).toULongLong());
 
  510  return tims_frameid_list;
 
 
  513std::vector<std::size_t>
 
  517  qDebug() << 
" rt_begin=" << rt_begin << 
" rt_end=" << rt_end;
 
  520  std::vector<std::size_t> tims_frameid_list;
 
  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 " 
  528  if(q.lastError().isValid())
 
  531      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, " 
  533                                        "command %3:\n%4\n%5\n%6")
 
  535                              .arg(qdb.databaseName())
 
  537                              .arg(qdb.lastError().databaseText())
 
  538                              .arg(qdb.lastError().driverText())
 
  539                              .arg(qdb.lastError().nativeErrorCode()));
 
  544      tims_frameid_list.push_back(q.value(0).toULongLong());
 
  546  return tims_frameid_list;
 
 
  553  qDebug() << 
" timsId=" << timsId << 
" m_mapFramesRecord.size()=" << 
m_mapFramesRecord.size();
 
  566      throw ExceptionNotFound(QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
 
  581      double T1_frame = frame_record.
frame_t1; 
 
  582      double T2_frame = frame_record.
frame_t2; 
 
  585      tims_frame.get()->setMzCalibrationInterfaceSPtr(
 
  591        QObject::tr(
"ERROR MzCalibration database id %1 not found for frame_id=%2")
 
  599  tims_frame.get()->setRtInSeconds(frame_record.
frame_time);
 
  600  tims_frame.get()->setMsMsType(frame_record.
msms_type);
 
  603  auto it_map_record_tims_calibration =
 
  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());
 
  623      throw ExceptionNotFound(QObject::tr(
"ERROR TimsCalibration database id %1 not found")
 
 
  634  qDebug() << 
" timsId=" << timsId << 
" scanNum=" << scanNum;
 
  637  return frame->getMassSpectrumCstSPtr(scanNum);
 
 
  680  return tims_frame.get()->getMsLevel();
 
 
  687                                             std::size_t global_scan_index,
 
  688                                             bool want_binary_data)
 
  692    msrun_id, mass_spectrum, global_scan_index, want_binary_data);
 
 
  698                                                    std::size_t global_scan_index,
 
  699                                                    bool want_binary_data)
 
  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));
 
  726      mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
 
  730        tims_frame.get()->getDriftTimeInMilliseconds(coordinate.second));
 
  734        tims_frame.get()->getOneOverK0Transformation(coordinate.second));
 
  740            tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
 
  741          if(mass_spectrum.
size() > 0)
 
  753      if(tims_frame.get()->getMsLevel() > 1)
 
  758              auto spectrum_descr =
 
  760              if(spectrum_descr.precursor_id > 0)
 
  768                    spectrum_descr.parent_frame, coordinate.second);
 
  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));
 
  778                                                  spectrum_descr.isolationMz);
 
  780                                                  spectrum_descr.isolationWidth);
 
  783                                                  spectrum_descr.collisionEnergy);
 
  786                    (quint64)spectrum_descr.precursor_id);
 
  793      throw PappsoException(QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex " 
  794                                        "spectrum_index=%1 :\n%2")
 
  795                              .arg(global_scan_index)
 
  796                              .arg(error.
qwhat()));
 
 
  809  using Pair     = std::pair<double, double>;
 
  810  using Map      = std::map<double, double>;
 
  811  using Iterator = Map::iterator;
 
  816    qdb.exec(QString(
"SELECT Time, SummedIntensities " 
  817                     "FROM Frames WHERE MsMsType = 0 " 
  820  if(q.lastError().isValid())
 
  823      throw PappsoException(QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, " 
  825                                        "command %3:\n%4\n%5\n%6")
 
  827                              .arg(qdb.databaseName())
 
  829                              .arg(qdb.lastError().databaseText())
 
  830                              .arg(qdb.lastError().driverText())
 
  831                              .arg(qdb.lastError().nativeErrorCode()));
 
  839      int cumulated_results = 2;
 
  841      double rt = q.value(0).toDouble(&ok);
 
  842      cumulated_results -= ok;
 
  844      double sumY = q.value(1).toDouble(&ok);
 
  845      cumulated_results -= ok;
 
  847      if(cumulated_results)
 
  850            QObject::tr(
"ERROR in TIMS sqlite database file: could not read either the " 
  851                        "retention time or the summed intensities (%1, database name " 
  854                        "command %3:\n%4\n%5\n%6")
 
  856              .arg(qdb.databaseName())
 
  858              .arg(qdb.lastError().databaseText())
 
  859              .arg(qdb.lastError().driverText())
 
  860              .arg(qdb.lastError().nativeErrorCode()));
 
  864      std::pair<Iterator, bool> res = rt_tic_map_trace.insert(Pair(rt, sumY));
 
  871          res.first->second += sumY;
 
  878  return rt_tic_map_trace.
toTrace();
 
 
  888      if(tims_frame.get()->getId() == timsId)
 
 
  910      if(tims_frame.get()->getId() == timsId)
 
 
  991  std::vector<double> timeline;
 
  995      if(frame_record.mz_calibration_id != 0)
 
  997          timeline.push_back(frame_record.frame_time);
 
 
 1012  qDebug() << 
" spectrum_index=" << index;
 
 1018  raw_spectrum.
clear();
 
 1019  tims_frame.get()->combineScansInTofIndexIntensityMap(
 
 1020    raw_spectrum, coordinate.second, coordinate.second);
 
 1021  return raw_spectrum;
 
 
 1024const std::vector<FrameIdDescr> &
 
 1030const std::vector<TimsFrameRecord> &
 
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)
 
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
 
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