105
112
@param calibrated_exp the calibrated peak map
106
113
@param ref_ids the reference peptide identifications
107
114
@param trafo_file_name file where the transformation function of the calibration is stored
109
template<typename InputPeakType>
110
void calibrateMapGlobally(const MSExperiment<InputPeakType>& exp, MSExperiment<InputPeakType>& calibrated_exp,std::vector<PeptideIdentification>& ref_ids,String trafo_file_name = "");
116
template <typename InputPeakType>
117
void calibrateMapGlobally(const MSExperiment<InputPeakType> & exp, MSExperiment<InputPeakType> & calibrated_exp, std::vector<PeptideIdentification> & ref_ids, String trafo_file_name = "");
113
120
@brief Calibrate an annotated feature map with one calibration function for the whole map.
115
Calibrate an annotated (!) feature map using the features' identifications. The calibration function is calculated for the whole map. The m/z-values of the reference identifications are calculated through the given sequence and charge of the peptide.
122
Calibrate an annotated (!) feature map using the features' identifications. The calibration function is calculated for the whole map. The m/z-values of the reference identifications are calculated through the given sequence and charge of the peptide.
116
123
@param feature_map the uncalibrated feature map (annotated with peptide ids)
117
124
@param calibrated_feature_map the calibrated feature map
118
125
@param trafo_file_name file where the transformation function of the calibration is stored
120
void calibrateMapGlobally(const FeatureMap<>& feature_map, FeatureMap<>& calibrated_feature_map,String trafo_file_name = "");
127
void calibrateMapGlobally(const FeatureMap<> & feature_map, FeatureMap<> & calibrated_feature_map, String trafo_file_name = "");
123
130
@brief Calibrate a feature map using given reference ids with one calibration function for the whole map.
125
Calibrate a feature map using given peptide identifications. The calibration function is calculated for the whole map. Even if the features are already annotated with peptide ids these annotations are ignored for the calibration, only the reference ids are used. The m/z-values of the reference identifications are calculated through the given sequence and charge of the peptide. The reference ids are mapped onto the FeatureMap using IDMapper with the mz_tolerance and rt_tolerance parameters.
132
Calibrate a feature map using given peptide identifications. The calibration function is calculated for the whole map. Even if the features are already annotated with peptide ids these annotations are ignored for the calibration, only the reference ids are used. The m/z-values of the reference identifications are calculated through the given sequence and charge of the peptide. The reference ids are mapped onto the FeatureMap using IDMapper with the mz_tolerance and rt_tolerance parameters.
126
133
@param feature_map the uncalibrated feature map
127
134
@param calibrated_feature_map the calibrated feature map
128
135
@param ref_ids the reference peptide identifications
129
136
@param trafo_file_name file where the transformation function of the calibration is stored
131
void calibrateMapGlobally(const FeatureMap<>& feature_map, FeatureMap<>& calibrated_feature_map,std::vector<PeptideIdentification>& ref_ids,String trafo_file_name = "");
135
template<typename InputPeakType>
136
void calibrateMapList(std::vector<MSExperiment<InputPeakType> >& exp_list,std::vector<MSExperiment<InputPeakType> >& calibrated_exp_list, std::vector<DoubleReal>& ref_masses, std::vector<DoubleReal>& detected_background_masses);
141
/// the actual calibration function
142
void makeLinearRegression_(std::vector<DoubleReal>& observed_masses, std::vector<DoubleReal>& theoretical_masses);
144
/// check if reference ids contain RT and MZ information as meta values
145
void checkReferenceIds_(std::vector<PeptideIdentification>& pep_ids);
147
/// check if reference ids contain RT and MZ information as meta values
148
void checkReferenceIds_(const FeatureMap<>& feature_map);
150
/// apply transformation to all features (including subordinates and convex hulls)
151
void applyTransformation_(const FeatureMap<>& feature_map,FeatureMap<>& calibrated_feature_map);
153
/// here the transformation is stored
154
TransformationDescription trafo_;
155
};// class InternalCalibration
158
template<typename InputPeakType>
159
void InternalCalibration::calibrateMapSpectrumwise(const MSExperiment<InputPeakType>& exp, MSExperiment<InputPeakType>& calibrated_exp,std::vector<DoubleReal>& ref_masses)
161
#ifdef DEBUG_CALIBRATION
162
std::cout.precision(writtenDigits<DoubleReal>());
166
std::cout << "Input is empty."<<std::endl;
170
if(exp[0].getType() != SpectrumSettings::PEAKS)
172
std::cout << "Attention: this function is assuming peak data."<<std::endl;
174
calibrated_exp = exp;
176
Size num_ref_peaks = ref_masses.size();
177
bool use_ppm = (param_.getValue("mz_tolerance_unit") == "ppm" ) ? true : false;
178
DoubleReal mz_tol = param_.getValue("mz_tolerance");
179
startProgress(0,exp.size(),"calibrate spectra");
181
for(Size spec=0;spec < exp.size(); ++spec)
183
// calibrate only MS1 spectra
184
if(exp[spec].getMSLevel() != 1)
190
std::vector<DoubleReal> corr_masses,rel_errors,found_ref_masses;
192
for(Size peak=0;peak < exp[spec].size(); ++peak)
194
for(Size ref_peak=0; ref_peak < num_ref_peaks;++ref_peak)
196
if(!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
198
found_ref_masses.push_back(ref_masses[ref_peak]);
199
corr_masses.push_back(exp[spec][peak].getMZ());
203
else if(use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6< mz_tol)
205
found_ref_masses.push_back(ref_masses[ref_peak]);
206
corr_masses.push_back(exp[spec][peak].getMZ());
214
std::cout << "spec: "<<spec
215
<< " less than 2 reference masses were detected within a reasonable error range\n";
216
std::cout << "This spectrum cannot be calibrated!\n";
220
// determine rel error in ppm for the two reference masses
221
for(Size ref_peak=0; ref_peak < found_ref_masses.size();++ref_peak)
223
rel_errors.push_back((found_ref_masses[ref_peak]-corr_masses[ref_peak])/corr_masses[ref_peak] * 1e6);
226
makeLinearRegression_(corr_masses,found_ref_masses);
228
// now calibrate the whole spectrum
229
for(unsigned int peak=0;peak < calibrated_exp[spec].size(); ++peak)
231
#ifdef DEBUG_CALIBRATION
232
std::cout << calibrated_exp[spec][peak].getMZ()<< "\t";
234
DoubleReal mz = calibrated_exp[spec][peak].getMZ();
235
mz = trafo_.apply(mz);
236
calibrated_exp[spec][peak].setMZ(mz);
237
#ifdef DEBUG_CALIBRATION
238
std::cout << calibrated_exp[spec][peak].getMZ()<< std::endl;
243
}// for(Size spec=0;spec < exp.size(); ++spec)
248
template<typename InputPeakType>
249
void InternalCalibration::calibrateMapGlobally(const MSExperiment<InputPeakType>& exp,
250
MSExperiment<InputPeakType>& calibrated_exp,
251
std::vector<PeptideIdentification>& ref_ids,String trafo_file_name)
253
bool use_ppm = param_.getValue("mz_tolerance_unit") == "ppm" ? true : false;
254
DoubleReal mz_tolerance = param_.getValue("mz_tolerance");
257
std::cout << "Input is empty."<<std::endl;
261
if(exp[0].getType() != SpectrumSettings::PEAKS)
263
std::cout << "Attention: this function is assuming peak data."<<std::endl;
265
// check if the ids contain meta information about the peak positions
266
checkReferenceIds_(ref_ids);
268
std::vector<DoubleReal> theoretical_masses,observed_masses;
269
for(Size p_id = 0; p_id < ref_ids.size();++p_id)
271
for(Size p_h = 0; p_h < ref_ids[p_id].getHits().size();++p_h)
273
Int charge = ref_ids[p_id].getHits()[p_h].getCharge();
274
DoubleReal theo_mass = ref_ids[p_id].getHits()[p_h].getSequence().getMonoWeight(Residue::Full,charge)/(DoubleReal)charge;
275
// first find corresponding ms1-spectrum
276
typename MSExperiment<InputPeakType>::ConstIterator rt_iter = exp.RTBegin(ref_ids[p_id].getMetaValue("RT"));
277
while(rt_iter != exp.begin() && rt_iter->getMSLevel() != 1)
281
// now find closest peak
282
typename MSSpectrum<InputPeakType>::ConstIterator mz_iter = rt_iter->MZBegin(ref_ids[p_id].getMetaValue("MZ"));
283
// std::cout << mz_iter->getMZ() <<" "<<(DoubleReal)ref_ids[p_id].getMetaValue("MZ")<<"\t";
284
DoubleReal dist = (DoubleReal)ref_ids[p_id].getMetaValue("MZ") - mz_iter->getMZ();
285
// std::cout << dist << "\t";
286
if((mz_iter +1) != rt_iter->end()
287
&& fabs((mz_iter +1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < fabs(dist)
288
&& mz_iter != rt_iter->begin()
289
&& fabs((mz_iter -1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < fabs((mz_iter +1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ"))) // if mz_iter +1 has smaller dist than mz_iter and mz_iter-1
292
fabs((mz_iter +1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) / (DoubleReal)ref_ids[p_id].getMetaValue("MZ") *1e06< mz_tolerance) ||
293
(!use_ppm && fabs((mz_iter+1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < mz_tolerance))
295
// std::cout <<(mz_iter +1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")<<"\t";
296
observed_masses.push_back((mz_iter +1)->getMZ());
297
theoretical_masses.push_back(theo_mass);
298
// std::cout << (mz_iter +1)->getMZ() << " ~ "<<theo_mass << " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
299
// << "\tplus 1"<< std::endl;
302
else if(mz_iter != rt_iter->begin()
303
&& fabs((mz_iter -1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < fabs(dist)) // if mz_iter-1 has smaller dist than mz_iter
306
fabs((mz_iter -1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) / (DoubleReal)ref_ids[p_id].getMetaValue("MZ") *1e06< mz_tolerance) ||
307
(!use_ppm && fabs((mz_iter-1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < mz_tolerance))
309
// std::cout <<(mz_iter -1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")<<"\t";
310
observed_masses.push_back((mz_iter -1)->getMZ());
311
theoretical_masses.push_back(theo_mass);
312
// std::cout << (mz_iter -1)->getMZ() << " ~ "<<theo_mass << " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
313
// << "\tminus 1"<< std::endl;
319
fabs((mz_iter)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) / (DoubleReal)ref_ids[p_id].getMetaValue("MZ") *1e06< mz_tolerance) ||
320
(!use_ppm && fabs((mz_iter)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < mz_tolerance))
323
observed_masses.push_back(mz_iter->getMZ());
324
theoretical_masses.push_back(theo_mass);
325
// std::cout <<"\t"<< mz_iter->getMZ() << " ~ "<<theo_mass<< " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
326
// << "\tat mz_iter"<< std::endl;
332
makeLinearRegression_(observed_masses,theoretical_masses);
333
static_cast<ExperimentalSettings&>(calibrated_exp) = exp;
334
calibrated_exp.resize(exp.size());
337
for(Size spec=0;spec < exp.size(); ++spec)
339
// calibrate only MS1 spectra
340
if(exp[spec].getMSLevel() != 1)
342
calibrated_exp[spec] = exp[spec];
345
// copy the spectrum meta data
346
calibrated_exp[spec] = exp[spec];
348
for(unsigned int peak=0;peak < exp[spec].size(); ++peak)
350
#ifdef DEBUG_CALIBRATION
351
std::cout << exp[spec][peak].getMZ()<< "\t";
353
DoubleReal mz = exp[spec][peak].getMZ();
354
mz = trafo_.apply(mz);
355
calibrated_exp[spec][peak].setMZ(mz);
356
#ifdef DEBUG_CALIBRATION
357
std::cout << calibrated_exp[spec][peak].getMZ()<< std::endl;
361
}// for(Size spec=0;spec < exp.size(); ++spec)
362
if(trafo_file_name != "")
364
TransformationXMLFile().store(trafo_file_name,trafo_);
369
template<typename InputPeakType>
370
void InternalCalibration::calibrateMapGlobally(const MSExperiment<InputPeakType>& exp, MSExperiment<InputPeakType>& calibrated_exp,std::vector<DoubleReal>& ref_masses,String trafo_file_name)
374
std::cout << "Input is empty."<<std::endl;
378
if(exp[0].getType() != SpectrumSettings::PEAKS)
380
std::cout << "Attention: this function is assuming peak data."<<std::endl;
384
Size num_ref_peaks = ref_masses.size();
385
bool use_ppm = (param_.getValue("mz_tolerance_unit") == "ppm" ) ? true : false;
386
DoubleReal mz_tol = param_.getValue("mz_tolerance");
387
startProgress(0,exp.size(),"calibrate spectra");
388
std::vector<DoubleReal> corr_masses,rel_errors,found_ref_masses;
391
for(Size spec=0;spec < exp.size(); ++spec)
393
// calibrate only MS1 spectra
394
if(exp[spec].getMSLevel() != 1) continue;
395
for(Size peak=0;peak < exp[spec].size(); ++peak)
397
for(Size ref_peak=0; ref_peak < num_ref_peaks;++ref_peak)
399
if(!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
401
found_ref_masses.push_back(ref_masses[ref_peak]);
402
corr_masses.push_back(exp[spec][peak].getMZ());
406
else if(use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6< mz_tol)
408
found_ref_masses.push_back(ref_masses[ref_peak]);
409
corr_masses.push_back(exp[spec][peak].getMZ());
418
std::cout << "Less than 2 reference masses were detected within a reasonable error range\n";
419
std::cout << "This spectrum cannot be calibrated!\n";
423
// calculate the (linear) calibration function
424
makeLinearRegression_(corr_masses,found_ref_masses);
425
static_cast<ExperimentalSettings&>(calibrated_exp) = exp;
426
calibrated_exp.resize(exp.size());
428
// apply the calibration function to each peak
429
for(Size spec=0;spec < exp.size(); ++spec)
431
// calibrate only MS1 spectra
432
if(exp[spec].getMSLevel() != 1)
434
calibrated_exp[spec] = exp[spec];
438
// copy the spectrum data
439
calibrated_exp[spec] = exp[spec];
441
for(unsigned int peak=0;peak < exp[spec].size(); ++peak)
443
#ifdef DEBUG_CALIBRATION
444
std::cout << exp[spec][peak].getMZ()<< "\t";
446
DoubleReal mz = exp[spec][peak].getMZ();
447
mz = trafo_.apply(mz);
448
calibrated_exp[spec][peak].setMZ(mz);
450
#ifdef DEBUG_CALIBRATION
451
std::cout << calibrated_exp[spec][peak].getMZ() << std::endl;
456
}// for(Size spec=0;spec < exp.size(); ++spec)
458
if(trafo_file_name != "")
460
TransformationXMLFile().store(trafo_file_name,trafo_);
138
void calibrateMapGlobally(const FeatureMap<> & feature_map, FeatureMap<> & calibrated_feature_map, std::vector<PeptideIdentification> & ref_ids, String trafo_file_name = "");
142
template <typename InputPeakType>
143
void calibrateMapList(std::vector<MSExperiment<InputPeakType> > & exp_list, std::vector<MSExperiment<InputPeakType> > & calibrated_exp_list, std::vector<DoubleReal> & ref_masses, std::vector<DoubleReal> & detected_background_masses);
148
/// the actual calibration function
149
void makeLinearRegression_(std::vector<DoubleReal> & observed_masses, std::vector<DoubleReal> & theoretical_masses);
151
/// check if reference ids contain RT and MZ information as meta values
152
void checkReferenceIds_(std::vector<PeptideIdentification> & pep_ids);
154
/// check if reference ids contain RT and MZ information as meta values
155
void checkReferenceIds_(const FeatureMap<> & feature_map);
157
/// apply transformation to all features (including subordinates and convex hulls)
158
void applyTransformation_(const FeatureMap<> & feature_map, FeatureMap<> & calibrated_feature_map);
160
/// here the transformation is stored
161
TransformationDescription trafo_;
162
}; // class InternalCalibration
165
template <typename InputPeakType>
166
void InternalCalibration::calibrateMapSpectrumwise(const MSExperiment<InputPeakType> & exp, MSExperiment<InputPeakType> & calibrated_exp, std::vector<DoubleReal> & ref_masses)
168
#ifdef DEBUG_CALIBRATION
169
std::cout.precision(writtenDigits<DoubleReal>());
173
std::cout << "Input is empty." << std::endl;
177
if (exp[0].getType() != SpectrumSettings::PEAKS)
179
std::cout << "Attention: this function is assuming peak data." << std::endl;
181
calibrated_exp = exp;
183
Size num_ref_peaks = ref_masses.size();
184
bool use_ppm = (param_.getValue("mz_tolerance_unit") == "ppm") ? true : false;
185
DoubleReal mz_tol = param_.getValue("mz_tolerance");
186
startProgress(0, exp.size(), "calibrate spectra");
188
for (Size spec = 0; spec < exp.size(); ++spec)
190
// calibrate only MS1 spectra
191
if (exp[spec].getMSLevel() != 1)
197
std::vector<DoubleReal> corr_masses, rel_errors, found_ref_masses;
199
for (Size peak = 0; peak < exp[spec].size(); ++peak)
201
for (Size ref_peak = 0; ref_peak < num_ref_peaks; ++ref_peak)
203
if (!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
205
found_ref_masses.push_back(ref_masses[ref_peak]);
206
corr_masses.push_back(exp[spec][peak].getMZ());
210
else if (use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6 < mz_tol)
212
found_ref_masses.push_back(ref_masses[ref_peak]);
213
corr_masses.push_back(exp[spec][peak].getMZ());
221
std::cout << "spec: " << spec
222
<< " less than 2 reference masses were detected within a reasonable error range\n";
223
std::cout << "This spectrum cannot be calibrated!\n";
227
// determine rel error in ppm for the two reference masses
228
for (Size ref_peak = 0; ref_peak < found_ref_masses.size(); ++ref_peak)
230
rel_errors.push_back((found_ref_masses[ref_peak] - corr_masses[ref_peak]) / corr_masses[ref_peak] * 1e6);
233
makeLinearRegression_(corr_masses, found_ref_masses);
235
// now calibrate the whole spectrum
236
for (unsigned int peak = 0; peak < calibrated_exp[spec].size(); ++peak)
238
#ifdef DEBUG_CALIBRATION
239
std::cout << calibrated_exp[spec][peak].getMZ() << "\t";
241
DoubleReal mz = calibrated_exp[spec][peak].getMZ();
242
mz = trafo_.apply(mz);
243
calibrated_exp[spec][peak].setMZ(mz);
244
#ifdef DEBUG_CALIBRATION
245
std::cout << calibrated_exp[spec][peak].getMZ() << std::endl;
250
} // for(Size spec=0;spec < exp.size(); ++spec)
254
template <typename InputPeakType>
255
void InternalCalibration::calibrateMapGlobally(const MSExperiment<InputPeakType> & exp,
256
MSExperiment<InputPeakType> & calibrated_exp,
257
std::vector<PeptideIdentification> & ref_ids, String trafo_file_name)
259
bool use_ppm = param_.getValue("mz_tolerance_unit") == "ppm" ? true : false;
260
DoubleReal mz_tolerance = param_.getValue("mz_tolerance");
263
std::cout << "Input is empty." << std::endl;
267
if (exp[0].getType() != SpectrumSettings::PEAKS)
269
std::cout << "Attention: this function is assuming peak data." << std::endl;
271
// check if the ids contain meta information about the peak positions
272
checkReferenceIds_(ref_ids);
274
std::vector<DoubleReal> theoretical_masses, observed_masses;
275
for (Size p_id = 0; p_id < ref_ids.size(); ++p_id)
277
for (Size p_h = 0; p_h < ref_ids[p_id].getHits().size(); ++p_h)
279
Int charge = ref_ids[p_id].getHits()[p_h].getCharge();
280
DoubleReal theo_mass = ref_ids[p_id].getHits()[p_h].getSequence().getMonoWeight(Residue::Full, charge) / (DoubleReal)charge;
281
// first find corresponding ms1-spectrum
282
typename MSExperiment<InputPeakType>::ConstIterator rt_iter = exp.RTBegin(ref_ids[p_id].getMetaValue("RT"));
283
while (rt_iter != exp.begin() && rt_iter->getMSLevel() != 1)
287
// now find closest peak
288
typename MSSpectrum<InputPeakType>::ConstIterator mz_iter = rt_iter->MZBegin(ref_ids[p_id].getMetaValue("MZ"));
289
//std::cout << mz_iter->getMZ() <<" "<<(DoubleReal)ref_ids[p_id].getMetaValue("MZ")<<"\t";
290
DoubleReal dist = (DoubleReal)ref_ids[p_id].getMetaValue("MZ") - mz_iter->getMZ();
291
//std::cout << dist << "\t";
292
if ((mz_iter + 1) != rt_iter->end()
293
&& fabs((mz_iter + 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < fabs(dist)
294
&& mz_iter != rt_iter->begin()
295
&& fabs((mz_iter - 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < fabs((mz_iter + 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ"))) // if mz_iter +1 has smaller dist than mz_iter and mz_iter-1
298
fabs((mz_iter + 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) / (DoubleReal)ref_ids[p_id].getMetaValue("MZ") * 1e06 < mz_tolerance) ||
299
(!use_ppm && fabs((mz_iter + 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < mz_tolerance))
301
//std::cout <<(mz_iter +1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")<<"\t";
302
observed_masses.push_back((mz_iter + 1)->getMZ());
303
theoretical_masses.push_back(theo_mass);
304
//std::cout << (mz_iter +1)->getMZ() << " ~ "<<theo_mass << " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
305
//<< "\tplus 1"<< std::endl;
308
else if (mz_iter != rt_iter->begin()
309
&& fabs((mz_iter - 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < fabs(dist)) // if mz_iter-1 has smaller dist than mz_iter
312
fabs((mz_iter - 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) / (DoubleReal)ref_ids[p_id].getMetaValue("MZ") * 1e06 < mz_tolerance) ||
313
(!use_ppm && fabs((mz_iter - 1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < mz_tolerance))
315
//std::cout <<(mz_iter -1)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")<<"\t";
316
observed_masses.push_back((mz_iter - 1)->getMZ());
317
theoretical_masses.push_back(theo_mass);
318
//std::cout << (mz_iter -1)->getMZ() << " ~ "<<theo_mass << " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
319
//<< "\tminus 1"<< std::endl;
325
fabs((mz_iter)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) / (DoubleReal)ref_ids[p_id].getMetaValue("MZ") * 1e06 < mz_tolerance) ||
326
(!use_ppm && fabs((mz_iter)->getMZ() - (DoubleReal)ref_ids[p_id].getMetaValue("MZ")) < mz_tolerance))
329
observed_masses.push_back(mz_iter->getMZ());
330
theoretical_masses.push_back(theo_mass);
331
// std::cout <<"\t"<< mz_iter->getMZ() << " ~ "<<theo_mass<< " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
332
// << "\tat mz_iter"<< std::endl;
338
makeLinearRegression_(observed_masses, theoretical_masses);
339
static_cast<ExperimentalSettings &>(calibrated_exp) = exp;
340
calibrated_exp.resize(exp.size());
343
for (Size spec = 0; spec < exp.size(); ++spec)
345
// calibrate only MS1 spectra
346
if (exp[spec].getMSLevel() != 1)
348
calibrated_exp[spec] = exp[spec];
351
// copy the spectrum meta data
352
calibrated_exp[spec] = exp[spec];
354
for (unsigned int peak = 0; peak < exp[spec].size(); ++peak)
356
#ifdef DEBUG_CALIBRATION
357
std::cout << exp[spec][peak].getMZ() << "\t";
359
DoubleReal mz = exp[spec][peak].getMZ();
360
mz = trafo_.apply(mz);
361
calibrated_exp[spec][peak].setMZ(mz);
362
#ifdef DEBUG_CALIBRATION
363
std::cout << calibrated_exp[spec][peak].getMZ() << std::endl;
367
} // for(Size spec=0;spec < exp.size(); ++spec)
368
if (trafo_file_name != "")
370
TransformationXMLFile().store(trafo_file_name, trafo_);
374
template <typename InputPeakType>
375
void InternalCalibration::calibrateMapGlobally(const MSExperiment<InputPeakType> & exp, MSExperiment<InputPeakType> & calibrated_exp, std::vector<DoubleReal> & ref_masses, String trafo_file_name)
379
std::cout << "Input is empty." << std::endl;
383
if (exp[0].getType() != SpectrumSettings::PEAKS)
385
std::cout << "Attention: this function is assuming peak data." << std::endl;
389
Size num_ref_peaks = ref_masses.size();
390
bool use_ppm = (param_.getValue("mz_tolerance_unit") == "ppm") ? true : false;
391
DoubleReal mz_tol = param_.getValue("mz_tolerance");
392
startProgress(0, exp.size(), "calibrate spectra");
393
std::vector<DoubleReal> corr_masses, rel_errors, found_ref_masses;
396
for (Size spec = 0; spec < exp.size(); ++spec)
398
// calibrate only MS1 spectra
399
if (exp[spec].getMSLevel() != 1)
401
for (Size peak = 0; peak < exp[spec].size(); ++peak)
403
for (Size ref_peak = 0; ref_peak < num_ref_peaks; ++ref_peak)
405
if (!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
407
found_ref_masses.push_back(ref_masses[ref_peak]);
408
corr_masses.push_back(exp[spec][peak].getMZ());
412
else if (use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6 < mz_tol)
414
found_ref_masses.push_back(ref_masses[ref_peak]);
415
corr_masses.push_back(exp[spec][peak].getMZ());
424
std::cout << "Less than 2 reference masses were detected within a reasonable error range\n";
425
std::cout << "This spectrum cannot be calibrated!\n";
429
// calculate the (linear) calibration function
430
makeLinearRegression_(corr_masses, found_ref_masses);
431
static_cast<ExperimentalSettings &>(calibrated_exp) = exp;
432
calibrated_exp.resize(exp.size());
434
// apply the calibration function to each peak
435
for (Size spec = 0; spec < exp.size(); ++spec)
437
// calibrate only MS1 spectra
438
if (exp[spec].getMSLevel() != 1)
440
calibrated_exp[spec] = exp[spec];
444
// copy the spectrum data
445
calibrated_exp[spec] = exp[spec];
447
for (unsigned int peak = 0; peak < exp[spec].size(); ++peak)
449
#ifdef DEBUG_CALIBRATION
450
std::cout << exp[spec][peak].getMZ() << "\t";
452
DoubleReal mz = exp[spec][peak].getMZ();
453
mz = trafo_.apply(mz);
454
calibrated_exp[spec][peak].setMZ(mz);
456
#ifdef DEBUG_CALIBRATION
457
std::cout << calibrated_exp[spec][peak].getMZ() << std::endl;
462
} // for(Size spec=0;spec < exp.size(); ++spec)
464
if (trafo_file_name != "")
466
TransformationXMLFile().store(trafo_file_name, trafo_);
465
470
} // namespace OpenMS
467
472
#endif // OPENMS_FILTERING_CALIBRATION_INTERNALCALIBRATION_H