libpappsomspp
Library for mass spectrometry
timsmsrunreaderms2selected.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 
31 #include "../../exception/exceptionnotimplemented.h"
32 #include <QDebug>
33 #include <QtConcurrent/QtConcurrent>
34 
35 using namespace pappso;
36 
38  MsRunIdCstSPtr &msrun_id_csp)
39  : TimsMsRunReaderMs2(msrun_id_csp)
40 {
41  initialize();
42 }
43 
45 {
46  if(mpa_timsData != nullptr)
47  {
48  delete mpa_timsData;
49  }
50 }
51 
52 void
54 {
55  mpa_timsData = new TimsData(mcsp_msRunId.get()->getFileName());
56 }
57 
58 void
61 {
62  if(mpa_timsData != nullptr)
63  {
65  }
66  else
67  {
68  throw PappsoException(
69  QObject::tr("ERROR in TimsMsRunReaderMs2Selected::setMs2FilterCstSPtr "
70  "mpa_timsData is null"));
71  }
72 }
73 
74 void
77 {
78  if(mpa_timsData != nullptr)
79  {
81  }
82  else
83  {
84  throw PappsoException(
85  QObject::tr("ERROR in TimsMsRunReaderMs2Selected::setMs1FilterCstSPtr "
86  "mpa_timsData is null"));
87  }
88 }
89 
90 bool
91 TimsMsRunReaderMs2Selected::accept(const QString &file_name) const
92 {
93  qDebug() << file_name;
94  return true;
95 }
96 
97 
100 {
101  QualifiedMassSpectrum mass_spectrum =
102  qualifiedMassSpectrum(spectrum_index, true);
103  return mass_spectrum.getMassSpectrumSPtr();
104 }
105 
106 
109 {
110  QualifiedMassSpectrum mass_spectrum =
111  qualifiedMassSpectrum(spectrum_index, true);
112  return mass_spectrum.getMassSpectrumSPtr();
113 }
114 
115 
118  bool want_binary_data) const
119 {
120 
121  std::size_t precursor_index = (spectrum_index / 2) + 1;
122 
123  if(spectrum_index % 2 == 0)
124  {
125  qDebug();
126  // this is an MS1 spectrum
127  QualifiedMassSpectrum mass_spectrum_ms1 =
129  spectrum_index, precursor_index, want_binary_data);
130  MassSpectrumId spectrum_id(mass_spectrum_ms1.getMassSpectrumId());
131  spectrum_id.setMsRunId(getMsRunId());
132  mass_spectrum_ms1.setMassSpectrumId(spectrum_id);
133  qDebug(); // << mass_spectrum_ms1.toString();
134 
135  // qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
136  return mass_spectrum_ms1;
137  }
138  else
139  {
140  qDebug();
141  QualifiedMassSpectrum mass_spectrum_ms2;
143  mass_spectrum_ms2, spectrum_index, precursor_index, want_binary_data);
144  MassSpectrumId spectrum_id(mass_spectrum_ms2.getMassSpectrumId());
145  spectrum_id.setMsRunId(getMsRunId());
146  mass_spectrum_ms2.setMassSpectrumId(spectrum_id);
147  qDebug(); // << mass_spectrum_ms2.toString();
148 
149  // qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
150  return mass_spectrum_ms2;
151  }
152 }
153 
154 
155 void
158 {
159  const bool want_binary_data = handler.needPeakList();
160  // const bool want_binary_data = false;
161 
162  // We'll need it to perform the looping in the spectrum list.
163  std::size_t spectrum_list_size = spectrumListSize();
164 
165  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
166 
167  // Inform the handler of the spectrum list so that it can handle feedback to
168  // the user.
169  handler.spectrumListHasSize(spectrum_list_size);
170 
171  std::vector<size_t> selected_precursor{63905,
172  73549,
173  105675,
174  130439,
175  177297,
176  177369,
177  177483,
178  190453,
179  196967,
180  246691,
181  271215,
182  289423,
183  310669};
184  // QFile temp_output("/data/temp.txt");
185  // temp_output.open(QIODevice::WriteOnly | QIODevice::Text);
186  // QTextStream out(&temp_output);
187 
188  // Iterate in the full list of spectra.
189  bool readAhead = handler.isReadAhead();
190 
191  if(readAhead)
192  {
193 
194  std::size_t process_list_size = 300;
195 
196  struct tmp_item
197  {
198  QualifiedMassSpectrum qualified_mass_spectrum;
199  std::size_t iter;
200  bool want_binary_data;
201  };
202 
203  for(std::size_t i = 0; i < spectrum_list_size; i += process_list_size)
204  {
205  // QTextStream out(&temp_output);
206  qDebug();
207  // If the user of this reader instance wants to stop reading the
208  // spectra, then break this loop.
209  if(handler.shouldStop())
210  {
211  qDebug() << "The operation was cancelled. Breaking the loop.";
212  break;
213  }
214  std::vector<tmp_item> item_list;
215  for(std::size_t iter = 0;
216  (iter < process_list_size) && ((iter + i) < spectrum_list_size);
217  iter++)
218  {
219  if(std::find(selected_precursor.begin(),
220  selected_precursor.end(),
221  iter + i) != selected_precursor.end())
222  {
223  bool get_data = want_binary_data;
224  if((iter + i) % 2 == 0)
225  { // MS1
226  get_data = handler.needMsLevelPeakList(1);
227  }
228  else
229  {
230  get_data = handler.needMsLevelPeakList(2);
231  }
232 
233  item_list.push_back(
234  {QualifiedMassSpectrum(), iter + i, get_data});
235  }
236  }
237  qDebug() << item_list.size();
238  // Use QtConcurrentBlocking::mapped to apply the scale function to all
239  // the images in the list.
240  QtConcurrent::blockingMap(
241  item_list.begin(), item_list.end(), [this](tmp_item &one_item) {
242  qDebug() << one_item.iter;
243  one_item.qualified_mass_spectrum =
244  qualifiedMassSpectrum(one_item.iter, one_item.want_binary_data);
245 
246  // qDebug() << one_item.qualified_mass_spectrum.size() << " " <<
247  // one_item.qualified_mass_spectrum.getMassSpectrumSPtr().get()->toString();
248  });
249 
250  qDebug() << item_list.size();
251  for(auto &item : item_list)
252  {
253  // qDebug() <<
254  // item.qualified_mass_spectrum.getMassSpectrumSPtr()
255  // .get()
256  // ->toString();
257  handler.setQualifiedMassSpectrum(item.qualified_mass_spectrum);
258  qDebug();
259  }
260  }
261  }
262  else
263  {
264  for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
265  {
266  qDebug();
267  // If the user of this reader instance wants to stop reading the
268  // spectra, then break this loop.
269  if(handler.shouldStop())
270  {
271  qDebug() << "The operation was cancelled. Breaking the loop.";
272  break;
273  }
274  bool get_data = want_binary_data;
275  if(iter % 2 == 0)
276  { // MS1
277  if(!handler.needMsLevelPeakList(1))
278  {
279  get_data = false;
280  }
281  }
282  QualifiedMassSpectrum qualified_mass_spectrum =
283  qualifiedMassSpectrum(iter, get_data);
284  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
285  qDebug();
286  }
287  }
288  // End of
289  // for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
290 
291  // Now let the loading handler know that the loading of the data has
292  // ended. The handler might need this "signal" to perform additional tasks
293  // or to cleanup cruft.
294 
295  // qDebug() << "Loading ended";
296  // temp_output.close();
297  handler.loadingEnded();
298 }
299 
300 
301 std::size_t
303 {
305 }
306 
307 
308 bool
310 {
311  return false;
312 }
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::TimsData::setMs2FilterCstSPtr
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter)
Definition: timsdata.cpp:1208
pappso::TimsData::getQualifiedMs1MassSpectrumByPrecursorId
QualifiedMassSpectrum getQualifiedMs1MassSpectrumByPrecursorId(std::size_t ms2_index, std::size_t precursor_index, bool want_binary_data)
Definition: timsdata.cpp:794
pappso::TimsMsRunReaderMs2Selected::readSpectrumCollection
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
Definition: timsmsrunreaderms2selected.cpp:156
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::TimsMsRunReaderMs2Selected::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: timsmsrunreaderms2selected.cpp:91
pappso::TimsMsRunReaderMs2Selected::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition: timsmsrunreaderms2selected.cpp:75
pappso::TimsMsRunReaderMs2Selected::TimsMsRunReaderMs2Selected
TimsMsRunReaderMs2Selected(MsRunIdCstSPtr &msrun_id_csp)
Definition: timsmsrunreaderms2selected.cpp:37
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks
Definition: aa.cpp:39
pappso::TimsMsRunReaderMs2Selected::spectrumListSize
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
Definition: timsmsrunreaderms2selected.cpp:302
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::TimsMsRunReaderMs2Selected::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: timsmsrunreaderms2selected.cpp:117
pappso::TimsMsRunReaderMs2Selected::mpa_timsData
TimsData * mpa_timsData
Definition: timsmsrunreaderms2selected.h:75
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::TimsMsRunReaderMs2Selected::~TimsMsRunReaderMs2Selected
virtual ~TimsMsRunReaderMs2Selected()
Definition: timsmsrunreaderms2selected.cpp:44
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::SpectrumCollectionHandlerInterface::needPeakList
virtual bool needPeakList() const =0
tells if we need the peak list (if we want the binary data) for each spectrum
pappso::QualifiedMassSpectrum::setMassSpectrumId
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:149
pappso::TimsData::getTotalNumberOfPrecursors
std::size_t getTotalNumberOfPrecursors() const
get the number of precursors analyzes by PASEF
Definition: timsdata.cpp:531
pappso::TimsMsRunReaderMs2
Definition: timsmsrunreaderms2.h:41
pappso::TimsMsRunReaderMs2Selected::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition: timsmsrunreaderms2selected.cpp:108
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:157
pappso::MassSpectrumId
Definition: massspectrumid.h:38
timsmsrunreaderms2selected.h
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::TimsMsRunReaderMs2Selected::initialize
virtual void initialize() override
Definition: timsmsrunreaderms2selected.cpp:53
pappso::TimsMsRunReaderMs2Selected::setMs2FilterCstSPtr
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition: timsmsrunreaderms2selected.cpp:59
pappso::TimsMsRunReaderMs2Selected::hasScanNumbers
virtual bool hasScanNumbers() const override
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
Definition: timsmsrunreaderms2selected.cpp:309
pappso::SpectrumCollectionHandlerInterface
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:59
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:54
pappso::TimsMsRunReaderMs2Selected::massSpectrumSPtr
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
Definition: timsmsrunreaderms2selected.cpp:99
pappso::PappsoException
Definition: pappsoexception.h:42