libpappsomspp
Library for mass spectrometry
msrunreader.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/msrun/msrunreader.cpp
3  * \date 29/05/2018
4  * \author Olivier Langella
5  * \brief base interface to read MSrun files
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2018 Olivier Langella <Olivier.Langella@u-psud.fr>.
10  *
11  * This file is part of the PAPPSOms++ library.
12  *
13  * PAPPSOms++ is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * PAPPSOms++ is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25  *
26  ******************************************************************************/
27 
28 #include <QDebug>
29 
30 #include "msrunreader.h"
31 #include "../../pappsomspp/exception/exceptionnotfound.h"
32 
33 
35  qRegisterMetaType<pappso::MsRunReaderSPtr>("pappso::MsRunReaderSPtr");
36 
37 
38 namespace pappso
39 {
40 
41 
42 bool
44 {
45  return false;
46 }
47 void
49 {
50 }
51 void
53  [maybe_unused]] std::size_t size)
54 {
55 }
56 void
58 {
59  m_isReadAhead = is_read_ahead;
60 }
61 
62 bool
64 {
65  return m_isReadAhead;
66 }
67 
68 bool
70  unsigned int ms_level) const
71 {
72  if(needPeakList() == true)
73  {
74  if(ms_level < m_needPeakListByMsLevel.size())
75  {
76  return m_needPeakListByMsLevel[ms_level];
77  }
78  else
79  return true;
80  }
81  else
82  {
83  return false;
84  }
85 }
86 void
88  unsigned int ms_level, bool want_peak_list)
89 {
90  if(ms_level < m_needPeakListByMsLevel.size())
91  {
92  m_needPeakListByMsLevel[ms_level] = want_peak_list;
93  }
94 }
95 
96 bool
98 {
99  return false;
100 }
101 
102 
103 void
105 {
106  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
107  //<< "The data loading process ended.";
108 }
109 
110 
111 void
113  const QualifiedMassSpectrum &qspectrum)
114 {
115  unsigned int ms_level = qspectrum.getMsLevel();
116  if(ms_level == 0)
117  return;
118  if(ms_level > m_countMsLevelSpectrum.size())
119  {
120  m_countMsLevelSpectrum.resize(ms_level);
121  }
122  m_countMsLevelSpectrum[ms_level - 1]++;
123 }
124 
125 
126 unsigned long
127 MsRunSimpleStatistics::getMsLevelCount(unsigned int ms_level) const
128 {
129  if(ms_level == 0)
130  return 0;
131  if(ms_level > m_countMsLevelSpectrum.size())
132  return 0;
133  return (m_countMsLevelSpectrum[ms_level - 1]);
134 }
135 
136 
137 unsigned long
139 {
140  unsigned long total = 0;
141  for(unsigned long count : m_countMsLevelSpectrum)
142  {
143  total += count;
144  }
145  return total;
146 }
147 
148 
150 {
151  // qDebug();
152 }
153 
154 
156 {
157  // qDebug();
158 }
159 
160 
161 bool
163 {
164  return false;
165 }
166 
167 void
169  const QualifiedMassSpectrum &qspectrum)
170 {
171  qDebug() << " " << qspectrum.getMassSpectrumId().getNativeId();
172 
173  QStringList native_id_list =
174  qspectrum.getMassSpectrumId().getNativeId().split("=");
175  if(native_id_list.size() < 2)
176  {
177  return;
178  }
179  else
180  {
181  std::size_t scan_number = native_id_list.back().toULong();
182  m_mmap_scan2index.insert(std::pair<std::size_t, std::size_t>(
183  scan_number, qspectrum.getMassSpectrumId().getSpectrumIndex()));
184 
185  qDebug() << "scan number " << scan_number << "=>"
186  << qspectrum.getMassSpectrumId().getSpectrumIndex();
187  }
188 }
189 
190 std::size_t
192  std::size_t scan_number) const
193 {
194 
195  qDebug() << m_mmap_scan2index.size();
196 
197  auto it = m_mmap_scan2index.find(scan_number);
198 
199  if(it == m_mmap_scan2index.end())
200  {
201  throw ExceptionNotFound(
202  QObject::tr("scan number %1 not found").arg(scan_number));
203  }
204 
205  std::size_t index = it->second;
206 
207  it++;
208  if((it != m_mmap_scan2index.end()) && (it->first == scan_number))
209  {
210  throw PappsoException(
211  QObject::tr("scan number %1 found multiple times").arg(scan_number));
212  }
213  return index;
214 }
215 
216 
218 {
219  // qDebug();
220 }
221 
222 
224 {
225  // qDebug();
226 }
227 
228 
229 bool
231 {
232  return false;
233 }
234 
235 
236 void
238  const QualifiedMassSpectrum &qspectrum)
239 {
240  qDebug() << " " << qspectrum.getMassSpectrumId().getNativeId();
241 
242  m_retention_time_list.push_back(qspectrum.getRtInSeconds());
243 }
244 
245 const std::vector<double> &
247 {
248  return m_retention_time_list;
249 }
250 
251 
252 MsRunReader::MsRunReader(MsRunIdCstSPtr &ms_run_id) : mcsp_msRunId(ms_run_id)
253 {
254 }
255 
257  : mcsp_msRunId(other.mcsp_msRunId)
258 {
259  mpa_multiMapScanNumber = nullptr;
260 }
261 
262 
263 const MsRunIdCstSPtr &
265 {
266  return mcsp_msRunId;
267 }
268 
269 
271 {
272  if(mpa_multiMapScanNumber == nullptr)
273  delete mpa_multiMapScanNumber;
274 }
275 
276 
277 std::size_t
279 {
280  qDebug() << " " << mpa_multiMapScanNumber;
281 
282  if(mpa_multiMapScanNumber == nullptr)
283  {
286  }
287  try
288  {
290  scan_number);
291  }
292 
293  catch(ExceptionNotFound &error)
294  {
295  throw ExceptionNotFound(QObject::tr("error reading file %1 : %2")
296  .arg(mcsp_msRunId.get()->getFileName())
297  .arg(error.qwhat()));
298  }
299  catch(PappsoException &error)
300  {
301  throw PappsoException(QObject::tr("error reading file %1 : %2")
302  .arg(mcsp_msRunId.get()->getFileName())
303  .arg(error.qwhat()));
304  }
305 }
306 
307 
308 bool
310 {
311  return false;
312 }
313 
314 std::vector<double>
316 {
317  qDebug();
318 
319  try
320  {
321 
322  MsRunReaderRetentionTimeLine reader_timeline;
323 
324  readSpectrumCollectionByMsLevel(reader_timeline, 1);
325 
326  return reader_timeline.getRetentionTimeLine();
327  }
328 
329  catch(ExceptionNotFound &error)
330  {
331  throw ExceptionNotFound(QObject::tr("error reading file %1 : %2")
332  .arg(mcsp_msRunId.get()->getFileName())
333  .arg(error.qwhat()));
334  }
335  catch(PappsoException &error)
336  {
337  throw PappsoException(QObject::tr("error reading file %1 : %2")
338  .arg(mcsp_msRunId.get()->getFileName())
339  .arg(error.qwhat()));
340  }
341 }
342 
343 
344 } // namespace pappso
std::size_t getSpectrumIndex() const
const QString & getNativeId() const
collect retention times along MS run
Definition: msrunreader.h:150
const std::vector< double > & getRetentionTimeLine() const
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
std::vector< double > m_retention_time_list
Definition: msrunreader.h:152
provides a multimap to find quickly spectrum index from scan number
Definition: msrunreader.h:132
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override
std::size_t getSpectrumIndexFromScanNumber(std::size_t scan_number) const
std::multimap< std::size_t, std::size_t > m_mmap_scan2index
Definition: msrunreader.h:134
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:173
MsRunIdCstSPtr mcsp_msRunId
Definition: msrunreader.h:263
MsRunReaderScanNumberMultiMap * mpa_multiMapScanNumber
Definition: msrunreader.h:264
virtual bool hasScanNumbers() const
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
virtual std::vector< double > getRetentionTimeLine()
retention timeline get retention times along the MSrun in seconds
virtual std::size_t scanNumber2SpectrumIndex(std::size_t scan_number)
if possible, converts a scan number into a spectrum index This is a convenient function to help trans...
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler)=0
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
MsRunReader(MsRunIdCstSPtr &ms_run_id)
virtual void readSpectrumCollectionByMsLevel(SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)=0
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler by Ms Levels
const MsRunIdCstSPtr & getMsRunId() const
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override
unsigned long getTotalCount() const
virtual void loadingEnded() override
std::vector< unsigned long > m_countMsLevelSpectrum
Definition: msrunreader.h:115
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
Definition: msrunreader.cpp:97
unsigned long getMsLevelCount(unsigned int ms_level) const
virtual const QString & qwhat() const
Class representing a fully specified mass spectrum.
uint getMsLevel() const
Get the mass spectrum level.
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
pappso_double getRtInSeconds() const
Get the retention time in seconds.
virtual bool isReadAhead() const
tells if we want to read ahead spectrum
Definition: msrunreader.cpp:63
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:69
virtual void setReadAhead(bool is_read_ahead) final
use threads to read a spectrum by batch of batch_size
Definition: msrunreader.cpp:57
virtual void setNeedMsLevelPeakList(unsigned int ms_level, bool want_peak_list) final
tells if we need the peak list given
Definition: msrunreader.cpp:87
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:52
int msRunReaderSPtrMetaTypeId
Definition: msrunreader.cpp:34
base interface to read MSrun files
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