36 #ifndef OPENMS_FILTERING_CALIBRATION_INTERNALCALIBRATION_H
37 #define OPENMS_FILTERING_CALIBRATION_INTERNALCALIBRATION_H
84 template <
typename InputPeakType>
87 #ifdef DEBUG_CALIBRATION
92 std::cout <<
"Input is empty." << std::endl;
98 std::cout <<
"Attention: this function is assuming peak data." << std::endl;
101 Size num_ref_peaks = ref_masses.size();
102 bool use_ppm = (param_.getValue(
"mz_tolerance_unit") ==
"ppm") ?
true :
false;
103 double mz_tol = param_.getValue(
"mz_tolerance");
104 startProgress(0, exp.
size(),
"calibrate spectra");
106 for (
Size spec = 0; spec < exp.
size(); ++spec)
109 if (exp[spec].getMSLevel() != 1)
115 std::vector<double> corr_masses, found_ref_masses;
117 for (
Size peak = 0; peak < exp[spec].
size(); ++peak)
119 for (
Size ref_peak = 0; ref_peak < num_ref_peaks; ++ref_peak)
121 if (!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
123 found_ref_masses.push_back(ref_masses[ref_peak]);
124 corr_masses.push_back(exp[spec][peak].getMZ());
128 else if (use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6 < mz_tol)
130 found_ref_masses.push_back(ref_masses[ref_peak]);
131 corr_masses.push_back(exp[spec][peak].getMZ());
139 std::cout <<
"spec: " << spec
140 <<
" less than 2 reference masses were detected within a reasonable error range\n";
141 std::cout <<
"This spectrum cannot be calibrated!\n";
145 makeLinearRegression_(corr_masses, found_ref_masses);
148 applyTransformation_<InputPeakType>(exp[spec]);
166 template <
typename InputPeakType>
168 const std::vector<double>& ref_masses,
169 const String& trafo_file_name =
"")
173 std::cout <<
"Input is empty." << std::endl;
179 std::cout <<
"Attention: this function is assuming peak data." << std::endl;
182 Size num_ref_peaks = ref_masses.size();
183 bool use_ppm = (param_.getValue(
"mz_tolerance_unit") ==
"ppm") ?
true :
false;
184 double mz_tol = param_.getValue(
"mz_tolerance");
185 startProgress(0, exp.
size(),
"finding calibration masses");
186 std::vector<double> corr_masses, found_ref_masses;
189 for (
Size spec = 0; spec < exp.
size(); ++spec)
192 if (exp[spec].getMSLevel() != 1)
196 for (
Size peak = 0; peak < exp[spec].
size(); ++peak)
198 for (
Size ref_peak = 0; ref_peak < num_ref_peaks; ++ref_peak)
200 if (!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
202 found_ref_masses.push_back(ref_masses[ref_peak]);
203 corr_masses.push_back(exp[spec][peak].getMZ());
207 else if (use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6 < mz_tol)
209 found_ref_masses.push_back(ref_masses[ref_peak]);
210 corr_masses.push_back(exp[spec][peak].getMZ());
222 std::cout <<
"Less than 2 reference masses were detected within a reasonable error range\n";
223 std::cout <<
"This spectrum cannot be calibrated!\n";
228 makeLinearRegression_(corr_masses, found_ref_masses);
230 applyTransformation_<InputPeakType>(exp);
232 if (trafo_file_name !=
"")
251 template <
typename InputPeakType>
253 const std::vector<PeptideIdentification>& ref_ids,
254 const String& trafo_file_name =
"")
256 bool use_ppm = param_.getValue(
"mz_tolerance_unit") ==
"ppm" ?
true :
false;
257 double mz_tolerance = param_.getValue(
"mz_tolerance");
260 std::cout <<
"Input is empty." << std::endl;
266 std::cout <<
"Attention: this function is assuming peak data." << std::endl;
269 checkReferenceIds_(ref_ids);
271 std::vector<double> theoretical_masses, observed_masses;
272 for (
Size p_id = 0; p_id < ref_ids.size(); ++p_id)
274 for (
Size p_h = 0; p_h < ref_ids[p_id].getHits().size(); ++p_h)
276 Int charge = ref_ids[p_id].getHits()[p_h].getCharge();
277 double theo_mass = ref_ids[p_id].getHits()[p_h].getSequence().getMonoWeight(
Residue::Full, charge) / (
double)charge;
280 while (rt_iter != exp.
begin() && rt_iter->getMSLevel() != 1)
287 double dist = ref_ids[p_id].getMZ() - mz_iter->getMZ();
289 if ((mz_iter + 1) != rt_iter->
end()
290 && fabs((mz_iter + 1)->getMZ() - ref_ids[p_id].getMZ()) < fabs(dist)
291 && mz_iter != rt_iter->
begin()
292 && fabs((mz_iter - 1)->getMZ() - ref_ids[p_id].getMZ()) < fabs((mz_iter + 1)->getMZ() - ref_ids[p_id].getMZ()))
295 fabs((mz_iter + 1)->getMZ() - ref_ids[p_id].getMZ()) / ref_ids[p_id].getMZ() * 1e06 < mz_tolerance) ||
296 (!use_ppm && fabs((mz_iter + 1)->getMZ() - ref_ids[p_id].getMZ()) < mz_tolerance))
299 observed_masses.push_back((mz_iter + 1)->getMZ());
300 theoretical_masses.push_back(theo_mass);
305 else if (mz_iter != rt_iter->
begin()
306 && fabs((mz_iter - 1)->getMZ() - ref_ids[p_id].getMZ()) < fabs(dist))
309 fabs((mz_iter - 1)->getMZ() - ref_ids[p_id].getMZ()) / ref_ids[p_id].getMZ() * 1e06 < mz_tolerance) ||
310 (!use_ppm && fabs((mz_iter - 1)->getMZ() - ref_ids[p_id].getMZ()) < mz_tolerance))
313 observed_masses.push_back((mz_iter - 1)->getMZ());
314 theoretical_masses.push_back(theo_mass);
322 fabs((mz_iter)->getMZ() - ref_ids[p_id].getMZ()) / ref_ids[p_id].getMZ() * 1e06 < mz_tolerance) ||
323 (!use_ppm && fabs((mz_iter)->getMZ() - ref_ids[p_id].getMZ()) < mz_tolerance))
326 observed_masses.push_back(mz_iter->getMZ());
327 theoretical_masses.push_back(theo_mass);
335 makeLinearRegression_(observed_masses, theoretical_masses);
337 applyTransformation_<InputPeakType>(exp);
339 if (trafo_file_name !=
"")
354 void calibrateMapGlobally(
FeatureMap& feature_map,
const String& trafo_file_name =
"");
368 void calibrateMapGlobally(
FeatureMap& feature_map, std::vector<PeptideIdentification>& ref_ids,
const String& trafo_file_name =
"");
374 void makeLinearRegression_(
const std::vector<double>& observed_masses,
const std::vector<double>& theoretical_masses);
377 void checkReferenceIds_(
const std::vector<PeptideIdentification>& pep_ids);
380 void checkReferenceIds_(
const FeatureMap& feature_map);
383 void applyTransformation_(
FeatureMap& feature_map);
386 template <
typename InputPeakType>
395 for (
unsigned int peak = 0; peak < spec.size(); ++peak)
397 #ifdef DEBUG_CALIBRATION
398 std::cout << spec[peak].getMZ() <<
"\t";
400 double mz = spec[peak].getMZ();
401 mz = trafo_.apply(mz);
402 spec[peak].setMZ(mz);
404 #ifdef DEBUG_CALIBRATION
405 std::cout << spec[peak].getMZ() << std::endl;
411 template <
typename InputPeakType>
414 startProgress(0, exp.
size(),
"applying calibration to data");
416 for (
Size spec = 0; spec < exp.
size(); ++spec)
418 applyTransformation_<InputPeakType>(exp[spec]);
431 #endif // OPENMS_FILTERING_CALIBRATION_INTERNALCALIBRATION_H
A more convenient string class.
Definition: String.h:57
UInt getMSLevel() const
Returns the MS level.
Definition: MSSpectrum.h:259
Size size() const
Definition: MSExperiment.h:119
A container for features.
Definition: FeatureMap.h:94
A simple calibration method using linear interpolation of given reference masses. ...
Definition: InternalCalibration.h:61
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:123
Iterator begin()
Definition: MSExperiment.h:149
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Iterator end()
Definition: MSExperiment.h:159
ConstIterator RTBegin(CoordinateType rt) const
Fast search for spectrum range begin.
Definition: MSExperiment.h:410
~InternalCalibration()
Destructor.
Definition: InternalCalibration.h:70
void calibrateMapSpectrumwise(MSExperiment< InputPeakType > &exp, std::vector< double > &ref_masses)
Calibrate a peak map using given reference masses with a separate calibration function for each spect...
Definition: InternalCalibration.h:85
void applyTransformation_(typename MSExperiment< InputPeakType >::SpectrumType &spec)
Definition: InternalCalibration.h:387
void calibrateMapGlobally(MSExperiment< InputPeakType > &exp, const std::vector< PeptideIdentification > &ref_ids, const String &trafo_file_name="")
Calibrate a peak map using given reference ids with one calibration function for the whole map...
Definition: InternalCalibration.h:252
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:71
void applyTransformation_(MSExperiment< InputPeakType > &exp)
Definition: InternalCalibration.h:412
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:105
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
bool empty() const
Definition: MSExperiment.h:129
Int writtenDigits< double >(const double &)
Number of digits commonly used for writing a double (a.k.a. precision).
Definition: Types.h:220
Definition: Residue.h:144
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
void calibrateMapGlobally(MSExperiment< InputPeakType > &exp, const std::vector< double > &ref_masses, const String &trafo_file_name="")
Calibrate a peak map using given reference masses with one calibration function for the whole map...
Definition: InternalCalibration.h:167
int Int
Signed integer type.
Definition: Types.h:103
TransformationDescription trafo_
here the transformation is stored
Definition: InternalCalibration.h:426