libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
mzcalibrationmodel1.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/vendors/tims/mzcalibration/mzcalibrationmodel1.cpp
3 * \date 11/11/2020
4 * \author Olivier Langella
5 * \brief implement Bruker's model type 1 formula to compute m/z
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2020 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#include "mzcalibrationmodel1.h"
29#include <cmath>
30#include <QDebug>
31#include <QObject>
33#include "cardano.h"
34
35
36using namespace pappso;
37
39 double T2_frame,
40 double digitizerTimebase,
41 double digitizerDelay,
42 double C0,
43 double C1,
44 double C2,
45 double C3,
46 double C4,
47 double T1_ref,
48 double T2_ref,
49 double dC1,
50 double dC2)
51 : MzCalibrationInterface(digitizerTimebase, digitizerDelay)
52{
53
54 double temperature_correction = dC1 * (T1_ref - T1_frame) + dC2 * (T2_ref - T2_frame);
55 temperature_correction = (double)1.0 + (temperature_correction / 1.0e6);
56
57 // temperature compensation
58 C1 = C1 * temperature_correction;
59 C2 = C2 / temperature_correction;
60
61
62 m_mzCalibrationArr.clear();
63
64 m_digitizerDelay = digitizerDelay;
65 m_digitizerTimebase = digitizerTimebase;
66
67 m_mzCalibrationArr.push_back(C0);
68 m_mzCalibrationArr.push_back(std::sqrt(std::pow(10, 12) / C1));
69 m_mzCalibrationArr.push_back(C2);
70 m_mzCalibrationArr.push_back(C3);
71 m_mzCalibrationArr.push_back(C4);
72}
73
77
78double
80{
81 double tof = ((double)tof_index * m_digitizerTimebase) + m_digitizerDelay;
82 // http://www.alglib.net/equations/polynomial.php
83 // http://www.alglib.net/translator/man/manual.cpp.html#sub_polynomialsolve
84 // https://math.stackexchange.com/questions/1291208/number-of-roots-of-a-polynomial-of-non-integer-degree
85 // 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
86 // https://stackoverflow.com/questions/26091323/how-to-plot-a-function-curve-in-r
87 /*
88 * beware to put the function on a single line in R:
89> eq <- function(m){ 1 + (sqrt((10^12)/670) * sqrt(m)) + (207.775676931964 * m)
90+ (59.2526676368822 * (m^1.5)) }
91> eq <- function(m){ 313.577620892277 + (sqrt((10^12)/157424.07710945) *
92sqrt(m)) + (0.000338743021989553 * m)
93+ (0 * (m^1.5)) }
94> plot(eq(1:1000), type='l')
95
96
97
98> eq2 <- function(m2){ 1 + sqrt((10^12)/670) * m2 + 207.775676931964 * (m2^2)
99+ 59.2526676368822 * (m2^3) }
100> plot(eq2(1:sqrt(1000)), type='l')
101*/
102 // How to Factor a Trinomial with Fractions as Coefficients
103
104 // formula
105 // a = c0 = 1
106 // b = sqrt((10^12)/c1), c1 = 670 * m^0.5 (1/2)
107 // c = c2, c2 = 207.775676931964 * m
108 // d = c3, c3 = 59.2526676368822 * m^1.5 (3/2)
109 // double mz = 0;
110
111
112 /* transformation formula given by Bruker 29/8/2019 :
113 * x = m + dm
114 *
115 * time = m_mzCalibrationArr[0]
116 * + sqrt ((10^12)/m_mzCalibrationArr[1]) * x^0.5
117 * + m_mzCalibrationArr[2] * x
118 * + m_mzCalibrationArr[3] * x^1.5
119 */
120
121
122 std::vector<double> X;
123 X.push_back((m_mzCalibrationArr[0] - (double)tof));
124 X.push_back(m_mzCalibrationArr[1]);
125 if(m_mzCalibrationArr[2] != 0)
126 {
127 X.push_back(m_mzCalibrationArr[2]);
128 }
129 if(m_mzCalibrationArr[3] != 0)
130 {
131 X.push_back(m_mzCalibrationArr[3]);
132 // qDebug() << "m_mzCalibrationArr[3]=" << m_mzCalibrationArr[3];
133 }
134 else
135 {
136 // qDebug() << "m_mzCalibrationArr[3]=" << m_mzCalibrationArr[3];
137 }
138
140
141 if(res.type == CardanoResultCase::notvalid)
142 {
143 throw pappso::PappsoException(QObject::tr("ERROR in %1 %2 %3"
144 "inHousePolynomialSolve :\nresult is not valid")
145 .arg(__FILE__)
146 .arg(__FUNCTION__)
147 .arg(__LINE__));
148 }
149 return (pow(res.x1, 2) - m_mzCalibrationArr[4]);
150}
151
152quint32
154{
155 // formula
156 // a = c0 = 1
157 // b = sqrt((10^12)/c1), c1 = 670 * m^0.5 (1/2)
158 // c = c2, c2 = 207.775676931964 * m
159 // d = c3, c3 = 59.2526676368822 * m^1.5 (3/2)
160 qDebug() << "mz=" << mz;
161
162 mz = mz + m_mzCalibrationArr[4]; // mz_corr
163
164 double tof = m_mzCalibrationArr[0];
165 qDebug() << "tof ( m_mzCalibrationArr[0])=" << tof;
166 // TODO cache value of std::sqrt((std::pow(10, 12) / m_mzCalibrationArr[1]))
167 tof += m_mzCalibrationArr[1] * std::sqrt(mz);
168 qDebug() << "tof=" << tof;
169 tof += m_mzCalibrationArr[2] * mz;
170 qDebug() << "tof=" << tof;
171 tof += m_mzCalibrationArr[3] * std::pow(mz, 1.5);
172 qDebug() << "tof=" << tof;
173 tof -= m_digitizerDelay;
174 qDebug() << "tof=" << tof;
175 tof = tof / m_digitizerTimebase;
176 qDebug() << "index=" << tof;
177 return (quint32)std::round(tof);
178}
179
181 double T2_frame,
182 double digitizerTimebase,
183 double digitizerDelay,
184 double C0,
185 double C1,
186 double C2,
187 double C3,
188 double C4,
189 double T1_ref,
190 double T2_ref,
191 double dC1,
192 double dC2)
193 : MzCalibrationModel1(T1_frame,
194 T2_frame,
195 digitizerTimebase,
196 digitizerDelay,
197 C0,
198 C1,
199 C2,
200 C3,
201 C4,
202 T1_ref,
203 T2_ref,
204 dC1,
205 dC2)
206{
207}
208
212
213
214double
216{
217 if(m_max > tof_index)
218 {
219 if(m_arrMasses[tof_index] == 0)
220 {
222 }
223 return m_arrMasses[tof_index];
224 }
225 else
226 {
228 }
229}
InHousePolynomialSolverResult inHousePolynomialSolve(const std::vector< double > &polynome)
Definition cardano.cpp:115
MzCalibrationInterface(double digitizerTimebase, double digitizerDelay)
std::vector< double > m_mzCalibrationArr
MZ calibration parameters.
MzCalibrationModel1Cached(double T1_frame, double T2_frame, double digitizerTimebase, double digitizerDelay, double C0, double C1, double C2, double C3, double C4, double T1_ref, double T2_ref, double dC1, double dC2)
virtual double getMzFromTofIndex(quint32 tof_index) override
get m/z from time of flight raw index
virtual double getMzFromTofIndex(quint32 tof_index) override
get m/z from time of flight raw index
MzCalibrationModel1(double T1_frame, double T2_frame, double digitizerTimebase, double digitizerDelay, double C0, double C1, double C2, double C3, double C4, double T1_ref, double T2_ref, double dC1, double dC2)
virtual quint32 getTofIndexFromMz(double mz) override
get raw TOF index of a given m/z
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39