libpappsomspp
Library for mass spectrometry
timsmsrunreaderms2.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/msrun/private/timsmsrunreaderms2.cpp
3  * \date 10/09/2019
4  * \author Olivier Langella
5  * \brief MSrun file reader for native Bruker TimsTOF specialized for MS2
6  * purpose
7  */
8 
9 
10 /*******************************************************************************
11  * Copyright (c) 2019 Olivier Langella <Olivier.Langella@u-psud.fr>.
12  *
13  * This file is part of the PAPPSOms++ library.
14  *
15  * PAPPSOms++ is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * PAPPSOms++ is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
27  *
28  ******************************************************************************/
29 
30 #include "timsmsrunreaderms2.h"
31 #include "../../exception/exceptionnotimplemented.h"
32 #include "../../exception/exceptionnotfound.h"
33 #include "../../msrun/xiccoord/xiccoordtims.h"
34 #include <QDebug>
35 #include <QtConcurrent/QtConcurrent>
36 
37 using namespace pappso;
38 
40  : MsRunReader(msrun_id_csp)
41 {
42  initialize();
43 }
44 
46 {
47  if(msp_timsData != nullptr)
48  {
49  msp_timsData = nullptr;
50  }
51 }
52 
53 void
55 {
56  msp_timsData = std::make_shared<TimsData>(mcsp_msRunId.get()->getFileName());
57  if(msp_timsData == nullptr)
58  {
59  throw PappsoException(
60  QObject::tr("ERROR in TimsMsRunReaderMs2::initialize "
61  "msp_timsData is null for MsRunId %1")
62  .arg(mcsp_msRunId.get()->toString()));
63  }
64 }
65 
66 void
68 {
69  m_builtinMs2Centroid = centroid;
70  if(msp_timsData != nullptr)
71  {
72  msp_timsData->setMs2BuiltinCentroid(m_builtinMs2Centroid);
73  }
74  else
75  {
76  throw PappsoException(
77  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2BuiltinCentroid "
78  "msp_timsData is null"));
79  }
80 }
81 
82 void
84 {
85  msp_ms2Filter = filter;
86  if(msp_timsData != nullptr)
87  {
88  msp_timsData->setMs2FilterCstSPtr(msp_ms2Filter);
89  }
90  else
91  {
92  throw PappsoException(
93  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2FilterCstSPtr "
94  "msp_timsData is null"));
95  }
96 }
97 
98 void
100 {
101  msp_ms1Filter = filter;
102  if(msp_timsData != nullptr)
103  {
104  msp_timsData->setMs1FilterCstSPtr(filter);
105  }
106  else
107  {
108  throw PappsoException(
109  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs1FilterCstSPtr "
110  "msp_timsData is null"));
111  }
112 }
113 
114 bool
115 TimsMsRunReaderMs2::accept(const QString &file_name) const
116 {
117  qDebug() << file_name;
118  return true;
119 }
120 
121 
123 TimsMsRunReaderMs2::massSpectrumSPtr(std::size_t spectrum_index)
124 {
125  QualifiedMassSpectrum mass_spectrum =
126  qualifiedMassSpectrum(spectrum_index, true);
127  return mass_spectrum.getMassSpectrumSPtr();
128 }
129 
130 
132 TimsMsRunReaderMs2::massSpectrumCstSPtr(std::size_t spectrum_index)
133 {
134  QualifiedMassSpectrum mass_spectrum =
135  qualifiedMassSpectrum(spectrum_index, true);
136  return mass_spectrum.getMassSpectrumSPtr();
137 }
138 
139 
141 TimsMsRunReaderMs2::qualifiedMassSpectrum(std::size_t spectrum_index,
142  bool want_binary_data) const
143 {
144 
145  std::size_t precursor_index = (spectrum_index / 2) + 1;
146  TimsData::SpectrumDescr spectrum_descr;
147  try
148  {
149  spectrum_descr =
150  msp_timsData.get()->getSpectrumDescrWithPrecursorId(precursor_index);
151  }
152  catch(ExceptionNotFound &error)
153  {
154  throw ExceptionNotFound(
155  QObject::tr("spectrum_index %1 NOT FOUND in file %2 : %3")
156  .arg(spectrum_index)
157  .arg(getMsRunId().get()->getFileName())
158  .arg(error.qwhat()));
159  }
160 
161  if(spectrum_index % 2 == 0)
162  {
163  qDebug() << "MS1 spectrum precursor_index=" << precursor_index;
164  // this is an MS1 spectrum
165  QualifiedMassSpectrum mass_spectrum_ms1;
166  msp_timsData->getQualifiedMs1MassSpectrumByPrecursorId(
167  getMsRunId(), mass_spectrum_ms1, spectrum_descr, want_binary_data);
168  qDebug(); // << mass_spectrum_ms1.toString();
169 
170  // qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
171  return mass_spectrum_ms1;
172  }
173  else
174  {
175  qDebug() << "MS2 spectrum precursor_index=" << precursor_index;
176  QualifiedMassSpectrum mass_spectrum_ms2;
177  if(spectrum_descr.ms2_index != spectrum_index)
178  {
179  qDebug();
180  throw PappsoException(
181  QObject::tr("ERROR in %1 %2 %3 spectrum_descr.ms2_index(%4) != "
182  "spectrum_index(%5)")
183  .arg(__FILE__)
184  .arg(__FUNCTION__)
185  .arg(__LINE__)
186  .arg(spectrum_descr.ms2_index)
187  .arg(spectrum_index));
188  }
189 
190  msp_timsData->getQualifiedMs2MassSpectrumByPrecursorId(
191  getMsRunId(), mass_spectrum_ms2, spectrum_descr, want_binary_data);
192  qDebug(); // << mass_spectrum_ms2.toString();
193 
194  // qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
195  return mass_spectrum_ms2;
196  }
197 }
198 
199 
200 void
203 {
205 }
206 
207 void
209  SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)
210 {
211  qDebug() << " ms_level=" << ms_level;
212  // We'll need it to perform the looping in the spectrum list.
213  std::size_t spectrum_list_size = spectrumListSize();
214 
215  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
216 
217  // Inform the handler of the spectrum list so that it can handle feedback to
218  // the user.
219  handler.spectrumListHasSize(spectrum_list_size);
220 
221  msp_timsData.get()->ms2ReaderSpectrumCollectionByMsLevel(
222  getMsRunId(), handler, ms_level);
223  // Now let the loading handler know that the loading of the data has ended.
224  // The handler might need this "signal" to perform additional tasks or to
225  // cleanup cruft.
226 
227  // qDebug() << "Loading ended";
228  handler.loadingEnded();
229 }
230 
231 
232 std::size_t
234 {
235  return (msp_timsData->getTotalNumberOfPrecursors() * 2);
236 }
237 
238 
239 bool
241 {
242  return false;
243 }
244 
245 
246 bool
248 {
249  msp_timsData = nullptr;
250  return true;
251 }
252 
253 bool
255 {
256  if(msp_timsData == nullptr)
257  {
258  initialize();
259  msp_timsData->setMs2BuiltinCentroid(m_builtinMs2Centroid);
260  msp_timsData->setMs1FilterCstSPtr(msp_ms1Filter);
261  msp_timsData->setMs2FilterCstSPtr(msp_ms2Filter);
262  }
263  return true;
264 }
265 
266 std::vector<std::size_t>
268  double mz_val,
269  double rt_sec,
270  double k0)
271 {
272  return msp_timsData->getPrecursorsFromMzRtCharge(charge, mz_val, rt_sec, k0);
273 }
274 
277 {
278  acquireDevice();
279  return msp_timsData;
280 }
281 
282 
285  std::size_t spectrum_index, pappso::PrecisionPtr precision) const
286 {
287  XicCoordTimsSPtr xic_coord = std::make_shared<XicCoordTims>();
288  std::size_t precursor_index = (spectrum_index / 2) + 1;
289  auto xic = this->msp_timsData.get()->getXicCoordTimsFromPrecursorId(
290  precursor_index, precision);
291 
292  xic_coord.get()->mzRange = xic.mzRange;
293  xic_coord.get()->rtTarget = xic.rtTarget;
294  xic_coord.get()->scanNumBegin = xic.scanNumBegin;
295  xic_coord.get()->scanNumEnd = xic.scanNumEnd;
296 
297 
298  return xic_coord;
299 }
300 
303  const pappso::QualifiedMassSpectrum &mass_spectrum,
304  pappso::PrecisionPtr precision) const
305 {
307  mass_spectrum.getMassSpectrumId().getSpectrumIndex(), precision);
308 }
309 
310 std::vector<double>
312 {
313  return msp_timsData.get()->getRetentionTimeLine();
314 }
std::size_t getSpectrumIndex() const
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:173
const MsRunIdCstSPtr & getMsRunId() const
virtual const QString & qwhat() const
Class representing a fully specified mass spectrum.
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:56
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:52
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
virtual QualifiedMassSpectrum qualifiedMassSpectrum(std::size_t spectrum_index, bool want_binary_data=true) const override
get a QualifiedMassSpectrum class given its scan number
TimsMsRunReaderMs2(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 MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
pappso::FilterInterfaceCstSPtr msp_ms2Filter
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
virtual bool releaseDevice() override
release data back end device if a the data back end is released, the developper has to use acquireDev...
pappso::FilterInterfaceCstSPtr msp_ms1Filter
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
virtual std::vector< std::size_t > getPrecursorsIDFromMzRt(int charge, double mz_val, double rt_sec, double k0)
Get all the precursors id which match the values.
virtual void initialize() override
virtual std::vector< double > getRetentionTimeLine() override
retention timeline get retention times along the MSrun in seconds
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
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
virtual bool acquireDevice() override
acquire data back end device
virtual bool hasScanNumbers() const override
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
virtual TimsDataSp getTimsDataSPtr()
give an access to the underlying raw data pointer
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 newXicCoordSPtrFromQualifiedMassSpectrum(const pappso::QualifiedMassSpectrum &mass_spectrum, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum
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
std::shared_ptr< const FilterInterface > FilterInterfaceCstSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:54
std::shared_ptr< XicCoordTims > XicCoordTimsSPtr
Definition: xiccoordtims.h:38
std::shared_ptr< XicCoord > XicCoordSPtr
Definition: xiccoord.h:41
MSrun file reader for native Bruker TimsTOF specialized for MS2 purpose.