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(mpa_timsData != nullptr)
46  {
47  delete mpa_timsData;
48  }
49 }
50 
51 void
53 {
54  mpa_timsData = new TimsData(mcsp_msRunId.get()->getFileName());
55 }
56 
57 void
59 {
60  msp_ms2Filter = filter;
61  if(mpa_timsData != nullptr)
62  {
64  }
65  else
66  {
67  throw PappsoException(
68  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2FilterCstSPtr "
69  "mpa_timsData is null"));
70  }
71 }
72 
73 void
75 {
76  msp_ms1Filter = filter;
77  if(mpa_timsData != nullptr)
78  {
80  }
81  else
82  {
83  throw PappsoException(
84  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs1FilterCstSPtr "
85  "mpa_timsData is null"));
86  }
87 }
88 
89 bool
90 TimsMsRunReaderMs2::accept(const QString &file_name) const
91 {
92  qDebug() << file_name;
93  return true;
94 }
95 
96 
98 TimsMsRunReaderMs2::massSpectrumSPtr(std::size_t spectrum_index)
99 {
100  QualifiedMassSpectrum mass_spectrum =
101  qualifiedMassSpectrum(spectrum_index, true);
102  return mass_spectrum.getMassSpectrumSPtr();
103 }
104 
105 
107 TimsMsRunReaderMs2::massSpectrumCstSPtr(std::size_t spectrum_index)
108 {
109  QualifiedMassSpectrum mass_spectrum =
110  qualifiedMassSpectrum(spectrum_index, true);
111  return mass_spectrum.getMassSpectrumSPtr();
112 }
113 
114 
116 TimsMsRunReaderMs2::qualifiedMassSpectrum(std::size_t spectrum_index,
117  bool want_binary_data) const
118 {
119 
120  std::size_t precursor_index = (spectrum_index / 2) + 1;
121 
122  if(spectrum_index % 2 == 0)
123  {
124  qDebug();
125  // this is an MS1 spectrum
126  QualifiedMassSpectrum mass_spectrum_ms1 =
128  spectrum_index, precursor_index, want_binary_data);
129  MassSpectrumId spectrum_id(mass_spectrum_ms1.getMassSpectrumId());
130  spectrum_id.setMsRunId(getMsRunId());
131  mass_spectrum_ms1.setMassSpectrumId(spectrum_id);
132  qDebug(); // << mass_spectrum_ms1.toString();
133 
134  // qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
135  return mass_spectrum_ms1;
136  }
137  else
138  {
139  qDebug();
140  QualifiedMassSpectrum mass_spectrum_ms2;
142  mass_spectrum_ms2, spectrum_index, precursor_index, want_binary_data);
143  MassSpectrumId spectrum_id(mass_spectrum_ms2.getMassSpectrumId());
144  spectrum_id.setMsRunId(getMsRunId());
145  mass_spectrum_ms2.setMassSpectrumId(spectrum_id);
146  qDebug(); // << mass_spectrum_ms2.toString();
147 
148  // qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
149  return mass_spectrum_ms2;
150  }
151 }
152 
153 
154 void
157 {
158  const bool want_binary_data = handler.needPeakList();
159  // const bool want_binary_data = false;
160 
161  // We'll need it to perform the looping in the spectrum list.
162  std::size_t spectrum_list_size = spectrumListSize();
163 
164  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
165 
166  // Inform the handler of the spectrum list so that it can handle feedback to
167  // the user.
168  handler.spectrumListHasSize(spectrum_list_size);
169 
170  // Iterate in the full list of spectra.
171  bool readAhead = handler.isReadAhead();
172 
173  if(readAhead)
174  {
175 
176  std::size_t process_list_size = 300;
177 
178  struct tmp_item
179  {
180  QualifiedMassSpectrum qualified_mass_spectrum;
181  std::size_t iter;
182  bool want_binary_data;
183  };
184 
185  for(std::size_t i = 0; i < spectrum_list_size; i += process_list_size)
186  {
187  qDebug();
188  // If the user of this reader instance wants to stop reading the
189  // spectra, then break this loop.
190  if(handler.shouldStop())
191  {
192  qDebug() << "The operation was cancelled. Breaking the loop.";
193  break;
194  }
195  std::vector<tmp_item> item_list;
196  for(std::size_t iter = 0;
197  (iter < process_list_size) && ((iter + i) < spectrum_list_size);
198  iter++)
199  {
200 
201  bool get_data = want_binary_data;
202  if((iter + i) % 2 == 0)
203  { // MS1
204  get_data = handler.needMsLevelPeakList(1);
205  }
206  else
207  {
208  get_data = handler.needMsLevelPeakList(2);
209  }
210 
211  item_list.push_back(
212  {QualifiedMassSpectrum(), iter + i, get_data});
213  }
214  qDebug() << item_list.size();
215  // Use QtConcurrentBlocking::mapped to apply the scale function to all
216  // the images in the list.
217  QtConcurrent::blockingMap(
218  item_list.begin(), item_list.end(), [this](tmp_item &one_item) {
219  qDebug() << one_item.iter;
220  one_item.qualified_mass_spectrum =
221  qualifiedMassSpectrum(one_item.iter, one_item.want_binary_data);
222 
223  // qDebug() << one_item.qualified_mass_spectrum.size() << " " <<
224  // one_item.qualified_mass_spectrum.getMassSpectrumSPtr().get()->toString();
225  });
226 
227  qDebug() << item_list.size();
228  for(auto &item : item_list)
229  {
230  // qDebug() << item.qualified_mass_spectrum.getMassSpectrumSPtr()
231  // .get()
232  // ->toString();
233  handler.setQualifiedMassSpectrum(item.qualified_mass_spectrum);
234  qDebug();
235  }
236  }
237  }
238  else
239  {
240  for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
241  {
242  qDebug();
243  // If the user of this reader instance wants to stop reading the
244  // spectra, then break this loop.
245  if(handler.shouldStop())
246  {
247  qDebug() << "The operation was cancelled. Breaking the loop.";
248  break;
249  }
250  bool get_data = want_binary_data;
251  if(iter % 2 == 0)
252  { // MS1
253  if(!handler.needMsLevelPeakList(1))
254  {
255  get_data = false;
256  }
257  }
258  QualifiedMassSpectrum qualified_mass_spectrum =
259  qualifiedMassSpectrum(iter, get_data);
260  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
261  qDebug();
262  }
263  }
264  // End of
265  // for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
266 
267  // Now let the loading handler know that the loading of the data has ended.
268  // The handler might need this "signal" to perform additional tasks or to
269  // cleanup cruft.
270 
271  // qDebug() << "Loading ended";
272  handler.loadingEnded();
273 }
274 
275 
276 std::size_t
278 {
280 }
281 
282 
283 bool
285 {
286  return false;
287 }
288 
289 
290 bool
292 {
293  delete mpa_timsData;
294  mpa_timsData = nullptr;
295  return true;
296 }
297 
298 bool
300 {
301  if(mpa_timsData == nullptr)
302  {
303  initialize();
306  }
307  return true;
308 }
309 
310 std::vector<std::size_t>
312  double mz_val,
313  double rt_sec,
314  double k0)
315 {
316  return mpa_timsData->getPrecursorsFromMzRtCharge(charge, mz_val, rt_sec, k0);
317 }
pappso::TimsMsRunReaderMs2::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition: timsmsrunreaderms2.cpp:74
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:116
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::TimsData::setMs2FilterCstSPtr
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
Definition: timsdata.cpp:1208
pappso::TimsMsRunReaderMs2::initialize
virtual void initialize() override
Definition: timsmsrunreaderms2.cpp:52
pappso::TimsData::getQualifiedMs1MassSpectrumByPrecursorId
QualifiedMassSpectrum getQualifiedMs1MassSpectrumByPrecursorId(std::size_t ms2_index, std::size_t precursor_index, bool want_binary_data)
Definition: timsdata.cpp:794
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:87
pappso::TimsMsRunReaderMs2::msp_ms2Filter
pappso::FilterInterfaceCstSPtr msp_ms2Filter
Definition: timsmsrunreaderms2.h:88
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks
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:311
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:44
pappso::TimsData::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
Definition: timsdata.cpp:1213
pappso::SpectrumCollectionHandlerInterface::shouldStop
virtual bool shouldStop()
Definition: msrunreader.cpp:46
pappso::SpectrumCollectionHandlerInterface::loadingEnded
virtual void loadingEnded()
Definition: msrunreader.cpp:51
pappso::TimsData
Definition: timsdata.h:65
pappso::TimsMsRunReaderMs2::acquireDevice
virtual bool acquireDevice() override
acquire data back end device
Definition: timsmsrunreaderms2.cpp:299
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::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::mpa_timsData
TimsData * mpa_timsData
Definition: timsmsrunreaderms2.h:85
pappso::TimsMsRunReaderMs2::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition: timsmsrunreaderms2.cpp:107
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:98
pappso::TimsData::getTotalNumberOfPrecursors
std::size_t getTotalNumberOfPrecursors() const
get the number of precursors analyzes by PASEF
Definition: timsdata.cpp:531
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:291
pappso::QualifiedMassSpectrum::getMassSpectrumId
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:157
pappso::TimsMsRunReaderMs2::~TimsMsRunReaderMs2
virtual ~TimsMsRunReaderMs2()
Definition: timsmsrunreaderms2.cpp:43
pappso::MassSpectrumId
Definition: massspectrumid.h:38
pappso::TimsData::getQualifiedMs2MassSpectrumByPrecursorId
void getQualifiedMs2MassSpectrumByPrecursorId(QualifiedMassSpectrum &mass_spectrum, std::size_t ms2_index, std::size_t precursor_index, bool want_binary_data)
Definition: timsdata.cpp:955
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:155
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:90
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:284
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:58
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:277