libpappsomspp
Library for mass spectrometry
timsframesmsrunreader.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/msrun/private/timsmsrunreader.h
3 * \date 05/09/2019
4 * \author Olivier Langella
5 * \brief MSrun file reader for native Bruker TimsTOF raw data
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2019 Olivier Langella <Olivier.Langella@u-psud.fr>.
10 *
11 * This file is part of the PAPPSOms++ library.
12 *
13 * PAPPSOms++ is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * PAPPSOms++ is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25 *
26 ******************************************************************************/
27
29#include "../../exception/exceptionnotimplemented.h"
30#include <QDebug>
31
32using namespace pappso;
33
34TimsFramesMsRunReader::TimsFramesMsRunReader(MsRunIdCstSPtr &msrun_id_csp)
35 : MsRunReader(msrun_id_csp)
36{
37 qDebug() << "Now initializing the TimsFramesMsRunReader.";
38
39 initialize();
40}
41
43{
44 msp_timsData = nullptr;
45}
46
47void
49{
50 msp_timsData = std::make_shared<TimsData>(mcsp_msRunId.get()->getFileName());
51 if(msp_timsData == nullptr)
52 {
53 throw PappsoException(
54 QObject::tr("ERROR in TimsFramesMsRunReader::initialize "
55 "msp_timsData is null for MsRunId %1")
56 .arg(mcsp_msRunId.get()->toString()));
57 }
58}
59
60
61bool
62TimsFramesMsRunReader::accept(const QString &file_name) const
63{
64 qDebug() << file_name;
65 return true;
66}
67
68
71 [[maybe_unused]] std::size_t spectrum_index)
72{
74 QObject::tr("Not yet implemented in TimsFramesMsRunReader %1.\n")
75 .arg(__LINE__));
76
78}
79
80
83{
84 return msp_timsData->getMassSpectrumCstSPtrByRawIndex(spectrum_index);
85}
86
87
90 bool want_binary_data) const
91{
92
93 QualifiedMassSpectrum mass_spectrum;
94
95 msp_timsData->getQualifiedMassSpectrumByRawIndex(
96 getMsRunId(), mass_spectrum, spectrum_index, want_binary_data);
97 return mass_spectrum;
98}
99
100
101void
104{
105 // Here we want to only handle mass spectra as combined scans spectra, that
106 // is, for each frame, we want all the mobility scans to be cumulated (at the
107 // integer level, that is, without conversion of all the integers to m/z
108 // values) so as to yield a *single* mass spectrum per frame.
109
110 // We can iterate in the vector of frames records and for each frame ask that
111 // all its scans be combined into a single mass spectrum.
112
113 const std::vector<FrameIdDescr> &frame_id_descr_list =
114 msp_timsData->getFrameIdDescrList();
115
116 // The scan index is the index of the scan in the *whole* mass data file, it
117 // is a sequential number of scan over all the frames.
118 std::size_t scan_index = 0; // iterate in each spectrum
119
120 // We'll need it to perform the looping in the spectrum list.
121 std::size_t frames_count = msp_timsData->getTotalNumberOfFrames();
122
123 qDebug() << "The spectrum list has this much frames:" << frames_count;
124
125 // Inform the handler of the spectrum list so that it can handle feedback to
126 // the user.
127 handler.spectrumListHasSize(frames_count);
128
129 for(const FrameIdDescr &current_frame : frame_id_descr_list)
130 {
131 TimsFrameCstSPtr tims_frame =
132 msp_timsData->getTimsFrameCstSPtrCached(current_frame.m_frameId);
133
134 // Get to know the size of the frame, that is, the number of mobility
135 // scans in it.
136
137 quint32 scan_count = tims_frame->getTotalNumberOfScans();
138
139 Trace spectrum = tims_frame->cumulateScanToTrace(0, scan_count - 1);
140
141 QualifiedMassSpectrum mass_spectrum;
142
143 MassSpectrumId spectrum_id;
144
145 spectrum_id.setSpectrumIndex(scan_index);
146
147 spectrum_id.setMsRunId(getMsRunId());
148
149 // Can modify to help our case
150 spectrum_id.setNativeId(QString("frame id =%1 scan index=%2")
151 .arg(current_frame.m_frameId)
152 .arg(scan_index));
153
154 mass_spectrum.setMassSpectrumId(spectrum_id);
155
156 // We want to document the retention time!
157 mass_spectrum.setRtInSeconds(tims_frame.get()->getTime());
158
159 // We do want to document the ms level of the spectrum and possibly the
160 // precursor's m/z and charge.
161 unsigned int frame_ms_level = tims_frame.get()->getMsLevel();
162 mass_spectrum.setMsLevel(frame_ms_level);
163
164 if(frame_ms_level > 1)
165 {
166 // FIXME
167 // We want to document the precusor's m/z, z and index.
168 }
169
170 mass_spectrum.setDtInMilliSeconds(-1);
171
172 mass_spectrum.setEmptyMassSpectrum(false);
173
174 mass_spectrum.setMassSpectrumSPtr(
175 std::make_shared<MassSpectrum>(spectrum));
176
177 handler.setQualifiedMassSpectrum(mass_spectrum);
178
179 scan_index += scan_count;
180 }
181}
182
183
184void
186 [[maybe_unused]] SpectrumCollectionHandlerInterface &handler,
187 [[maybe_unused]] unsigned int ms_level)
188{
189 qDebug();
190}
191
192void
194const MsRunReadConfig &config,
196{
197 qDebug() << "20230713 with" << config.getRetentionTimeStart() << "-" << config.getRetentionTimeEnd();
198
199 // We want to restrict the data reading process to the configuration provided
200 // as parameter.
201
202 // We have the TimsData that hold a list of FrameIdDescr that hold the
203 // retention time. We could use that list to search for the right retention
204 // times.
205
206 double rt_start = config.getRetentionTimeStart();
207 double rt_end = config.getRetentionTimeEnd();
208
209 // If retention time is not a critrion, load all frames.
210 if((rt_start < 0 && rt_end < 0) || (rt_end < rt_start))
211 return readSpectrumCollection(handler);
212
213 // Retention time is indeed a criterium to select mass spectra to be
214 // loaded.
215
216 const std::vector<TimsFrameRecord> &frame_record_list =
217 msp_timsData->getTimsFrameRecordList();
218
219 std::size_t frame_record_list_size = frame_record_list.size();
220
221 std::size_t list_start_index = 0;
222 std::size_t list_end_index = 0;
223
224 for(std::size_t iter = 0; iter < frame_record_list_size; ++iter)
225 {
226 const TimsFrameRecord &frame_record = frame_record_list.at(iter);
227
228 bool start_found = false;
229
230 if(frame_record.frame_time < rt_start)
231 {
232 // We have not reached the start of the RT range yet. Move
233 // the start index to the current index.
234 list_start_index = iter;
235 }
236 else
237 {
238 // We encounter one RT value that matches the start
239 // requirement. Is this the very first one such RT value ?
240 if(!start_found)
241 {
242 // This is indeed the very first time we encounter a RT
243 // value matching the start requirement. Store the
244 // indices and set the bool to true.
245 list_start_index = iter;
246 list_end_index = iter;
247
248 start_found = true;
249 }
250 else
251 {
252 // We did already encounter an RT value matching the start
253 // requirement. Do not change the start index, then.
254 // Now check the RT end requirement.
255 if(frame_record.frame_time <= rt_end)
256 {
257 // We still are fine with the RT end requirement.
258 // Update the end index.
259 list_end_index = iter;
260 }
261 else
262 {
263 // We are already outside of the searched RT range.
264 // Just break.
265 break;
266 }
267 }
268 }
269 }
270
271 // At this point, we have two indices into the frame_record_list. Note that
272 // the indices in the frame_record_list *are* the frame_id (see timsdata.cpp,
273 // around line 200).
274
275 std::size_t frames_count = list_end_index - list_start_index;
276 qDebug() << "The number of retained RT range-matching frames:"
277 << frames_count;
278 handler.spectrumListHasSize(frames_count);
279
280 // We need to get to the list of frame descriptors.
281 const std::vector<FrameIdDescr> &frame_id_descr_list =
282 msp_timsData->getFrameIdDescrList();
283
284 // The scan index is the index of the scan in the *whole* mass data file, it
285 // is a sequential number of scan over all the frames.
286 std::size_t scan_index = 0; // iterate in each spectrum
287
288 // In order to compute the spectrum index, we need to iterate in the
289 // frame_id_descr_list from the very first frame desriptor to the last before
290 // the one we will start to load in the next loop.
291 for(std::size_t iter = 0; iter < list_start_index; ++iter)
292 {
293 TimsFrameCstSPtr tims_frame = msp_timsData->getTimsFrameCstSPtrCached(
294 frame_id_descr_list.at(iter).m_frameId);
295
296 // Get to know the size of the frame, that is, the number of mobility
297 // scans in it.
298 quint32 scan_count = tims_frame->getTotalNumberOfScans();
299 scan_index += scan_count;
300 }
301
302 // We can now start iterating in the positions where we actually want to load
303 // frames. We will keep incrementing the scan_index.
304
305 for(std::size_t iter = list_start_index; iter < list_end_index; ++iter)
306 {
307 TimsFrameCstSPtr tims_frame =
308 msp_timsData->getTimsFrameCstSPtrCached(iter);
309
310 // Get to know the size of the frame, that is, the number of mobility
311 // scans in it.
312 quint32 scan_count = tims_frame->getTotalNumberOfScans();
313
314 Trace spectrum = tims_frame->cumulateScanToTrace(0, scan_count - 1);
315
316 QualifiedMassSpectrum mass_spectrum;
317
318 MassSpectrumId spectrum_id;
319 spectrum_id.setSpectrumIndex(scan_index);
320 spectrum_id.setMsRunId(getMsRunId());
321
322 // Can modify to help our case
323 spectrum_id.setNativeId(QString("frame id=%1 scan index=%2")
324 .arg(iter)
325 .arg(scan_index));
326
327 mass_spectrum.setMassSpectrumId(spectrum_id);
328
329 // We want to document the retention time!
330 mass_spectrum.setRtInSeconds(tims_frame.get()->getTime());
331
332 // We do want to document the ms level of the spectrum and possibly the
333 // precursor's m/z and charge.
334 unsigned int frame_ms_level = tims_frame.get()->getMsLevel();
335 mass_spectrum.setMsLevel(frame_ms_level);
336
337 if(frame_ms_level > 1)
338 {
339 // FIXME
340 // We want to document the precusor's m/z, z and index.
341 }
342
343 mass_spectrum.setDtInMilliSeconds(-1);
344
345 mass_spectrum.setEmptyMassSpectrum(false);
346
347 mass_spectrum.setMassSpectrumSPtr(
348 std::make_shared<MassSpectrum>(spectrum));
349
350 handler.setQualifiedMassSpectrum(mass_spectrum);
351
352 scan_index += scan_count;
353 }
354}
355
356std::size_t
358{
359 return msp_timsData->getTotalNumberOfScans();
360}
361
362
363bool
365{
366 return false;
367}
368
369
370bool
372{
373 msp_timsData = nullptr;
374 return true;
375}
376
377bool
379{
380 if(msp_timsData == nullptr)
381 {
382 initialize();
383 }
384 return true;
385}
386
387
390 std::size_t spectrum_index [[maybe_unused]],
391 pappso::PrecisionPtr precision [[maybe_unused]]) const
392{
393 throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
394 .arg(__FILE__)
395 .arg(__FUNCTION__)
396 .arg(__LINE__));
397}
398
401 const pappso::QualifiedMassSpectrum &mass_spectrum [[maybe_unused]],
402 pappso::PrecisionPtr precision [[maybe_unused]]) const
403{
404 throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
405 .arg(__FILE__)
406 .arg(__FUNCTION__)
407 .arg(__LINE__));
408}
409
412{
414 return msp_timsData;
415}
416
417
418Trace
420{
421 // Use the Sqlite database to fetch the total ion current chromatogram (TIC
422 // chromatogram).
423
425
426 return msp_timsData->getTicChromatogram();
427}
428
429
430Trace
432{
433
434 // We want to compute the TIC chromatogram, not load the chromatogram that
435 // is located in the SQL database.
436 //
437 // For this, we need to iterated into the frames and ask for MS1 spectra
438 // only. msp_timsData has that information:
439 //
440 // std::vector<FrameIdDescr> m_frameIdDescrList;
441 //
442 // and
443
444 // struct FrameIdDescr
445 // {
446 // std::size_t m_frameId; // frame id
447 // std::size_t m_size; // frame size (number of TOF scans in frame)
448 // std::size_t m_cumulSize; // cumulative size
449 // };
450
451 Trace tic_chromatogram;
452
453 const std::vector<FrameIdDescr> frame_descr_list =
454 msp_timsData->getFrameIdDescrList();
455
456 for(FrameIdDescr frame_id_descr : frame_descr_list)
457 {
458 TimsFrameCstSPtr tims_frame_csp =
459 msp_timsData->getTimsFrameCstSPtrCached(frame_id_descr.m_frameId);
460 std::size_t scan_begin = 0;
461 std::size_t scan_end = tims_frame_csp->getTotalNumberOfScans() - 1;
462
463 // By convention, a TIC chromatogram is only performed using MS1
464 // spectra.
465 if(tims_frame_csp->getMsLevel() == 1)
466 {
467
468 double rt = tims_frame_csp->getTime();
469
470 tic_chromatogram.append(DataPoint(
471 rt,
472 tims_frame_csp->cumulateScansIntensities(scan_begin, scan_end)));
473 }
474 else
475 continue;
476 }
477
478 return tic_chromatogram;
479}
480
481
void setNativeId(const QString &native_id)
void setMsRunId(MsRunIdCstSPtr other)
void setSpectrumIndex(std::size_t index)
double getRetentionTimeEnd() const
double getRetentionTimeStart() const
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:63
MsRunIdCstSPtr mcsp_msRunId
Definition: msrunreader.h:175
const MsRunIdCstSPtr & getMsRunId() const
Definition: msrunreader.cpp:56
Class representing a fully specified mass spectrum.
void setDtInMilliSeconds(pappso_double rt)
Set the drift time in milliseconds.
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
void setMsLevel(uint ms_level)
Set the mass spectrum level.
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 void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
virtual pappso::XicCoordSPtr newXicCoordSPtrFromQualifiedMassSpectrum(const pappso::QualifiedMassSpectrum &mass_spectrum, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum
virtual QualifiedMassSpectrum qualifiedMassSpectrum(std::size_t spectrum_index, bool want_binary_data=true) const override
get a QualifiedMassSpectrum class given its scan number
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
virtual Trace getTicChromatogram() override
get a TIC chromatogram
virtual bool releaseDevice() override
release data back end device if a the data back end is released, the developper has to use acquireDev...
virtual void initialize() override
virtual void readSpectrumCollection2(const MsRunReadConfig &config, SpectrumCollectionHandlerInterface &handler) override
virtual TimsDataSp getTimsDataSPtr()
give an access to the underlying raw data pointer
virtual bool hasScanNumbers() const override
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
virtual bool acquireDevice() override
acquire data back end device
virtual bool accept(const QString &file_name) const override
tells if the reader is able to handle this file must be implemented by private MS run reader,...
virtual pappso::XicCoordSPtr newXicCoordSPtrFromSpectrumIndex(std::size_t spectrum_index, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum index
virtual void readSpectrumCollectionByMsLevel(SpectrumCollectionHandlerInterface &handler, unsigned int ms_level) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler by Ms Levels
A simple container of DataPoint instances.
Definition: trace.h:148
size_t append(const DataPoint &data_point)
appends a datapoint and return new size
Definition: trace.cpp:648
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:46
std::shared_ptr< TimsData > TimsDataSp
shared pointer on a TimsData object
Definition: timsdata.h:50
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
@ rt
Retention time.
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:54
std::shared_ptr< const TimsFrame > TimsFrameCstSPtr
Definition: timsframe.h:42
std::shared_ptr< XicCoord > XicCoordSPtr
Definition: xiccoord.h:43