pappsomspp
Library for mass spectrometry
msrunreader.h
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/msrun/msrunreader.h
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  * Contributors:
27  * Olivier Langella <Olivier.Langella@u-psud.fr> - initial API and
28  *implementation
29  ******************************************************************************/
30 
31 #pragma once
32 
33 
34 /////////////////////// StdLib includes
35 #include <memory>
36 #include <map>
37 
38 
39 /////////////////////// Qt includes
40 #include <QMutex>
41 
42 
43 /////////////////////// pappsomspp includes
44 
45 
46 /////////////////////// Local includes
47 #include "msrunid.h"
48 #include "../massspectrum/qualifiedmassspectrum.h"
49 #include "../msfile/msfilereader.h"
50 #include "../exportinmportconfig.h"
51 
52 
53 namespace pappso
54 {
55 
56 /** @brief interface to collect spectrums from the MsRunReader class
57  */
58 class PMSPP_LIB_DECL SpectrumCollectionHandlerInterface
59 {
60  public:
61  virtual void
62  setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) = 0;
63 
64  /** @brief tells if we need the peak list (if we want the binary data) for
65  * each spectrum
66  */
67  virtual bool needPeakList() const = 0;
68 
69  /** @brief tells if we need the peak list (if we want the binary data) for
70  * each spectrum, given an MS level
71  */
72  virtual bool needMsLevelPeakList(unsigned int ms_level) const final;
73 
74  /** @brief tells if we need the peak list given
75  */
76  virtual void setNeedMsLevelPeakList(unsigned int ms_level,
77  bool want_peak_list) final;
78  virtual bool shouldStop();
79  virtual void loadingEnded();
80  virtual void spectrumListHasSize(std::size_t size);
81 
82 
83  /** @brief use threads to read a spectrum by batch of batch_size
84  * @param is_read_ahead boolean to use threads or not
85  */
86  virtual void setReadAhead(bool is_read_ahead) final;
87 
88  /** @brief tells if we want to read ahead spectrum
89  */
90  virtual bool isReadAhead() const;
91 
92  private:
93  bool m_isReadAhead = false;
94  std::vector<bool> m_needPeakListByMsLevel = {true,
95  true,
96  true,
97  true,
98  true,
99  true,
100  true,
101  true,
102  true,
103  true,
104  true,
105  true,
106  true,
107  true,
108  true};
109 };
110 
111 
112 /** @brief example of interface to count MS levels of all spectrum in an MSrun
113  */
114 class PMSPP_LIB_DECL MsRunSimpleStatistics : public SpectrumCollectionHandlerInterface
115 {
116  private:
117  std::vector<unsigned long> m_countMsLevelSpectrum;
118 
119  public:
120  virtual void
121  setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override;
122  virtual bool needPeakList() const override;
123  virtual void loadingEnded() override;
124 
125  unsigned long getMsLevelCount(unsigned int ms_level) const;
126 
127  unsigned long getTotalCount() const;
128 };
129 
130 /** @brief provides a multimap to find quickly spectrum index from scan number
131  */
133 {
134  private:
135  std::multimap<std::size_t, std::size_t> m_mmap_scan2index;
136 
137  public:
140  virtual void
141  setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override;
142  virtual bool needPeakList() const override;
143 
144  std::size_t getSpectrumIndexFromScanNumber(std::size_t scan_number) const;
145 };
146 
147 
149 typedef std::shared_ptr<MsRunReader> MsRunReaderSPtr;
150 typedef std::shared_ptr<const MsRunReader> MsRunReaderCstSPtr;
151 
152 /** @brief base class to read MSrun
153  * the only way to build a MsRunReader object is to use the MsRunReaderFactory
154  */
156 {
157 
158  friend class MsFileAccessor;
159 
160  public:
161  MsRunReader(MsRunIdCstSPtr &ms_run_id);
162  MsRunReader(const MsRunReader &other);
163  virtual ~MsRunReader();
164 
165  const MsRunIdCstSPtr &getMsRunId() const;
166 
167  /** @brief get a MassSpectrumSPtr class given its spectrum index
168  */
169  virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) = 0;
170  virtual MassSpectrumCstSPtr
171  massSpectrumCstSPtr(std::size_t spectrum_index) = 0;
172 
173  /** @brief get a QualifiedMassSpectrum class given its scan number
174  */
175  virtual QualifiedMassSpectrum
176  qualifiedMassSpectrum(std::size_t spectrum_index,
177  bool want_binary_data = true) const = 0;
178 
179  /** @brief get the totat number of spectrum conained in the MSrun data file
180  */
181  virtual std::size_t spectrumListSize() const = 0;
182 
183  /** @brief function to visit an MsRunReader and get each Spectrum in a
184  * spectrum collection handler
185  */
186  virtual void
187  readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) = 0;
188 
189  /** @brief if possible, converts a scan number into a spectrum index
190  * This is a convenient function to help transition from the old scan number
191  * (not implemented by all vendors) to more secure spectrum index (not vendor
192  * dependant).
193  * It is better to not rely on this function.
194  */
195  virtual std::size_t scanNumber2SpectrumIndex(std::size_t scan_number);
196 
197  /** @brief tells if spectra can be accessed using scan numbers
198  * by default, it returns false. Only overrided functions can check if scan
199  * numbers are available in the current file
200  */
201  virtual bool hasScanNumbers() const;
202 
203 
204  protected:
205  MsRunIdCstSPtr mcsp_msRunId;
206  MsRunReaderScanNumberMultiMap *mpa_multiMapScanNumber = nullptr;
207 
208  virtual void initialize() = 0;
209 
210  /** @brief tells if the reader is able to handle this file
211  * must be implemented by private MS run reader, specific of one or more file
212  * format
213  */
214  virtual bool accept(const QString &file_name) const = 0;
215 };
216 
217 
218 } // namespace pappso
219 
221 extern int msRunReaderSPtrMetaTypeId;
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:74
pappso::MsRunReader
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:174
PMSPP_LIB_DECL
#define PMSPP_LIB_DECL
Definition: exportinmportconfig.h:12
pappso
Definition: aa.cpp:38
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:168
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:63
pappso::MsFileAccessor
Definition: msfileaccessor.h:34
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(pappso::MsRunReaderSPtr)
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:83
pappso::MsRunReaderCstSPtr
std::shared_ptr< const MsRunReader > MsRunReaderCstSPtr
Definition: msrunreader.h:169
msrunid.h
pappso::MsRunReader
class PMSPP_LIB_DECL MsRunReader
Definition: msrunreader.h:167
pappso::MsRunReaderScanNumberMultiMap
provides a multimap to find quickly spectrum index from scan number
Definition: msrunreader.h:151
msRunReaderSPtrMetaTypeId
int msRunReaderSPtrMetaTypeId
Definition: msrunreader.cpp:36
pappso::SpectrumCollectionHandlerInterface
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:77
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:73