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  if(mpa_timsData != nullptr)
61  {
63  }
64  else
65  {
66  throw PappsoException(
67  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs2FilterCstSPtr "
68  "mpa_timsData is null"));
69  }
70 }
71 
72 void
74 {
75  if(mpa_timsData != nullptr)
76  {
78  }
79  else
80  {
81  throw PappsoException(
82  QObject::tr("ERROR in TimsMsRunReaderMs2::setMs1FilterCstSPtr "
83  "mpa_timsData is null"));
84  }
85 }
86 
87 bool
88 TimsMsRunReaderMs2::accept(const QString &file_name) const
89 {
90  qDebug() << file_name;
91  return true;
92 }
93 
94 
96 TimsMsRunReaderMs2::massSpectrumSPtr(std::size_t spectrum_index)
97 {
98  QualifiedMassSpectrum mass_spectrum =
99  qualifiedMassSpectrum(spectrum_index, true);
100  return mass_spectrum.getMassSpectrumSPtr();
101 }
102 
103 
105 TimsMsRunReaderMs2::massSpectrumCstSPtr(std::size_t spectrum_index)
106 {
107  QualifiedMassSpectrum mass_spectrum =
108  qualifiedMassSpectrum(spectrum_index, true);
109  return mass_spectrum.getMassSpectrumSPtr();
110 }
111 
112 
114 TimsMsRunReaderMs2::qualifiedMassSpectrum(std::size_t spectrum_index,
115  bool want_binary_data) const
116 {
117 
118  std::size_t precursor_index = (spectrum_index / 2) + 1;
119 
120  if(spectrum_index % 2 == 0)
121  {
122  qDebug();
123  // this is an MS1 spectrum
124  QualifiedMassSpectrum mass_spectrum_ms1 =
126  spectrum_index, precursor_index, want_binary_data);
127  MassSpectrumId spectrum_id(mass_spectrum_ms1.getMassSpectrumId());
128  spectrum_id.setMsRunId(getMsRunId());
129  mass_spectrum_ms1.setMassSpectrumId(spectrum_id);
130  qDebug(); // << mass_spectrum_ms1.toString();
131 
132  // qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
133  return mass_spectrum_ms1;
134  }
135  else
136  {
137  qDebug();
138  QualifiedMassSpectrum mass_spectrum_ms2;
140  mass_spectrum_ms2, spectrum_index, precursor_index, want_binary_data);
141  MassSpectrumId spectrum_id(mass_spectrum_ms2.getMassSpectrumId());
142  spectrum_id.setMsRunId(getMsRunId());
143  mass_spectrum_ms2.setMassSpectrumId(spectrum_id);
144  qDebug(); // << mass_spectrum_ms2.toString();
145 
146  // qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
147  return mass_spectrum_ms2;
148  }
149 }
150 
151 
152 void
155 {
156  const bool want_binary_data = handler.needPeakList();
157  // const bool want_binary_data = false;
158 
159  // We'll need it to perform the looping in the spectrum list.
160  std::size_t spectrum_list_size = spectrumListSize();
161 
162  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
163 
164  // Inform the handler of the spectrum list so that it can handle feedback to
165  // the user.
166  handler.spectrumListHasSize(spectrum_list_size);
167 
168  // Iterate in the full list of spectra.
169  bool readAhead = handler.isReadAhead();
170 
171  if(readAhead)
172  {
173 
174  std::size_t process_list_size = 300;
175 
176  struct tmp_item
177  {
178  QualifiedMassSpectrum qualified_mass_spectrum;
179  std::size_t iter;
180  bool want_binary_data;
181  };
182 
183  for(std::size_t i = 0; i < spectrum_list_size; i += process_list_size)
184  {
185  qDebug();
186  // If the user of this reader instance wants to stop reading the
187  // spectra, then break this loop.
188  if(handler.shouldStop())
189  {
190  qDebug() << "The operation was cancelled. Breaking the loop.";
191  break;
192  }
193  std::vector<tmp_item> item_list;
194  for(std::size_t iter = 0;
195  (iter < process_list_size) && ((iter + i) < spectrum_list_size);
196  iter++)
197  {
198 
199  bool get_data = want_binary_data;
200  if((iter + i) % 2 == 0)
201  { // MS1
202  get_data = handler.needMsLevelPeakList(1);
203  }
204  else
205  {
206  get_data = handler.needMsLevelPeakList(2);
207  }
208 
209  item_list.push_back(
210  {QualifiedMassSpectrum(), iter + i, get_data});
211  }
212  qDebug() << item_list.size();
213  // Use QtConcurrentBlocking::mapped to apply the scale function to all
214  // the images in the list.
215  QtConcurrent::blockingMap(
216  item_list.begin(), item_list.end(), [this](tmp_item &one_item) {
217  qDebug() << one_item.iter;
218  one_item.qualified_mass_spectrum =
219  qualifiedMassSpectrum(one_item.iter, one_item.want_binary_data);
220 
221  // qDebug() << one_item.qualified_mass_spectrum.size() << " " <<
222  // one_item.qualified_mass_spectrum.getMassSpectrumSPtr().get()->toString();
223  });
224 
225  qDebug() << item_list.size();
226  for(auto &item : item_list)
227  {
228  // qDebug() << item.qualified_mass_spectrum.getMassSpectrumSPtr()
229  // .get()
230  // ->toString();
231  handler.setQualifiedMassSpectrum(item.qualified_mass_spectrum);
232  qDebug();
233  }
234  }
235  }
236  else
237  {
238  for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
239  {
240  qDebug();
241  // If the user of this reader instance wants to stop reading the
242  // spectra, then break this loop.
243  if(handler.shouldStop())
244  {
245  qDebug() << "The operation was cancelled. Breaking the loop.";
246  break;
247  }
248  bool get_data = want_binary_data;
249  if(iter % 2 == 0)
250  { // MS1
251  if(!handler.needMsLevelPeakList(1))
252  {
253  get_data = false;
254  }
255  }
256  QualifiedMassSpectrum qualified_mass_spectrum =
257  qualifiedMassSpectrum(iter, get_data);
258  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
259  qDebug();
260  }
261  }
262  // End of
263  // for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
264 
265  // Now let the loading handler know that the loading of the data has ended.
266  // The handler might need this "signal" to perform additional tasks or to
267  // cleanup cruft.
268 
269  // qDebug() << "Loading ended";
270  handler.loadingEnded();
271 }
272 
273 
274 std::size_t
276 {
278 }
279 
280 
281 bool
283 {
284  return false;
285 }
pappso::TimsMsRunReaderMs2::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition: timsmsrunreaderms2.cpp:73
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
pappso::QualifiedMassSpectrum::getMassSpectrumSPtr
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
Definition: qualifiedmassspectrum.cpp:144
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:114
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:1080
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:664
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
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::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:44
pappso::TimsData::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
Definition: timsdata.cpp:1085
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::MsRunReader::getMsRunId
const MsRunIdCstSPtr & getMsRunId() const
Definition: msrunreader.cpp:232
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:68
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:74
pappso::TimsMsRunReaderMs2::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition: timsmsrunreaderms2.cpp:105
pappso::QualifiedMassSpectrum::setMassSpectrumId
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:120
pappso::TimsMsRunReaderMs2::massSpectrumSPtr
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
Definition: timsmsrunreaderms2.cpp:96
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::QualifiedMassSpectrum::getMassSpectrumId
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:128
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:828
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:153
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:88
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:282
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:275