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 "../../msrun/xiccoord/xiccoordtims.h"
37 #include "../../msrun/msrunreader.h"
38 #include <deque>
39 #include <QMutex>
40 #include <QSqlQuery>
42 
43 namespace pappso
44 {
45 
46 class TimsData;
47 
48 /** \brief shared pointer on a TimsData object
49  */
50 typedef std::shared_ptr<TimsData> TimsDataSp;
51 
52 /**
53  * @todo write docs
54  */
56 {
57  public:
58  /** @brief build using the tims data directory
59  */
60  TimsData(QDir timsDataDirectory);
61 
62  /**
63  * Copy constructor
64  *
65  * @param other TODO
66  */
67  TimsData(const TimsData &other);
68 
69  /**
70  * Destructor
71  */
72  virtual ~TimsData();
73 
74 
75  /** @brief get a mass spectrum given its spectrum index
76  * @param raw_index a number begining at 0, corresponding to a Tims Scan in
77  * the order they lies in the binary data file
78  */
80  getMassSpectrumCstSPtrByRawIndex(std::size_t raw_index);
81 
82  /** @brief get a mass spectrum given the tims frame database id and scan
83  * number within tims frame
84  */
85  pappso::MassSpectrumCstSPtr getMassSpectrumCstSPtr(std::size_t timsId,
86  std::size_t scanNum);
87 
88  /** @brief get the total number of scans
89  */
90  std::size_t getTotalNumberOfScans() const;
91 
92  /** @brief get the number of precursors analyzes by PASEF
93  */
94  std::size_t getTotalNumberOfPrecursors() const;
95 
96  /** @brief guess possible precursor ids given a charge, m/z, retention time
97  * and k0
98  * @return a list of possible precursor ids
99  */
100  std::vector<std::size_t> getPrecursorsFromMzRtCharge(int charge,
101  double mz_val,
102  double rt_sec,
103  double k0);
104 
105  unsigned int getMsLevelBySpectrumIndex(std::size_t spectrum_index);
106 
107  void getQualifiedMassSpectrumByRawIndex(const MsRunIdCstSPtr &msrun_id,
108  QualifiedMassSpectrum &mass_spectrum,
109  std::size_t spectrum_index,
110  bool want_binary_data);
111 
113  {
114  std::size_t parent_frame = 0;
115  std::size_t precursor_id = 0;
116  std::size_t scan_mobility_start = 0;
117  std::size_t scan_mobility_end = 0;
118  std::size_t ms1_index = 0;
119  std::size_t ms2_index = 0;
120  double isolationMz = 0;
121  double isolationWidth = 0;
122  float collisionEnergy = 0;
123  std::vector<std::size_t> tims_frame_list;
125  };
126 
127  void
128  getQualifiedMs2MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id,
129  QualifiedMassSpectrum &mass_spectrum,
130  SpectrumDescr &spectrum_descr,
131  bool want_binary_data);
132 
133  void
134  getQualifiedMs1MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id,
135  QualifiedMassSpectrum &mass_spectrum,
136  SpectrumDescr &spectrum_descr,
137  bool want_binary_data);
138 
139  /** @brief filter interface to apply just after raw MS2 specturm extraction
140  * the filter can be a list of filters inside a FilterSuite object
141  */
142  void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
143 
144  /** @brief filter interface to apply just after raw MS1 specturm extraction
145  * the filter can be a list of filters inside a FilterSuite object
146  */
147  void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
148 
149  /** @brief enable or disable simple centroid filter on raw tims data for MS2
150  */
151  void setMs2BuiltinCentroid(bool centroid);
152 
153 
154  /** @brief tells if simple centroid filter on raw tims data for MS2 is enabled
155  * or not
156  */
157  bool getMs2BuiltinCentroid() const;
158 
159 
160  std::vector<std::size_t> getTimsMS1FrameIdRange(double rt_begin,
161  double rt_end) const;
162 
163 
164  /** @brief get a Tims frame with his database ID
165  */
166  TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId) const;
167 
168  XicCoordTims getXicCoordTimsFromPrecursorId(std::size_t precursor_id,
169  PrecisionPtr precision_ptr);
170 
171 
172  /** @brief function to visit an MsRunReader and get each Spectrum in a
173  * spectrum collection handler by Ms Levels
174  *
175  * this function will retrieve processed qualified spectrum depending on each
176  * Bruker precursors
177  */
178  void ms2ReaderSpectrumCollectionByMsLevel(
179  const MsRunIdCstSPtr &msrun_id,
181  unsigned int ms_level);
182 
183 
184  /** @brief function to visit an MsRunReader and get each raw Spectrum in a
185  * spectrum collection handler by Ms Levels
186  *
187  * this function will retrieve every scans as a qualified mass spectrum
188  */
189  void rawReaderSpectrumCollectionByMsLevel(
190  const MsRunIdCstSPtr &msrun_id,
192  unsigned int ms_level);
193 
194  /** @brief get cumulated raw signal for a given precursor
195  * only to use to see the raw signal
196  *
197  * @param precursor_index precursor index to extract signal from
198  * @result a map of integers, x=time of flights, y= intensities
199  */
200  std::map<quint32, quint32>
201  getRawMs2ByPrecursorId(std::size_t precursor_index);
202 
203  /** @brief get raw signal for a spectrum index
204  * only to use to see the raw signal
205  *
206  * @param spectrum_index spcetrum index
207  * @result a map of integers, x=time of flights, y= intensities
208  */
209  std::map<quint32, quint32>
210  getRawMsBySpectrumIndex(std::size_t spectrum_index);
211 
212 
213  /** @brief retention timeline
214  * get retention times along the MSrun in seconds
215  * @return vector of retention times (seconds)
216  */
217  virtual std::vector<double> getRetentionTimeLine() const;
218 
219  /** @brief get an intermediate structure describing a spectrum
220  */
221  SpectrumDescr getSpectrumDescrWithPrecursorId(std::size_t precursor_id);
222 
223 
224  private:
225  SpectrumDescr getSpectrumDescrWithScanCoordinate(
226  const std::pair<std::size_t, std::size_t> &scan_coordinate);
227 
228 
229  std::pair<std::size_t, std::size_t>
230  getScanCoordinateFromRawIndex(std::size_t spectrum_index) const;
231 
232  std::size_t getRawIndexFromCoordinate(std::size_t frame_id,
233  std::size_t scan_num) const;
234 
235  QSqlDatabase openDatabaseConnection() const;
236 
237 
238  /** @brief get a Tims frame base (no binary data file access) with his
239  * database ID
240  */
241  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId) const;
242 
243 
244  /** @brief get a Tims frame with his database ID
245  * but look in the cache first
246  */
247  TimsFrameCstSPtr getTimsFrameCstSPtrCached(std::size_t timsId);
248 
249  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId);
250 
251 
252  std::vector<std::size_t>
253  getMatchPrecursorIdByKo(std::vector<std::vector<double>> ids,
254  double ko_value);
255 
256  /** @todo documentation
257  */
258  std::vector<std::size_t>
259  getClosestPrecursorIdByMz(std::vector<std::vector<double>> ids,
260  double mz_value);
261 
262 
263  /** @brief private function to fill m_frameIdDescrList
264  */
265  void fillFrameIdDescrList();
266 
267 
268  private:
269  void
270  ms2ReaderGenerateMS1MS2Spectrum(const MsRunIdCstSPtr &msrun_id,
272  SpectrumDescr &spectrum_descr,
273  unsigned int ms_level);
274 
275  void fillSpectrumDescriptionWithSqlRecord(SpectrumDescr &spectrum_descr,
276  QSqlQuery &qprecursor_list);
277 
278  private:
280  TimsBinDec *mpa_timsBinDec = nullptr;
281  // QSqlDatabase *mpa_qdb = nullptr;
282  std::size_t m_totalNumberOfScans;
284  std::size_t m_cacheSize = 60;
285  std::deque<TimsFrameCstSPtr> m_timsFrameCache;
286  std::deque<TimsFrameBaseCstSPtr> m_timsFrameBaseCache;
287 
288  pappso::FilterInterfaceCstSPtr mcsp_ms2Filter = nullptr;
289  pappso::FilterInterfaceCstSPtr mcsp_ms1Filter = nullptr;
290 
291  /** @brief enable builtin centroid on raw tims integers by default
292  */
293  bool m_builtinMs2Centroid = true;
294 
295 
296  std::map<int, QSqlRecord> m_mapMzCalibrationRecord;
297  std::map<int, QSqlRecord> m_mapTimsCalibrationRecord;
298  std::map<std::size_t, QSqlRecord> m_mapFramesRecord;
299  std::map<std::size_t, QSqlRecord> m_mapXicCoordRecord;
300 
302 
303 
305  {
306  std::size_t m_frameId; // frame id
307  std::size_t m_size; // frame size
308  std::size_t m_cumulSize; // cumulative size
309  };
310 
311 
312  /** @brief store every frame id and corresponding sizes
313  */
314  std::vector<FrameIdDescr> m_frameIdDescrList;
315 
316  /** @brief index to find quickly a frameId in the description list with the
317  * raw index of spectrum modulo 1000
318  */
319  std::map<std::size_t, std::size_t> m_thousandIndexToFrameIdDescrListIndex;
320 
321  QMutex m_mutex;
322 };
323 } // namespace pappso
Class representing a fully specified mass spectrum.
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:56
std::vector< FrameIdDescr > m_frameIdDescrList
store every frame id and corresponding sizes
Definition: timsdata.h:314
std::map< std::size_t, QSqlRecord > m_mapFramesRecord
Definition: timsdata.h:298
std::map< std::size_t, QSqlRecord > m_mapXicCoordRecord
Definition: timsdata.h:299
std::size_t m_totalNumberOfScans
Definition: timsdata.h:282
std::deque< TimsFrameCstSPtr > m_timsFrameCache
Definition: timsdata.h:285
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
Definition: timsdata.h:296
std::map< int, QSqlRecord > m_mapTimsCalibrationRecord
Definition: timsdata.h:297
QMutex m_mutex
Definition: timsdata.h:321
TimsData(const TimsData &other)
QDir m_timsDataDirectory
Definition: timsdata.h:279
MzCalibrationStore * mpa_mzCalibrationStore
Definition: timsdata.h:301
std::deque< TimsFrameBaseCstSPtr > m_timsFrameBaseCache
Definition: timsdata.h:286
std::map< std::size_t, std::size_t > m_thousandIndexToFrameIdDescrListIndex
index to find quickly a frameId in the description list with the raw index of spectrum modulo 1000
Definition: timsdata.h:319
std::size_t m_totalNumberOfPrecursors
Definition: timsdata.h:283
#define PMSPP_LIB_DECL
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< const TimsFrameBase > TimsFrameBaseCstSPtr
Definition: timsframebase.h:41
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:44
std::shared_ptr< TimsData > TimsDataSp
shared pointer on a TimsData object
Definition: timsdata.h:46
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
std::shared_ptr< const FilterInterface > FilterInterfaceCstSPtr
std::shared_ptr< const TimsFrame > TimsFrameCstSPtr
Definition: timsframe.h:43
std::vector< std::size_t > tims_frame_list
Definition: timsdata.h:123
PrecursorIonData precursor_ion_data
Definition: timsdata.h:124
coordinates of the XIC to extract and the resulting XIC after extraction
Definition: xiccoordtims.h:51
binary file handler of Bruker's TimsTof raw data
handle a single Bruker's TimsTof frame