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 
112  Trace getTicChromatogram();
113 
115  {
116  std::size_t parent_frame = 0;
117  std::size_t precursor_id = 0;
118  std::size_t scan_mobility_start = 0;
119  std::size_t scan_mobility_end = 0;
120  std::size_t ms1_index = 0;
121  std::size_t ms2_index = 0;
122  double isolationMz = 0;
123  double isolationWidth = 0;
124  float collisionEnergy = 0;
125  std::vector<std::size_t> tims_frame_list;
127  };
128 
129  void
130  getQualifiedMs2MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id,
131  QualifiedMassSpectrum &mass_spectrum,
132  SpectrumDescr &spectrum_descr,
133  bool want_binary_data);
134 
135  void
136  getQualifiedMs1MassSpectrumByPrecursorId(const MsRunIdCstSPtr &msrun_id,
137  QualifiedMassSpectrum &mass_spectrum,
138  SpectrumDescr &spectrum_descr,
139  bool want_binary_data);
140 
141  /** @brief filter interface to apply just after raw MS2 specturm extraction
142  * the filter can be a list of filters inside a FilterSuite object
143  */
144  void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
145 
146  /** @brief filter interface to apply just after raw MS1 specturm extraction
147  * the filter can be a list of filters inside a FilterSuite object
148  */
149  void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr &filter);
150 
151  /** @brief enable or disable simple centroid filter on raw tims data for MS2
152  */
153  void setMs2BuiltinCentroid(bool centroid);
154 
155 
156  /** @brief tells if simple centroid filter on raw tims data for MS2 is enabled
157  * or not
158  */
159  bool getMs2BuiltinCentroid() const;
160 
161 
162  std::vector<std::size_t> getTimsMS1FrameIdRange(double rt_begin,
163  double rt_end) const;
164 
165 
166  /** @brief get a Tims frame with his database ID
167  * but look in the cache first
168  *
169  * thread safe
170  */
171  TimsFrameCstSPtr getTimsFrameCstSPtrCached(std::size_t timsId);
172 
173  /** @brief get a Tims frame with his database ID
174  *
175  * this function is not thread safe
176  */
177  TimsFrameCstSPtr getTimsFrameCstSPtr(std::size_t timsId);
178 
179  XicCoordTims getXicCoordTimsFromPrecursorId(std::size_t precursor_id,
180  PrecisionPtr precision_ptr);
181 
182 
183  /** @brief function to visit an MsRunReader and get each Spectrum in a
184  * spectrum collection handler by Ms Levels
185  *
186  * this function will retrieve processed qualified spectrum depending on each
187  * Bruker precursors
188  */
189  void ms2ReaderSpectrumCollectionByMsLevel(
190  const MsRunIdCstSPtr &msrun_id,
192  unsigned int ms_level);
193 
194 
195  /** @brief function to visit an MsRunReader and get each raw Spectrum in a
196  * spectrum collection handler by Ms Levels
197  *
198  * this function will retrieve every scans as a qualified mass spectrum
199  */
200  void rawReaderSpectrumCollectionByMsLevel(
201  const MsRunIdCstSPtr &msrun_id,
203  unsigned int ms_level);
204 
205  /** @brief get cumulated raw signal for a given precursor
206  * only to use to see the raw signal
207  *
208  * @param precursor_index precursor index to extract signal from
209  * @result a map of integers, x=time of flights, y= intensities
210  */
211  std::map<quint32, quint32>
212  getRawMs2ByPrecursorId(std::size_t precursor_index);
213 
214  /** @brief get raw signal for a spectrum index
215  * only to use to see the raw signal
216  *
217  * @param spectrum_index spcetrum index
218  * @result a map of integers, x=time of flights, y= intensities
219  */
220  std::map<quint32, quint32>
221  getRawMsBySpectrumIndex(std::size_t spectrum_index);
222 
223 
224  /** @brief retention timeline
225  * get retention times along the MSrun in seconds
226  * @return vector of retention times (seconds)
227  */
228  virtual std::vector<double> getRetentionTimeLine() const;
229 
230  /** @brief get an intermediate structure describing a spectrum
231  */
232  SpectrumDescr getSpectrumDescrWithPrecursorId(std::size_t precursor_id);
233 
234  /** @brief set only one is_mono_thread to true
235  *
236  * this avoid to use qtconcurrent
237  */
238  void setMonoThread(bool is_mono_thread);
239 
240 
241  private:
242  SpectrumDescr getSpectrumDescrWithScanCoordinate(
243  const std::pair<std::size_t, std::size_t> &scan_coordinate);
244 
245 
246  std::pair<std::size_t, std::size_t>
247  getScanCoordinateFromRawIndex(std::size_t spectrum_index) const;
248 
249  std::size_t getRawIndexFromCoordinate(std::size_t frame_id,
250  std::size_t scan_num) const;
251 
252  QSqlDatabase openDatabaseConnection() const;
253 
254 
255  /** @brief get a Tims frame base (no binary data file access) with his
256  * database ID
257  */
258  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtr(std::size_t timsId);
259 
260 
261  TimsFrameBaseCstSPtr getTimsFrameBaseCstSPtrCached(std::size_t timsId);
262 
263 
264  std::vector<std::size_t>
265  getMatchPrecursorIdByKo(std::vector<std::vector<double>> ids,
266  double ko_value);
267 
268  /** @todo documentation
269  */
270  std::vector<std::size_t>
271  getClosestPrecursorIdByMz(std::vector<std::vector<double>> ids,
272  double mz_value);
273 
274 
275  /** @brief private function to fill m_frameIdDescrList
276  */
277  void fillFrameIdDescrList();
278 
279 
280  private:
281  void ms2ReaderGenerateMS1MS2Spectrum(
282  const MsRunIdCstSPtr &msrun_id,
283  std::vector<QualifiedMassSpectrum> &qualified_mass_spectrum_list,
285  SpectrumDescr &spectrum_descr,
286  unsigned int ms_level);
287 
288  void fillSpectrumDescriptionWithSqlRecord(SpectrumDescr &spectrum_descr,
289  QSqlQuery &qprecursor_list);
290 
291  private:
293  TimsBinDec *mpa_timsBinDec = nullptr;
294  // QSqlDatabase *mpa_qdb = nullptr;
295  std::size_t m_totalNumberOfScans;
298  std::size_t m_cacheSize = 60;
299  std::deque<TimsFrameCstSPtr> m_timsFrameCache;
300  std::deque<TimsFrameBaseCstSPtr> m_timsFrameBaseCache;
301 
302  pappso::FilterInterfaceCstSPtr mcsp_ms2Filter = nullptr;
303  pappso::FilterInterfaceCstSPtr mcsp_ms1Filter = nullptr;
304 
305  /** @brief enable builtin centroid on raw tims integers by default
306  */
307  bool m_builtinMs2Centroid = true;
308 
309 
310  std::map<int, QSqlRecord> m_mapMzCalibrationRecord;
311  std::map<int, QSqlRecord> m_mapTimsCalibrationRecord;
312  std::vector<TimsFrameRecord> m_mapFramesRecord;
313  std::map<std::size_t, QSqlRecord> m_mapXicCoordRecord;
314 
316 
317 
319  {
320  std::size_t m_frameId; // frame id
321  std::size_t m_size; // frame size (number of TOF scans in frame)
322  std::size_t m_cumulSize; // cumulative size
323  };
324 
325 
326  /** @brief store every frame id and corresponding sizes
327  */
328  std::vector<FrameIdDescr> m_frameIdDescrList;
329 
330  /** @brief index to find quickly a frameId in the description list with the
331  * raw index of spectrum modulo 1000
332  * @key thousands of TOF scans
333  * @value corresponding m_frameIdDescrList index
334  */
335  std::map<std::size_t, std::size_t> m_thousandIndexToFrameIdDescrListIndex;
336 
337 
338  /** @brief tells if someone is loading a frame id
339  */
340  std::vector<std::size_t> m_someoneIsLoadingFrameId;
341 
342  bool m_isMonoThread = false;
343 
344  QMutex m_mutex;
345 };
346 } // 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:328
std::map< std::size_t, QSqlRecord > m_mapXicCoordRecord
Definition: timsdata.h:313
std::size_t m_totalNumberOfFrames
Definition: timsdata.h:297
std::size_t m_totalNumberOfScans
Definition: timsdata.h:295
std::deque< TimsFrameCstSPtr > m_timsFrameCache
Definition: timsdata.h:299
std::vector< TimsFrameRecord > m_mapFramesRecord
Definition: timsdata.h:312
std::map< int, QSqlRecord > m_mapMzCalibrationRecord
Definition: timsdata.h:310
std::map< int, QSqlRecord > m_mapTimsCalibrationRecord
Definition: timsdata.h:311
QMutex m_mutex
Definition: timsdata.h:344
std::vector< std::size_t > m_someoneIsLoadingFrameId
tells if someone is loading a frame id
Definition: timsdata.h:340
TimsData(const TimsData &other)
QDir m_timsDataDirectory
Definition: timsdata.h:292
MzCalibrationStore * mpa_mzCalibrationStore
Definition: timsdata.h:315
std::deque< TimsFrameBaseCstSPtr > m_timsFrameBaseCache
Definition: timsdata.h:300
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 @k...
Definition: timsdata.h:335
std::size_t m_totalNumberOfPrecursors
Definition: timsdata.h:296
A simple container of DataPoint instances.
Definition: trace.h:148
#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:125
PrecursorIonData precursor_ion_data
Definition: timsdata.h:126
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