pappsomspp
Library for mass spectrometry
timsframebase.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/vendors/tims/timsframebase.cpp
3  * \date 16/12/2019
4  * \author Olivier Langella
5  * \brief handle a single Bruker's TimsTof frame without binary data
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 #include "timsframebase.h"
28 #include "../../../pappsomspp/pappsoexception.h"
29 #include "../../../pappsomspp/exception/exceptionoutofrange.h"
30 #include <QDebug>
31 #include <cmath>
32 #include <solvers.h>
33 
34 namespace pappso
35 {
36 
37 TimsFrameBase::TimsFrameBase(std::size_t timsId, quint32 scanNum)
38 {
39  qDebug() << timsId;
40  m_timsId = timsId;
41 
42  m_scanNumber = scanNum;
43 }
44 
45 TimsFrameBase::TimsFrameBase(const TimsFrameBase &other)
46 {
47 }
48 
50 {
51 }
52 
53 void
54 TimsFrameBase::setAccumulationTime(double accumulation_time_ms)
55 {
56  m_accumulationTime = accumulation_time_ms;
57 }
58 
59 
60 void
61 TimsFrameBase::setMzCalibration(double temperature_correction,
62  double digitizerTimebase,
63  double digitizerDelay,
64  double C0,
65  double C1,
66  double C2,
67  double C3)
68 {
69 
70  // temperature compensation
71  C1 = C1 * temperature_correction;
72  C2 = C2 / temperature_correction;
73 
74  m_digitizerDelay = digitizerDelay;
75  m_digitizerTimebase = digitizerTimebase;
76 
77  m_mzCalibrationArr.push_back(C0);
78  m_mzCalibrationArr.push_back(std::sqrt(std::pow(10, 12) / C1));
79  m_mzCalibrationArr.push_back(C2);
80  m_mzCalibrationArr.push_back(C3);
81 }
82 
83 bool
84 TimsFrameBase::checkScanNum(std::size_t scanNum) const
85 {
86  if(scanNum < 0)
87  {
89  QObject::tr("Invalid scan number : scanNum%1 < 0").arg(scanNum));
90  }
91  if(scanNum >= m_scanNumber)
92  {
94  QObject::tr("Invalid scan number : scanNum%1 > m_scanNumber")
95  .arg(scanNum));
96  }
97 
98  return true;
99 }
100 
101 std::size_t
102 TimsFrameBase::getNbrPeaks(std::size_t scanNum) const
103 {
104  throw PappsoException(
105  QObject::tr(
106  "ERROR unable to get number of peaks in TimsFrameBase for scan number %1")
107  .arg(scanNum));
108 }
109 
111 TimsFrameBase::getMassSpectrumSPtr(std::size_t scanNum) const
112 {
113  throw PappsoException(
114  QObject::tr(
115  "ERROR unable to getMassSpectrumSPtr in TimsFrameBase for scan number %1")
116  .arg(scanNum));
117 }
119 TimsFrameBase::cumulateScanToTrace(std::size_t scanNumBegin,
120  std::size_t scanNumEnd) const
121 {
122  throw PappsoException(
123  QObject::tr("ERROR unable to cumulateScanToTrace in TimsFrameBase for scan "
124  "number begin %1 end %2")
125  .arg(scanNumBegin)
126  .arg(scanNumEnd));
127 }
128 double
129 TimsFrameBase::getTofFromIndex(double index) const
130 {
131  // mz calibration
132  return (index * m_digitizerTimebase) + m_digitizerDelay;
133 }
134 double
135 TimsFrameBase::getTofFromIndex(quint32 index) const
136 {
137  // mz calibration
138  return ((double)index * m_digitizerTimebase) + m_digitizerDelay;
139 }
140 double
141 TimsFrameBase::getMzFromTof(double tof) const
142 {
143  // http://www.alglib.net/equations/polynomial.php
144  // http://www.alglib.net/translator/man/manual.cpp.html#sub_polynomialsolve
145  // https://math.stackexchange.com/questions/1291208/number-of-roots-of-a-polynomial-of-non-integer-degree
146  // https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=2ahUKEwiWhLOFxqrkAhVLxYUKHVqqDFcQFjABegQIAxAB&url=https%3A%2F%2Fkluge.in-chemnitz.de%2Fopensource%2Fspline%2Fexample_alglib.cpp&usg=AOvVaw0guGejJGPmkOVg48_GJYR8
147  // https://stackoverflow.com/questions/26091323/how-to-plot-a-function-curve-in-r
148  /*
149  * beware to put the function on a single line in R:
150 > eq <- function(m){ 1 + (sqrt((10^12)/670) * sqrt(m)) + (207.775676931964 * m)
151 + (59.2526676368822 * (m^1.5)) }
152 > eq <- function(m){ 313.577620892277 + (sqrt((10^12)/157424.07710945) *
153 sqrt(m)) + (0.000338743021989553 * m)
154 + (0 * (m^1.5)) }
155 > plot(eq(1:1000), type='l')
156 
157 
158 
159 > eq2 <- function(m2){ 1 + sqrt((10^12)/670) * m2 + 207.775676931964 * (m2^2)
160 + 59.2526676368822 * (m2^3) }
161 > plot(eq2(1:sqrt(1000)), type='l')
162 */
163  // How to Factor a Trinomial with Fractions as Coefficients
164 
165  // formula
166  // a = c0 = 1
167  // b = sqrt((10^12)/c1), c1 = 670 * m^0.5 (1/2)
168  // c = c2, c2 = 207.775676931964 * m
169  // d = c3, c3 = 59.2526676368822 * m^1.5 (3/2)
170  // double mz = 0;
171  std::vector<double> X;
172  X.push_back((m_mzCalibrationArr[0] - (double)tof));
173  X.push_back(m_mzCalibrationArr[1]);
174  if(m_mzCalibrationArr[2] != 0)
175  {
176  X.push_back(m_mzCalibrationArr[2]);
177  }
178  if(m_mzCalibrationArr[3] != 0)
179  {
180  X.push_back(m_mzCalibrationArr[3]);
181  }
182  else
183  {
184  qDebug() << m_mzCalibrationArr[3];
185  }
186 
187  alglib::real_1d_array polynom_array;
188  try
189  {
190  polynom_array.setcontent(X.size(), &(X[0]));
191  }
192  catch(alglib::ap_error &error)
193  {
194  // PolynomialSolve: A[N]=0
196  QObject::tr("ERROR in alglib::polynom_array.setcontent :\n%1")
197  .arg(error.msg.c_str()));
198  }
199 
200 
201  /*
202  alglib::polynomialsolve(
203 real_1d_array a,
204 ae_int_t n,
205 complex_1d_array& x,
206 polynomialsolverreport& rep,
207 const xparams _params = alglib::xdefault);
208 */
209  alglib::complex_1d_array m;
210  alglib::polynomialsolverreport rep;
211  // qDebug();
212  try
213  {
214  alglib::polynomialsolve(polynom_array, X.size() - 1, m, rep);
215  }
216  catch(alglib::ap_error &error)
217  {
218  qDebug() << " X.size() - 1 = " << X.size() - 1;
219  qDebug() << m_mzCalibrationArr[0];
220  qDebug() << m_mzCalibrationArr[1];
221  qDebug() << m_mzCalibrationArr[2];
222  qDebug() << m_mzCalibrationArr[3];
223 
224  // PolynomialSolve: A[N]=0
226  QObject::tr("ERROR in alglib::polynomialsolve :\n%1")
227  .arg(error.msg.c_str()));
228  }
229 
230 
231  // qDebug();
232 
233  if(m.length() == 0)
234  {
236  QObject::tr("ERROR in TimsFrame::getMzFromTof m.size() == 0"));
237  }
238  // qDebug();
239  if(m[0].y != 0)
240  {
242  QObject::tr("ERROR in TimsFrame::getMzFromTof m[0].y!= 0"));
243  }
244 
245  return pow(m[0].x, 2);
246 }
247 
248 quint32
249 TimsFrameBase::getRawIndexFromMz(double mz) const
250 {
251  // formula
252  // a = c0 = 1
253  // b = sqrt((10^12)/c1), c1 = 670 * m^0.5 (1/2)
254  // c = c2, c2 = 207.775676931964 * m
255  // d = c3, c3 = 59.2526676368822 * m^1.5 (3/2)
256  qDebug() << "mz=" << mz;
257 
258  double tof = m_mzCalibrationArr[0];
259  qDebug() << "tof ( m_mzCalibrationArr[0])=" << tof;
260  // TODO cache value of std::sqrt((std::pow(10, 12) / m_mzCalibrationArr[1]))
261  tof += m_mzCalibrationArr[1] * std::sqrt(mz);
262  qDebug() << "tof=" << tof;
263  tof += m_mzCalibrationArr[2] * mz;
264  qDebug() << "tof=" << tof;
265  tof += m_mzCalibrationArr[3] * std::pow(mz, 1.5);
266  qDebug() << "tof=" << tof;
267  tof -= m_digitizerDelay;
268  qDebug() << "tof=" << tof;
269  tof = tof / m_digitizerTimebase;
270  qDebug() << "index=" << tof;
271  return (quint32)std::round(tof);
272 }
273 
274 void
275 TimsFrameBase::setTime(double time)
276 {
277  m_time = time;
278 }
279 
280 void
281 TimsFrameBase::setMsMsType(quint8 type)
282 {
283 
284  qDebug() << " m_msMsType=" << type;
285  m_msMsType = type;
286 }
287 
288 unsigned int
290 {
291  if(m_msMsType == 0)
292  return 1;
293  return 2;
294 }
295 
296 double
298 {
299  return m_time;
300 }
301 
302 std::size_t
303 TimsFrameBase::getId() const
304 {
305  return m_timsId;
306 }
307 void
308 TimsFrameBase::setTimsCalibration(int tims_model_type,
309  double C0,
310  double C1,
311  double C2,
312  double C3,
313  double C4,
314  double C5,
315  double C6,
316  double C7,
317  double C8,
318  double C9)
319 {
320  if(tims_model_type != 2)
321  {
322  throw pappso::PappsoException(QObject::tr(
323  "ERROR in TimsFrame::setTimsCalibration tims_model_type != 2"));
324  }
325  m_timsDvStart = C2; // C2 from TimsCalibration
326  m_timsTtrans = C4; // C4 from TimsCalibration
327  m_timsNdelay = C0; // C0 from TimsCalibration
328  m_timsVmin = C8; // C8 from TimsCalibration
329  m_timsVmax = C9; // C9 from TimsCalibration
330  m_timsC6 = C6;
331  m_timsC7 = C7;
332 
333 
334  m_timsSlope =
335  (C3 - m_timsDvStart) / C1; // //C3 from TimsCalibration // C2 from
336  // TimsCalibration // C1 from TimsCalibration
337 }
338 double
339 TimsFrameBase::getVoltageTransformation(std::size_t scanNum) const
340 {
341  double v = m_timsDvStart +
342  m_timsSlope * ((double)scanNum - m_timsTtrans - m_timsNdelay);
343 
344  if(v < m_timsVmin)
345  {
347  QObject::tr("ERROR in TimsFrame::getVoltageTransformation invalid tims "
348  "calibration, v < m_timsVmin"));
349  }
350 
351 
352  if(v > m_timsVmax)
353  {
355  QObject::tr("ERROR in TimsFrame::getVoltageTransformation invalid tims "
356  "calibration, v > m_timsVmax"));
357  }
358  return v;
359 }
360 double
361 TimsFrameBase::getDriftTime(std::size_t scanNum) const
362 {
363  return (m_accumulationTime / (double)m_scanNumber) * ((double)scanNum);
364 }
365 
366 double
367 TimsFrameBase::getOneOverK0Transformation(std::size_t scanNum) const
368 {
369  return 1 / (m_timsC6 + (m_timsC7 / getVoltageTransformation(scanNum)));
370 }
371 } // namespace pappso
pappso::TimsFrameBase::m_digitizerTimebase
double m_digitizerTimebase
Definition: timsframebase.h:171
pappso::TimsFrameBase::checkScanNum
bool checkScanNum(std::size_t scanNum) const
Definition: timsframebase.cpp:100
pappso::TimsFrameBase::m_timsId
std::size_t m_timsId
Tims frame database id (the SQL identifier of this frame)
Definition: timsframebase.h:165
pappso::TimsFrameBase::setTimsCalibration
void setTimsCalibration(int tims_model_type, double C0, double C1, double C2, double C3, double C4, double C5, double C6, double C7, double C8, double C9)
Definition: timsframebase.cpp:324
pappso::TimsFrameBase::getMsLevel
unsigned int getMsLevel() const
Definition: timsframebase.cpp:305
pappso::TimsFrameBase::cumulateScanToTrace
virtual Trace cumulateScanToTrace(std::size_t scanNumBegin, std::size_t scanNumEnd) const
Definition: timsframebase.cpp:135
pappso::TimsFrameBase::m_timsSlope
double m_timsSlope
Definition: timsframebase.h:185
pappso::TimsFrameBase::m_timsC7
double m_timsC7
Definition: timsframebase.h:193
pappso
Definition: aa.cpp:38
pappso::TimsFrameBase::setAccumulationTime
void setAccumulationTime(double accumulation_time_ms)
Definition: timsframebase.cpp:70
pappso::TimsFrameBase::setMzCalibration
void setMzCalibration(double temperature_correction, double digitizerTimebase, double digitizerDelay, double C0, double C1, double C2, double C3)
Definition: timsframebase.cpp:77
pappso::PeptideIonCter::y
pappso::TimsFrameBase::setTime
void setTime(double time)
Definition: timsframebase.cpp:291
pappso::TimsFrameBase::getNbrPeaks
virtual std::size_t getNbrPeaks(std::size_t scanNum) const
Definition: timsframebase.cpp:118
pappso::TimsFrameBase::getRawIndexFromMz
quint32 getRawIndexFromMz(double mz) const
get raw index of a given m/z
Definition: timsframebase.cpp:265
pappso::TimsFrameBase::getDriftTime
double getDriftTime(std::size_t scanNum) const
get drift time of a scan number in milliseconds
Definition: timsframebase.cpp:377
pappso::PeptideIonCter::x
pappso::TimsFrameBase::m_accumulationTime
double m_accumulationTime
accumulation time in milliseconds
Definition: timsframebase.h:169
pappso::TimsFrameBase::m_time
double m_time
retention time
Definition: timsframebase.h:182
pappso::ExceptionOutOfRange
Definition: exceptionoutofrange.h:50
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:125
pappso::TimsFrameBase::getVoltageTransformation
double getVoltageTransformation(std::size_t scanNum) const
Definition: timsframebase.cpp:355
pappso::TimsFrameBase::getId
std::size_t getId() const
Definition: timsframebase.cpp:319
pappso::TimsFrameBase::m_timsDvStart
double m_timsDvStart
Definition: timsframebase.h:184
pappso::TimsFrameBase::getMassSpectrumSPtr
virtual MassSpectrumSPtr getMassSpectrumSPtr(std::size_t scanNum) const
Definition: timsframebase.cpp:127
timsframebase.h
handle a single Bruker's TimsTof frame without binary data
pappso::TimsFrameBase::~TimsFrameBase
~TimsFrameBase()
Definition: timsframebase.cpp:65
pappso::TimsFrameBase::TimsFrameBase
TimsFrameBase(std::size_t timsId, quint32 scanNum)
constructor for binary independant tims frame
Definition: timsframebase.cpp:53
pappso::TimsFrameBase::setMsMsType
void setMsMsType(quint8 type)
Definition: timsframebase.cpp:297
pappso::TimsFrameBase::m_timsNdelay
double m_timsNdelay
Definition: timsframebase.h:189
pappso::TimsFrameBase::m_timsVmin
double m_timsVmin
Definition: timsframebase.h:190
pappso::TimsFrameBase::getOneOverK0Transformation
double getOneOverK0Transformation(std::size_t scanNum) const
get 1/K0 value of a given scan (mobility value)
Definition: timsframebase.cpp:383
pappso::PrecisionUnit::mz
pappso::TimsFrameBase::getMzFromTof
double getMzFromTof(double tof) const
get m/z from time of flight
Definition: timsframebase.cpp:157
pappso::TimsFrameBase::m_scanNumber
quint32 m_scanNumber
total number of scans contained in this frame
Definition: timsframebase.h:159
pappso::TimsFrameBase::m_timsC6
double m_timsC6
Definition: timsframebase.h:192
pappso::TimsFrameBase::getTofFromIndex
double getTofFromIndex(quint32 index) const
get time of flight from raw index
Definition: timsframebase.cpp:151
pappso::TimsFrameBase::m_digitizerDelay
double m_digitizerDelay
Definition: timsframebase.h:172
pappso::TimsFrameBase::m_mzCalibrationArr
std::vector< double > m_mzCalibrationArr
MZ calibration parameters.
Definition: timsframebase.h:176
pappso::TimsFrameBase::m_msMsType
quint8 m_msMsType
Definition: timsframebase.h:178
pappso::TimsFrameBase::m_timsVmax
double m_timsVmax
Definition: timsframebase.h:191
pappso::TimsFrameBase::m_timsTtrans
double m_timsTtrans
Definition: timsframebase.h:188
pappso::TimsFrameBase::getTime
double getTime() const
Definition: timsframebase.cpp:313
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:73
pappso::PappsoException
Definition: pappsoexception.h:60