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 
61 /**
62  * @todo write docs
63  */
65 {
66  public:
67  /** @brief build using the tims data directory
68  */
69  TimsData(QDir timsDataDirectory);
70 
71  /**
72  * Copy constructor
73  *
74  * @param other TODO
75  */
76  TimsData(const TimsData &other);
77 
78  /**
79  * Destructor
80  */
81  ~TimsData();
82 
83 
84  /** @brief get a mass spectrum given its spectrum index
85  * @param raw_index a number begining at 0, corresponding to a Tims Scan in
86  * the order they lies in the binary data file
87  */
89  getMassSpectrumCstSPtrByRawIndex(std::size_t raw_index);
90 
91  /** @brief get a mass spectrum given the tims frame database id and scan
92  * number within tims frame
93  */
94  pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtr(std::size_t timsId,
95  std::size_t scanNum);
96 
97  /** @brief get the total number of scans
98  */
99  std::size_t getTotalNumberOfScans() const;
100 
101  /** @brief get the number of precursors analyzes by PASEF
102  */
103  std::size_t getTotalNumberOfPrecursors() const;
104 
105  /** @brief guess possible precursor ids given a charge, m/z, retention time
106  * and k0
107  * @return a list of possible precursor ids
108  */
109  std::vector<std::size_t> getPrecursorsFromMzRtCharge(int charge,
110  double mz_val,
111  double rt_sec,
112  double k0);
113 
114  unsigned int getMsLevelBySpectrumIndex(std::size_t spectrum_index);
115 
117  getQualifiedMassSpectrumByRawIndex(std::size_t spectrum_index,
118  bool want_binary_data);
119 
120  void
121  getQualifiedMs2MassSpectrumByPrecursorId(QualifiedMassSpectrum &mass_spectrum,
122  std::size_t ms2_index,
123  std::size_t precursor_index,
124  bool want_binary_data);
125 
126  QualifiedMassSpectrum getQualifiedMs1MassSpectrumByPrecursorId(
127  std::size_t ms2_index, std::size_t precursor_index, bool want_binary_data);
128 
129  void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
130  void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
131 
132 
133  std::vector<std::size_t> getTimsMS1FrameIdRange(double rt_begin,
134  double rt_end) const;
135 
136 
137  /** @brief get a Tims frame with his database ID
138  */
139  TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId) const;
140 
141  private:
142  std::pair<std::size_t, std::size_t>
143  getScanCoordinateFromRawIndex(std::size_t spectrum_index) const;
144 
145  std::size_t getRawIndexFromCoordinate(std::size_t frame_id,
146  std::size_t scan_num) const;
147 
148  QSqlDatabase openDatabaseConnection() const;
149 
150 
151  /** @brief get a Tims frame base (no binary data file access) with his
152  * database ID
153  */
154  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId) const;
155 
156 
157  /** @brief get a Tims frame with his database ID
158  * but look in the cache first
159  */
160  TimsFrameCstSPtr getTimsFrameCstSPtrCached(std::size_t timsId);
161 
162  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId);
163 
164  /** @brief extract a list of XICs from Tims data
165  *
166  * @param precursor_id_list the list of precursors to extract
167  * @param precision_ptr precision to compute the mz range to extract for each
168  * precursor mass
169  * @param xicExtractMethod XIC extraction method (sum or max) to use
170  * @param rtRange retention time range in seconds to extract XIC from rtTarget
171  * - rtRange to rtTarget + rtRange
172  * @result the corresponding XIC list as a specific structure
173  */
174  std::vector<TimsXicStructure> extractXicListByPrecursorIds(
175  const std::vector<std::size_t> &precursor_id_list,
176  PrecisionPtr precision_ptr,
177  XicExtractMethod xicExtractMethod,
178  double rtRange) const;
179 
180  std::vector<std::size_t>
181  getMatchPrecursorIdByKo(std::vector<std::vector<double>> ids,
182  double ko_value);
183  std::vector<std::size_t>
184  getClosestIdByMz(std::vector<std::vector<double>> ids, double mz_value);
185 
186  private:
188  TimsBinDec *mpa_timsBinDec = nullptr;
189  // QSqlDatabase *mpa_qdb = nullptr;
190  std::size_t m_totalNumberOfScans;
192  std::size_t m_cacheSize = 60;
193  std::deque<TimsFrameCstSPtr> m_timsFrameCache;
194  std::deque<TimsFrameBaseCstSPtr> m_timsFrameBaseCache;
195 
196  pappso::FilterInterfaceCstSPtr mcsp_ms2Filter = nullptr;
197  pappso::FilterInterfaceCstSPtr mcsp_ms1Filter = nullptr;
198  QMutex m_mutex;
199 };
200 } // namespace pappso
pappso::TimsData::m_totalNumberOfPrecursors
std::size_t m_totalNumberOfPrecursors
Definition: timsdata.h:191
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:194
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
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:65
pappso::TimsData::m_mutex
QMutex m_mutex
Definition: timsdata.h:198
pappso::MzRange
Definition: mzrange.h:46
pappso::TimsXicStructure::xicSptr
XicSPtr xicSptr
Definition: timsdata.h:56
pappso::XicExtractMethod
XicExtractMethod
Definition: types.h:183
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:85
pappso::TimsData::m_timsDataDirectory
QDir m_timsDataDirectory
Definition: timsdata.h:187
pappso::TimsData::m_totalNumberOfScans
std::size_t m_totalNumberOfScans
Definition: timsdata.h:190
pappso::TimsData::m_timsFrameCache
std::deque< TimsFrameCstSPtr > m_timsFrameCache
Definition: timsdata.h:193
pappso::TimsXicStructure
structure needed to extract XIC from Tims data
Definition: timsdata.h:48
pappso::PrecisionBase
Definition: precision.h:44
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