29 #include "../../exception/exceptionnotfound.h"
30 #include "../../exception/exceptioninterrupted.h"
31 #include "../../processing/combiners/tracepluscombiner.h"
32 #include "../../processing/filters/filtertriangle.h"
33 #include "../../processing/filters/filterpass.h"
34 #include "../../processing/filters/filtersuitestring.h"
41 #include <QMutexLocker>
49 : m_timsDataDirectory(timsDataDirectory)
52 qDebug() <<
"Start of construction of TimsData";
57 QObject::tr(
"ERROR TIMS data directory %1 not found")
65 QObject::tr(
"ERROR TIMS data directory, %1 sqlite file not found")
74 if(!q.exec(
"select Key, Value from GlobalMetadata where "
75 "Key='TimsCompressionType';"))
80 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
81 "command %2:\n%3\n%4\n%5")
84 .arg(q.lastError().databaseText())
85 .arg(q.lastError().driverText())
86 .arg(q.lastError().nativeErrorCode()));
90 int compression_type = 0;
93 compression_type = q.value(1).toInt();
95 qDebug() <<
" compression_type=" << compression_type;
103 if(!q.exec(
"SELECT COUNT( DISTINCT Id) FROM Precursors;"))
107 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
108 "command %2:\n%3\n%4\n%5")
111 .arg(qdb.lastError().databaseText())
112 .arg(qdb.lastError().driverText())
113 .arg(qdb.lastError().nativeErrorCode()));
124 if(!q.exec(
"SELECT SUM(NumScans) FROM Frames"))
128 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
129 "command %2:\n%3\n%4\n%5")
132 .arg(qdb.lastError().databaseText())
133 .arg(qdb.lastError().driverText())
134 .arg(qdb.lastError().nativeErrorCode()));
141 if(!q.exec(
"select * from MzCalibration;"))
145 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
146 "command %2:\n%3\n%4\n%5")
149 .arg(q.lastError().databaseText())
150 .arg(q.lastError().driverText())
151 .arg(q.lastError().nativeErrorCode()));
156 QSqlRecord record = q.record();
158 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
163 if(!q.exec(
"select * from TimsCalibration;"))
167 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
168 "command %2:\n%3\n%4\n%5")
171 .arg(q.lastError().databaseText())
172 .arg(q.lastError().driverText())
173 .arg(q.lastError().nativeErrorCode()));
177 QSqlRecord record = q.record();
179 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
184 if(!q.exec(
"select Frames.TimsId, Frames.AccumulationTime, "
185 "Frames.MzCalibration, "
186 "Frames.T1, Frames.T2, "
187 "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, "
193 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
194 "command %2:\n%3\n%4\n%5")
197 .arg(q.lastError().databaseText())
198 .arg(q.lastError().driverText())
199 .arg(q.lastError().nativeErrorCode()));
203 QSqlRecord record = q.record();
205 record.value(8).toULongLong(), record));
209 "chargeDeconvolution|0.02dalton mzExclusion|0.01dalton");
212 std::shared_ptr<FilterTriangle> ms1filter =
213 std::make_shared<FilterTriangle>();
214 ms1filter.get()->setTriangleSlope(50, 0.01);
222 QString database_connection_name = QString(
"%1_%2")
224 .arg((quintptr)QThread::currentThread());
226 QSqlDatabase qdb = QSqlDatabase::database(database_connection_name);
229 qDebug() << database_connection_name;
230 qdb = QSqlDatabase::addDatabase(
"QSQLITE", database_connection_name);
239 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()));
287 qdb.exec(QString(
"SELECT Id, NumScans FROM "
288 "Frames ORDER BY Id"));
289 if(q.lastError().isValid())
293 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
294 "command %2:\n%3\n%4\n%5")
297 .arg(qdb.lastError().databaseText())
298 .arg(qdb.lastError().driverText())
299 .arg(qdb.lastError().nativeErrorCode()));
302 bool index_found =
false;
306 std::size_t numberScans;
307 std::size_t cumulScans = 0;
308 while(q.next() && (!index_found))
310 timsId = q.value(0).toULongLong();
311 numberScans = q.value(1).toULongLong();
316 std::pair<std::size_t, std::size_t>((cumulScans / 1000),
320 cumulScans += numberScans;
325 std::pair<std::size_t, std::size_t>
329 std::size_t fast_access = raw_index / 1000;
330 qDebug() <<
" fast_access=" << fast_access;
335 QObject::tr(
"ERROR raw index %1 not found (fast_access)")
338 std::size_t start_point_index = map_it->second;
339 while((start_point_index > 0) &&
350 return std::pair<std::size_t, std::size_t>(
357 QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
363 std::size_t scan_num)
const
368 if(frameDescr.m_frameId == frame_id)
370 return frameDescr.m_cumulSize + scan_num;
375 QObject::tr(
"ERROR raw index with frame=%1 scan=%2 not found")
388 qDebug() <<
" raw_index=" << raw_index;
398 "Error TimsData::getMassSpectrumCstSPtrByRawIndex raw_index=%1 :\n%2")
400 .arg(error.
qwhat()));
409 qDebug() <<
" timsId=" << timsId;
415 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
420 tims_frame = std::make_shared<TimsFrameBase>(
421 TimsFrameBase(timsId, it_map_record_frames->second.value(0).toULongLong()));
424 it_map_record_frames->second.value(2).toInt());
429 it_map_record_frames->second.value(3).toDouble();
431 it_map_record_frames->second.value(4).toDouble();
434 tims_frame.get()->setMzCalibrationInterfaceSPtr(
436 T1_frame, T2_frame, it_map_record->second));
441 QObject::tr(
"ERROR MzCalibration database id %1 not found")
442 .arg(it_map_record_frames->second.value(2).toInt()));
445 tims_frame.get()->setAccumulationTime(
446 it_map_record_frames->second.value(1).toDouble());
448 tims_frame.get()->setTime(it_map_record_frames->second.value(5).toDouble());
449 tims_frame.get()->setMsMsType(it_map_record_frames->second.value(6).toUInt());
453 it_map_record_frames->second.value(7).toInt());
457 tims_frame.get()->setTimsCalibration(
458 it_map_record_tims_calibration->second.value(1).toInt(),
459 it_map_record_tims_calibration->second.value(2).toDouble(),
460 it_map_record_tims_calibration->second.value(3).toDouble(),
461 it_map_record_tims_calibration->second.value(4).toDouble(),
462 it_map_record_tims_calibration->second.value(5).toDouble(),
463 it_map_record_tims_calibration->second.value(6).toDouble(),
464 it_map_record_tims_calibration->second.value(7).toDouble(),
465 it_map_record_tims_calibration->second.value(8).toDouble(),
466 it_map_record_tims_calibration->second.value(9).toDouble(),
467 it_map_record_tims_calibration->second.value(10).toDouble(),
468 it_map_record_tims_calibration->second.value(11).toDouble());
473 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
474 .arg(it_map_record_frames->second.value(7).toInt()));
480 std::vector<std::size_t>
484 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
487 std::vector<std::size_t> tims_frameid_list;
489 QSqlQuery q = qdb.exec(QString(
"SELECT Frames.Id FROM Frames WHERE "
490 "Frames.MsMsType=0 AND (Frames.Time>=%1) AND "
491 "(Frames.Time<=%2) ORDER BY Frames.Time;")
494 if(q.lastError().isValid())
498 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
500 "command %3:\n%4\n%5\n%6")
502 .arg(qdb.databaseName())
504 .arg(qdb.lastError().databaseText())
505 .arg(qdb.lastError().driverText())
506 .arg(qdb.lastError().nativeErrorCode()));
511 tims_frameid_list.push_back(q.value(0).toULongLong());
513 return tims_frameid_list;
520 qDebug() <<
" timsId=" << timsId
537 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
545 timsId, it_map_record_frames->second.value(0).toULongLong());
549 it_map_record_frames->second.value(2).toInt());
554 it_map_record_frames->second.value(3).toDouble();
556 it_map_record_frames->second.value(4).toDouble();
559 tims_frame.get()->setMzCalibrationInterfaceSPtr(
561 T1_frame, T2_frame, it_map_record->second));
566 QObject::tr(
"ERROR MzCalibration database id %1 not found")
567 .arg(it_map_record_frames->second.value(2).toInt()));
570 tims_frame.get()->setAccumulationTime(
571 it_map_record_frames->second.value(1).toDouble());
573 tims_frame.get()->setTime(it_map_record_frames->second.value(5).toDouble());
574 tims_frame.get()->setMsMsType(it_map_record_frames->second.value(6).toUInt());
578 it_map_record_frames->second.value(7).toInt());
582 tims_frame.get()->setTimsCalibration(
583 it_map_record_tims_calibration->second.value(1).toInt(),
584 it_map_record_tims_calibration->second.value(2).toDouble(),
585 it_map_record_tims_calibration->second.value(3).toDouble(),
586 it_map_record_tims_calibration->second.value(4).toDouble(),
587 it_map_record_tims_calibration->second.value(5).toDouble(),
588 it_map_record_tims_calibration->second.value(6).toDouble(),
589 it_map_record_tims_calibration->second.value(7).toDouble(),
590 it_map_record_tims_calibration->second.value(8).toDouble(),
591 it_map_record_tims_calibration->second.value(9).toDouble(),
592 it_map_record_tims_calibration->second.value(10).toDouble(),
593 it_map_record_tims_calibration->second.value(11).toDouble());
598 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
599 .arg(it_map_record_frames->second.value(7).toInt()));
609 qDebug() <<
" timsId=" << timsId <<
" scanNum=" << scanNum;
612 return frame->getMassSpectrumCstSPtr(scanNum);
628 std::vector<std::size_t>
634 std::vector<std::size_t> precursor_ids;
635 std::vector<std::vector<double>> ids;
638 QSqlQuery q = qdb.exec(
640 "SELECT Frames.Time, Precursors.MonoisotopicMz, Precursors.Charge, "
641 "Precursors.Id, Frames.Id, PasefFrameMsMsInfo.ScanNumBegin, "
642 "PasefFrameMsMsInfo.scanNumEnd "
644 "INNER JOIN PasefFrameMsMsInfo ON Frames.Id = PasefFrameMsMsInfo.Frame "
645 "INNER JOIN Precursors ON PasefFrameMsMsInfo.Precursor = Precursors.Id "
646 "WHERE Precursors.Charge == %1 "
647 "AND Precursors.MonoisotopicMz > %2 -0.01 "
648 "AND Precursors.MonoisotopicMz < %2 +0.01 "
649 "AND Frames.Time >= %3 -1 "
650 "AND Frames.Time < %3 +1; ")
654 if(q.lastError().isValid())
658 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
660 "command %3:\n%4\n%5\n%6")
662 .arg(qdb.databaseName())
664 .arg(qdb.lastError().databaseText())
665 .arg(qdb.lastError().driverText())
666 .arg(qdb.lastError().nativeErrorCode()));
673 std::vector<double> sql_values;
674 sql_values.push_back(q.value(4).toDouble());
675 sql_values.push_back(q.value(3).toDouble());
676 sql_values.push_back(q.value(5).toDouble());
677 sql_values.push_back(q.value(6).toDouble());
678 sql_values.push_back(q.value(1).toDouble());
680 ids.push_back(sql_values);
683 if(std::find(precursor_ids.begin(),
685 q.value(3).toDouble()) == precursor_ids.end())
687 precursor_ids.push_back(q.value(3).toDouble());
691 if(precursor_ids.size() > 1)
695 if(precursor_ids.size() > 1)
699 return precursor_ids;
703 return precursor_ids;
707 std::vector<std::size_t>
711 std::vector<std::size_t> precursor_id;
712 for(std::vector<double> index : ids)
719 double bko = tims_frame.get()->getOneOverK0Transformation(index[2]);
720 double eko = tims_frame.get()->getOneOverK0Transformation(index[3]);
723 double mean_ko = (bko + eko) / 2;
725 if(mean_ko > ko_value - 0.1 && mean_ko < ko_value + 0.1)
727 precursor_id.push_back(index[1]);
733 std::vector<std::size_t>
737 std::vector<std::size_t> best_precursor;
738 double best_value = 1;
740 int best_val_position = 0;
742 for(std::vector<double> values : ids)
744 double new_val = abs(mz_value - values[4]);
745 if(new_val < best_value)
747 best_value = new_val;
748 best_val_position = count;
752 best_precursor.push_back(ids[best_val_position][1]);
753 return best_precursor;
762 return tims_frame.get()->getMsLevel();
770 std::size_t spectrum_index,
771 bool want_binary_data)
789 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
790 .arg(coordinate.first)
791 .arg(coordinate.second)
792 .arg(spectrum_index));
796 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
800 tims_frame.get()->getDriftTime(coordinate.second));
804 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
810 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
811 if(mass_spectrum.
size() > 0)
823 if(tims_frame.get()->getMsLevel() > 1)
827 if(spectrum_descr.precursor_id > 0)
831 spectrum_descr.precursor_ion_data);
836 spectrum_descr.parent_frame, coordinate.second);
840 QString(
"frame=%1 scan=%2 index=%3")
841 .arg(spectrum_descr.parent_frame)
842 .arg(coordinate.second)
843 .arg(prec_spectrum_index));
847 spectrum_descr.isolationMz);
850 spectrum_descr.isolationWidth);
854 spectrum_descr.collisionEnergy);
857 (quint64)spectrum_descr.precursor_id);
864 QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex "
865 "spectrum_index=%1 :\n%2")
867 .arg(error.
qwhat()));
881 using Pair = std::pair<double, double>;
882 using Map = std::map<double, double>;
883 using Iterator = Map::iterator;
888 qdb.exec(QString(
"SELECT Time, SummedIntensities "
889 "FROM Frames WHERE MsMsType = 0 "
892 if(q.lastError().isValid())
896 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
898 "command %3:\n%4\n%5\n%6")
900 .arg(qdb.databaseName())
902 .arg(qdb.lastError().databaseText())
903 .arg(qdb.lastError().driverText())
904 .arg(qdb.lastError().nativeErrorCode()));
912 int cumulated_results = 2;
914 double rt = q.value(0).toDouble(&ok);
915 cumulated_results -= ok;
917 double sumY = q.value(1).toDouble(&ok);
918 cumulated_results -= ok;
920 if(cumulated_results)
924 "ERROR in TIMS sqlite database file: could not read either the "
925 "retention time or the summed intensities (%1, database name %2, "
927 "command %3:\n%4\n%5\n%6")
929 .arg(qdb.databaseName())
931 .arg(qdb.lastError().databaseText())
932 .arg(qdb.lastError().driverText())
933 .arg(qdb.lastError().nativeErrorCode()));
937 std::pair<Iterator, bool>
res = rt_tic_map_trace.insert(Pair(
rt, sumY));
944 res.first->second += sumY;
951 return rt_tic_map_trace.
toTrace();
960 bool want_binary_data)
963 qDebug() <<
" ms2_index=" << spectrum_descr.
ms2_index
978 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
993 qDebug() <<
"bindec";
1005 tims_frame.get()->getOneOverK0Transformation(
1010 tims_frame.get()->getOneOverK0Transformation(
1014 if(want_binary_data)
1016 combiner.
combine(combiner_result,
1017 tims_frame.get()->cumulateScanToTrace(
1024 if(trace.size() > 0)
1048 catch(std::exception &error)
1050 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1061 if(tims_frame.get()->getId() == timsId)
1080 if(tims_frame.get()->getId() == timsId)
1114 if(m_mapXicCoordRecord.size() == 0)
1139 QSqlDatabase qdb = openDatabaseConnection();
1140 QSqlQuery q = qdb.exec(
1141 QString(
"SELECT Precursors.id, "
1142 "min(Frames.Time), "
1143 "min(PasefFrameMsMsInfo.ScanNumBegin), "
1144 "max(PasefFrameMsMsInfo.ScanNumEnd), "
1145 "Precursors.MonoisotopicMz "
1147 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1148 "PasefFrameMsMsInfo.Precursor=Precursors.Id INNER JOIN "
1149 "Frames ON PasefFrameMsMsInfo.Frame=Frames.Id "
1150 "GROUP BY Precursors.id;"));
1151 if(q.lastError().isValid())
1156 "ERROR in TIMS sqlite database file %1, executing SQL "
1157 "command %2:\n%3\n%4\n%5")
1158 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1160 .arg(qdb.lastError().databaseText())
1161 .arg(qdb.lastError().driverText())
1162 .arg(qdb.lastError().nativeErrorCode()));
1171 QSqlRecord record = q.record();
1172 m_mapXicCoordRecord.insert(std::pair<std::size_t, QSqlRecord>(
1173 (std::size_t)record.value(0).toULongLong(), record));
1179 auto it_map_xiccoord = m_mapXicCoordRecord.find(precursor_id);
1180 if(it_map_xiccoord == m_mapXicCoordRecord.end())
1184 QObject::tr(
"ERROR Precursors database id %1 not found")
1185 .arg(precursor_id));
1188 auto &q = it_map_xiccoord->second;
1189 xic_coord_tims_struct.
mzRange =
1190 MzRange(q.value(4).toDouble(), precision_ptr);
1191 xic_coord_tims_struct.
scanNumBegin = q.value(2).toUInt();
1192 xic_coord_tims_struct.
scanNumEnd = q.value(3).toUInt();
1193 xic_coord_tims_struct.
rtTarget = q.value(1).toDouble();
1195 xic_coord_tims_struct.
xicSptr = std::make_shared<Xic>();
1201 catch(std::exception &error)
1203 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1205 return xic_coord_tims_struct;
1209 std::map<quint32, quint32>
1213 std::map<quint32, quint32> raw_spectrum;
1220 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.*, Precursors.* FROM "
1221 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1222 "PasefFrameMsMsInfo.Precursor=Precursors.Id where "
1223 "Precursors.Id=%1;")
1224 .arg(precursor_index));
1225 if(q.lastError().isValid())
1229 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1230 "command %2:\n%3\n%4\n%5")
1233 .arg(qdb.lastError().databaseText())
1234 .arg(qdb.lastError().driverText())
1235 .arg(qdb.lastError().nativeErrorCode()));
1244 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1246 .arg(precursor_index));
1253 std::size_t scan_mobility_start = 0;
1254 std::size_t scan_mobility_end = 0;
1255 std::vector<std::size_t> tims_frame_list;
1259 tims_frame_list.push_back(q.value(0).toLongLong());
1263 scan_mobility_start = q.value(1).toLongLong();
1264 scan_mobility_end = q.value(2).toLongLong();
1274 for(std::size_t tims_id : tims_frame_list)
1281 if(previous_frame.get() !=
nullptr)
1283 if(previous_frame.get()->hasSameCalibrationData(
1291 "ERROR in %1 %2, different calibration data "
1292 "between frame id %3 and frame id %4")
1295 .arg(previous_frame.get()->getId())
1296 .arg(tims_frame.get()->getId()));
1299 tims_frame.get()->cumulateScansInRawMap(
1300 raw_spectrum, scan_mobility_start, scan_mobility_end);
1303 previous_frame = tims_frame;
1305 qDebug() <<
" precursor_index=" << precursor_index
1306 <<
" num_rows=" << tims_frame_list.size()
1307 <<
" sql=" << q.lastQuery() <<
" "
1308 << (std::size_t)QThread::currentThreadId();
1313 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1315 .arg(precursor_index));
1323 throw PappsoException(QObject::tr(
"ERROR in %1 (precursor_index=%2):\n%3")
1325 .arg(precursor_index)
1326 .arg(error.
qwhat()));
1328 catch(std::exception &error)
1330 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1332 return raw_spectrum;
1342 bool want_binary_data)
1349 spectrum_id.
setNativeId(QString(
"precursor=%1 idxms2=%2")
1357 qDebug() <<
"spectrum_descr.precursor_id=" << spectrum_descr.
precursor_id
1358 <<
" spectrum_descr.ms1_index=" << spectrum_descr.
ms1_index
1359 <<
" spectrum_descr.ms2_index=" << spectrum_descr.
ms2_index;
1370 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
1396 std::map<quint32, quint32> raw_spectrum;
1400 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1401 <<
" tims_id=" << tims_id;
1402 if(want_binary_data)
1404 qDebug() <<
"bindec";
1417 tims_frame.get()->getOneOverK0Transformation(
1422 tims_frame.get()->getOneOverK0Transformation(
1429 if(want_binary_data)
1435 if(previous_frame.get() !=
nullptr)
1437 if(previous_frame.get()->hasSameCalibrationData(
1445 "ERROR in %1 %2, different calibration data "
1446 "between frame id %3 and frame id %4")
1449 .arg(previous_frame.get()->getId())
1450 .arg(tims_frame.get()->getId()));
1453 tims_frame.get()->cumulateScansInRawMap(
1459 previous_frame = tims_frame;
1461 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1463 << (std::size_t)QThread::currentThreadId();
1468 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1472 if(want_binary_data)
1474 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1480 tims_frame.get()->getTraceFromCumulatedScansBuiltinCentroid(
1488 tims_frame.get()->getTraceFromCumulatedScans(raw_spectrum);
1491 if(trace.size() > 0)
1493 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1494 <<
" " << trace.size() <<
" "
1495 << (std::size_t)QThread::currentThreadId();
1509 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1528 QObject::tr(
"ERROR in %1 (ms2_index=%2 precursor_index=%3):\n%4")
1532 .arg(error.
qwhat()));
1534 catch(std::exception &error)
1536 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1545 unsigned int ms_level)
1547 qDebug() <<
" ms_level=" << ms_level;
1548 QSqlDatabase qdb = openDatabaseConnection();
1549 QSqlQuery qprecursor_list = qdb.exec(QString(
1550 "SELECT PasefFrameMsMsInfo.Frame, "
1551 "PasefFrameMsMsInfo.ScanNumBegin, "
1552 "PasefFrameMsMsInfo.ScanNumEnd, "
1553 "PasefFrameMsMsInfo.IsolationMz, "
1554 "PasefFrameMsMsInfo.IsolationWidth, "
1555 "PasefFrameMsMsInfo.CollisionEnergy, "
1556 "PasefFrameMsMsInfo.Precursor, "
1558 "Precursors.LargestPeakMz, "
1559 "Precursors.AverageMz, "
1560 "Precursors.MonoisotopicMz, "
1561 "Precursors.Charge, "
1562 "Precursors.ScanNumber, "
1563 "Precursors.Intensity, "
1564 "Precursors.Parent "
1565 "FROM PasefFrameMsMsInfo "
1566 "INNER JOIN Precursors ON "
1567 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1568 "ORDER BY PasefFrameMsMsInfo.Precursor, PasefFrameMsMsInfo.Frame ;"));
1569 if(qprecursor_list.lastError().isValid())
1573 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1574 "command %2:\n%3\n%4\n%5")
1575 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1576 .arg(qprecursor_list.lastQuery())
1577 .arg(qdb.lastError().databaseText())
1578 .arg(qdb.lastError().driverText())
1579 .arg(qdb.lastError().nativeErrorCode()));
1583 qDebug() <<
"qprecursor_list.size()=" << qprecursor_list.size();
1584 qDebug() << QObject::tr(
1585 "TIMS sqlite database file %1, executing SQL "
1586 "command %2:\n%3\n%4\n%5")
1587 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1588 .arg(qprecursor_list.lastQuery())
1589 .arg(qdb.lastError().databaseText())
1590 .arg(qdb.lastError().driverText())
1591 .arg(qdb.lastError().nativeErrorCode());
1593 qDebug() <<
"qprecursor_list.isActive()=" << qprecursor_list.isActive();
1594 qDebug() <<
"qprecursor_list.isSelect()=" << qprecursor_list.isSelect();
1603 qprecursor_list.last();
1606 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1607 qprecursor_list.first();
1611 qDebug() <<
"i=" << i;
1616 qDebug() <<
"The operation was cancelled. Breaking the loop.";
1618 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
1619 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf")));
1621 qDebug() <<
" spectrum_descr.precursor_id ="
1623 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1624 << qprecursor_list.value(6).toLongLong();
1627 (std::size_t)qprecursor_list.value(6).toLongLong())
1633 ms2ReaderGenerateMS1MS2Spectrum(
1634 msrun_id, handler, spectrum_descr, ms_level);
1640 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1641 << qprecursor_list.value(6).toLongLong();
1643 (std::size_t)qprecursor_list.value(6).toLongLong();
1644 qDebug() <<
" spectrum_descr.precursor_id ="
1646 qDebug() <<
" cumul tims frame:" << qprecursor_list.value(0).toLongLong();
1648 qprecursor_list.value(0).toLongLong());
1649 qDebug() <<
" first =" << first;
1658 qprecursor_list.value(11).toInt(),
1659 qprecursor_list.value(13).toDouble());
1666 qprecursor_list.value(1).toLongLong();
1668 qprecursor_list.value(2).toLongLong();
1670 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1671 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1673 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1683 while(qprecursor_list.next());
1688 ms2ReaderGenerateMS1MS2Spectrum(
1689 msrun_id, handler, spectrum_descr, ms_level);
1699 unsigned int ms_level)
1702 qDebug() <<
" ms_level=" << ms_level;
1705 if((ms_level == 0) || (ms_level == 1))
1709 getQualifiedMs1MassSpectrumByPrecursorId(msrun_id,
1716 if((ms_level == 0) || (ms_level == 2))
1721 getQualifiedMs2MassSpectrumByPrecursorId(msrun_id,
1736 QSqlDatabase qdb = openDatabaseConnection();
1737 QSqlQuery q = qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1738 "PasefFrameMsMsInfo.ScanNumBegin, "
1739 "PasefFrameMsMsInfo.ScanNumEnd, "
1740 "PasefFrameMsMsInfo.IsolationMz, "
1741 "PasefFrameMsMsInfo.IsolationWidth, "
1742 "PasefFrameMsMsInfo.CollisionEnergy, "
1743 "PasefFrameMsMsInfo.Precursor, "
1745 "Precursors.LargestPeakMz, "
1746 "Precursors.AverageMz, "
1747 "Precursors.MonoisotopicMz, "
1748 "Precursors.Charge, "
1749 "Precursors.ScanNumber, "
1750 "Precursors.Intensity, "
1751 "Precursors.Parent "
1752 "FROM PasefFrameMsMsInfo "
1753 "INNER JOIN Precursors ON "
1754 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1755 "WHERE Precursors.Id=%1;")
1756 .arg(precursor_id));
1757 if(q.lastError().isValid())
1761 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1762 "command %2:\n%3\n%4\n%5")
1763 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1765 .arg(qdb.lastError().databaseText())
1766 .arg(qdb.lastError().driverText())
1767 .arg(qdb.lastError().nativeErrorCode()));
1775 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1784 q.value(11).toInt(),
1785 q.value(13).toDouble());
1794 spectrum_descr.
isolationMz = q.value(3).toDouble();
1797 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1806 QObject::tr(
"ERROR in %1 %2 : precursor id (%3) NOT FOUND ")
1809 .arg(precursor_id));
1811 return spectrum_descr;
1817 std::vector<double> timeline;
1818 auto it_map_record_frame = m_mapFramesRecord.begin();
1819 auto it_map_record_frame_end = m_mapFramesRecord.end();
1820 while(it_map_record_frame != it_map_record_frame_end)
1823 if(it_map_record_frame->second.value(6).toInt() ==
1825 timeline.push_back(it_map_record_frame->second.value(5).toDouble());
1826 it_map_record_frame++;
1833 const std::pair<std::size_t, std::size_t> &scan_coordinate)
1837 QSqlDatabase qdb = openDatabaseConnection();
1839 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1840 "PasefFrameMsMsInfo.ScanNumBegin, "
1841 "PasefFrameMsMsInfo.ScanNumEnd, "
1842 "PasefFrameMsMsInfo.IsolationMz, "
1843 "PasefFrameMsMsInfo.IsolationWidth, "
1844 "PasefFrameMsMsInfo.CollisionEnergy, "
1845 "PasefFrameMsMsInfo.Precursor, "
1847 "Precursors.LargestPeakMz, "
1848 "Precursors.AverageMz, "
1849 "Precursors.MonoisotopicMz, "
1850 "Precursors.Charge, "
1851 "Precursors.ScanNumber, "
1852 "Precursors.Intensity, "
1853 "Precursors.Parent "
1854 "FROM PasefFrameMsMsInfo "
1855 "INNER JOIN Precursors ON "
1856 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1858 "PasefFrameMsMsInfo.Frame=%1 and "
1859 "(PasefFrameMsMsInfo.ScanNumBegin "
1860 "<= %2 and PasefFrameMsMsInfo.ScanNumEnd >= %2);")
1861 .arg(scan_coordinate.first)
1862 .arg(scan_coordinate.second));
1863 if(q.lastError().isValid())
1867 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1868 "command %2:\n%3\n%4\n%5")
1869 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1871 .arg(qdb.lastError().databaseText())
1872 .arg(qdb.lastError().driverText())
1873 .arg(qdb.lastError().nativeErrorCode()));
1879 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1885 q.value(10).toDouble(), q.value(11).toInt(), q.value(13).toDouble());
1894 spectrum_descr.
isolationMz = q.value(3).toDouble();
1897 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1899 return spectrum_descr;
1910 qprecursor_list.value(0).toLongLong());
1916 qprecursor_list.value(11).toInt(),
1917 qprecursor_list.value(13).toDouble());
1919 spectrum_descr.
precursor_id = qprecursor_list.value(6).toLongLong();
1926 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1927 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1929 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1937 unsigned int ms_level)
1942 std::size_t spectrum_list_size = getTotalNumberOfScans();
1950 QSqlDatabase qdb = openDatabaseConnection();
1951 QSqlQuery qprecursor_list = qdb.exec(QString(
1953 "PasefFrameMsMsInfo.Frame, "
1954 "PasefFrameMsMsInfo.ScanNumBegin, "
1955 "PasefFrameMsMsInfo.ScanNumEnd, "
1956 "PasefFrameMsMsInfo.IsolationMz, "
1957 "PasefFrameMsMsInfo.IsolationWidth, "
1958 "PasefFrameMsMsInfo.CollisionEnergy, "
1959 "PasefFrameMsMsInfo.Precursor, "
1961 "Precursors.LargestPeakMz, "
1962 "Precursors.AverageMz, "
1963 "Precursors.MonoisotopicMz, "
1964 "Precursors.Charge, "
1965 "Precursors.ScanNumber, "
1966 "Precursors.Intensity, "
1967 "Precursors.Parent "
1968 "FROM PasefFrameMsMsInfo "
1969 "INNER JOIN Precursors ON "
1970 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1971 "ORDER BY PasefFrameMsMsInfo.Frame, PasefFrameMsMsInfo.ScanNumBegin ;"));
1972 if(qprecursor_list.lastError().isValid())
1976 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1977 "command %2:\n%3\n%4\n%5")
1978 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1979 .arg(qprecursor_list.lastQuery())
1980 .arg(qdb.lastError().databaseText())
1981 .arg(qdb.lastError().driverText())
1982 .arg(qdb.lastError().nativeErrorCode()));
1988 qprecursor_list.last();
1991 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1992 qprecursor_list.first();
2004 qDebug() <<
"The operation was cancelled. Breaking the loop.";
2006 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
2007 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf")));
2010 tims_frame = getTimsFrameBaseCstSPtrCached(current_frame.m_frameId);
2011 unsigned int tims_ms_level = tims_frame.get()->getMsLevel();
2013 if((ms_level != 0) && (ms_level != tims_ms_level))
2015 i += current_frame.m_size;
2020 qDebug() <<
"want_binary_data=" << want_binary_data;
2021 if(want_binary_data)
2023 qDebug() <<
"bindec";
2024 tims_frame = getTimsFrameCstSPtrCached(current_frame.m_frameId);
2027 bool possible_precursor =
false;
2028 if(tims_ms_level == 2)
2031 while(qprecursor_list.value(0).toULongLong() <
2032 current_frame.m_frameId)
2034 qprecursor_list.next();
2036 if(qprecursor_list.value(0).toULongLong() ==
2037 current_frame.m_frameId)
2039 possible_precursor =
true;
2041 fillSpectrumDescriptionWithSqlRecord(spectrum_descr,
2046 for(std::size_t scan_num = 0; scan_num < current_frame.m_size;
2049 bool has_a_precursor =
false;
2050 if(possible_precursor)
2055 while(qprecursor_list.value(0).toULongLong() <
2056 current_frame.m_frameId)
2058 qprecursor_list.next();
2060 if(qprecursor_list.value(0).toULongLong() !=
2061 current_frame.m_frameId)
2063 possible_precursor =
false;
2065 fillSpectrumDescriptionWithSqlRecord(spectrum_descr,
2070 if(possible_precursor &&
2074 has_a_precursor =
true;
2085 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
2086 .arg(current_frame.m_frameId)
2092 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
2096 tims_frame.get()->getDriftTime(scan_num));
2100 tims_frame.get()->getOneOverK0Transformation(scan_num));
2103 if(want_binary_data)
2108 tims_frame.get()->getMassSpectrumSPtr(scan_num));
2114 "ERROR in %1 (scan_num=%2 spectrum_index=%3):\n%4")
2118 .arg(error.
qwhat()));
2120 if(mass_spectrum.
size() > 0)
2140 std::size_t prec_spectrum_index =
2145 prec_spectrum_index);
2147 QString(
"frame=%1 scan=%2 index=%3")
2150 .arg(prec_spectrum_index));
2175 std::map<quint32, quint32>
2179 qDebug() <<
" spectrum_index=" << spectrum_index;
2180 auto coordinate = getScanCoordinateFromRawIndex(spectrum_index);
2182 tims_frame = getTimsFrameCstSPtrCached(coordinate.first);
2184 std::map<quint32, quint32> raw_spectrum;
2185 tims_frame.get()->cumulateScansInRawMap(
2186 raw_spectrum, coordinate.second, coordinate.second);
2187 return raw_spectrum;
void setNativeId(const QString &native_id)
void setMsRunId(MsRunIdCstSPtr other)
std::size_t getSpectrumIndex() const
void setSpectrumIndex(std::size_t index)
Class to represent a mass spectrum.
MzCalibrationInterfaceSPtr getInstance(double T1_frame, double T2_frame, const QSqlRecord &mzcalibration_record)
virtual const QString & qwhat() const
const char * what() const noexcept override
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)
interface to collect spectrums from the MsRunReader class
virtual bool shouldStop()
virtual bool needMsLevelPeakList(unsigned int ms_level) const final
tells if we need the peak list (if we want the binary data) for each spectrum, given an MS level
virtual void spectrumListHasSize(std::size_t size)
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
TimsFrameSPtr getTimsFrameSPtrByOffset(std::size_t timsId, std::size_t timsOffset)
QSqlDatabase openDatabaseConnection() const
void ms2ReaderGenerateMS1MS2Spectrum(const MsRunIdCstSPtr &msrun_id, SpectrumCollectionHandlerInterface &handler, SpectrumDescr &spectrum_descr, unsigned int ms_level)
TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId)
get a Tims frame with his database ID
std::vector< FrameIdDescr > m_frameIdDescrList
store every frame id and corresponding sizes
std::map< std::size_t, QSqlRecord > m_mapFramesRecord
void ms2ReaderSpectrumCollectionByMsLevel(const MsRunIdCstSPtr &msrun_id, SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler by Ms Levels
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
SpectrumDescr getSpectrumDescrWithPrecursorId(std::size_t precursor_id)
get an intermediate structure describing a spectrum
TimsData(QDir timsDataDirectory)
build using the tims data directory
std::map< quint32, quint32 > getRawMs2ByPrecursorId(std::size_t precursor_index)
get cumulated raw signal for a given precursor only to use to see the raw signal
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId)
std::size_t m_totalNumberOfScans
std::deque< TimsFrameCstSPtr > m_timsFrameCache
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
std::pair< std::size_t, std::size_t > getScanCoordinateFromRawIndex(std::size_t spectrum_index) const
std::vector< std::size_t > getClosestPrecursorIdByMz(std::vector< std::vector< double >> ids, double mz_value)
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
std::vector< std::size_t > getPrecursorsFromMzRtCharge(int charge, double mz_val, double rt_sec, double k0)
guess possible precursor ids given a charge, m/z, retention time and k0
void fillSpectrumDescriptionWithSqlRecord(SpectrumDescr &spectrum_descr, QSqlQuery &qprecursor_list)
std::map< int, QSqlRecord > m_mapTimsCalibrationRecord
bool m_builtinMs2Centroid
enable builtin centroid on raw tims integers by default
void setMs2BuiltinCentroid(bool centroid)
enable or disable simple centroid filter on raw tims data for MS2
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
void rawReaderSpectrumCollectionByMsLevel(const MsRunIdCstSPtr &msrun_id, SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)
function to visit an MsRunReader and get each raw Spectrum in a spectrum collection handler by Ms Lev...
std::size_t getTotalNumberOfPrecursors() const
get the number of precursors analyzes by PASEF
MzCalibrationStore * mpa_mzCalibrationStore
std::vector< std::size_t > getTimsMS1FrameIdRange(double rt_begin, double rt_end) const
virtual std::vector< double > getRetentionTimeLine() const
retention timeline get retention times along the MSrun in seconds
unsigned int getMsLevelBySpectrumIndex(std::size_t spectrum_index)
bool getMs2BuiltinCentroid() const
tells if simple centroid filter on raw tims data for MS2 is enabled or not
void getQualifiedMs1MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, SpectrumDescr &spectrum_descr, bool want_binary_data)
SpectrumDescr getSpectrumDescrWithScanCoordinate(const std::pair< std::size_t, std::size_t > &scan_coordinate)
std::map< quint32, quint32 > getRawMsBySpectrumIndex(std::size_t spectrum_index)
get raw signal for a spectrum index only to use to see the raw signal
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
void getQualifiedMassSpectrumByRawIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t spectrum_index, bool want_binary_data)
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
filter interface to apply just after raw MS1 specturm extraction the filter can be a list of filters ...
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
filter interface to apply just after raw MS2 specturm extraction the filter can be a list of filters ...
pappso::FilterInterfaceCstSPtr mcsp_ms1Filter
std::size_t getTotalNumberOfScans() const
get the total number of scans
std::vector< std::size_t > getMatchPrecursorIdByKo(std::vector< std::vector< double >> ids, double ko_value)
std::size_t getRawIndexFromCoordinate(std::size_t frame_id, std::size_t scan_num) const
pappso::FilterInterfaceCstSPtr mcsp_ms2Filter
void getQualifiedMs2MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, SpectrumDescr &spectrum_descr, bool want_binary_data)
Trace getTicChromatogram()
XicCoordTims getXicCoordTimsFromPrecursorId(std::size_t precursor_id, PrecisionPtr precision_ptr)
std::size_t m_totalNumberOfPrecursors
virtual MapTrace & combine(MapTrace &map_trace, const Trace &trace) const override
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
@ CollisionEnergy
Bruker's Tims tof collision energy.
@ IsolationMz
isolation window
@ OneOverK0
1/kO of a simple scan
@ OneOverK0end
1/k0 of last acquisition for composite pasef MS/MS spectrum
@ IsolationWidth
isolation window width
@ BrukerPrecursorIndex
Bruker's Tims tof precursor index.
std::shared_ptr< const FilterInterface > FilterInterfaceCstSPtr
std::shared_ptr< const TimsFrame > TimsFrameCstSPtr
std::size_t scan_mobility_end
std::vector< std::size_t > tims_frame_list
PrecursorIonData precursor_ion_data
std::size_t scan_mobility_start
coordinates of the XIC to extract and the resulting XIC after extraction
std::size_t scanNumEnd
mobility index end
std::size_t scanNumBegin
mobility index begin
XicSPtr xicSptr
extracted xic
double rtTarget
the targeted retention time to extract around intended in seconds, and related to one msrun....
MzRange mzRange
the mass to extract