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 
28 #include "timsframesmsrunreader.h"
29 #include "../../exception/exceptionnotimplemented.h"
30 #include <QDebug>
31 
32 using namespace pappso;
33 
35  : MsRunReader(msrun_id_csp)
36 {
37  qDebug() << "Now initializing the TimsFramesMsRunReader.";
38 
39  initialize();
40 }
41 
43 {
44  msp_timsData = nullptr;
45 }
46 
47 void
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 
61 bool
62 TimsFramesMsRunReader::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 
77  return pappso::MassSpectrumSPtr();
78 }
79 
80 
82 TimsFramesMsRunReader::massSpectrumCstSPtr(std::size_t spectrum_index)
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 
101 void
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=%1 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 
184 void
186  [[maybe_unused]] SpectrumCollectionHandlerInterface &handler,
187  [[maybe_unused]] unsigned int ms_level)
188 {
189 
190  qDebug();
191 }
192 
193 
194 std::size_t
196 {
197  return msp_timsData->getTotalNumberOfScans();
198 }
199 
200 
201 bool
203 {
204  return false;
205 }
206 
207 
208 bool
210 {
211  msp_timsData = nullptr;
212  return true;
213 }
214 
215 bool
217 {
218  if(msp_timsData == nullptr)
219  {
220  initialize();
221  }
222  return true;
223 }
224 
225 
228  std::size_t spectrum_index [[maybe_unused]],
229  pappso::PrecisionPtr precision [[maybe_unused]]) const
230 {
231  throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
232  .arg(__FILE__)
233  .arg(__FUNCTION__)
234  .arg(__LINE__));
235 }
236 
239  const pappso::QualifiedMassSpectrum &mass_spectrum [[maybe_unused]],
240  pappso::PrecisionPtr precision [[maybe_unused]]) const
241 {
242  throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
243  .arg(__FILE__)
244  .arg(__FUNCTION__)
245  .arg(__LINE__));
246 }
247 
250 {
251  acquireDevice();
252  return msp_timsData;
253 }
254 
255 
256 Trace
258 {
259  // Use the Sqlite database to fetch the total ion current chromatogram (TIC
260  // chromatogram).
261 
262  acquireDevice();
263 
264  return msp_timsData->getTicChromatogram();
265 }
266 
267 
268 Trace
270 {
271 
272  // We want to compute the TIC chromatogram, not load the chromatogram that is
273  // located in the SQL database.
274  //
275  // For this, we need to iterated into the frames and ask for MS1 spectra only.
276  // msp_timsData has that information:
277  //
278  // std::vector<FrameIdDescr> m_frameIdDescrList;
279  //
280  // and
281 
282  // struct FrameIdDescr
283  // {
284  // std::size_t m_frameId; // frame id
285  // std::size_t m_size; // frame size (number of TOF scans in frame)
286  // std::size_t m_cumulSize; // cumulative size
287  // };
288 
289  Trace tic_chromatogram;
290 
291  const std::vector<FrameIdDescr> frame_descr_list =
292  msp_timsData->getFrameIdDescrList();
293 
294  for(FrameIdDescr frame_id_descr : frame_descr_list)
295  {
296  TimsFrameCstSPtr tims_frame_csp =
297  msp_timsData->getTimsFrameCstSPtrCached(frame_id_descr.m_frameId);
298  std::size_t scan_begin = 0;
299  std::size_t scan_end = tims_frame_csp->getTotalNumberOfScans() - 1;
300 
301  // By convention, a TIC chromatogram is only performed using MS1 spectra.
302  if(tims_frame_csp->getMsLevel() == 1)
303  {
304 
305  double rt = tims_frame_csp->getTime();
306 
307  tic_chromatogram.append(DataPoint(
308  rt,
309  tims_frame_csp->cumulateScansIntensities(scan_begin, scan_end)));
310  }
311  else
312  continue;
313  }
314 
315  return tic_chromatogram;
316 }
317 
void setNativeId(const QString &native_id)
void setMsRunId(MsRunIdCstSPtr other)
void setSpectrumIndex(std::size_t index)
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:192
MsRunIdCstSPtr mcsp_msRunId
Definition: msrunreader.h:301
const MsRunIdCstSPtr & getMsRunId() const
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
Definition: msrunreader.h:56
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:53
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 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,...
TimsFramesMsRunReader(MsRunIdCstSPtr &msrun_id_csp)
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:583
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:44
std::shared_ptr< TimsData > TimsDataSp
shared pointer on a TimsData object
Definition: timsdata.h:46
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:41