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 }
56 
57 void
59 {
60  m_builtinMs2Centroid = centroid;
61  if(msp_timsData != nullptr)
62  {
63  msp_timsData->setMs2BuiltinCentroid(m_builtinMs2Centroid);
64  }
65  else
66  {
67  throw PappsoException(
68  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2BuiltinCentroid "
69  "msp_timsData is null"));
70  }
71 }
72 
73 void
75 {
76  msp_ms2Filter = filter;
77  if(msp_timsData != nullptr)
78  {
79  msp_timsData->setMs2FilterCstSPtr(msp_ms2Filter);
80  }
81  else
82  {
83  throw PappsoException(
84  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2FilterCstSPtr "
85  "msp_timsData is null"));
86  }
87 }
88 
89 void
91 {
92  msp_ms1Filter = filter;
93  if(msp_timsData != nullptr)
94  {
95  msp_timsData->setMs1FilterCstSPtr(filter);
96  }
97  else
98  {
99  throw PappsoException(
100  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs1FilterCstSPtr "
101  "msp_timsData is null"));
102  }
103 }
104 
105 bool
106 TimsMsRunReaderMs2::accept(const QString &file_name) const
107 {
108  qDebug() << file_name;
109  return true;
110 }
111 
112 
114 TimsMsRunReaderMs2::massSpectrumSPtr(std::size_t spectrum_index)
115 {
116  QualifiedMassSpectrum mass_spectrum =
117  qualifiedMassSpectrum(spectrum_index, true);
118  return mass_spectrum.getMassSpectrumSPtr();
119 }
120 
121 
123 TimsMsRunReaderMs2::massSpectrumCstSPtr(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::qualifiedMassSpectrum(std::size_t spectrum_index,
133  bool want_binary_data) const
134 {
135 
136  std::size_t precursor_index = (spectrum_index / 2) + 1;
137 
138  if(spectrum_index % 2 == 0)
139  {
140  qDebug();
141  // this is an MS1 spectrum
142  QualifiedMassSpectrum mass_spectrum_ms1 =
143  msp_timsData->getQualifiedMs1MassSpectrumByPrecursorId(
144  spectrum_index, precursor_index, want_binary_data);
145  MassSpectrumId spectrum_id(mass_spectrum_ms1.getMassSpectrumId());
146  spectrum_id.setMsRunId(getMsRunId());
147  mass_spectrum_ms1.setMassSpectrumId(spectrum_id);
148  qDebug(); // << mass_spectrum_ms1.toString();
149 
150  // qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
151  return mass_spectrum_ms1;
152  }
153  else
154  {
155  qDebug();
156  QualifiedMassSpectrum mass_spectrum_ms2;
157  msp_timsData->getQualifiedMs2MassSpectrumByPrecursorId(
158  mass_spectrum_ms2, spectrum_index, precursor_index, want_binary_data);
159  MassSpectrumId spectrum_id(mass_spectrum_ms2.getMassSpectrumId());
160  spectrum_id.setMsRunId(getMsRunId());
161  mass_spectrum_ms2.setMassSpectrumId(spectrum_id);
162  qDebug(); // << mass_spectrum_ms2.toString();
163 
164  // qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
165  return mass_spectrum_ms2;
166  }
167 }
168 
169 
170 void
173 {
174  const bool want_binary_data = handler.needPeakList();
175  // const bool want_binary_data = false;
176 
177  // We'll need it to perform the looping in the spectrum list.
178  std::size_t spectrum_list_size = spectrumListSize();
179 
180  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
181 
182  // Inform the handler of the spectrum list so that it can handle feedback to
183  // the user.
184  handler.spectrumListHasSize(spectrum_list_size);
185 
186  // Iterate in the full list of spectra.
187  bool readAhead = handler.isReadAhead();
188 
189  if(readAhead)
190  {
191 
192  std::size_t process_list_size = 300;
193 
194  struct tmp_item
195  {
196  QualifiedMassSpectrum qualified_mass_spectrum;
197  std::size_t iter;
198  bool want_binary_data;
199  };
200 
201  for(std::size_t i = 0; i < spectrum_list_size; i += process_list_size)
202  {
203  qDebug();
204  // If the user of this reader instance wants to stop reading the
205  // spectra, then break this loop.
206  if(handler.shouldStop())
207  {
208  qDebug() << "The operation was cancelled. Breaking the loop.";
209  break;
210  }
211  std::vector<tmp_item> item_list;
212  for(std::size_t iter = 0;
213  (iter < process_list_size) && ((iter + i) < spectrum_list_size);
214  iter++)
215  {
216 
217  bool get_data = want_binary_data;
218  if((iter + i) % 2 == 0)
219  { // MS1
220  get_data = handler.needMsLevelPeakList(1);
221  }
222  else
223  {
224  get_data = handler.needMsLevelPeakList(2);
225  }
226 
227  item_list.push_back(
228  {QualifiedMassSpectrum(), iter + i, get_data});
229  }
230  qDebug() << item_list.size();
231  // Use QtConcurrentBlocking::mapped to apply the scale function to all
232  // the images in the list.
233  QtConcurrent::blockingMap(
234  item_list.begin(), item_list.end(), [this](tmp_item &one_item) {
235  qDebug() << one_item.iter;
236  one_item.qualified_mass_spectrum =
237  qualifiedMassSpectrum(one_item.iter, one_item.want_binary_data);
238 
239  // qDebug() << one_item.qualified_mass_spectrum.size() << " " <<
240  // one_item.qualified_mass_spectrum.getMassSpectrumSPtr().get()->toString();
241  });
242 
243  qDebug() << item_list.size();
244  for(auto &item : item_list)
245  {
246  // qDebug() << item.qualified_mass_spectrum.getMassSpectrumSPtr()
247  // .get()
248  // ->toString();
249  handler.setQualifiedMassSpectrum(item.qualified_mass_spectrum);
250  qDebug();
251  }
252  }
253  }
254  else
255  {
256  for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
257  {
258  qDebug();
259  // If the user of this reader instance wants to stop reading the
260  // spectra, then break this loop.
261  if(handler.shouldStop())
262  {
263  qDebug() << "The operation was cancelled. Breaking the loop.";
264  break;
265  }
266  bool get_data = want_binary_data;
267  if(iter % 2 == 0)
268  { // MS1
269  if(!handler.needMsLevelPeakList(1))
270  {
271  get_data = false;
272  }
273  }
274  QualifiedMassSpectrum qualified_mass_spectrum =
275  qualifiedMassSpectrum(iter, get_data);
276  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
277  qDebug();
278  }
279  }
280  // End of
281  // for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
282 
283  // Now let the loading handler know that the loading of the data has ended.
284  // The handler might need this "signal" to perform additional tasks or to
285  // cleanup cruft.
286 
287  // qDebug() << "Loading ended";
288  handler.loadingEnded();
289 }
290 
291 
292 std::size_t
294 {
295  return (msp_timsData->getTotalNumberOfPrecursors() * 2);
296 }
297 
298 
299 bool
301 {
302  return false;
303 }
304 
305 
306 bool
308 {
309  msp_timsData = nullptr;
310  return true;
311 }
312 
313 bool
315 {
316  if(msp_timsData == nullptr)
317  {
318  initialize();
319  msp_timsData->setMs2BuiltinCentroid(m_builtinMs2Centroid);
320  msp_timsData->setMs1FilterCstSPtr(msp_ms1Filter);
321  msp_timsData->setMs2FilterCstSPtr(msp_ms2Filter);
322  }
323  return true;
324 }
325 
326 std::vector<std::size_t>
328  double mz_val,
329  double rt_sec,
330  double k0)
331 {
332  return msp_timsData->getPrecursorsFromMzRtCharge(charge, mz_val, rt_sec, k0);
333 }
334 
337 {
338  acquireDevice();
339  return msp_timsData;
340 }
pappso::TimsMsRunReaderMs2::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition: timsmsrunreaderms2.cpp:90
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
pappso::QualifiedMassSpectrum::getMassSpectrumSPtr
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
Definition: qualifiedmassspectrum.cpp:133
pappso::TimsMsRunReaderMs2::qualifiedMassSpectrum
virtual QualifiedMassSpectrum qualifiedMassSpectrum(std::size_t spectrum_index, bool want_binary_data=true) const override
get a QualifiedMassSpectrum class given its scan number
Definition: timsmsrunreaderms2.cpp:132
pappso::MsRunReader
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:158
pappso::TimsMsRunReaderMs2::initialize
virtual void initialize() override
Definition: timsmsrunreaderms2.cpp:52
pappso::SpectrumCollectionHandlerInterface::spectrumListHasSize
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:55
pappso::SpectrumCollectionHandlerInterface::isReadAhead
virtual bool isReadAhead() const
tells if we want to read ahead spectrum
Definition: msrunreader.cpp:66
pappso::TimsMsRunReaderMs2::msp_ms1Filter
pappso::FilterInterfaceCstSPtr msp_ms1Filter
Definition: timsmsrunreaderms2.h:95
pappso::TimsMsRunReaderMs2::msp_ms2Filter
pappso::FilterInterfaceCstSPtr msp_ms2Filter
Definition: timsmsrunreaderms2.h:96
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::SpectrumCollectionHandlerInterface::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
pappso::FilterInterfaceCstSPtr
std::shared_ptr< const FilterInterface > FilterInterfaceCstSPtr
Definition: filterinterface.h:46
pappso::TimsMsRunReaderMs2::getPrecursorsIDFromMzRt
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.
Definition: timsmsrunreaderms2.cpp:327
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:44
pappso::TimsMsRunReaderMs2::setMs2BuiltinCentroid
void setMs2BuiltinCentroid(bool centroid)
enable or disable simple centroid filter on raw tims data for MS2
Definition: timsmsrunreaderms2.cpp:58
pappso::TimsMsRunReaderMs2::m_builtinMs2Centroid
bool m_builtinMs2Centroid
enable builtin centroid on raw tims integers by default
Definition: timsmsrunreaderms2.h:99
pappso::TimsMsRunReaderMs2::msp_timsData
TimsDataSp msp_timsData
Definition: timsmsrunreaderms2.h:93
pappso::SpectrumCollectionHandlerInterface::shouldStop
virtual bool shouldStop()
Definition: msrunreader.cpp:46
pappso::SpectrumCollectionHandlerInterface::loadingEnded
virtual void loadingEnded()
Definition: msrunreader.cpp:51
pappso::TimsMsRunReaderMs2::acquireDevice
virtual bool acquireDevice() override
acquire data back end device
Definition: timsmsrunreaderms2.cpp:314
pappso::MsRunReader::getMsRunId
const MsRunIdCstSPtr & getMsRunId() const
Definition: msrunreader.cpp:232
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:85
pappso::TimsMsRunReaderMs2::getTimsDataSPtr
virtual TimsDataSp getTimsDataSPtr()
give an access to the underlying raw data pointer
Definition: timsmsrunreaderms2.cpp:336
pappso::TimsMsRunReaderMs2::TimsMsRunReaderMs2
TimsMsRunReaderMs2(MsRunIdCstSPtr &msrun_id_csp)
Definition: timsmsrunreaderms2.cpp:37
pappso::SpectrumCollectionHandlerInterface::needPeakList
virtual bool needPeakList() const =0
tells if we need the peak list (if we want the binary data) for each spectrum
pappso::TimsMsRunReaderMs2::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition: timsmsrunreaderms2.cpp:123
pappso::QualifiedMassSpectrum::setMassSpectrumId
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:149
pappso::TimsMsRunReaderMs2::massSpectrumSPtr
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
Definition: timsmsrunreaderms2.cpp:114
pappso::SpectrumCollectionHandlerInterface::needMsLevelPeakList
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
pappso::TimsMsRunReaderMs2::releaseDevice
virtual bool releaseDevice() override
release data back end device if a the data back end is released, the developper has to use acquireDev...
Definition: timsmsrunreaderms2.cpp:307
pappso::QualifiedMassSpectrum::getMassSpectrumId
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:157
pappso::TimsDataSp
std::shared_ptr< TimsData > TimsDataSp
shared pointer on a TimsData object
Definition: timsdata.h:60
pappso::TimsMsRunReaderMs2::~TimsMsRunReaderMs2
virtual ~TimsMsRunReaderMs2()
Definition: timsmsrunreaderms2.cpp:43
pappso::MassSpectrumId
Definition: massspectrumid.h:38
pappso::MassSpectrumId::setMsRunId
void setMsRunId(MsRunIdCstSPtr other)
Definition: massspectrumid.cpp:74
pappso::TimsMsRunReaderMs2::readSpectrumCollection
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
Definition: timsmsrunreaderms2.cpp:171
pappso::TimsMsRunReaderMs2::accept
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,...
Definition: timsmsrunreaderms2.cpp:106
timsmsrunreaderms2.h
MSrun file reader for native Bruker TimsTOF specialized for MS2 purpose.
pappso::TimsMsRunReaderMs2::hasScanNumbers
virtual bool hasScanNumbers() const override
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
Definition: timsmsrunreaderms2.cpp:300
pappso::SpectrumCollectionHandlerInterface
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:59
pappso::TimsMsRunReaderMs2::setMs2FilterCstSPtr
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition: timsmsrunreaderms2.cpp:74
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:54
pappso::PappsoException
Definition: pappsoexception.h:42
pappso::TimsMsRunReaderMs2::spectrumListSize
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
Definition: timsmsrunreaderms2.cpp:293