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;
304 std::size_t numberScans;
305 std::size_t cumulScans = 0;
306 while(q.next() && (!index_found))
308 timsId = q.value(0).toULongLong();
309 numberScans = q.value(1).toULongLong();
314 std::pair<std::size_t, std::size_t>((cumulScans / 1000),
318 cumulScans += numberScans;
323 std::pair<std::size_t, std::size_t>
327 std::size_t fast_access = raw_index / 1000;
328 qDebug() <<
" fast_access=" << fast_access;
333 QObject::tr(
"ERROR raw index %1 not found (fast_access)")
336 std::size_t start_point_index = map_it->second;
337 while((start_point_index > 0) &&
348 return std::pair<std::size_t, std::size_t>(
355 QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
361 std::size_t scan_num)
const
366 if(frameDescr.m_frameId == frame_id)
368 return frameDescr.m_cumulSize + scan_num;
373 QObject::tr(
"ERROR raw index with frame=%1 scan=%2 not found")
386 qDebug() <<
" raw_index=" << raw_index;
396 "Error TimsData::getMassSpectrumCstSPtrByRawIndex raw_index=%1 :\n%2")
398 .arg(error.
qwhat()));
407 qDebug() <<
" timsId=" << timsId;
413 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
418 tims_frame = std::make_shared<TimsFrameBase>(
419 TimsFrameBase(timsId, it_map_record_frames->second.value(0).toULongLong()));
422 it_map_record_frames->second.value(2).toInt());
427 it_map_record_frames->second.value(3).toDouble();
429 it_map_record_frames->second.value(4).toDouble();
432 tims_frame.get()->setMzCalibrationInterfaceSPtr(
434 T1_frame, T2_frame, it_map_record->second));
439 QObject::tr(
"ERROR MzCalibration database id %1 not found")
440 .arg(it_map_record_frames->second.value(2).toInt()));
443 tims_frame.get()->setAccumulationTime(
444 it_map_record_frames->second.value(1).toDouble());
446 tims_frame.get()->setTime(it_map_record_frames->second.value(5).toDouble());
447 tims_frame.get()->setMsMsType(it_map_record_frames->second.value(6).toUInt());
451 it_map_record_frames->second.value(7).toInt());
455 tims_frame.get()->setTimsCalibration(
456 it_map_record_tims_calibration->second.value(1).toInt(),
457 it_map_record_tims_calibration->second.value(2).toDouble(),
458 it_map_record_tims_calibration->second.value(3).toDouble(),
459 it_map_record_tims_calibration->second.value(4).toDouble(),
460 it_map_record_tims_calibration->second.value(5).toDouble(),
461 it_map_record_tims_calibration->second.value(6).toDouble(),
462 it_map_record_tims_calibration->second.value(7).toDouble(),
463 it_map_record_tims_calibration->second.value(8).toDouble(),
464 it_map_record_tims_calibration->second.value(9).toDouble(),
465 it_map_record_tims_calibration->second.value(10).toDouble(),
466 it_map_record_tims_calibration->second.value(11).toDouble());
471 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
472 .arg(it_map_record_frames->second.value(7).toInt()));
478 std::vector<std::size_t>
482 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
485 std::vector<std::size_t> tims_frameid_list;
487 QSqlQuery q = qdb.exec(QString(
"SELECT Frames.Id FROM Frames WHERE "
488 "Frames.MsMsType=0 AND (Frames.Time>=%1) AND "
489 "(Frames.Time<=%2) ORDER BY Frames.Time;")
492 if(q.lastError().isValid())
496 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
498 "command %3:\n%4\n%5\n%6")
500 .arg(qdb.databaseName())
502 .arg(qdb.lastError().databaseText())
503 .arg(qdb.lastError().driverText())
504 .arg(qdb.lastError().nativeErrorCode()));
509 tims_frameid_list.push_back(q.value(0).toULongLong());
511 return tims_frameid_list;
518 qDebug() <<
" timsId=" << timsId
535 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
543 timsId, it_map_record_frames->second.value(0).toULongLong());
547 it_map_record_frames->second.value(2).toInt());
552 it_map_record_frames->second.value(3).toDouble();
554 it_map_record_frames->second.value(4).toDouble();
557 tims_frame.get()->setMzCalibrationInterfaceSPtr(
559 T1_frame, T2_frame, it_map_record->second));
564 QObject::tr(
"ERROR MzCalibration database id %1 not found")
565 .arg(it_map_record_frames->second.value(2).toInt()));
568 tims_frame.get()->setAccumulationTime(
569 it_map_record_frames->second.value(1).toDouble());
571 tims_frame.get()->setTime(it_map_record_frames->second.value(5).toDouble());
572 tims_frame.get()->setMsMsType(it_map_record_frames->second.value(6).toUInt());
576 it_map_record_frames->second.value(7).toInt());
580 tims_frame.get()->setTimsCalibration(
581 it_map_record_tims_calibration->second.value(1).toInt(),
582 it_map_record_tims_calibration->second.value(2).toDouble(),
583 it_map_record_tims_calibration->second.value(3).toDouble(),
584 it_map_record_tims_calibration->second.value(4).toDouble(),
585 it_map_record_tims_calibration->second.value(5).toDouble(),
586 it_map_record_tims_calibration->second.value(6).toDouble(),
587 it_map_record_tims_calibration->second.value(7).toDouble(),
588 it_map_record_tims_calibration->second.value(8).toDouble(),
589 it_map_record_tims_calibration->second.value(9).toDouble(),
590 it_map_record_tims_calibration->second.value(10).toDouble(),
591 it_map_record_tims_calibration->second.value(11).toDouble());
596 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
597 .arg(it_map_record_frames->second.value(7).toInt()));
607 qDebug() <<
" timsId=" << timsId <<
" scanNum=" << scanNum;
610 return frame->getMassSpectrumCstSPtr(scanNum);
626 std::vector<std::size_t>
632 std::vector<std::size_t> precursor_ids;
633 std::vector<std::vector<double>> ids;
636 QSqlQuery q = qdb.exec(
638 "SELECT Frames.Time, Precursors.MonoisotopicMz, Precursors.Charge, "
639 "Precursors.Id, Frames.Id, PasefFrameMsMsInfo.ScanNumBegin, "
640 "PasefFrameMsMsInfo.scanNumEnd "
642 "INNER JOIN PasefFrameMsMsInfo ON Frames.Id = PasefFrameMsMsInfo.Frame "
643 "INNER JOIN Precursors ON PasefFrameMsMsInfo.Precursor = Precursors.Id "
644 "WHERE Precursors.Charge == %1 "
645 "AND Precursors.MonoisotopicMz > %2 -0.01 "
646 "AND Precursors.MonoisotopicMz < %2 +0.01 "
647 "AND Frames.Time >= %3 -1 "
648 "AND Frames.Time < %3 +1; ")
652 if(q.lastError().isValid())
656 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
658 "command %3:\n%4\n%5\n%6")
660 .arg(qdb.databaseName())
662 .arg(qdb.lastError().databaseText())
663 .arg(qdb.lastError().driverText())
664 .arg(qdb.lastError().nativeErrorCode()));
671 std::vector<double> sql_values;
672 sql_values.push_back(q.value(4).toDouble());
673 sql_values.push_back(q.value(3).toDouble());
674 sql_values.push_back(q.value(5).toDouble());
675 sql_values.push_back(q.value(6).toDouble());
676 sql_values.push_back(q.value(1).toDouble());
678 ids.push_back(sql_values);
681 if(std::find(precursor_ids.begin(),
683 q.value(3).toDouble()) == precursor_ids.end())
685 precursor_ids.push_back(q.value(3).toDouble());
689 if(precursor_ids.size() > 1)
693 if(precursor_ids.size() > 1)
697 return precursor_ids;
701 return precursor_ids;
705 std::vector<std::size_t>
709 std::vector<std::size_t> precursor_id;
710 for(std::vector<double> index : ids)
717 double bko = tims_frame.get()->getOneOverK0Transformation(index[2]);
718 double eko = tims_frame.get()->getOneOverK0Transformation(index[3]);
721 double mean_ko = (bko + eko) / 2;
723 if(mean_ko > ko_value - 0.1 && mean_ko < ko_value + 0.1)
725 precursor_id.push_back(index[1]);
731 std::vector<std::size_t>
735 std::vector<std::size_t> best_precursor;
736 double best_value = 1;
738 int best_val_position = 0;
740 for(std::vector<double> values : ids)
742 double new_val = abs(mz_value - values[4]);
743 if(new_val < best_value)
745 best_value = new_val;
746 best_val_position = count;
750 best_precursor.push_back(ids[best_val_position][1]);
751 return best_precursor;
760 return tims_frame.get()->getMsLevel();
768 std::size_t spectrum_index,
769 bool want_binary_data)
787 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
788 .arg(coordinate.first)
789 .arg(coordinate.second)
790 .arg(spectrum_index));
794 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
798 tims_frame.get()->getDriftTime(coordinate.second));
802 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
808 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
809 if(mass_spectrum.
size() > 0)
821 if(tims_frame.get()->getMsLevel() > 1)
825 if(spectrum_descr.precursor_id > 0)
829 spectrum_descr.precursor_ion_data);
834 spectrum_descr.parent_frame, coordinate.second);
838 QString(
"frame=%1 scan=%2 index=%3")
839 .arg(spectrum_descr.parent_frame)
840 .arg(coordinate.second)
841 .arg(prec_spectrum_index));
845 spectrum_descr.isolationMz);
848 spectrum_descr.isolationWidth);
852 spectrum_descr.collisionEnergy);
855 (quint64)spectrum_descr.precursor_id);
862 QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex "
863 "spectrum_index=%1 :\n%2")
865 .arg(error.
qwhat()));
879 using Pair = std::pair<double, double>;
880 using Map = std::map<double, double>;
881 using Iterator = Map::iterator;
886 qdb.exec(QString(
"SELECT Time, SummedIntensities "
887 "FROM Frames WHERE MsMsType = 0 "
890 if(q.lastError().isValid())
894 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
896 "command %3:\n%4\n%5\n%6")
898 .arg(qdb.databaseName())
900 .arg(qdb.lastError().databaseText())
901 .arg(qdb.lastError().driverText())
902 .arg(qdb.lastError().nativeErrorCode()));
910 int cumulated_results = 2;
912 double rt = q.value(0).toDouble(&ok);
913 cumulated_results -= ok;
915 double sumY = q.value(1).toDouble(&ok);
916 cumulated_results -= ok;
918 if(cumulated_results)
922 "ERROR in TIMS sqlite database file: could not read either the "
923 "retention time or the summed intensities (%1, database name %2, "
925 "command %3:\n%4\n%5\n%6")
927 .arg(qdb.databaseName())
929 .arg(qdb.lastError().databaseText())
930 .arg(qdb.lastError().driverText())
931 .arg(qdb.lastError().nativeErrorCode()));
935 std::pair<Iterator, bool>
res = rt_tic_map_trace.insert(Pair(
rt, sumY));
942 res.first->second += sumY;
949 return rt_tic_map_trace.
toTrace();
958 bool want_binary_data)
961 qDebug() <<
" ms2_index=" << spectrum_descr.
ms2_index
976 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
991 qDebug() <<
"bindec";
1003 tims_frame.get()->getOneOverK0Transformation(
1008 tims_frame.get()->getOneOverK0Transformation(
1012 if(want_binary_data)
1014 combiner.
combine(combiner_result,
1015 tims_frame.get()->cumulateScanToTrace(
1022 if(trace.size() > 0)
1046 catch(std::exception &error)
1048 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1059 if(tims_frame.get()->getId() == timsId)
1078 if(tims_frame.get()->getId() == timsId)
1112 if(m_mapXicCoordRecord.size() == 0)
1137 QSqlDatabase qdb = openDatabaseConnection();
1138 QSqlQuery q = qdb.exec(
1139 QString(
"SELECT Precursors.id, "
1140 "min(Frames.Time), "
1141 "min(PasefFrameMsMsInfo.ScanNumBegin), "
1142 "max(PasefFrameMsMsInfo.ScanNumEnd), "
1143 "Precursors.MonoisotopicMz "
1145 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1146 "PasefFrameMsMsInfo.Precursor=Precursors.Id INNER JOIN "
1147 "Frames ON PasefFrameMsMsInfo.Frame=Frames.Id "
1148 "GROUP BY Precursors.id;"));
1149 if(q.lastError().isValid())
1154 "ERROR in TIMS sqlite database file %1, executing SQL "
1155 "command %2:\n%3\n%4\n%5")
1156 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1158 .arg(qdb.lastError().databaseText())
1159 .arg(qdb.lastError().driverText())
1160 .arg(qdb.lastError().nativeErrorCode()));
1169 QSqlRecord record = q.record();
1170 m_mapXicCoordRecord.insert(std::pair<std::size_t, QSqlRecord>(
1171 (std::size_t)record.value(0).toULongLong(), record));
1177 auto it_map_xiccoord = m_mapXicCoordRecord.find(precursor_id);
1178 if(it_map_xiccoord == m_mapXicCoordRecord.end())
1182 QObject::tr(
"ERROR Precursors database id %1 not found")
1183 .arg(precursor_id));
1186 auto &q = it_map_xiccoord->second;
1187 xic_coord_tims_struct.
mzRange =
1188 MzRange(q.value(4).toDouble(), precision_ptr);
1189 xic_coord_tims_struct.
scanNumBegin = q.value(2).toUInt();
1190 xic_coord_tims_struct.
scanNumEnd = q.value(3).toUInt();
1191 xic_coord_tims_struct.
rtTarget = q.value(1).toDouble();
1193 xic_coord_tims_struct.
xicSptr = std::make_shared<Xic>();
1199 catch(std::exception &error)
1201 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1203 return xic_coord_tims_struct;
1207 std::map<quint32, quint32>
1211 std::map<quint32, quint32> raw_spectrum;
1218 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.*, Precursors.* FROM "
1219 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1220 "PasefFrameMsMsInfo.Precursor=Precursors.Id where "
1221 "Precursors.Id=%1;")
1222 .arg(precursor_index));
1223 if(q.lastError().isValid())
1227 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1228 "command %2:\n%3\n%4\n%5")
1231 .arg(qdb.lastError().databaseText())
1232 .arg(qdb.lastError().driverText())
1233 .arg(qdb.lastError().nativeErrorCode()));
1242 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1244 .arg(precursor_index));
1251 std::size_t scan_mobility_start = 0;
1252 std::size_t scan_mobility_end = 0;
1253 std::vector<std::size_t> tims_frame_list;
1257 tims_frame_list.push_back(q.value(0).toLongLong());
1261 scan_mobility_start = q.value(1).toLongLong();
1262 scan_mobility_end = q.value(2).toLongLong();
1272 for(std::size_t tims_id : tims_frame_list)
1279 if(previous_frame.get() !=
nullptr)
1281 if(previous_frame.get()->hasSameCalibrationData(
1289 "ERROR in %1 %2, different calibration data "
1290 "between frame id %3 and frame id %4")
1293 .arg(previous_frame.get()->getId())
1294 .arg(tims_frame.get()->getId()));
1297 tims_frame.get()->cumulateScansInRawMap(
1298 raw_spectrum, scan_mobility_start, scan_mobility_end);
1301 previous_frame = tims_frame;
1303 qDebug() <<
" precursor_index=" << precursor_index
1304 <<
" num_rows=" << tims_frame_list.size()
1305 <<
" sql=" << q.lastQuery() <<
" "
1306 << (std::size_t)QThread::currentThreadId();
1311 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1313 .arg(precursor_index));
1321 throw PappsoException(QObject::tr(
"ERROR in %1 (precursor_index=%2):\n%3")
1323 .arg(precursor_index)
1324 .arg(error.
qwhat()));
1326 catch(std::exception &error)
1328 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1330 return raw_spectrum;
1340 bool want_binary_data)
1347 spectrum_id.
setNativeId(QString(
"precursor=%1 idxms2=%2")
1355 qDebug() <<
"spectrum_descr.precursor_id=" << spectrum_descr.
precursor_id
1356 <<
" spectrum_descr.ms1_index=" << spectrum_descr.
ms1_index
1357 <<
" spectrum_descr.ms2_index=" << spectrum_descr.
ms2_index;
1368 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
1394 std::map<quint32, quint32> raw_spectrum;
1398 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1399 <<
" tims_id=" << tims_id;
1400 if(want_binary_data)
1402 qDebug() <<
"bindec";
1415 tims_frame.get()->getOneOverK0Transformation(
1420 tims_frame.get()->getOneOverK0Transformation(
1427 if(want_binary_data)
1433 if(previous_frame.get() !=
nullptr)
1435 if(previous_frame.get()->hasSameCalibrationData(
1443 "ERROR in %1 %2, different calibration data "
1444 "between frame id %3 and frame id %4")
1447 .arg(previous_frame.get()->getId())
1448 .arg(tims_frame.get()->getId()));
1451 tims_frame.get()->cumulateScansInRawMap(
1457 previous_frame = tims_frame;
1459 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1461 << (std::size_t)QThread::currentThreadId();
1466 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1470 if(want_binary_data)
1472 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1478 tims_frame.get()->getTraceFromCumulatedScansBuiltinCentroid(
1486 tims_frame.get()->getTraceFromCumulatedScans(raw_spectrum);
1489 if(trace.size() > 0)
1491 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1492 <<
" " << trace.size() <<
" "
1493 << (std::size_t)QThread::currentThreadId();
1507 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1526 QObject::tr(
"ERROR in %1 (ms2_index=%2 precursor_index=%3):\n%4")
1530 .arg(error.
qwhat()));
1532 catch(std::exception &error)
1534 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1543 unsigned int ms_level)
1545 qDebug() <<
" ms_level=" << ms_level;
1546 QSqlDatabase qdb = openDatabaseConnection();
1547 QSqlQuery qprecursor_list = qdb.exec(QString(
1548 "SELECT PasefFrameMsMsInfo.Frame, "
1549 "PasefFrameMsMsInfo.ScanNumBegin, "
1550 "PasefFrameMsMsInfo.ScanNumEnd, "
1551 "PasefFrameMsMsInfo.IsolationMz, "
1552 "PasefFrameMsMsInfo.IsolationWidth, "
1553 "PasefFrameMsMsInfo.CollisionEnergy, "
1554 "PasefFrameMsMsInfo.Precursor, "
1556 "Precursors.LargestPeakMz, "
1557 "Precursors.AverageMz, "
1558 "Precursors.MonoisotopicMz, "
1559 "Precursors.Charge, "
1560 "Precursors.ScanNumber, "
1561 "Precursors.Intensity, "
1562 "Precursors.Parent "
1563 "FROM PasefFrameMsMsInfo "
1564 "INNER JOIN Precursors ON "
1565 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1566 "ORDER BY PasefFrameMsMsInfo.Precursor, PasefFrameMsMsInfo.Frame ;"));
1567 if(qprecursor_list.lastError().isValid())
1571 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1572 "command %2:\n%3\n%4\n%5")
1573 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1574 .arg(qprecursor_list.lastQuery())
1575 .arg(qdb.lastError().databaseText())
1576 .arg(qdb.lastError().driverText())
1577 .arg(qdb.lastError().nativeErrorCode()));
1581 qDebug() <<
"qprecursor_list.size()=" << qprecursor_list.size();
1582 qDebug() << QObject::tr(
1583 "TIMS sqlite database file %1, executing SQL "
1584 "command %2:\n%3\n%4\n%5")
1585 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1586 .arg(qprecursor_list.lastQuery())
1587 .arg(qdb.lastError().databaseText())
1588 .arg(qdb.lastError().driverText())
1589 .arg(qdb.lastError().nativeErrorCode());
1591 qDebug() <<
"qprecursor_list.isActive()=" << qprecursor_list.isActive();
1592 qDebug() <<
"qprecursor_list.isSelect()=" << qprecursor_list.isSelect();
1601 qprecursor_list.last();
1604 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1605 qprecursor_list.first();
1609 qDebug() <<
"i=" << i;
1614 qDebug() <<
"The operation was cancelled. Breaking the loop.";
1616 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
1617 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf")));
1619 qDebug() <<
" spectrum_descr.precursor_id ="
1621 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1622 << qprecursor_list.value(6).toLongLong();
1625 (std::size_t)qprecursor_list.value(6).toLongLong())
1631 ms2ReaderGenerateMS1MS2Spectrum(
1632 msrun_id, handler, spectrum_descr, ms_level);
1638 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1639 << qprecursor_list.value(6).toLongLong();
1641 (std::size_t)qprecursor_list.value(6).toLongLong();
1642 qDebug() <<
" spectrum_descr.precursor_id ="
1644 qDebug() <<
" cumul tims frame:" << qprecursor_list.value(0).toLongLong();
1646 qprecursor_list.value(0).toLongLong());
1647 qDebug() <<
" first =" << first;
1656 qprecursor_list.value(11).toInt(),
1657 qprecursor_list.value(13).toDouble());
1664 qprecursor_list.value(1).toLongLong();
1666 qprecursor_list.value(2).toLongLong();
1668 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1669 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1671 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1681 while(qprecursor_list.next());
1686 ms2ReaderGenerateMS1MS2Spectrum(
1687 msrun_id, handler, spectrum_descr, ms_level);
1697 unsigned int ms_level)
1700 qDebug() <<
" ms_level=" << ms_level;
1703 if((ms_level == 0) || (ms_level == 1))
1707 getQualifiedMs1MassSpectrumByPrecursorId(msrun_id,
1714 if((ms_level == 0) || (ms_level == 2))
1719 getQualifiedMs2MassSpectrumByPrecursorId(msrun_id,
1734 QSqlDatabase qdb = openDatabaseConnection();
1735 QSqlQuery q = qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1736 "PasefFrameMsMsInfo.ScanNumBegin, "
1737 "PasefFrameMsMsInfo.ScanNumEnd, "
1738 "PasefFrameMsMsInfo.IsolationMz, "
1739 "PasefFrameMsMsInfo.IsolationWidth, "
1740 "PasefFrameMsMsInfo.CollisionEnergy, "
1741 "PasefFrameMsMsInfo.Precursor, "
1743 "Precursors.LargestPeakMz, "
1744 "Precursors.AverageMz, "
1745 "Precursors.MonoisotopicMz, "
1746 "Precursors.Charge, "
1747 "Precursors.ScanNumber, "
1748 "Precursors.Intensity, "
1749 "Precursors.Parent "
1750 "FROM PasefFrameMsMsInfo "
1751 "INNER JOIN Precursors ON "
1752 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1753 "WHERE Precursors.Id=%1;")
1754 .arg(precursor_id));
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()));
1773 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1782 q.value(11).toInt(),
1783 q.value(13).toDouble());
1792 spectrum_descr.
isolationMz = q.value(3).toDouble();
1795 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1804 QObject::tr(
"ERROR in %1 %2 : precursor id (%3) NOT FOUND ")
1807 .arg(precursor_id));
1809 return spectrum_descr;
1815 std::vector<double> timeline;
1816 auto it_map_record_frame = m_mapFramesRecord.begin();
1817 auto it_map_record_frame_end = m_mapFramesRecord.end();
1818 while(it_map_record_frame != it_map_record_frame_end)
1821 if(it_map_record_frame->second.value(6).toInt() ==
1823 timeline.push_back(it_map_record_frame->second.value(5).toDouble());
1824 it_map_record_frame++;
1831 const std::pair<std::size_t, std::size_t> &scan_coordinate)
1835 QSqlDatabase qdb = openDatabaseConnection();
1837 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1838 "PasefFrameMsMsInfo.ScanNumBegin, "
1839 "PasefFrameMsMsInfo.ScanNumEnd, "
1840 "PasefFrameMsMsInfo.IsolationMz, "
1841 "PasefFrameMsMsInfo.IsolationWidth, "
1842 "PasefFrameMsMsInfo.CollisionEnergy, "
1843 "PasefFrameMsMsInfo.Precursor, "
1845 "Precursors.LargestPeakMz, "
1846 "Precursors.AverageMz, "
1847 "Precursors.MonoisotopicMz, "
1848 "Precursors.Charge, "
1849 "Precursors.ScanNumber, "
1850 "Precursors.Intensity, "
1851 "Precursors.Parent "
1852 "FROM PasefFrameMsMsInfo "
1853 "INNER JOIN Precursors ON "
1854 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1856 "PasefFrameMsMsInfo.Frame=%1 and "
1857 "(PasefFrameMsMsInfo.ScanNumBegin "
1858 "<= %2 and PasefFrameMsMsInfo.ScanNumEnd >= %2);")
1859 .arg(scan_coordinate.first)
1860 .arg(scan_coordinate.second));
1861 if(q.lastError().isValid())
1865 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1866 "command %2:\n%3\n%4\n%5")
1867 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1869 .arg(qdb.lastError().databaseText())
1870 .arg(qdb.lastError().driverText())
1871 .arg(qdb.lastError().nativeErrorCode()));
1877 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1883 q.value(10).toDouble(), q.value(11).toInt(), q.value(13).toDouble());
1892 spectrum_descr.
isolationMz = q.value(3).toDouble();
1895 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1897 return spectrum_descr;
1908 qprecursor_list.value(0).toLongLong());
1914 qprecursor_list.value(11).toInt(),
1915 qprecursor_list.value(13).toDouble());
1917 spectrum_descr.
precursor_id = qprecursor_list.value(6).toLongLong();
1924 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1925 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1927 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1935 unsigned int ms_level)
1940 std::size_t spectrum_list_size = getTotalNumberOfScans();
1948 QSqlDatabase qdb = openDatabaseConnection();
1949 QSqlQuery qprecursor_list = qdb.exec(QString(
1951 "PasefFrameMsMsInfo.Frame, "
1952 "PasefFrameMsMsInfo.ScanNumBegin, "
1953 "PasefFrameMsMsInfo.ScanNumEnd, "
1954 "PasefFrameMsMsInfo.IsolationMz, "
1955 "PasefFrameMsMsInfo.IsolationWidth, "
1956 "PasefFrameMsMsInfo.CollisionEnergy, "
1957 "PasefFrameMsMsInfo.Precursor, "
1959 "Precursors.LargestPeakMz, "
1960 "Precursors.AverageMz, "
1961 "Precursors.MonoisotopicMz, "
1962 "Precursors.Charge, "
1963 "Precursors.ScanNumber, "
1964 "Precursors.Intensity, "
1965 "Precursors.Parent "
1966 "FROM PasefFrameMsMsInfo "
1967 "INNER JOIN Precursors ON "
1968 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1969 "ORDER BY PasefFrameMsMsInfo.Frame, PasefFrameMsMsInfo.ScanNumBegin ;"));
1970 if(qprecursor_list.lastError().isValid())
1974 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1975 "command %2:\n%3\n%4\n%5")
1976 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf"))
1977 .arg(qprecursor_list.lastQuery())
1978 .arg(qdb.lastError().databaseText())
1979 .arg(qdb.lastError().driverText())
1980 .arg(qdb.lastError().nativeErrorCode()));
1986 qprecursor_list.last();
1989 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1990 qprecursor_list.first();
2002 qDebug() <<
"The operation was cancelled. Breaking the loop.";
2004 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
2005 .arg(m_timsDataDirectory.absoluteFilePath(
"analysis.tdf")));
2008 tims_frame = getTimsFrameBaseCstSPtrCached(current_frame.m_frameId);
2009 unsigned int tims_ms_level = tims_frame.get()->getMsLevel();
2011 if((ms_level != 0) && (ms_level != tims_ms_level))
2013 i += current_frame.m_size;
2018 qDebug() <<
"want_binary_data=" << want_binary_data;
2019 if(want_binary_data)
2021 qDebug() <<
"bindec";
2022 tims_frame = getTimsFrameCstSPtrCached(current_frame.m_frameId);
2025 bool possible_precursor =
false;
2026 if(tims_ms_level == 2)
2029 while(qprecursor_list.value(0).toULongLong() <
2030 current_frame.m_frameId)
2032 qprecursor_list.next();
2034 if(qprecursor_list.value(0).toULongLong() ==
2035 current_frame.m_frameId)
2037 possible_precursor =
true;
2039 fillSpectrumDescriptionWithSqlRecord(spectrum_descr,
2044 for(std::size_t scan_num = 0; scan_num < current_frame.m_size;
2047 bool has_a_precursor =
false;
2048 if(possible_precursor)
2053 while(qprecursor_list.value(0).toULongLong() <
2054 current_frame.m_frameId)
2056 qprecursor_list.next();
2058 if(qprecursor_list.value(0).toULongLong() !=
2059 current_frame.m_frameId)
2061 possible_precursor =
false;
2063 fillSpectrumDescriptionWithSqlRecord(spectrum_descr,
2068 if(possible_precursor &&
2072 has_a_precursor =
true;
2083 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
2084 .arg(current_frame.m_frameId)
2090 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
2094 tims_frame.get()->getDriftTime(scan_num));
2098 tims_frame.get()->getOneOverK0Transformation(scan_num));
2101 if(want_binary_data)
2106 tims_frame.get()->getMassSpectrumSPtr(scan_num));
2112 "ERROR in %1 (scan_num=%2 spectrum_index=%3):\n%4")
2116 .arg(error.
qwhat()));
2118 if(mass_spectrum.
size() > 0)
2138 std::size_t prec_spectrum_index =
2143 prec_spectrum_index);
2145 QString(
"frame=%1 scan=%2 index=%3")
2148 .arg(prec_spectrum_index));
2173 std::map<quint32, quint32>
2177 qDebug() <<
" spectrum_index=" << spectrum_index;
2178 auto coordinate = getScanCoordinateFromRawIndex(spectrum_index);
2180 tims_frame = getTimsFrameCstSPtrCached(coordinate.first);
2182 std::map<quint32, quint32> raw_spectrum;
2183 tims_frame.get()->cumulateScansInRawMap(
2184 raw_spectrum, coordinate.second, coordinate.second);
2185 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
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