libpappsomspp
Library for mass spectrometry
timsdata.h
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/vendors/tims/timsdata.h
3  * \date 27/08/2019
4  * \author Olivier Langella
5  * \brief main Tims data handler
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2019 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 #pragma once
29 
30 #include <QDir>
31 #include <QSqlDatabase>
32 #include "timsbindec.h"
33 #include "timsframe.h"
34 #include "../../massspectrum/qualifiedmassspectrum.h"
35 #include "../../processing/filters/filterinterface.h"
36 #include <deque>
37 #include <QMutex>
38 #include <QSqlQuery>
40 
41 namespace pappso
42 {
43 
44 
45 /** @brief structure needed to extract XIC from Tims data
46  */
47 
49 {
51  : mzRange(pappso_double(1), PrecisionFactory::getPpmInstance(10.0)){};
52  std::size_t precursorId;
54  std::size_t scanNumBegin;
55  std::size_t scanNumEnd;
57  XicSPtr xicSptr = nullptr;
58  unsigned int charge;
59 };
60 
61 class TimsData;
62 
63 /** \brief shared pointer on a TimsData object
64  */
65 typedef std::shared_ptr<TimsData> TimsDataSp;
66 
67 /**
68  * @todo write docs
69  */
71 {
72  public:
73  /** @brief build using the tims data directory
74  */
75  TimsData(QDir timsDataDirectory);
76 
77  /**
78  * Copy constructor
79  *
80  * @param other TODO
81  */
82  TimsData(const TimsData &other);
83 
84  /**
85  * Destructor
86  */
87  ~TimsData();
88 
89 
90  /** @brief get a mass spectrum given its spectrum index
91  * @param raw_index a number begining at 0, corresponding to a Tims Scan in
92  * the order they lies in the binary data file
93  */
95  getMassSpectrumCstSPtrByRawIndex(std::size_t raw_index);
96 
97  /** @brief get a mass spectrum given the tims frame database id and scan
98  * number within tims frame
99  */
100  pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtr(std::size_t timsId,
101  std::size_t scanNum);
102 
103  /** @brief get the total number of scans
104  */
105  std::size_t getTotalNumberOfScans() const;
106 
107  /** @brief get the number of precursors analyzes by PASEF
108  */
109  std::size_t getTotalNumberOfPrecursors() const;
110 
111  /** @brief guess possible precursor ids given a charge, m/z, retention time
112  * and k0
113  * @return a list of possible precursor ids
114  */
115  std::vector<std::size_t> getPrecursorsFromMzRtCharge(int charge,
116  double mz_val,
117  double rt_sec,
118  double k0);
119 
120  unsigned int getMsLevelBySpectrumIndex(std::size_t spectrum_index);
121 
123  getQualifiedMassSpectrumByRawIndex(std::size_t spectrum_index,
124  bool want_binary_data);
125 
126  void
127  getQualifiedMs2MassSpectrumByPrecursorId(QualifiedMassSpectrum &mass_spectrum,
128  std::size_t ms2_index,
129  std::size_t precursor_index,
130  bool want_binary_data);
131 
132  QualifiedMassSpectrum getQualifiedMs1MassSpectrumByPrecursorId(
133  std::size_t ms2_index, std::size_t precursor_index, bool want_binary_data);
134 
135  /** @brief filter interface to apply just after raw MS2 specturm extraction
136  * the filter can be a list of filters inside a FilterSuite object
137  */
138  void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
139 
140  /** @brief filter interface to apply just after raw MS1 specturm extraction
141  * the filter can be a list of filters inside a FilterSuite object
142  */
143  void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
144 
145  /** @brief enable or disable simple centroid filter on raw tims data for MS2
146  */
147  void setMs2BuiltinCentroid(bool centroid);
148 
149 
150  /** @brief tells if simple centroid filter on raw tims data for MS2 is enabled
151  * or not
152  */
153  bool getMs2BuiltinCentroid() const;
154 
155 
156  std::vector<std::size_t> getTimsMS1FrameIdRange(double rt_begin,
157  double rt_end) const;
158 
159 
160  /** @brief get a Tims frame with his database ID
161  */
162  TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId) const;
163 
164  private:
165  std::pair<std::size_t, std::size_t>
166  getScanCoordinateFromRawIndex(std::size_t spectrum_index) const;
167 
168  std::size_t getRawIndexFromCoordinate(std::size_t frame_id,
169  std::size_t scan_num) const;
170 
171  QSqlDatabase openDatabaseConnection() const;
172 
173 
174  /** @brief get a Tims frame base (no binary data file access) with his
175  * database ID
176  */
177  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId) const;
178 
179 
180  /** @brief get a Tims frame with his database ID
181  * but look in the cache first
182  */
183  TimsFrameCstSPtr getTimsFrameCstSPtrCached(std::size_t timsId);
184 
185  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId);
186 
187  /** @brief extract a list of XICs from Tims data
188  *
189  * @param precursor_id_list the list of precursors to extract
190  * @param precision_ptr precision to compute the mz range to extract for each
191  * precursor mass
192  * @param xicExtractMethod XIC extraction method (sum or max) to use
193  * @param rtRange retention time range in seconds to extract XIC from rtTarget
194  * - rtRange to rtTarget + rtRange
195  * @result the corresponding XIC list as a specific structure
196  */
197  std::vector<TimsXicStructure> extractXicListByPrecursorIds(
198  const std::vector<std::size_t> &precursor_id_list,
199  PrecisionPtr precision_ptr,
200  XicExtractMethod xicExtractMethod,
201  double rtRange) const;
202 
203  std::vector<std::size_t>
204  getMatchPrecursorIdByKo(std::vector<std::vector<double>> ids,
205  double ko_value);
206 
207  /** @todo documentation
208  */
209  std::vector<std::size_t>
210  getClosestPrecursorIdByMz(std::vector<std::vector<double>> ids,
211  double mz_value);
212 
213  private:
215  TimsBinDec *mpa_timsBinDec = nullptr;
216  // QSqlDatabase *mpa_qdb = nullptr;
217  std::size_t m_totalNumberOfScans;
219  std::size_t m_cacheSize = 60;
220  std::deque<TimsFrameCstSPtr> m_timsFrameCache;
221  std::deque<TimsFrameBaseCstSPtr> m_timsFrameBaseCache;
222 
223  pappso::FilterInterfaceCstSPtr mcsp_ms2Filter = nullptr;
224  pappso::FilterInterfaceCstSPtr mcsp_ms1Filter = nullptr;
225 
226  /** @brief enable builtin centroid on raw tims integers by default
227  */
228  bool m_builtinMs2Centroid = true;
229 
230 
231  std::map<int, QSqlRecord> m_mapMzCalibrationRecord;
232 
234 
235  QMutex m_mutex;
236 };
237 } // namespace pappso
pappso::TimsData::m_mapMzCalibrationRecord
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
Definition: timsdata.h:231
pappso::TimsData::m_totalNumberOfPrecursors
std::size_t m_totalNumberOfPrecursors
Definition: timsdata.h:218
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:48
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
pappso::TimsXicStructure::TimsXicStructure
TimsXicStructure()
Definition: timsdata.h:50
pappso::PrecisionFactory
Definition: precision.h:127
pappso::XicSPtr
std::shared_ptr< Xic > XicSPtr
Definition: xic.h:39
pappso::TimsXicStructure::precursorId
std::size_t precursorId
Definition: timsdata.h:51
timsbindec.h
binary file handler of Bruker's TimsTof raw data
pappso::TimsData::m_timsFrameBaseCache
std::deque< TimsFrameBaseCstSPtr > m_timsFrameBaseCache
Definition: timsdata.h:221
pappso::MzCalibrationStore
Definition: mzcalibrationstore.h:40
pappso::TimsBinDec
Definition: timsbindec.h:41
PMSPP_LIB_DECL
#define PMSPP_LIB_DECL
Definition: exportinmportconfig.h:14
timsframe.h
handle a single Bruker's TimsTof frame
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::FilterInterfaceCstSPtr
std::shared_ptr< const FilterInterface > FilterInterfaceCstSPtr
Definition: filterinterface.h:47
mzcalibrationstore.h
pappso::TimsFrameBaseCstSPtr
std::shared_ptr< const TimsFrameBase > TimsFrameBaseCstSPtr
Definition: timsframebase.h:41
pappso::TimsXicStructure::scanNumBegin
std::size_t scanNumBegin
Definition: timsdata.h:54
pappso::TimsXicStructure::charge
unsigned int charge
Definition: timsdata.h:58
pappso::TimsData
Definition: timsdata.h:71
pappso::TimsData::m_mutex
QMutex m_mutex
Definition: timsdata.h:235
pappso::MzRange
Definition: mzrange.h:46
pappso::TimsXicStructure::xicSptr
XicSPtr xicSptr
Definition: timsdata.h:57
pappso::XicExtractMethod
XicExtractMethod
Definition: types.h:200
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:85
pappso::TimsData::m_timsDataDirectory
QDir m_timsDataDirectory
Definition: timsdata.h:214
pappso::TimsData::m_totalNumberOfScans
std::size_t m_totalNumberOfScans
Definition: timsdata.h:217
pappso::TimsData::m_timsFrameCache
std::deque< TimsFrameCstSPtr > m_timsFrameCache
Definition: timsdata.h:220
pappso::TimsXicStructure
structure needed to extract XIC from Tims data
Definition: timsdata.h:49
pappso::PrecisionBase
Definition: precision.h:44
pappso::TimsDataSp
std::shared_ptr< TimsData > TimsDataSp
shared pointer on a TimsData object
Definition: timsdata.h:61
pappso::TimsXicStructure::mzRange
MzRange mzRange
Definition: timsdata.h:53
pappso::TimsData::TimsData
TimsData(const TimsData &other)
pappso::TimsXicStructure::scanNumEnd
std::size_t scanNumEnd
Definition: timsdata.h:55
pappso::TimsData::mpa_mzCalibrationStore
MzCalibrationStore * mpa_mzCalibrationStore
Definition: timsdata.h:233
pappso::TimsFrameCstSPtr
std::shared_ptr< const TimsFrame > TimsFrameCstSPtr
Definition: timsframe.h:42
pappso::TimsXicStructure::rtTarget
pappso::pappso_double rtTarget
Definition: timsdata.h:56