Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
InternalCalibration.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2016.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Alexandra Zerck, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 
36 #ifndef OPENMS_FILTERING_CALIBRATION_INTERNALCALIBRATION_H
37 #define OPENMS_FILTERING_CALIBRATION_INTERNALCALIBRATION_H
44 // TODO remove dependency from file reader here!
46 
47 namespace OpenMS
48 {
49 
61  class OPENMS_DLLAPI InternalCalibration :
62  public DefaultParamHandler,
63  public ProgressLogger
64  {
65 public:
68 
71 
84  template <typename InputPeakType>
85  void calibrateMapSpectrumwise(MSExperiment<InputPeakType>& exp, std::vector<double>& ref_masses)
86  {
87 #ifdef DEBUG_CALIBRATION
88  std::cout.precision(writtenDigits<double>(0.0));
89 #endif
90  if (exp.empty())
91  {
92  std::cout << "Input is empty." << std::endl;
93  return;
94  }
95 
96  if (exp[0].getType() != SpectrumSettings::PEAKS)
97  {
98  std::cout << "Attention: this function is assuming peak data." << std::endl;
99  }
100 
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");
105  // for each spectrum
106  for (Size spec = 0; spec < exp.size(); ++spec)
107  {
108  // calibrate only MS1 spectra
109  if (exp[spec].getMSLevel() != 1)
110  {
111  continue;
112  }
113 
114 
115  std::vector<double> corr_masses, found_ref_masses;
116  UInt corr_peaks = 0;
117  for (Size peak = 0; peak < exp[spec].size(); ++peak)
118  {
119  for (Size ref_peak = 0; ref_peak < num_ref_peaks; ++ref_peak)
120  {
121  if (!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
122  {
123  found_ref_masses.push_back(ref_masses[ref_peak]);
124  corr_masses.push_back(exp[spec][peak].getMZ());
125  ++corr_peaks;
126  break;
127  }
128  else if (use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6 < mz_tol)
129  {
130  found_ref_masses.push_back(ref_masses[ref_peak]);
131  corr_masses.push_back(exp[spec][peak].getMZ());
132  ++corr_peaks;
133  break;
134  }
135  }
136  }
137  if (corr_peaks < 2)
138  {
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";
142  continue;
143  }
144 
145  makeLinearRegression_(corr_masses, found_ref_masses);
146 
147  // now calibrate the whole spectrum
148  applyTransformation_<InputPeakType>(exp[spec]);
149  setProgress(spec);
150  } // for(Size spec=0;spec < exp.size(); ++spec)
151  endProgress();
152  }
153 
166  template <typename InputPeakType>
168  const std::vector<double>& ref_masses,
169  const String& trafo_file_name = "")
170  {
171  if (exp.empty())
172  {
173  std::cout << "Input is empty." << std::endl;
174  return;
175  }
176 
177  if (exp[0].getType() != SpectrumSettings::PEAKS)
178  {
179  std::cout << "Attention: this function is assuming peak data." << std::endl;
180  }
181 
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;
187  UInt corr_peaks = 0;
188  // for each spectrum
189  for (Size spec = 0; spec < exp.size(); ++spec)
190  {
191  // obtain calibration points only from MS1 spectra
192  if (exp[spec].getMSLevel() != 1)
193  {
194  continue;
195  }
196  for (Size peak = 0; peak < exp[spec].size(); ++peak)
197  {
198  for (Size ref_peak = 0; ref_peak < num_ref_peaks; ++ref_peak)
199  {
200  if (!use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) < mz_tol)
201  {
202  found_ref_masses.push_back(ref_masses[ref_peak]);
203  corr_masses.push_back(exp[spec][peak].getMZ());
204  ++corr_peaks;
205  break;
206  }
207  else if (use_ppm && fabs(exp[spec][peak].getMZ() - ref_masses[ref_peak]) / ref_masses[ref_peak] * 1e6 < mz_tol)
208  {
209  found_ref_masses.push_back(ref_masses[ref_peak]);
210  corr_masses.push_back(exp[spec][peak].getMZ());
211  ++corr_peaks;
212  break;
213  }
214  }
215  }
216  setProgress(spec);
217  }
218  endProgress();
219 
220  if (corr_peaks < 2)
221  {
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";
224  return;
225  }
226 
227  // calculate the (linear) calibration function
228  makeLinearRegression_(corr_masses, found_ref_masses);
229 
230  applyTransformation_<InputPeakType>(exp);
231 
232  if (trafo_file_name != "")
233  {
234  TransformationXMLFile().store(trafo_file_name, trafo_);
235  }
236  }
237 
251  template <typename InputPeakType>
253  const std::vector<PeptideIdentification>& ref_ids,
254  const String& trafo_file_name = "")
255  {
256  bool use_ppm = param_.getValue("mz_tolerance_unit") == "ppm" ? true : false;
257  double mz_tolerance = param_.getValue("mz_tolerance");
258  if (exp.empty())
259  {
260  std::cout << "Input is empty." << std::endl;
261  return;
262  }
263 
264  if (exp[0].getType() != SpectrumSettings::PEAKS)
265  {
266  std::cout << "Attention: this function is assuming peak data." << std::endl;
267  }
268  // check if the ids contain meta information about the peak positions
269  checkReferenceIds_(ref_ids);
270 
271  std::vector<double> theoretical_masses, observed_masses;
272  for (Size p_id = 0; p_id < ref_ids.size(); ++p_id)
273  {
274  for (Size p_h = 0; p_h < ref_ids[p_id].getHits().size(); ++p_h)
275  {
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;
278  // first find corresponding ms1-spectrum
279  typename MSExperiment<InputPeakType>::ConstIterator rt_iter = exp.RTBegin(ref_ids[p_id].getRT());
280  while (rt_iter != exp.begin() && rt_iter->getMSLevel() != 1)
281  {
282  --rt_iter;
283  }
284  // now find closest peak
285  typename MSSpectrum<InputPeakType>::ConstIterator mz_iter = rt_iter->MZBegin(ref_ids[p_id].getMZ());
286  //std::cout << mz_iter->getMZ() <<" "<<(double)ref_ids[p_id].getMZ()<<"\t";
287  double dist = ref_ids[p_id].getMZ() - mz_iter->getMZ();
288  //std::cout << dist << "\t";
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())) // if mz_iter +1 has smaller dist than mz_iter and mz_iter-1
293  {
294  if ((use_ppm &&
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))
297  {
298  //std::cout <<(mz_iter +1)->getMZ() - ref_ids[p_id].getMZ()<<"\t";
299  observed_masses.push_back((mz_iter + 1)->getMZ());
300  theoretical_masses.push_back(theo_mass);
301  //std::cout << (mz_iter +1)->getMZ() << " ~ "<<theo_mass << " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
302  //<< "\tplus 1"<< std::endl;
303  }
304  }
305  else if (mz_iter != rt_iter->begin()
306  && fabs((mz_iter - 1)->getMZ() - ref_ids[p_id].getMZ()) < fabs(dist)) // if mz_iter-1 has smaller dist than mz_iter
307  {
308  if ((use_ppm &&
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))
311  {
312  //std::cout <<(mz_iter -1)->getMZ() - ref_ids[p_id].getMZ()<<"\t";
313  observed_masses.push_back((mz_iter - 1)->getMZ());
314  theoretical_masses.push_back(theo_mass);
315  //std::cout << (mz_iter -1)->getMZ() << " ~ "<<theo_mass << " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
316  //<< "\tminus 1"<< std::endl;
317  }
318  }
319  else
320  {
321  if ((use_ppm &&
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))
324  {
325 
326  observed_masses.push_back(mz_iter->getMZ());
327  theoretical_masses.push_back(theo_mass);
328  // std::cout <<"\t"<< mz_iter->getMZ() << " ~ "<<theo_mass<< " charge: "<<ref_ids[p_id].getHits()[p_h].getCharge()
329  // << "\tat mz_iter"<< std::endl;
330  }
331  }
332  }
333  }
334 
335  makeLinearRegression_(observed_masses, theoretical_masses);
336 
337  applyTransformation_<InputPeakType>(exp);
338 
339  if (trafo_file_name != "")
340  {
341  TransformationXMLFile().store(trafo_file_name, trafo_);
342  }
343  }
344 
354  void calibrateMapGlobally(FeatureMap& feature_map, const String& trafo_file_name = "");
355 
368  void calibrateMapGlobally(FeatureMap& feature_map, std::vector<PeptideIdentification>& ref_ids, const String& trafo_file_name = "");
369 
370 
371 protected:
372 
374  void makeLinearRegression_(const std::vector<double>& observed_masses, const std::vector<double>& theoretical_masses);
375 
377  void checkReferenceIds_(const std::vector<PeptideIdentification>& pep_ids);
378 
380  void checkReferenceIds_(const FeatureMap& feature_map);
381 
383  void applyTransformation_(FeatureMap& feature_map);
384 
385 
386  template <typename InputPeakType>
388  {
389  // calibrate only MS1 spectra
390  if (spec.getMSLevel() != 1)
391  {
392  return;
393  }
394 
395  for (unsigned int peak = 0; peak < spec.size(); ++peak)
396  {
397 #ifdef DEBUG_CALIBRATION
398  std::cout << spec[peak].getMZ() << "\t";
399 #endif
400  double mz = spec[peak].getMZ();
401  mz = trafo_.apply(mz);
402  spec[peak].setMZ(mz);
403 
404 #ifdef DEBUG_CALIBRATION
405  std::cout << spec[peak].getMZ() << std::endl;
406 #endif
407 
408  }
409  }
410 
411  template <typename InputPeakType>
413  {
414  startProgress(0, exp.size(), "applying calibration to data");
415  // apply the calibration function to each peak
416  for (Size spec = 0; spec < exp.size(); ++spec)
417  {
418  applyTransformation_<InputPeakType>(exp[spec]);
419  setProgress(spec);
420  } // for(Size spec=0;spec < exp.size(); ++spec)
421  endProgress();
422 
423  }
424 
427  }; // class InternalCalibration
428 
429 } // namespace OpenMS
430 
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
void store(String filename, const TransformationDescription &transformation)
Stores the data in an TransformationXML file.
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
Used to load and store TransformationXML files.
Definition: TransformationXMLFile.h:57
~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
Peak data (also called centroided data or stick data)
Definition: SpectrumSettings.h:74
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
Generic description of a coordinate transformation.
Definition: TransformationDescription.h:58
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

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Jan 31 2017 06:39:46 using doxygen 1.8.8