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