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/filtersuitestring.h"
38#include <QMutexLocker>
41#include <QtConcurrent>
47 : m_timsDataDirectory(timsDataDirectory)
50 qDebug() <<
"Start of construction of TimsData";
55 QObject::tr(
"ERROR TIMS data directory %1 not found")
63 QObject::tr(
"ERROR TIMS data directory, %1 sqlite file not found")
72 if(!q.exec(
"select Key, Value from GlobalMetadata where "
73 "Key='TimsCompressionType';"))
78 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
79 "command %2:\n%3\n%4\n%5")
82 .arg(q.lastError().databaseText())
83 .arg(q.lastError().driverText())
84 .arg(q.lastError().nativeErrorCode()));
88 int compression_type = 0;
91 compression_type = q.value(1).toInt();
93 qDebug() <<
" compression_type=" << compression_type;
102 if(!q.exec(
"SELECT COUNT( DISTINCT Id) FROM Precursors;"))
119 if(!q.exec(
"SELECT SUM(NumScans),COUNT(Id) FROM Frames"))
123 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
124 "command %2:\n%3\n%4\n%5")
127 .arg(qdb.lastError().databaseText())
128 .arg(qdb.lastError().driverText())
129 .arg(qdb.lastError().nativeErrorCode()));
137 if(!q.exec(
"select * from MzCalibration;"))
141 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
142 "command %2:\n%3\n%4\n%5")
145 .arg(q.lastError().databaseText())
146 .arg(q.lastError().driverText())
147 .arg(q.lastError().nativeErrorCode()));
152 QSqlRecord record = q.record();
154 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
159 if(!q.exec(
"select * from TimsCalibration;"))
163 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
164 "command %2:\n%3\n%4\n%5")
167 .arg(q.lastError().databaseText())
168 .arg(q.lastError().driverText())
169 .arg(q.lastError().nativeErrorCode()));
173 QSqlRecord record = q.record();
175 std::pair<int, QSqlRecord>(record.value(0).toInt(), record));
180 if(!q.exec(
"select Frames.TimsId, Frames.AccumulationTime, "
181 "Frames.MzCalibration, "
182 "Frames.T1, Frames.T2, "
183 "Frames.Time, Frames.MsMsType, Frames.TimsCalibration, "
189 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
190 "command %2:\n%3\n%4\n%5")
193 .arg(q.lastError().databaseText())
194 .arg(q.lastError().driverText())
195 .arg(q.lastError().nativeErrorCode()));
201 QSqlRecord record = q.record();
205 frame_record.
frame_id = record.value(8).toULongLong();
206 frame_record.
tims_offset = record.value(0).toULongLong();
209 frame_record.
frame_t1 = record.value(3).toDouble();
210 frame_record.
frame_t2 = record.value(4).toDouble();
211 frame_record.
frame_time = record.value(5).toDouble();
212 frame_record.
msms_type = record.value(6).toInt();
217 "chargeDeconvolution|0.02dalton mzExclusion|0.01dalton");
220 std::shared_ptr<FilterTriangle> ms1filter =
221 std::make_shared<FilterTriangle>();
222 ms1filter.get()->setTriangleSlope(50, 0.01);
236 QString database_connection_name = QString(
"%1_%2")
238 .arg((quintptr)QThread::currentThread());
240 QSqlDatabase qdb = QSqlDatabase::database(database_connection_name);
243 qDebug() << database_connection_name;
244 qdb = QSqlDatabase::addDatabase(
"QSQLITE", database_connection_name);
253 QObject::tr(
"ERROR opening TIMS sqlite database file %1, database name "
256 .arg(database_connection_name)
257 .arg(qdb.lastError().databaseText())
258 .arg(qdb.lastError().driverText())
259 .arg(qdb.lastError().nativeErrorCode()));
301 qdb.exec(QString(
"SELECT Id, NumScans FROM "
302 "Frames ORDER BY Id"));
303 if(q.lastError().isValid())
307 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
308 "command %2:\n%3\n%4\n%5")
311 .arg(qdb.lastError().databaseText())
312 .arg(qdb.lastError().driverText())
313 .arg(qdb.lastError().nativeErrorCode()));
316 bool index_found =
false;
320 std::size_t numberScans;
321 std::size_t cumulScans = 0;
322 while(q.next() && (!index_found))
324 timsId = q.value(0).toULongLong();
325 numberScans = q.value(1).toULongLong();
330 std::pair<std::size_t, std::size_t>((cumulScans / 1000),
334 cumulScans += numberScans;
339std::pair<std::size_t, std::size_t>
343 std::size_t fast_access = raw_index / 1000;
344 qDebug() <<
" fast_access=" << fast_access;
349 QObject::tr(
"ERROR raw index %1 not found (fast_access)")
352 std::size_t start_point_index = map_it->second;
353 while((start_point_index > 0) &&
364 return std::pair<std::size_t, std::size_t>(
371 QObject::tr(
"ERROR raw index %1 not found").arg(raw_index));
377 std::size_t scan_num)
const
382 if(frameDescr.m_frameId == frame_id)
384 return frameDescr.m_cumulSize + scan_num;
389 QObject::tr(
"ERROR raw index with frame=%1 scan=%2 not found")
402 qDebug() <<
" raw_index=" << raw_index;
411 QObject::tr(
"Error TimsData::getMassSpectrumCstSPtrByRawIndex "
412 "raw_index=%1 :\n%2")
414 .arg(error.
qwhat()));
423 qDebug() <<
" timsId=" << timsId;
429 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
434 tims_frame = std::make_shared<TimsFrameBase>(
442 double T1_frame = frame_record.
frame_t1;
443 double T2_frame = frame_record.
frame_t2;
446 tims_frame.get()->setMzCalibrationInterfaceSPtr(
448 T1_frame, T2_frame, it_map_record->second));
453 QObject::tr(
"ERROR MzCalibration database id %1 not found")
459 tims_frame.get()->setTime(frame_record.
frame_time);
460 tims_frame.get()->setMsMsType(frame_record.
msms_type);
463 auto it_map_record_tims_calibration =
468 tims_frame.get()->setTimsCalibration(
469 it_map_record_tims_calibration->second.value(1).toInt(),
470 it_map_record_tims_calibration->second.value(2).toDouble(),
471 it_map_record_tims_calibration->second.value(3).toDouble(),
472 it_map_record_tims_calibration->second.value(4).toDouble(),
473 it_map_record_tims_calibration->second.value(5).toDouble(),
474 it_map_record_tims_calibration->second.value(6).toDouble(),
475 it_map_record_tims_calibration->second.value(7).toDouble(),
476 it_map_record_tims_calibration->second.value(8).toDouble(),
477 it_map_record_tims_calibration->second.value(9).toDouble(),
478 it_map_record_tims_calibration->second.value(10).toDouble(),
479 it_map_record_tims_calibration->second.value(11).toDouble());
484 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
491std::vector<std::size_t>
495 qDebug() <<
" rt_begin=" << rt_begin <<
" rt_end=" << rt_end;
498 std::vector<std::size_t> tims_frameid_list;
500 QSqlQuery q = qdb.exec(QString(
"SELECT Frames.Id FROM Frames WHERE "
501 "Frames.MsMsType=0 AND (Frames.Time>=%1) AND "
502 "(Frames.Time<=%2) ORDER BY Frames.Time;")
505 if(q.lastError().isValid())
509 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
511 "command %3:\n%4\n%5\n%6")
513 .arg(qdb.databaseName())
515 .arg(qdb.lastError().databaseText())
516 .arg(qdb.lastError().driverText())
517 .arg(qdb.lastError().nativeErrorCode()));
522 tims_frameid_list.push_back(q.value(0).toULongLong());
524 return tims_frameid_list;
531 qDebug() <<
" timsId=" << timsId
546 QObject::tr(
"ERROR Frames database id %1 not found").arg(timsId));
563 double T1_frame = frame_record.
frame_t1;
564 double T2_frame = frame_record.
frame_t2;
567 tims_frame.get()->setMzCalibrationInterfaceSPtr(
569 T1_frame, T2_frame, it_map_record->second));
575 "ERROR MzCalibration database id %1 not found for frame_id=%2")
582 tims_frame.get()->setTime(frame_record.
frame_time);
583 tims_frame.get()->setMsMsType(frame_record.
msms_type);
586 auto it_map_record_tims_calibration =
591 tims_frame.get()->setTimsCalibration(
592 it_map_record_tims_calibration->second.value(1).toInt(),
593 it_map_record_tims_calibration->second.value(2).toDouble(),
594 it_map_record_tims_calibration->second.value(3).toDouble(),
595 it_map_record_tims_calibration->second.value(4).toDouble(),
596 it_map_record_tims_calibration->second.value(5).toDouble(),
597 it_map_record_tims_calibration->second.value(6).toDouble(),
598 it_map_record_tims_calibration->second.value(7).toDouble(),
599 it_map_record_tims_calibration->second.value(8).toDouble(),
600 it_map_record_tims_calibration->second.value(9).toDouble(),
601 it_map_record_tims_calibration->second.value(10).toDouble(),
602 it_map_record_tims_calibration->second.value(11).toDouble());
607 QObject::tr(
"ERROR TimsCalibration database id %1 not found")
618 qDebug() <<
" timsId=" << timsId <<
" scanNum=" << scanNum;
621 return frame->getMassSpectrumCstSPtr(scanNum);
645std::vector<std::size_t>
651 std::vector<std::size_t> precursor_ids;
652 std::vector<std::vector<double>> ids;
655 QSqlQuery q = qdb.exec(
657 "SELECT Frames.Time, Precursors.MonoisotopicMz, Precursors.Charge, "
658 "Precursors.Id, Frames.Id, PasefFrameMsMsInfo.ScanNumBegin, "
659 "PasefFrameMsMsInfo.scanNumEnd "
661 "INNER JOIN PasefFrameMsMsInfo ON Frames.Id = PasefFrameMsMsInfo.Frame "
662 "INNER JOIN Precursors ON PasefFrameMsMsInfo.Precursor = Precursors.Id "
663 "WHERE Precursors.Charge == %1 "
664 "AND Precursors.MonoisotopicMz > %2 -0.01 "
665 "AND Precursors.MonoisotopicMz < %2 +0.01 "
666 "AND Frames.Time >= %3 -1 "
667 "AND Frames.Time < %3 +1; ")
671 if(q.lastError().isValid())
675 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
677 "command %3:\n%4\n%5\n%6")
679 .arg(qdb.databaseName())
681 .arg(qdb.lastError().databaseText())
682 .arg(qdb.lastError().driverText())
683 .arg(qdb.lastError().nativeErrorCode()));
690 std::vector<double> sql_values;
691 sql_values.push_back(q.value(4).toDouble());
692 sql_values.push_back(q.value(3).toDouble());
693 sql_values.push_back(q.value(5).toDouble());
694 sql_values.push_back(q.value(6).toDouble());
695 sql_values.push_back(q.value(1).toDouble());
697 ids.push_back(sql_values);
700 if(std::find(precursor_ids.begin(),
702 q.value(3).toDouble()) == precursor_ids.end())
704 precursor_ids.push_back(q.value(3).toDouble());
708 if(precursor_ids.size() > 1)
712 if(precursor_ids.size() > 1)
716 return precursor_ids;
720 return precursor_ids;
724std::vector<std::size_t>
728 std::vector<std::size_t> precursor_id;
729 for(std::vector<double> index : ids)
736 double bko = tims_frame.get()->getOneOverK0Transformation(index[2]);
737 double eko = tims_frame.get()->getOneOverK0Transformation(index[3]);
740 double mean_ko = (bko + eko) / 2;
742 if(mean_ko > ko_value - 0.1 && mean_ko < ko_value + 0.1)
744 precursor_id.push_back(index[1]);
750std::vector<std::size_t>
754 std::vector<std::size_t> best_precursor;
755 double best_value = 1;
757 int best_val_position = 0;
759 for(std::vector<double> values : ids)
761 double new_val = abs(mz_value - values[4]);
762 if(new_val < best_value)
764 best_value = new_val;
765 best_val_position = count;
769 best_precursor.push_back(ids[best_val_position][1]);
770 return best_precursor;
779 return tims_frame.get()->getMsLevel();
787 std::size_t spectrum_index,
788 bool want_binary_data)
806 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
807 .arg(coordinate.first)
808 .arg(coordinate.second)
809 .arg(spectrum_index));
813 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
817 tims_frame.get()->getDriftTime(coordinate.second));
821 tims_frame.get()->getOneOverK0Transformation(coordinate.second));
827 tims_frame.get()->getMassSpectrumSPtr(coordinate.second));
828 if(mass_spectrum.
size() > 0)
840 if(tims_frame.get()->getMsLevel() > 1)
844 if(spectrum_descr.precursor_id > 0)
848 spectrum_descr.precursor_ion_data);
853 spectrum_descr.parent_frame, coordinate.second);
857 QString(
"frame=%1 scan=%2 index=%3")
858 .arg(spectrum_descr.parent_frame)
859 .arg(coordinate.second)
860 .arg(prec_spectrum_index));
864 spectrum_descr.isolationMz);
867 spectrum_descr.isolationWidth);
871 spectrum_descr.collisionEnergy);
874 (quint64)spectrum_descr.precursor_id);
881 QObject::tr(
"Error TimsData::getQualifiedMassSpectrumByRawIndex "
882 "spectrum_index=%1 :\n%2")
884 .arg(error.
qwhat()));
898 using Pair = std::pair<double, double>;
899 using Map = std::map<double, double>;
900 using Iterator = Map::iterator;
905 qdb.exec(QString(
"SELECT Time, SummedIntensities "
906 "FROM Frames WHERE MsMsType = 0 "
909 if(q.lastError().isValid())
913 QObject::tr(
"ERROR in TIMS sqlite database file %1, database name %2, "
915 "command %3:\n%4\n%5\n%6")
917 .arg(qdb.databaseName())
919 .arg(qdb.lastError().databaseText())
920 .arg(qdb.lastError().driverText())
921 .arg(qdb.lastError().nativeErrorCode()));
929 int cumulated_results = 2;
931 double rt = q.value(0).toDouble(&ok);
932 cumulated_results -= ok;
934 double sumY = q.value(1).toDouble(&ok);
935 cumulated_results -= ok;
937 if(cumulated_results)
941 "ERROR in TIMS sqlite database file: could not read either the "
942 "retention time or the summed intensities (%1, database name "
945 "command %3:\n%4\n%5\n%6")
947 .arg(qdb.databaseName())
949 .arg(qdb.lastError().databaseText())
950 .arg(qdb.lastError().driverText())
951 .arg(qdb.lastError().nativeErrorCode()));
955 std::pair<Iterator, bool>
res = rt_tic_map_trace.insert(Pair(
rt, sumY));
962 res.first->second += sumY;
969 return rt_tic_map_trace.
toTrace();
978 bool want_binary_data)
981 qDebug() <<
" ms2_index=" << spectrum_descr.
ms2_index
996 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
1009 if(want_binary_data)
1011 qDebug() <<
"bindec";
1023 tims_frame.get()->getOneOverK0Transformation(
1028 tims_frame.get()->getOneOverK0Transformation(
1032 if(want_binary_data)
1034 combiner.
combine(combiner_result,
1035 tims_frame.get()->cumulateScansToTrace(
1039 Trace trace(combiner_result);
1042 if(trace.size() > 0)
1067 catch(std::exception &error)
1069 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1077 QMutexLocker locker(&
m_mutex);
1080 if(tims_frame.get()->getId() == timsId)
1099 QMutexLocker locker(&
m_mutex);
1102 if(tims_frame.get()->getId() == timsId)
1220 QSqlQuery q = qdb.exec(
1221 QString(
"SELECT Precursors.id, "
1222 "min(Frames.Time), "
1223 "min(PasefFrameMsMsInfo.ScanNumBegin), "
1224 "max(PasefFrameMsMsInfo.ScanNumEnd), "
1225 "Precursors.MonoisotopicMz "
1227 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1228 "PasefFrameMsMsInfo.Precursor=Precursors.Id INNER JOIN "
1229 "Frames ON PasefFrameMsMsInfo.Frame=Frames.Id "
1230 "GROUP BY Precursors.id;"));
1231 if(q.lastError().isValid())
1236 "ERROR in TIMS sqlite database file %1, executing SQL "
1237 "command %2:\n%3\n%4\n%5")
1240 .arg(qdb.lastError().databaseText())
1241 .arg(qdb.lastError().driverText())
1242 .arg(qdb.lastError().nativeErrorCode()));
1251 QSqlRecord record = q.record();
1253 (std::size_t)record.value(0).toULongLong(), record));
1264 QObject::tr(
"ERROR Precursors database id %1 not found")
1265 .arg(precursor_id));
1268 auto &q = it_map_xiccoord->second;
1269 xic_coord_tims_struct.
mzRange =
1270 MzRange(q.value(4).toDouble(), precision_ptr);
1271 xic_coord_tims_struct.
scanNumBegin = q.value(2).toUInt();
1272 xic_coord_tims_struct.
scanNumEnd = q.value(3).toUInt();
1273 xic_coord_tims_struct.
rtTarget = q.value(1).toDouble();
1275 xic_coord_tims_struct.
xicSptr = std::make_shared<Xic>();
1281 catch(std::exception &error)
1283 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1285 return xic_coord_tims_struct;
1289std::map<quint32, quint32>
1293 std::map<quint32, quint32> raw_spectrum;
1300 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.*, Precursors.* FROM "
1301 "PasefFrameMsMsInfo INNER JOIN Precursors ON "
1302 "PasefFrameMsMsInfo.Precursor=Precursors.Id where "
1303 "Precursors.Id=%1;")
1304 .arg(precursor_index));
1305 if(q.lastError().isValid())
1309 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1310 "command %2:\n%3\n%4\n%5")
1313 .arg(qdb.lastError().databaseText())
1314 .arg(qdb.lastError().driverText())
1315 .arg(qdb.lastError().nativeErrorCode()));
1324 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1326 .arg(precursor_index));
1333 std::size_t scan_mobility_start = 0;
1334 std::size_t scan_mobility_end = 0;
1335 std::vector<std::size_t> tims_frame_list;
1339 tims_frame_list.push_back(q.value(0).toLongLong());
1343 scan_mobility_start = q.value(1).toLongLong();
1344 scan_mobility_end = q.value(2).toLongLong();
1354 for(std::size_t tims_id : tims_frame_list)
1361 if(previous_frame.get() !=
nullptr)
1363 if(previous_frame.get()->hasSameCalibrationData(
1371 "ERROR in %1 %2, different calibration data "
1372 "between frame id %3 and frame id %4")
1375 .arg(previous_frame.get()->getId())
1376 .arg(tims_frame.get()->getId()));
1379 tims_frame.get()->cumulateScansInRawMap(
1380 raw_spectrum, scan_mobility_start, scan_mobility_end);
1383 previous_frame = tims_frame;
1385 qDebug() <<
" precursor_index=" << precursor_index
1386 <<
" num_rows=" << tims_frame_list.size()
1387 <<
" sql=" << q.lastQuery() <<
" "
1388 << (std::size_t)QThread::currentThreadId();
1393 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1395 .arg(precursor_index));
1403 throw PappsoException(QObject::tr(
"ERROR in %1 (precursor_index=%2):\n%3")
1405 .arg(precursor_index)
1406 .arg(error.
qwhat()));
1408 catch(std::exception &error)
1410 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1412 return raw_spectrum;
1422 bool want_binary_data)
1430 spectrum_id.
setNativeId(QString(
"precursor=%1 idxms2=%2")
1438 qDebug() <<
"spectrum_descr.precursor_id=" << spectrum_descr.
precursor_id
1439 <<
" spectrum_descr.ms1_index=" << spectrum_descr.
ms1_index
1440 <<
" spectrum_descr.ms2_index=" << spectrum_descr.
ms2_index;
1451 QString(
"frame=%1 begin=%2 end=%3 precursor=%4 idxms1=%5")
1477 std::map<quint32, quint32> raw_spectrum;
1481 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1482 <<
" tims_id=" << tims_id
1483 << (std::size_t)QThread::currentThreadId();
1485 if(want_binary_data)
1487 qDebug() <<
"bindec";
1494 qDebug() << (std::size_t)QThread::currentThreadId();
1502 tims_frame.get()->getOneOverK0Transformation(
1507 tims_frame.get()->getOneOverK0Transformation(
1514 if(want_binary_data)
1520 if(previous_frame.get() !=
nullptr)
1522 if(previous_frame.get()->hasSameCalibrationData(
1530 "ERROR in %1 %2, different calibration data "
1531 "between frame id %3 and frame id %4")
1534 .arg(previous_frame.get()->getId())
1535 .arg(tims_frame.get()->getId()));
1538 qDebug() << (std::size_t)QThread::currentThreadId();
1540 tims_frame.get()->cumulateScansInRawMap(
1544 qDebug() << (std::size_t)QThread::currentThreadId();
1547 previous_frame = tims_frame;
1549 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1551 << (std::size_t)QThread::currentThreadId();
1556 "ERROR in getQualifiedMassSpectrumByPrecursorId, precursor "
1560 if(want_binary_data)
1562 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1568 tims_frame.get()->getTraceFromCumulatedScansBuiltinCentroid(
1576 tims_frame.get()->getTraceFromCumulatedScans(raw_spectrum);
1579 if(trace.size() > 0)
1581 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id
1582 <<
" " << trace.size() <<
" "
1583 << (std::size_t)QThread::currentThreadId();
1597 qDebug() <<
" precursor_index=" << spectrum_descr.
precursor_id;
1616 QObject::tr(
"ERROR in %1 (ms2_index=%2 precursor_index=%3):\n%4")
1620 .arg(error.
qwhat()));
1622 catch(std::exception &error)
1624 qDebug() << QString(
"Failure %1 ").arg(error.
what());
1633 unsigned int ms_level)
1635 qDebug() <<
" ms_level=" << ms_level;
1639 QObject::tr(
"unable to read spectrum list : this data file does not "
1640 "contain MS2 data, no precursor found."));
1644 QSqlQuery qprecursor_list = qdb.exec(QString(
1645 "SELECT PasefFrameMsMsInfo.Frame, "
1646 "PasefFrameMsMsInfo.ScanNumBegin, "
1647 "PasefFrameMsMsInfo.ScanNumEnd, "
1648 "PasefFrameMsMsInfo.IsolationMz, "
1649 "PasefFrameMsMsInfo.IsolationWidth, "
1650 "PasefFrameMsMsInfo.CollisionEnergy, "
1651 "PasefFrameMsMsInfo.Precursor, "
1653 "Precursors.LargestPeakMz, "
1654 "Precursors.AverageMz, "
1655 "Precursors.MonoisotopicMz, "
1656 "Precursors.Charge, "
1657 "Precursors.ScanNumber, "
1658 "Precursors.Intensity, "
1659 "Precursors.Parent "
1660 "FROM PasefFrameMsMsInfo "
1661 "INNER JOIN Precursors ON "
1662 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1663 "ORDER BY PasefFrameMsMsInfo.Precursor, PasefFrameMsMsInfo.Frame ;"));
1664 if(qprecursor_list.lastError().isValid())
1668 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1669 "command %2:\n%3\n%4\n%5")
1671 .arg(qprecursor_list.lastQuery())
1672 .arg(qdb.lastError().databaseText())
1673 .arg(qdb.lastError().driverText())
1674 .arg(qdb.lastError().nativeErrorCode()));
1678 qDebug() <<
"qprecursor_list.size()=" << qprecursor_list.size();
1679 qDebug() << QObject::tr(
1680 "TIMS sqlite database file %1, executing SQL "
1681 "command %2:\n%3\n%4\n%5")
1683 .arg(qprecursor_list.lastQuery())
1684 .arg(qdb.lastError().databaseText())
1685 .arg(qdb.lastError().driverText())
1686 .arg(qdb.lastError().nativeErrorCode());
1688 qDebug() <<
"qprecursor_list.isActive()=" << qprecursor_list.isActive();
1689 qDebug() <<
"qprecursor_list.isSelect()=" << qprecursor_list.isSelect();
1701 qprecursor_list.last();
1704 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
1705 qprecursor_list.first();
1706 std::vector<TimsData::SpectrumDescr> spectrum_description_list;
1714 (std::size_t)qprecursor_list.value(6).toLongLong())
1719 spectrum_description_list.push_back(spectrum_descr);
1725 qDebug() <<
" qprecursor_list.value(6).toLongLong() ="
1726 << qprecursor_list.value(6).toLongLong();
1728 (std::size_t)qprecursor_list.value(6).toLongLong();
1729 qDebug() <<
" spectrum_descr.precursor_id ="
1731 qDebug() <<
" cumul tims frame:" << qprecursor_list.value(0).toLongLong();
1733 qprecursor_list.value(0).toLongLong());
1734 qDebug() <<
" first =" << first;
1743 qprecursor_list.value(11).toInt(),
1744 qprecursor_list.value(13).toDouble());
1751 qprecursor_list.value(1).toLongLong();
1753 qprecursor_list.value(2).toLongLong();
1755 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
1756 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
1758 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
1768 while(qprecursor_list.next());
1775 spectrum_description_list.push_back(spectrum_descr);
1783 for(
SpectrumDescr &spectrum_descr : spectrum_description_list)
1786 std::vector<QualifiedMassSpectrum> mass_spectrum_list;
1788 msrun_id, mass_spectrum_list, handler, spectrum_descr, ms_level);
1790 for(
auto &qualified_spectrum : mass_spectrum_list)
1797 qDebug() <<
"The operation was cancelled. Breaking the loop.";
1799 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
1800 .arg(local_filepath));
1812 std::function<std::vector<QualifiedMassSpectrum>(
1814 map_function_generate_spectrum =
1815 [itself, msrun_id, pointer_handler, ms_level](
1817 -> std::vector<QualifiedMassSpectrum> {
1818 std::vector<QualifiedMassSpectrum> mass_spectrum_list;
1826 return mass_spectrum_list;
1831 const std::vector<QualifiedMassSpectrum> &qualified_spectrum_list)>
1832 reduce_function_spectrum_list =
1833 [pointer_handler, local_filepath](
1835 const std::vector<QualifiedMassSpectrum> &qualified_spectrum_list) {
1836 for(
auto &qualified_spectrum : qualified_spectrum_list)
1843 qDebug() <<
"The operation was cancelled. Breaking the loop.";
1845 QObject::tr(
"reading TimsTOF job on %1 cancelled by the user")
1846 .arg(local_filepath));
1852 QFuture<std::size_t>
res;
1853 res = QtConcurrent::mappedReduced<std::size_t>(
1854 spectrum_description_list.begin(),
1855 spectrum_description_list.end(),
1856 map_function_generate_spectrum,
1857 reduce_function_spectrum_list,
1858 QtConcurrent::OrderedReduce);
1859 res.waitForFinished();
1869 std::vector<QualifiedMassSpectrum> &qualified_mass_spectrum_list,
1872 unsigned int ms_level)
1875 qDebug() <<
" ms_level=" << ms_level;
1878 if((ms_level == 0) || (ms_level == 1))
1883 qualified_mass_spectrum_list.back(),
1887 if((ms_level == 0) || (ms_level == 2))
1892 qualified_mass_spectrum_list.back(),
1906 QSqlQuery q = qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
1907 "PasefFrameMsMsInfo.ScanNumBegin, "
1908 "PasefFrameMsMsInfo.ScanNumEnd, "
1909 "PasefFrameMsMsInfo.IsolationMz, "
1910 "PasefFrameMsMsInfo.IsolationWidth, "
1911 "PasefFrameMsMsInfo.CollisionEnergy, "
1912 "PasefFrameMsMsInfo.Precursor, "
1914 "Precursors.LargestPeakMz, "
1915 "Precursors.AverageMz, "
1916 "Precursors.MonoisotopicMz, "
1917 "Precursors.Charge, "
1918 "Precursors.ScanNumber, "
1919 "Precursors.Intensity, "
1920 "Precursors.Parent "
1921 "FROM PasefFrameMsMsInfo "
1922 "INNER JOIN Precursors ON "
1923 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
1924 "WHERE Precursors.Id=%1;")
1925 .arg(precursor_id));
1926 if(q.lastError().isValid())
1930 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
1931 "command %2:\n%3\n%4\n%5")
1934 .arg(qdb.lastError().databaseText())
1935 .arg(qdb.lastError().driverText())
1936 .arg(qdb.lastError().nativeErrorCode()));
1944 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
1953 q.value(11).toInt(),
1954 q.value(13).toDouble());
1963 spectrum_descr.
isolationMz = q.value(3).toDouble();
1966 spectrum_descr.
parent_frame = q.value(14).toLongLong();
1975 QObject::tr(
"ERROR in %1 %2 : precursor id (%3) NOT FOUND ")
1978 .arg(precursor_id));
1980 return spectrum_descr;
1986 std::vector<double> timeline;
1990 if(frame_record.mz_calibration_id != 0)
1992 timeline.push_back(frame_record.frame_time);
2000 const std::pair<std::size_t, std::size_t> &scan_coordinate)
2006 qdb.exec(QString(
"SELECT PasefFrameMsMsInfo.Frame, "
2007 "PasefFrameMsMsInfo.ScanNumBegin, "
2008 "PasefFrameMsMsInfo.ScanNumEnd, "
2009 "PasefFrameMsMsInfo.IsolationMz, "
2010 "PasefFrameMsMsInfo.IsolationWidth, "
2011 "PasefFrameMsMsInfo.CollisionEnergy, "
2012 "PasefFrameMsMsInfo.Precursor, "
2014 "Precursors.LargestPeakMz, "
2015 "Precursors.AverageMz, "
2016 "Precursors.MonoisotopicMz, "
2017 "Precursors.Charge, "
2018 "Precursors.ScanNumber, "
2019 "Precursors.Intensity, "
2020 "Precursors.Parent "
2021 "FROM PasefFrameMsMsInfo "
2022 "INNER JOIN Precursors ON "
2023 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
2025 "PasefFrameMsMsInfo.Frame=%1 and "
2026 "(PasefFrameMsMsInfo.ScanNumBegin "
2027 "<= %2 and PasefFrameMsMsInfo.ScanNumEnd >= %2);")
2028 .arg(scan_coordinate.first)
2029 .arg(scan_coordinate.second));
2030 if(q.lastError().isValid())
2034 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
2035 "command %2:\n%3\n%4\n%5")
2038 .arg(qdb.lastError().databaseText())
2039 .arg(qdb.lastError().driverText())
2040 .arg(qdb.lastError().nativeErrorCode()));
2046 qDebug() <<
" cumul tims frame:" << q.value(0).toLongLong();
2052 q.value(10).toDouble(), q.value(11).toInt(), q.value(13).toDouble());
2061 spectrum_descr.
isolationMz = q.value(3).toDouble();
2064 spectrum_descr.
parent_frame = q.value(14).toLongLong();
2066 return spectrum_descr;
2077 qprecursor_list.value(0).toLongLong());
2083 qprecursor_list.value(11).toInt(),
2084 qprecursor_list.value(13).toDouble());
2086 spectrum_descr.
precursor_id = qprecursor_list.value(6).toLongLong();
2093 spectrum_descr.
isolationMz = qprecursor_list.value(3).toDouble();
2094 spectrum_descr.
isolationWidth = qprecursor_list.value(4).toDouble();
2096 spectrum_descr.
parent_frame = qprecursor_list.value(14).toLongLong();
2104 unsigned int ms_level)
2110 QObject::tr(
"unable to read spectrum list : this data file does not "
2111 "contain MS2 data, no precursor found."));
2124 QSqlQuery qprecursor_list = qdb.exec(QString(
2126 "PasefFrameMsMsInfo.Frame, "
2127 "PasefFrameMsMsInfo.ScanNumBegin, "
2128 "PasefFrameMsMsInfo.ScanNumEnd, "
2129 "PasefFrameMsMsInfo.IsolationMz, "
2130 "PasefFrameMsMsInfo.IsolationWidth, "
2131 "PasefFrameMsMsInfo.CollisionEnergy, "
2132 "PasefFrameMsMsInfo.Precursor, "
2134 "Precursors.LargestPeakMz, "
2135 "Precursors.AverageMz, "
2136 "Precursors.MonoisotopicMz, "
2137 "Precursors.Charge, "
2138 "Precursors.ScanNumber, "
2139 "Precursors.Intensity, "
2140 "Precursors.Parent "
2141 "FROM PasefFrameMsMsInfo "
2142 "INNER JOIN Precursors ON "
2143 "PasefFrameMsMsInfo.Precursor=Precursors.Id "
2144 "ORDER BY PasefFrameMsMsInfo.Frame, PasefFrameMsMsInfo.ScanNumBegin ;"));
2145 if(qprecursor_list.lastError().isValid())
2148 QObject::tr(
"ERROR in TIMS sqlite database file %1, executing SQL "
2149 "command %2:\n%3\n%4\n%5")
2151 .arg(qprecursor_list.lastQuery())
2152 .arg(qdb.lastError().databaseText())
2153 .arg(qdb.lastError().driverText())
2154 .arg(qdb.lastError().nativeErrorCode()));
2160 qprecursor_list.last();
2163 qDebug() <<
"qprecursor_list.at()=" << qprecursor_list.at();
2164 qprecursor_list.first();
2176 qDebug() <<
"The operation was cancelled. Breaking the loop.";
2178 QObject::tr(
"reading TimsTOF job cancelled by the user :\n%1")
2183 unsigned int tims_ms_level = tims_frame.get()->getMsLevel();
2185 if((ms_level != 0) && (ms_level != tims_ms_level))
2187 i += current_frame.m_size;
2192 qDebug() <<
"want_binary_data=" << want_binary_data;
2193 if(want_binary_data)
2195 qDebug() <<
"bindec";
2199 bool possible_precursor =
false;
2200 if(tims_ms_level == 2)
2203 while(qprecursor_list.value(0).toULongLong() <
2204 current_frame.m_frameId)
2206 qprecursor_list.next();
2208 if(qprecursor_list.value(0).toULongLong() ==
2209 current_frame.m_frameId)
2211 possible_precursor =
true;
2218 for(std::size_t scan_num = 0; scan_num < current_frame.m_size;
2221 bool has_a_precursor =
false;
2222 if(possible_precursor)
2227 while(qprecursor_list.value(0).toULongLong() <
2228 current_frame.m_frameId)
2230 qprecursor_list.next();
2232 if(qprecursor_list.value(0).toULongLong() !=
2233 current_frame.m_frameId)
2235 possible_precursor =
false;
2242 if(possible_precursor &&
2246 has_a_precursor =
true;
2258 spectrum_id.
setNativeId(QString(
"frame=%1 scan=%2 index=%3")
2259 .arg(current_frame.m_frameId)
2265 mass_spectrum.
setMsLevel(tims_frame.get()->getMsLevel());
2269 tims_frame.get()->getDriftTime(scan_num));
2273 tims_frame.get()->getOneOverK0Transformation(scan_num));
2276 if(want_binary_data)
2281 tims_frame.get()->getMassSpectrumSPtr(scan_num));
2287 "ERROR in %1 (scan_num=%2 spectrum_index=%3):\n%4")
2291 .arg(error.
qwhat()));
2293 if(mass_spectrum.
size() > 0)
2313 std::size_t prec_spectrum_index =
2318 prec_spectrum_index);
2320 QString(
"frame=%1 scan=%2 index=%3")
2323 .arg(prec_spectrum_index));
2348std::map<quint32, quint32>
2352 qDebug() <<
" spectrum_index=" << spectrum_index;
2357 std::map<quint32, quint32> raw_spectrum;
2358 tims_frame.get()->cumulateScansInRawMap(
2359 raw_spectrum, coordinate.second, coordinate.second);
2360 return raw_spectrum;
2364const std::vector<FrameIdDescr> &
2370const std::vector<TimsFrameRecord> &
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)
const char * what() const noexcept override
virtual const QString & qwhat() const
Class representing a fully specified mass spectrum.
void setPrecursorNativeId(const QString &native_id)
Set the scan native id of the precursor ion.
void setDtInMilliSeconds(pappso_double rt)
Set the drift time in milliseconds.
void appendPrecursorIonData(const PrecursorIonData &precursor_ion_data)
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
void setMsLevel(uint ms_level)
Set the mass spectrum level.
void setPrecursorSpectrumIndex(std::size_t precursor_scan_num)
Set the scan number of the precursor ion.
void setParameterValue(QualifiedMassSpectrumParameter parameter, const QVariant &value)
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
void setEmptyMassSpectrum(bool is_empty_mass_spectrum)
interface to collect spectrums from the MsRunReader class
virtual bool shouldStop()
virtual void loadingEnded()
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 frameId, const std::vector< pappso::TimsFrameRecord > &frame_record_list)
void closeLinearRead()
close file access and flush cache
QSqlDatabase openDatabaseConnection() const
TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId)
get a Tims frame with his database ID
const std::vector< TimsFrameRecord > & getTimsFrameRecordList() const
std::vector< FrameIdDescr > m_frameIdDescrList
store every frame id and corresponding sizes
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
void getQualifiedMs1MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, const SpectrumDescr &spectrum_descr, bool want_binary_data)
const std::vector< FrameIdDescr > & getFrameIdDescrList() const
std::map< std::size_t, QSqlRecord > m_mapXicCoordRecord
std::size_t getTotalNumberOfFrames() const
Get total number of frames.
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 getQualifiedMs2MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, const SpectrumDescr &spectrum_descr, bool want_binary_data)
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 precursorCMakeLists.txt.userCMakeLists.txt.userCMakeLists....
std::size_t m_totalNumberOfFrames
Trace getTicChromatogram() const
TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId)
std::size_t m_totalNumberOfScans
std::deque< TimsFrameCstSPtr > m_timsFrameCache
std::pair< std::size_t, std::size_t > getScanCoordinateFromRawIndex(std::size_t spectrum_index) const
std::vector< TimsFrameRecord > m_mapFramesRecord
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
std::vector< std::size_t > getClosestPrecursorIdByMz(std::vector< std::vector< double > > ids, double mz_value)
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...
SpectrumDescr getSpectrumDescrWithScanCoordinate(const std::pair< std::size_t, std::size_t > &scan_coordinate)
std::size_t getTotalNumberOfPrecursors() const
get the number of precursors analyzed 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
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...
void ms2ReaderGenerateMS1MS2Spectrum(const MsRunIdCstSPtr &msrun_id, std::vector< QualifiedMassSpectrum > &qualified_mass_spectrum_list, SpectrumCollectionHandlerInterface &handler, const SpectrumDescr &spectrum_descr, unsigned int ms_level)
TimsBinDec * mpa_timsBinDec
void getQualifiedMassSpectrumByRawIndex(const MsRunIdCstSPtr &msrun_id, QualifiedMassSpectrum &mass_spectrum, std::size_t spectrum_index, bool want_binary_data)
XicCoordTims getXicCoordTimsFromPrecursorId(std::size_t precursor_id, PrecisionPtr precision_ptr)
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 setMonoThread(bool is_mono_thread)
set only one is_mono_thread to true
std::size_t m_totalNumberOfPrecursors
virtual MapTrace & combine(MapTrace &map_trace, const Trace &trace) const override
A simple container of DataPoint instances.
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
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
std::size_t mz_calibration_id
std::size_t tims_calibration_id
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