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/filterpseudocentroid.h"
34 #include "../../processing/filters/filterpass.h"
41 #include <QMutexLocker>
48 : m_timsDataDirectory(timsDataDirectory)
56 QObject::tr(
"ERROR TIMS data directory %1 not found")
64 QObject::tr(
"ERROR TIMS data directory, %1 sqlite file not found")
73 if(!q.exec(
"select Key, Value from GlobalMetadata where "
74 "Key='TimsCompressionType';"))
79 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
80 "command %2:\n%3\n%4\n%5")
83 .arg(q.lastError().databaseText())
84 .arg(q.lastError().driverText())
85 .arg(q.lastError().nativeErrorCode()));
89 int compression_type = 0;
92 compression_type = q.value(1).toInt();
94 qDebug() <<
" compression_type=" << compression_type;
102 if(!q.exec(
"SELECT COUNT( DISTINCT Id) FROM Precursors;"))
106 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
107 "command %2:\n%3\n%4\n%5")
110 .arg(qdb.lastError().databaseText())
111 .arg(qdb.lastError().driverText())
112 .arg(qdb.lastError().nativeErrorCode()));
123 if(!q.exec(
"SELECT SUM(NumScans) FROM Frames"))
127 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
128 "command %2:\n%3\n%4\n%5")
131 .arg(qdb.lastError().databaseText())
132 .arg(qdb.lastError().driverText())
133 .arg(qdb.lastError().nativeErrorCode()));
140 if(!q.exec(
"select * from MzCalibration;"))
144 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
145 "command %2:\n%3\n%4\n%5")
148 .arg(q.lastError().databaseText())
149 .arg(q.lastError().driverText())
150 .arg(q.lastError().nativeErrorCode()));
155 QSqlRecord record = q.record();
157 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
162 if(!q.exec(
"select * from TimsCalibration;"))
166 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
167 "command %2:\n%3\n%4\n%5")
170 .arg(q.lastError().databaseText())
171 .arg(q.lastError().driverText())
172 .arg(q.lastError().nativeErrorCode()));
176 QSqlRecord record = q.record();
178 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
183 if(!q.exec(
"select Frames.TimsId, Frames.AccumulationTime, "
184 "Frames.MzCalibration, "
185 "Frames.T1, Frames.T2, "
186 "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, "
192 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
193 "command %2:\n%3\n%4\n%5")
196 .arg(q.lastError().databaseText())
197 .arg(q.lastError().driverText())
198 .arg(q.lastError().nativeErrorCode()));
202 QSqlRecord record = q.record();
204 record.value(8).toULongLong(), record));
208 std::shared_ptr<pappso::FilterPseudoCentroid> ms2filter =
209 std::make_shared<pappso::FilterPseudoCentroid>(35000, 5, 0.5, 0.1);
213 std::shared_ptr<FilterTriangle> ms1filter =
214 std::make_shared<FilterTriangle>();
215 ms1filter.get()->setTriangleSlope(50, 0.01);
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);
240 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()));
288 qdb.exec(QString(
"SELECT Id, NumScans FROM "
289 "Frames ORDER BY Id"));
290 if(q.lastError().isValid())
294 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
295 "command %2:\n%3\n%4\n%5")
298 .arg(qdb.lastError().databaseText())
299 .arg(qdb.lastError().driverText())
300 .arg(qdb.lastError().nativeErrorCode()));
303 bool index_found =
false;
305 std::size_t numberScans;
306 std::size_t cumulScans = 0;
307 while(q.next() && (!index_found))
309 timsId = q.value(0).toULongLong();
310 numberScans = q.value(1).toULongLong();
315 std::pair<std::size_t, std::size_t>((cumulScans / 1000),
319 cumulScans += numberScans;
324 std::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)")
337 std::size_t start_point_index = map_it->second;
338 while((start_point_index > 0) &&
349 return std::pair<std::size_t, std::size_t>(
356 QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
362 std::size_t scan_num)
const
367 if(frameDescr.m_frameId == frame_id)
369 return frameDescr.m_cumulSize + scan_num;
374 QObject::tr(
"ERROR raw index with frame=%1 scan=%2 not found")
387 qDebug() <<
" raw_index=" << raw_index;
397 "Error TimsData::getMassSpectrumCstSPtrByRawIndex raw_index=%1 :\n%2")
399 .arg(error.
qwhat()));
408 qDebug() <<
" timsId=" << timsId;
414 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
419 tims_frame = std::make_shared<TimsFrameBase>(
420 TimsFrameBase(timsId, it_map_record_frames->second.value(0).toULongLong()));
423 it_map_record_frames->second.value(2).toInt());
428 it_map_record_frames->second.value(3).toDouble();
430 it_map_record_frames->second.value(4).toDouble();
433 tims_frame.get()->setMzCalibrationInterfaceSPtr(
435 T1_frame, T2_frame, it_map_record->second));
440 QObject::tr(
"ERROR MzCalibration database id %1 not found")
441 .arg(it_map_record_frames->second.value(2).toInt()));
444 tims_frame.get()->setAccumulationTime(
445 it_map_record_frames->second.value(1).toDouble());
447 tims_frame.get()->setTime(it_map_record_frames->second.value(5).toDouble());
448 tims_frame.get()->setMsMsType(it_map_record_frames->second.value(6).toUInt());
452 it_map_record_frames->second.value(7).toInt());
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());
472 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
473 .arg(it_map_record_frames->second.value(7).toInt()));
479 std::vector<std::size_t>
483 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
486 std::vector<std::size_t> tims_frameid_list;
488 QSqlQuery q = qdb.exec(QString(
"SELECT Frames.Id FROM Frames WHERE "
489 "Frames.MsMsType=0 AND (Frames.Time>=%1) AND "
490 "(Frames.Time<=%2) ORDER BY Frames.Time;")
493 if(q.lastError().isValid())
497 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
499 "command %3:\n%4\n%5\n%6")
501 .arg(qdb.databaseName())
503 .arg(qdb.lastError().databaseText())
504 .arg(qdb.lastError().driverText())
505 .arg(qdb.lastError().nativeErrorCode()));
510 tims_frameid_list.push_back(q.value(0).toULongLong());
512 return tims_frameid_list;
519 qDebug() <<
" timsId=" << timsId
534 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
542 timsId, it_map_record_frames->second.value(0).toULongLong());
546 it_map_record_frames->second.value(2).toInt());
551 it_map_record_frames->second.value(3).toDouble();
553 it_map_record_frames->second.value(4).toDouble();
556 tims_frame.get()->setMzCalibrationInterfaceSPtr(
558 T1_frame, T2_frame, it_map_record->second));
563 QObject::tr(
"ERROR MzCalibration database id %1 not found")
564 .arg(it_map_record_frames->second.value(2).toInt()));
567 tims_frame.get()->setAccumulationTime(
568 it_map_record_frames->second.value(1).toDouble());
570 tims_frame.get()->setTime(it_map_record_frames->second.value(5).toDouble());
571 tims_frame.get()->setMsMsType(it_map_record_frames->second.value(6).toUInt());
575 it_map_record_frames->second.value(7).toInt());
579 tims_frame.get()->setTimsCalibration(
580 it_map_record_tims_calibration->second.value(1).toInt(),
581 it_map_record_tims_calibration->second.value(2).toDouble(),
582 it_map_record_tims_calibration->second.value(3).toDouble(),
583 it_map_record_tims_calibration->second.value(4).toDouble(),
584 it_map_record_tims_calibration->second.value(5).toDouble(),
585 it_map_record_tims_calibration->second.value(6).toDouble(),
586 it_map_record_tims_calibration->second.value(7).toDouble(),
587 it_map_record_tims_calibration->second.value(8).toDouble(),
588 it_map_record_tims_calibration->second.value(9).toDouble(),
589 it_map_record_tims_calibration->second.value(10).toDouble(),
590 it_map_record_tims_calibration->second.value(11).toDouble());
595 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
596 .arg(it_map_record_frames->second.value(7).toInt()));
606 qDebug() <<
" timsId=" << timsId <<
" scanNum=" << scanNum;
609 return frame->getMassSpectrumCstSPtr(scanNum);
625 std::vector<std::size_t>
631 std::vector<std::size_t> precursor_ids;
632 std::vector<std::vector<double>> ids;
635 QSqlQuery q = qdb.exec(
637 "SELECT Frames.Time, Precursors.MonoisotopicMz, Precursors.Charge, "
638 "Precursors.Id, Frames.Id, PasefFrameMsMsInfo.ScanNumBegin, "
639 "PasefFrameMsMsInfo.scanNumEnd "
641 "INNER JOIN PasefFrameMsMsInfo ON Frames.Id = PasefFrameMsMsInfo.Frame "
642 "INNER JOIN Precursors ON PasefFrameMsMsInfo.Precursor = Precursors.Id "
643 "WHERE Precursors.Charge == %1 "
644 "AND Precursors.MonoisotopicMz > %2 -0.01 "
645 "AND Precursors.MonoisotopicMz < %2 +0.01 "
646 "AND Frames.Time >= %3 -1 "
647 "AND Frames.Time < %3 +1; ")
651 if(q.lastError().isValid())
655 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
657 "command %3:\n%4\n%5\n%6")
659 .arg(qdb.databaseName())
661 .arg(qdb.lastError().databaseText())
662 .arg(qdb.lastError().driverText())
663 .arg(qdb.lastError().nativeErrorCode()));
670 std::vector<double> sql_values;
671 sql_values.push_back(q.value(4).toDouble());
672 sql_values.push_back(q.value(3).toDouble());
673 sql_values.push_back(q.value(5).toDouble());
674 sql_values.push_back(q.value(6).toDouble());
675 sql_values.push_back(q.value(1).toDouble());
677 ids.push_back(sql_values);
680 if(std::find(precursor_ids.begin(),
682 q.value(3).toDouble()) == precursor_ids.end())
684 precursor_ids.push_back(q.value(3).toDouble());
688 if(precursor_ids.size() > 1)
692 if(precursor_ids.size() > 1)
696 return precursor_ids;
700 return precursor_ids;
704 std::vector<std::size_t>
708 std::vector<std::size_t> precursor_id;
709 for(std::vector<double> index : ids)
716 double bko = tims_frame.get()->getOneOverK0Transformation(index[2]);
717 double eko = tims_frame.get()->getOneOverK0Transformation(index[3]);
720 double mean_ko = (bko + eko) / 2;
722 if(mean_ko > ko_value - 0.1 && mean_ko < ko_value + 0.1)
724 precursor_id.push_back(index[1]);
730 std::vector<std::size_t>
734 std::vector<std::size_t> best_precursor;
735 double best_value = 1;
737 int best_val_position = 0;
739 for(std::vector<double> values : ids)
741 double new_val = abs(mz_value - values[4]);
742 if(new_val < best_value)
744 best_value = new_val;
745 best_val_position = count;
749 best_precursor.push_back(ids[best_val_position][1]);
750 return best_precursor;
759 return tims_frame.get()->getMsLevel();
767 std::size_t spectrum_index,
768 bool want_binary_data)
786 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
787 .arg(coordinate.first)
788 .arg(coordinate.second)
789 .arg(spectrum_index));
793 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
797 tims_frame.get()->getDriftTime(coordinate.second));
801 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
807 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
808 if(mass_spectrum.
size() > 0)
820 if(tims_frame.get()->getMsLevel() > 1)
824 if(spectrum_descr.precursor_id > 0)
828 spectrum_descr.precursor_ion_data);
833 spectrum_descr.parent_frame, coordinate.second);
837 QString(
"frame=%1 scan=%2 index=%3")
838 .arg(spectrum_descr.parent_frame)
839 .arg(coordinate.second)
840 .arg(prec_spectrum_index));
844 spectrum_descr.isolationMz);
847 spectrum_descr.isolationWidth);
851 spectrum_descr.collisionEnergy);
854 (quint64)spectrum_descr.precursor_id);
861 QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex "
862 "spectrum_index=%1 :\n%2")
864 .arg(error.
qwhat()));
873 bool want_binary_data)
876 qDebug() <<
" ms2_index=" << spectrum_descr.
ms2_index
891 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
906 qDebug() <<
"bindec";
918 tims_frame.get()->getOneOverK0Transformation(
923 tims_frame.get()->getOneOverK0Transformation(
929 combiner.
combine(combiner_result,
930 tims_frame.get()->cumulateScanToTrace(
961 catch(std::exception &error)
963 qDebug() << QString(
"Failure %1 ").arg(error.
what());
974 if(tims_frame.get()->getId() == timsId)
993 if(tims_frame.get()->getId() == timsId)
1027 if(m_mapXicCoordRecord.size() == 0)
1031 QSqlDatabase qdb = openDatabaseConnection();
1032 QSqlQuery q = qdb.exec(
1033 QString(
"SELECT Precursors.id, "
1034 "min(Frames.Time), "
1035 "min(PasefFrameMsMsInfo.ScanNumBegin), "
1036 "max(PasefFrameMsMsInfo.ScanNumEnd), "
1037 "Precursors.MonoisotopicMz "
1039 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1040 "PasefFrameMsMsInfo.Precursor=Precursors.Id INNER JOIN "
1041 "Frames ON PasefFrameMsMsInfo.Frame=Frames.Id "
1042 "GROUP BY Precursors.id;"));
1043 if(q.lastError().isValid())
1048 "ERROR in TIMS sqlite database file %1, executing SQL "
1049 "command %2:\n%3\n%4\n%5")
1050 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1052 .arg(qdb.lastError().databaseText())
1053 .arg(qdb.lastError().driverText())
1054 .arg(qdb.lastError().nativeErrorCode()));
1063 QSqlRecord record = q.record();
1064 m_mapXicCoordRecord.insert(std::pair<std::size_t, QSqlRecord>(
1065 (std::size_t)record.value(0).toULongLong(), record));
1071 auto it_map_xiccoord = m_mapXicCoordRecord.find(precursor_id);
1072 if(it_map_xiccoord == m_mapXicCoordRecord.end())
1076 QObject::tr(
"ERROR Precursor database id %1 not found")
1077 .arg(precursor_id));
1080 auto &q = it_map_xiccoord->second;
1082 MzRange(q.value(4).toDouble(), precision_ptr);
1084 tims_xic_structure.
scanNumEnd = q.value(3).toUInt();
1085 tims_xic_structure.
rtTarget = q.value(1).toDouble();
1087 tims_xic_structure.
xicSptr = std::make_shared<Xic>();
1093 catch(std::exception &error)
1095 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1097 return tims_xic_structure;
1101 std::map<quint32, quint32>
1105 std::map<quint32, quint32> raw_spectrum;
1112 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.*, Precursors.* FROM "
1113 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1114 "PasefFrameMsMsInfo.Precursor=Precursors.Id where "
1115 "Precursors.Id=%1;")
1116 .arg(precursor_index));
1117 if(q.lastError().isValid())
1121 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1122 "command %2:\n%3\n%4\n%5")
1125 .arg(qdb.lastError().databaseText())
1126 .arg(qdb.lastError().driverText())
1127 .arg(qdb.lastError().nativeErrorCode()));
1136 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1138 .arg(precursor_index));
1145 std::size_t scan_mobility_start = 0;
1146 std::size_t scan_mobility_end = 0;
1147 std::vector<std::size_t> tims_frame_list;
1151 tims_frame_list.push_back(q.value(0).toLongLong());
1155 scan_mobility_start = q.value(1).toLongLong();
1156 scan_mobility_end = q.value(2).toLongLong();
1166 for(std::size_t tims_id : tims_frame_list)
1173 if(previous_frame.get() !=
nullptr)
1175 if(previous_frame.get()->hasSameCalibrationData(
1183 "ERROR in %1 %2, different calibration data "
1184 "between frame id %3 and frame id %4")
1187 .arg(previous_frame.get()->getId())
1188 .arg(tims_frame.get()->getId()));
1191 tims_frame.get()->cumulateScansInRawMap(
1192 raw_spectrum, scan_mobility_start, scan_mobility_end);
1195 previous_frame = tims_frame;
1197 qDebug() <<
" precursor_index=" << precursor_index
1198 <<
" num_rows=" << tims_frame_list.size()
1199 <<
" sql=" << q.lastQuery() <<
" "
1200 << (std::size_t)QThread::currentThreadId();
1205 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1207 .arg(precursor_index));
1215 throw PappsoException(QObject::tr(
"ERROR in %1 (precursor_index=%2):\n%3")
1217 .arg(precursor_index)
1218 .arg(error.
qwhat()));
1220 catch(std::exception &error)
1222 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1224 return raw_spectrum;
1234 bool want_binary_data)
1241 spectrum_id.
setNativeId(QString(
"precursor=%1 idxms2=%2")
1249 qDebug() <<
"spectrum_descr.precursor_id=" << spectrum_descr.
precursor_id
1250 <<
" spectrum_descr.ms1_index=" << spectrum_descr.
ms1_index
1251 <<
" spectrum_descr.ms2_index=" << spectrum_descr.
ms2_index;
1262 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
1288 std::map<quint32, quint32> raw_spectrum;
1292 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1293 <<
" tims_id=" << tims_id;
1294 if(want_binary_data)
1296 qDebug() <<
"bindec";
1309 tims_frame.get()->getOneOverK0Transformation(
1314 tims_frame.get()->getOneOverK0Transformation(
1321 if(want_binary_data)
1327 if(previous_frame.get() !=
nullptr)
1329 if(previous_frame.get()->hasSameCalibrationData(
1337 "ERROR in %1 %2, different calibration data "
1338 "between frame id %3 and frame id %4")
1341 .arg(previous_frame.get()->getId())
1342 .arg(tims_frame.get()->getId()));
1345 tims_frame.get()->cumulateScansInRawMap(
1351 previous_frame = tims_frame;
1353 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1355 << (std::size_t)QThread::currentThreadId();
1360 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1364 if(want_binary_data)
1366 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1372 tims_frame.get()->getTraceFromCumulatedScansBuiltinCentroid(
1380 tims_frame.get()->getTraceFromCumulatedScans(raw_spectrum);
1383 if(trace.size() > 0)
1385 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1386 <<
" " << trace.size() <<
" "
1387 << (std::size_t)QThread::currentThreadId();
1401 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1420 QObject::tr(
"ERROR in %1 (ms2_index=%2 precursor_index=%3):\n%4")
1424 .arg(error.
qwhat()));
1426 catch(std::exception &error)
1428 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1437 unsigned int ms_level)
1439 qDebug() <<
" ms_level=" << ms_level;
1440 QSqlDatabase qdb = openDatabaseConnection();
1441 QSqlQuery qprecursor_list = qdb.exec(QString(
1442 "SELECT PasefFrameMsMsInfo.Frame, "
1443 "PasefFrameMsMsInfo.ScanNumBegin, "
1444 "PasefFrameMsMsInfo.ScanNumEnd, "
1445 "PasefFrameMsMsInfo.IsolationMz, "
1446 "PasefFrameMsMsInfo.IsolationWidth, "
1447 "PasefFrameMsMsInfo.CollisionEnergy, "
1448 "PasefFrameMsMsInfo.Precursor, "
1450 "Precursors.LargestPeakMz, "
1451 "Precursors.AverageMz, "
1452 "Precursors.MonoisotopicMz, "
1453 "Precursors.Charge, "
1454 "Precursors.ScanNumber, "
1455 "Precursors.Intensity, "
1456 "Precursors.Parent "
1457 "FROM PasefFrameMsMsInfo "
1458 "INNER JOIN Precursors ON "
1459 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1460 "ORDER BY PasefFrameMsMsInfo.Precursor, PasefFrameMsMsInfo.Frame ;"));
1461 if(qprecursor_list.lastError().isValid())
1465 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1466 "command %2:\n%3\n%4\n%5")
1467 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1468 .arg(qprecursor_list.lastQuery())
1469 .arg(qdb.lastError().databaseText())
1470 .arg(qdb.lastError().driverText())
1471 .arg(qdb.lastError().nativeErrorCode()));
1475 qDebug() <<
"qprecursor_list.size()=" << qprecursor_list.size();
1476 qDebug() << QObject::tr(
1477 "TIMS sqlite database file %1, executing SQL "
1478 "command %2:\n%3\n%4\n%5")
1479 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1480 .arg(qprecursor_list.lastQuery())
1481 .arg(qdb.lastError().databaseText())
1482 .arg(qdb.lastError().driverText())
1483 .arg(qdb.lastError().nativeErrorCode());
1485 qDebug() <<
"qprecursor_list.isActive()=" << qprecursor_list.isActive();
1486 qDebug() <<
"qprecursor_list.isSelect()=" << qprecursor_list.isSelect();
1495 qprecursor_list.last();
1498 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1499 qprecursor_list.first();
1503 qDebug() <<
"i=" << i;
1508 qDebug() <<
"The operation was cancelled. Breaking the loop.";
1510 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
1511 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf")));
1513 qDebug() <<
" spectrum_descr.precursor_id ="
1515 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1516 << qprecursor_list.value(6).toLongLong();
1519 (std::size_t)qprecursor_list.value(6).toLongLong())
1525 ms2ReaderGenerateMS1MS2Spectrum(
1526 msrun_id, handler, spectrum_descr, ms_level);
1532 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1533 << qprecursor_list.value(6).toLongLong();
1535 (std::size_t)qprecursor_list.value(6).toLongLong();
1536 qDebug() <<
" spectrum_descr.precursor_id ="
1538 qDebug() <<
" cumul tims frame:" << qprecursor_list.value(0).toLongLong();
1540 qprecursor_list.value(0).toLongLong());
1541 qDebug() <<
" first =" << first;
1550 qprecursor_list.value(11).toInt(),
1551 qprecursor_list.value(13).toDouble());
1558 qprecursor_list.value(1).toLongLong();
1560 qprecursor_list.value(2).toLongLong();
1562 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1563 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1565 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1575 while(qprecursor_list.next());
1580 ms2ReaderGenerateMS1MS2Spectrum(
1581 msrun_id, handler, spectrum_descr, ms_level);
1591 unsigned int ms_level)
1594 qDebug() <<
" ms_level=" << ms_level;
1597 if((ms_level == 0) || (ms_level == 1))
1601 getQualifiedMs1MassSpectrumByPrecursorId(msrun_id,
1608 if((ms_level == 0) || (ms_level == 2))
1613 getQualifiedMs2MassSpectrumByPrecursorId(msrun_id,
1628 QSqlDatabase qdb = openDatabaseConnection();
1629 QSqlQuery q = qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1630 "PasefFrameMsMsInfo.ScanNumBegin, "
1631 "PasefFrameMsMsInfo.ScanNumEnd, "
1632 "PasefFrameMsMsInfo.IsolationMz, "
1633 "PasefFrameMsMsInfo.IsolationWidth, "
1634 "PasefFrameMsMsInfo.CollisionEnergy, "
1635 "PasefFrameMsMsInfo.Precursor, "
1637 "Precursors.LargestPeakMz, "
1638 "Precursors.AverageMz, "
1639 "Precursors.MonoisotopicMz, "
1640 "Precursors.Charge, "
1641 "Precursors.ScanNumber, "
1642 "Precursors.Intensity, "
1643 "Precursors.Parent "
1644 "FROM PasefFrameMsMsInfo "
1645 "INNER JOIN Precursors ON "
1646 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1647 "WHERE Precursors.Id=%1;")
1648 .arg(precursor_id));
1649 if(q.lastError().isValid())
1653 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1654 "command %2:\n%3\n%4\n%5")
1655 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1657 .arg(qdb.lastError().databaseText())
1658 .arg(qdb.lastError().driverText())
1659 .arg(qdb.lastError().nativeErrorCode()));
1667 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1676 q.value(11).toInt(),
1677 q.value(13).toDouble());
1686 spectrum_descr.
isolationMz = q.value(3).toDouble();
1689 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1698 QObject::tr(
"ERROR in %1 %2 : precursor id (%3) NOT FOUND ")
1701 .arg(precursor_id));
1703 return spectrum_descr;
1709 std::vector<double> timeline;
1710 auto it_map_record_frame = m_mapFramesRecord.begin();
1711 auto it_map_record_frame_end = m_mapFramesRecord.end();
1712 while(it_map_record_frame != it_map_record_frame_end)
1715 if(it_map_record_frame->second.value(6).toInt() ==
1717 timeline.push_back(it_map_record_frame->second.value(5).toDouble());
1718 it_map_record_frame++;
1725 const std::pair<std::size_t, std::size_t> &scan_coordinate)
1729 QSqlDatabase qdb = openDatabaseConnection();
1731 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1732 "PasefFrameMsMsInfo.ScanNumBegin, "
1733 "PasefFrameMsMsInfo.ScanNumEnd, "
1734 "PasefFrameMsMsInfo.IsolationMz, "
1735 "PasefFrameMsMsInfo.IsolationWidth, "
1736 "PasefFrameMsMsInfo.CollisionEnergy, "
1737 "PasefFrameMsMsInfo.Precursor, "
1739 "Precursors.LargestPeakMz, "
1740 "Precursors.AverageMz, "
1741 "Precursors.MonoisotopicMz, "
1742 "Precursors.Charge, "
1743 "Precursors.ScanNumber, "
1744 "Precursors.Intensity, "
1745 "Precursors.Parent "
1746 "FROM PasefFrameMsMsInfo "
1747 "INNER JOIN Precursors ON "
1748 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1750 "PasefFrameMsMsInfo.Frame=%1 and "
1751 "(PasefFrameMsMsInfo.ScanNumBegin "
1752 "<= %2 and PasefFrameMsMsInfo.ScanNumEnd >= %2);")
1753 .arg(scan_coordinate.first)
1754 .arg(scan_coordinate.second));
1755 if(q.lastError().isValid())
1759 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1760 "command %2:\n%3\n%4\n%5")
1761 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1763 .arg(qdb.lastError().databaseText())
1764 .arg(qdb.lastError().driverText())
1765 .arg(qdb.lastError().nativeErrorCode()));
1771 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1777 q.value(10).toDouble(), q.value(11).toInt(), q.value(13).toDouble());
1786 spectrum_descr.
isolationMz = q.value(3).toDouble();
1789 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1791 return spectrum_descr;
1802 qprecursor_list.value(0).toLongLong());
1808 qprecursor_list.value(11).toInt(),
1809 qprecursor_list.value(13).toDouble());
1811 spectrum_descr.
precursor_id = qprecursor_list.value(6).toLongLong();
1818 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1819 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1821 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1829 unsigned int ms_level)
1834 std::size_t spectrum_list_size = getTotalNumberOfScans();
1842 QSqlDatabase qdb = openDatabaseConnection();
1843 QSqlQuery qprecursor_list = qdb.exec(QString(
1845 "PasefFrameMsMsInfo.Frame, "
1846 "PasefFrameMsMsInfo.ScanNumBegin, "
1847 "PasefFrameMsMsInfo.ScanNumEnd, "
1848 "PasefFrameMsMsInfo.IsolationMz, "
1849 "PasefFrameMsMsInfo.IsolationWidth, "
1850 "PasefFrameMsMsInfo.CollisionEnergy, "
1851 "PasefFrameMsMsInfo.Precursor, "
1853 "Precursors.LargestPeakMz, "
1854 "Precursors.AverageMz, "
1855 "Precursors.MonoisotopicMz, "
1856 "Precursors.Charge, "
1857 "Precursors.ScanNumber, "
1858 "Precursors.Intensity, "
1859 "Precursors.Parent "
1860 "FROM PasefFrameMsMsInfo "
1861 "INNER JOIN Precursors ON "
1862 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1863 "ORDER BY PasefFrameMsMsInfo.Frame, PasefFrameMsMsInfo.ScanNumBegin ;"));
1864 if(qprecursor_list.lastError().isValid())
1868 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1869 "command %2:\n%3\n%4\n%5")
1870 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1871 .arg(qprecursor_list.lastQuery())
1872 .arg(qdb.lastError().databaseText())
1873 .arg(qdb.lastError().driverText())
1874 .arg(qdb.lastError().nativeErrorCode()));
1880 qprecursor_list.last();
1883 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1884 qprecursor_list.first();
1896 qDebug() <<
"The operation was cancelled. Breaking the loop.";
1898 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
1899 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf")));
1902 tims_frame = getTimsFrameBaseCstSPtrCached(current_frame.m_frameId);
1903 unsigned int tims_ms_level = tims_frame.get()->getMsLevel();
1905 if((ms_level != 0) && (ms_level != tims_ms_level))
1907 i += current_frame.m_size;
1912 qDebug() <<
"want_binary_data=" << want_binary_data;
1913 if(want_binary_data)
1915 qDebug() <<
"bindec";
1916 tims_frame = getTimsFrameCstSPtrCached(current_frame.m_frameId);
1919 bool possible_precursor =
false;
1920 if(tims_ms_level == 2)
1923 while(qprecursor_list.value(0).toULongLong() <
1924 current_frame.m_frameId)
1926 qprecursor_list.next();
1928 if(qprecursor_list.value(0).toULongLong() ==
1929 current_frame.m_frameId)
1931 possible_precursor =
true;
1933 fillSpectrumDescriptionWithSqlRecord(spectrum_descr,
1938 for(std::size_t scan_num = 0; scan_num < current_frame.m_size;
1941 bool has_a_precursor =
false;
1942 if(possible_precursor)
1947 while(qprecursor_list.value(0).toULongLong() <
1948 current_frame.m_frameId)
1950 qprecursor_list.next();
1952 if(qprecursor_list.value(0).toULongLong() !=
1953 current_frame.m_frameId)
1955 possible_precursor =
false;
1957 fillSpectrumDescriptionWithSqlRecord(spectrum_descr,
1962 if(possible_precursor &&
1966 has_a_precursor =
true;
1977 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
1978 .arg(current_frame.m_frameId)
1984 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
1988 tims_frame.get()->getDriftTime(scan_num));
1992 tims_frame.get()->getOneOverK0Transformation(scan_num));
1995 if(want_binary_data)
2000 tims_frame.get()->getMassSpectrumSPtr(scan_num));
2006 "ERROR in %1 (scan_num=%2 spectrum_index=%3):\n%4")
2010 .arg(error.
qwhat()));
2012 if(mass_spectrum.
size() > 0)
2032 std::size_t prec_spectrum_index =
2037 prec_spectrum_index);
2039 QString(
"frame=%1 scan=%2 index=%3")
2042 .arg(prec_spectrum_index));
2067 std::map<quint32, quint32>
2071 qDebug() <<
" spectrum_index=" << spectrum_index;
2072 auto coordinate = getScanCoordinateFromRawIndex(spectrum_index);
2074 tims_frame = getTimsFrameCstSPtrCached(coordinate.first);
2076 std::map<quint32, quint32> raw_spectrum;
2077 tims_frame.get()->cumulateScansInRawMap(
2078 raw_spectrum, coordinate.second, coordinate.second);
2079 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)
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
filter interface to apply just after raw MS1 specturm extraction the filter can be a list of filters ...
void getQualifiedMs2MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, SpectrumDescr &spectrum_descr, bool want_binary_data)
void ms2ReaderGenerateMS1MS2Spectrum(const MsRunIdCstSPtr &msrun_id, SpectrumCollectionHandlerInterface &handler, SpectrumDescr &spectrum_descr, unsigned int ms_level)
void fillFrameIdDescrList()
private function to fill m_frameIdDescrList
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId) const
get a Tims frame base (no binary data file access) with his database ID
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
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
filter interface to apply just after raw MS2 specturm extraction the filter can be a list of filters ...
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
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
TimsData(QDir timsDataDirectory)
build using the tims data directory
SpectrumDescr getSpectrumDescrWithPrecursorId(std::size_t precursor_id)
get an intermediate structure describing a spectrum
QSqlDatabase openDatabaseConnection() const
TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId) const
get a Tims frame with his database ID
void getQualifiedMassSpectrumByRawIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t spectrum_index, bool want_binary_data)
std::size_t getRawIndexFromCoordinate(std::size_t frame_id, std::size_t scan_num) const
void getQualifiedMs1MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, SpectrumDescr &spectrum_descr, bool want_binary_data)
std::size_t m_totalNumberOfScans
std::deque< TimsFrameCstSPtr > m_timsFrameCache
unsigned int getMsLevelBySpectrumIndex(std::size_t spectrum_index)
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
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
std::vector< std::size_t > getTimsMS1FrameIdRange(double rt_begin, double rt_end) const
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::vector< std::size_t > getClosestPrecursorIdByMz(std::vector< std::vector< double >> ids, double mz_value)
MzCalibrationStore * mpa_mzCalibrationStore
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId)
virtual std::vector< double > getRetentionTimeLine() const
retention timeline get retention times along the MSrun in seconds
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
std::size_t getTotalNumberOfScans() const
get the total number of scans
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
TimsBinDec * mpa_timsBinDec
void setMs2BuiltinCentroid(bool centroid)
enable or disable simple centroid filter on raw tims data for MS2
std::vector< std::size_t > getMatchPrecursorIdByKo(std::vector< std::vector< double >> ids, double ko_value)
pappso::FilterInterfaceCstSPtr mcsp_ms1Filter
bool getMs2BuiltinCentroid() const
tells if simple centroid filter on raw tims data for MS2 is enabled or not
std::pair< std::size_t, std::size_t > getScanCoordinateFromRawIndex(std::size_t spectrum_index) const
pappso::FilterInterfaceCstSPtr mcsp_ms2Filter
pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtrByRawIndex(std::size_t raw_index)
get a mass spectrum given its spectrum index
XicCoordTims getXicCoordTimsFromPrecursorId(std::size_t precursor_id, PrecisionPtr precision_ptr)
std::size_t getTotalNumberOfPrecursors() const
get the number of precursors analyzes by PASEF
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