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 <QDebug>
33 #include <QtConcurrent/QtConcurrent>
34 
35 using namespace pappso;
36 
38  : MsRunReader(msrun_id_csp)
39 {
40  initialize();
41 }
42 
44 {
45  if(msp_timsData != nullptr)
46  {
47  msp_timsData = nullptr;
48  }
49 }
50 
51 void
53 {
54  msp_timsData = std::make_shared<TimsData>(mcsp_msRunId.get()->getFileName());
55  if(msp_timsData == nullptr)
56  {
57  throw PappsoException(
58  QObject::tr("ERROR in TimsMsRunReaderMs2::initialize "
59  "msp_timsData is null for MsRunId %1")
60  .arg(mcsp_msRunId.get()->toString()));
61  }
62 }
63 
64 void
66 {
67  m_builtinMs2Centroid = centroid;
68  if(msp_timsData != nullptr)
69  {
70  msp_timsData->setMs2BuiltinCentroid(m_builtinMs2Centroid);
71  }
72  else
73  {
74  throw PappsoException(
75  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2BuiltinCentroid "
76  "msp_timsData is null"));
77  }
78 }
79 
80 void
82 {
83  msp_ms2Filter = filter;
84  if(msp_timsData != nullptr)
85  {
86  msp_timsData->setMs2FilterCstSPtr(msp_ms2Filter);
87  }
88  else
89  {
90  throw PappsoException(
91  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2FilterCstSPtr "
92  "msp_timsData is null"));
93  }
94 }
95 
96 void
98 {
99  msp_ms1Filter = filter;
100  if(msp_timsData != nullptr)
101  {
102  msp_timsData->setMs1FilterCstSPtr(filter);
103  }
104  else
105  {
106  throw PappsoException(
107  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs1FilterCstSPtr "
108  "msp_timsData is null"));
109  }
110 }
111 
112 bool
113 TimsMsRunReaderMs2::accept(const QString &file_name) const
114 {
115  qDebug() << file_name;
116  return true;
117 }
118 
119 
121 TimsMsRunReaderMs2::massSpectrumSPtr(std::size_t spectrum_index)
122 {
123  QualifiedMassSpectrum mass_spectrum =
124  qualifiedMassSpectrum(spectrum_index, true);
125  return mass_spectrum.getMassSpectrumSPtr();
126 }
127 
128 
130 TimsMsRunReaderMs2::massSpectrumCstSPtr(std::size_t spectrum_index)
131 {
132  QualifiedMassSpectrum mass_spectrum =
133  qualifiedMassSpectrum(spectrum_index, true);
134  return mass_spectrum.getMassSpectrumSPtr();
135 }
136 
137 
139 TimsMsRunReaderMs2::qualifiedMassSpectrum(std::size_t spectrum_index,
140  bool want_binary_data) const
141 {
142 
143  std::size_t precursor_index = (spectrum_index / 2) + 1;
144 
145  if(spectrum_index % 2 == 0)
146  {
147  qDebug();
148  // this is an MS1 spectrum
149  QualifiedMassSpectrum mass_spectrum_ms1 =
150  msp_timsData->getQualifiedMs1MassSpectrumByPrecursorId(
151  spectrum_index, precursor_index, want_binary_data);
152  MassSpectrumId spectrum_id(mass_spectrum_ms1.getMassSpectrumId());
153  spectrum_id.setMsRunId(getMsRunId());
154  mass_spectrum_ms1.setMassSpectrumId(spectrum_id);
155  qDebug(); // << mass_spectrum_ms1.toString();
156 
157  // qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
158  return mass_spectrum_ms1;
159  }
160  else
161  {
162  qDebug();
163  QualifiedMassSpectrum mass_spectrum_ms2;
164  msp_timsData->getQualifiedMs2MassSpectrumByPrecursorId(
165  mass_spectrum_ms2, spectrum_index, precursor_index, want_binary_data);
166  MassSpectrumId spectrum_id(mass_spectrum_ms2.getMassSpectrumId());
167  spectrum_id.setMsRunId(getMsRunId());
168  mass_spectrum_ms2.setMassSpectrumId(spectrum_id);
169  qDebug(); // << mass_spectrum_ms2.toString();
170 
171  // qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
172  return mass_spectrum_ms2;
173  }
174 }
175 
176 
177 void
180 {
182 }
183 
184 void
186  SpectrumCollectionHandlerInterface &handler, unsigned int ms_level [[maybe_unused]])
187 {
188 
189  const bool want_binary_data = handler.needPeakList();
190  // const bool want_binary_data = false;
191 
192  // We'll need it to perform the looping in the spectrum list.
193  std::size_t spectrum_list_size = spectrumListSize();
194 
195  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
196 
197  // Inform the handler of the spectrum list so that it can handle feedback to
198  // the user.
199  handler.spectrumListHasSize(spectrum_list_size);
200 
201  // Iterate in the full list of spectra.
202  bool readAhead = handler.isReadAhead();
203 
204  if(readAhead)
205  {
206 
207  std::size_t process_list_size = 300;
208 
209  struct tmp_item
210  {
211  QualifiedMassSpectrum qualified_mass_spectrum;
212  std::size_t iter;
213  bool want_binary_data;
214  };
215 
216  for(std::size_t i = 0; i < spectrum_list_size; i += process_list_size)
217  {
218  qDebug();
219  // If the user of this reader instance wants to stop reading the
220  // spectra, then break this loop.
221  if(handler.shouldStop())
222  {
223  qDebug() << "The operation was cancelled. Breaking the loop.";
224  break;
225  }
226  std::vector<tmp_item> item_list;
227  for(std::size_t iter = 0;
228  (iter < process_list_size) && ((iter + i) < spectrum_list_size);
229  iter++)
230  {
231 
232  bool get_data = want_binary_data;
233  if((iter + i) % 2 == 0)
234  { // MS1
235  get_data = handler.needMsLevelPeakList(1);
236  }
237  else
238  {
239  get_data = handler.needMsLevelPeakList(2);
240  }
241 
242  item_list.push_back(
243  {QualifiedMassSpectrum(), iter + i, get_data});
244  }
245  qDebug() << item_list.size();
246  // Use QtConcurrentBlocking::mapped to apply the scale function to all
247  // the images in the list.
248  QtConcurrent::blockingMap(
249  item_list.begin(), item_list.end(), [this](tmp_item &one_item) {
250  qDebug() << one_item.iter;
251  one_item.qualified_mass_spectrum =
252  qualifiedMassSpectrum(one_item.iter, one_item.want_binary_data);
253 
254  // qDebug() << one_item.qualified_mass_spectrum.size() << " " <<
255  // one_item.qualified_mass_spectrum.getMassSpectrumSPtr().get()->toString();
256  });
257 
258  qDebug() << item_list.size();
259  for(auto &item : item_list)
260  {
261  // qDebug() << item.qualified_mass_spectrum.getMassSpectrumSPtr()
262  // .get()
263  // ->toString();
264  handler.setQualifiedMassSpectrum(item.qualified_mass_spectrum);
265  qDebug();
266  }
267  }
268  }
269  else
270  {
271  for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
272  {
273  qDebug();
274  // If the user of this reader instance wants to stop reading the
275  // spectra, then break this loop.
276  if(handler.shouldStop())
277  {
278  qDebug() << "The operation was cancelled. Breaking the loop.";
279  break;
280  }
281  bool get_data = want_binary_data;
282  if(iter % 2 == 0)
283  { // MS1
284  if(!handler.needMsLevelPeakList(1))
285  {
286  get_data = false;
287  }
288  }
289  QualifiedMassSpectrum qualified_mass_spectrum =
290  qualifiedMassSpectrum(iter, get_data);
291  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
292  qDebug();
293  }
294  }
295  // End of
296  // for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
297 
298  // Now let the loading handler know that the loading of the data has ended.
299  // The handler might need this "signal" to perform additional tasks or to
300  // cleanup cruft.
301 
302  // qDebug() << "Loading ended";
303  handler.loadingEnded();
304 }
305 
306 
307 std::size_t
309 {
310  return (msp_timsData->getTotalNumberOfPrecursors() * 2);
311 }
312 
313 
314 bool
316 {
317  return false;
318 }
319 
320 
321 bool
323 {
324  msp_timsData = nullptr;
325  return true;
326 }
327 
328 bool
330 {
331  if(msp_timsData == nullptr)
332  {
333  initialize();
334  msp_timsData->setMs2BuiltinCentroid(m_builtinMs2Centroid);
335  msp_timsData->setMs1FilterCstSPtr(msp_ms1Filter);
336  msp_timsData->setMs2FilterCstSPtr(msp_ms2Filter);
337  }
338  return true;
339 }
340 
341 std::vector<std::size_t>
343  double mz_val,
344  double rt_sec,
345  double k0)
346 {
347  return msp_timsData->getPrecursorsFromMzRtCharge(charge, mz_val, rt_sec, k0);
348 }
349 
352 {
353  acquireDevice();
354  return msp_timsData;
355 }
void setMsRunId(MsRunIdCstSPtr other)
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:158
const MsRunIdCstSPtr & getMsRunId() const
Class representing a fully specified mass spectrum.
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:59
virtual bool isReadAhead() const
tells if we want to read ahead spectrum
Definition: msrunreader.cpp:66
virtual bool needPeakList() const =0
tells if we need the peak list (if we want the binary data) for each spectrum
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
Definition: msrunreader.cpp:72
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:55
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
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 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
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,...
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:81
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
MSrun file reader for native Bruker TimsTOF specialized for MS2 purpose.