Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
MRMTransitionGroupPicker.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: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_ANALYSIS_OPENSWATH_MRMTRANSITIONGROUPPICKER_H
36 #define OPENMS_ANALYSIS_OPENSWATH_MRMTRANSITIONGROUPPICKER_H
37 
43 
45 
48 
51 
52 #include <numeric>
53 
54 // Cross-correlation
57 
58 //#define DEBUG_TRANSITIONGROUPPICKER
59 
60 namespace OpenMS
61 {
62 
81  class OPENMS_DLLAPI MRMTransitionGroupPicker :
82  public DefaultParamHandler
83  {
84 
85 public:
86 
87  // this is the type in which we store the chromatograms for this analysis
89 
93 
97 
117  template <typename SpectrumT, typename TransitionT>
119  {
120  std::vector<RichPeakChromatogram> picked_chroms_;
121 
122  PeakPickerMRM picker;
123  picker.setParameters(param_.copy("PeakPickerMRM:", true));
124 
125  // Pick chromatograms
126  for (Size k = 0; k < transition_group.getChromatograms().size(); k++)
127  {
128  RichPeakChromatogram& chromatogram = transition_group.getChromatograms()[k];
129  String native_id = chromatogram.getNativeID();
130  if (transition_group.getTransitions().size() > 0 &&
131  transition_group.hasTransition(native_id) &&
132  !transition_group.getTransition(native_id).isDetectingTransition() )
133  {
134  continue;
135  }
136 
137  if (!chromatogram.isSorted())
138  {
139  chromatogram.sortByPosition();
140  }
141  RichPeakChromatogram picked_chrom;
142  picker.pickChromatogram(chromatogram, picked_chrom);
143  picked_chrom.sortByIntensity(); // we could do without that
144  picked_chroms_.push_back(picked_chrom);
145  }
146 
147  // Find features (peak groups) in this group of transitions.
148  // While there are still peaks left, one will be picked and used to create
149  // a feature. Whenever we run out of peaks, we will get -1 back as index
150  // and terminate.
151  int chr_idx, peak_idx, cnt = 0;
152  std::vector<MRMFeature> features;
153  while (true)
154  {
155  chr_idx = -1; peak_idx = -1;
156  findLargestPeak(picked_chroms_, chr_idx, peak_idx);
157  if (chr_idx == -1 && peak_idx == -1) break;
158 
159  // Compute a feature from the individual chromatograms and add non-zero features
160  MRMFeature mrm_feature = createMRMFeature(transition_group, picked_chroms_, chr_idx, peak_idx);
161  if (mrm_feature.getIntensity() > 0)
162  {
163  features.push_back(mrm_feature);
164  }
165 
166  cnt++;
167  if ((stop_after_feature_ > 0 && cnt > stop_after_feature_) &&
168  mrm_feature.getIntensity() / (double)mrm_feature.getMetaValue("total_xic") < stop_after_intensity_ratio_)
169  {
170  break;
171  }
172  }
173 
174  // Check for completely overlapping features
175  for (Size i = 0; i < features.size(); i++)
176  {
177  MRMFeature& mrm_feature = features[i];
178  bool skip = false;
179  for (Size j = 0; j < i; j++)
180  {
181  if ((double)mrm_feature.getMetaValue("leftWidth") >= (double)features[j].getMetaValue("leftWidth") &&
182  (double)mrm_feature.getMetaValue("rightWidth") <= (double)features[j].getMetaValue("rightWidth") )
183  { skip = true; }
184  }
185  if (mrm_feature.getIntensity() > 0 && !skip)
186  {
187  transition_group.addFeature(mrm_feature);
188  }
189  }
190 
191  }
192 
194  template <typename SpectrumT, typename TransitionT>
196  std::vector<SpectrumT>& picked_chroms, int& chr_idx, int& peak_idx)
197  {
198  MRMFeature mrmFeature;
199  mrmFeature.setIntensity(0.0);
200  double best_left = picked_chroms[chr_idx].getFloatDataArrays()[1][peak_idx];
201  double best_right = picked_chroms[chr_idx].getFloatDataArrays()[2][peak_idx];
202  double peak_apex = picked_chroms[chr_idx][peak_idx].getRT();
203  LOG_DEBUG << "**** Creating MRMFeature for peak " << chr_idx << " " << peak_idx << " " <<
204  picked_chroms[chr_idx][peak_idx] << " with borders " << best_left << " " <<
205  best_right << " (" << best_right - best_left << ")" << std::endl;
206 
207  if (recalculate_peaks_)
208  {
209  // This may change best_left / best_right
210  recalculatePeakBorders_(picked_chroms, best_left, best_right, recalculate_peaks_max_z_);
211  if (peak_apex < best_left || peak_apex > best_right)
212  {
213  // apex fell out of range, lets correct it
214  peak_apex = (best_left + best_right) / 2.0;
215  }
216  }
217  picked_chroms[chr_idx][peak_idx].setIntensity(0.0);
218 
219  // Remove other, overlapping, picked peaks (in this and other
220  // chromatograms) and then ensure that at least one peak is set to zero
221  // (the currently best peak).
222  remove_overlapping_features(picked_chroms, best_left, best_right);
223 
224  // Check for minimal peak width -> return empty feature (Intensity zero)
225  if (min_peak_width_ > 0.0 && std::fabs(best_right - best_left) < min_peak_width_) {return mrmFeature;}
226 
227  if (compute_peak_quality_)
228  {
229  String outlier = "none";
230  double qual = computeQuality_(transition_group, picked_chroms, chr_idx, best_left, best_right, outlier);
231  if (qual < min_qual_) {return mrmFeature; }
232  mrmFeature.setMetaValue("potentialOutlier", outlier);
233  mrmFeature.setMetaValue("initialPeakQuality", qual);
234  mrmFeature.setOverallQuality(qual);
235  }
236 
237  // Prepare linear resampling of all the chromatograms, here creating the
238  // empty master_peak_container with the same RT (m/z) values as the reference
239  // chromatogram.
240  SpectrumT master_peak_container;
241  const SpectrumT& ref_chromatogram = transition_group.getChromatogram(picked_chroms[chr_idx].getNativeID());
242  prepareMasterContainer_(ref_chromatogram, master_peak_container, best_left, best_right);
243 
244  double total_intensity = 0; double total_peak_apices = 0; double total_xic = 0;
245  for (Size k = 0; k < transition_group.getTransitions().size(); k++)
246  {
247  const SpectrumT& chromatogram = transition_group.getChromatogram(transition_group.getTransitions()[k].getNativeID());
248  if (transition_group.getTransitions()[k].isDetectingTransition())
249  {
250  for (typename SpectrumT::const_iterator it = chromatogram.begin(); it != chromatogram.end(); it++)
251  {
252  total_xic += it->getIntensity();
253  }
254  }
255 
256  // resample the current chromatogram
257  const SpectrumT used_chromatogram = resampleChromatogram_(chromatogram, master_peak_container, best_left, best_right);
258  // const SpectrumT& used_chromatogram = chromatogram; // instead of resampling
259 
260  Feature f;
261  double quality = 0;
262  f.setQuality(0, quality);
263  f.setOverallQuality(quality);
264 
265  ConvexHull2D::PointArrayType hull_points;
266  double intensity_sum(0.0), rt_sum(0.0);
267  double peak_apex_int = -1;
268  double peak_apex_dist = std::fabs(used_chromatogram.begin()->getMZ() - peak_apex);
269  // FEATURE : use RTBegin / MZBegin -> for this we need to know whether the template param is a real chromatogram or a spectrum!
270  for (typename SpectrumT::const_iterator it = used_chromatogram.begin(); it != used_chromatogram.end(); it++)
271  {
272  if (it->getMZ() > best_left && it->getMZ() < best_right)
273  {
274  DPosition<2> p;
275  p[0] = it->getMZ();
276  p[1] = it->getIntensity();
277  hull_points.push_back(p);
278  if (std::fabs(it->getMZ() - peak_apex) <= peak_apex_dist)
279  {
280  peak_apex_int = p[1];
281  peak_apex_dist = std::fabs(it->getMZ() - peak_apex);
282  }
283  rt_sum += it->getMZ();
284  intensity_sum += it->getIntensity();
285  }
286  }
287 
288  if (background_subtraction_ != "none")
289  {
290  double background = 0;
291  if (background_subtraction_ == "smoothed")
292  {
293  throw Exception::NotImplemented(__FILE__, __LINE__, __PRETTY_FUNCTION__);
294  /*
295  * Currently we do not have access to the smoothed chromatogram any more
296  if (smoothed_chroms_.size() <= k)
297  {
298  std::cerr << "Tried to calculate background estimation without any smoothed chromatograms" << std::endl;
299  background = 0;
300  }
301  else
302  {
303  background = calculateBgEstimation_(smoothed_chroms_[k], best_left, best_right);
304  }
305  */
306  }
307  else if (background_subtraction_ == "original")
308  {
309  background = calculateBgEstimation_(used_chromatogram, best_left, best_right);
310  }
311  intensity_sum -= background;
312  if (intensity_sum < 0)
313  {
314  std::cerr << "Warning: Intensity was below 0 after background subtraction: " << intensity_sum << ". Setting it to 0." << std::endl;
315  intensity_sum = 0;
316  }
317  }
318 
319  f.setRT(picked_chroms[chr_idx][peak_idx].getMZ());
320  f.setMZ(chromatogram.getMetaValue("product_mz"));
321  f.setIntensity(intensity_sum);
322  ConvexHull2D hull;
323  hull.setHullPoints(hull_points);
324  f.getConvexHulls().push_back(hull);
325  f.setMetaValue("MZ", chromatogram.getMetaValue("product_mz"));
326  f.setMetaValue("native_id", chromatogram.getNativeID());
327  f.setMetaValue("peak_apex_int", peak_apex_int);
328  //f.setMetaValue("leftWidth", best_left);
329  //f.setMetaValue("rightWidth", best_right);
330 
331  if (transition_group.getTransitions()[k].isDetectingTransition())
332  {
333  total_intensity += intensity_sum;
334  total_peak_apices += peak_apex_int;
335  }
336  mrmFeature.addFeature(f, chromatogram.getNativeID()); //map index and feature
337  }
338 
339  // Also pick the precursor chromatogram (note total_xic is not extracted here, only for fragment traces)
340  if (transition_group.hasPrecursorChromatogram("Precursor_i0"))
341  {
342  const SpectrumT& chromatogram = transition_group.getPrecursorChromatogram("Precursor_i0");
343 
344  // resample the current chromatogram
345  const SpectrumT used_chromatogram = resampleChromatogram_(chromatogram, master_peak_container, best_left, best_right);
346 
347  Feature f;
348  double quality = 0;
349  f.setQuality(0, quality);
350  f.setOverallQuality(quality);
351 
352  ConvexHull2D::PointArrayType hull_points;
353  double intensity_sum(0.0), rt_sum(0.0);
354  double peak_apex_int = -1;
355  double peak_apex_dist = std::fabs(used_chromatogram.begin()->getMZ() - peak_apex);
356  // FEATURE : use RTBegin / MZBegin -> for this we need to know whether the template param is a real chromatogram or a spectrum!
357  for (typename SpectrumT::const_iterator it = used_chromatogram.begin(); it != used_chromatogram.end(); it++)
358  {
359  if (it->getMZ() > best_left && it->getMZ() < best_right)
360  {
361  DPosition<2> p;
362  p[0] = it->getMZ();
363  p[1] = it->getIntensity();
364  hull_points.push_back(p);
365  if (std::fabs(it->getMZ() - peak_apex) <= peak_apex_dist)
366  {
367  peak_apex_int = p[1];
368  peak_apex_dist = std::fabs(it->getMZ() - peak_apex);
369  }
370  rt_sum += it->getMZ();
371  intensity_sum += it->getIntensity();
372  }
373  }
374 
375  if (chromatogram.metaValueExists("precursor_mz"))
376  {
377  f.setMZ(chromatogram.getMetaValue("precursor_mz"));
378  }
379  f.setRT(picked_chroms[chr_idx][peak_idx].getMZ());
380  f.setIntensity(intensity_sum);
381  ConvexHull2D hull;
382  hull.setHullPoints(hull_points);
383  f.getConvexHulls().push_back(hull);
384  f.setMetaValue("native_id", chromatogram.getNativeID());
385  f.setMetaValue("peak_apex_int", peak_apex_int);
386 
387  mrmFeature.addPrecursorFeature(f, "Precursor_i0");
388  }
389 
390  mrmFeature.setRT(peak_apex);
391  mrmFeature.setIntensity(total_intensity);
392  mrmFeature.setMetaValue("PeptideRef", transition_group.getTransitionGroupID());
393  mrmFeature.setMetaValue("leftWidth", best_left);
394  mrmFeature.setMetaValue("rightWidth", best_right);
395  mrmFeature.setMetaValue("total_xic", total_xic);
396  mrmFeature.setMetaValue("peak_apices_sum", total_peak_apices);
397 
398  mrmFeature.ensureUniqueId();
399  return mrmFeature;
400  }
401 
402  // maybe private, but we have tests
403 
415  template <typename SpectrumT>
416  void remove_overlapping_features(std::vector<SpectrumT>& picked_chroms, double best_left, double best_right)
417  {
418  // delete all seeds that lie within the current seed
419  //std::cout << "Removing features for peak between " << best_left << " " << best_right << std::endl;
420  for (Size k = 0; k < picked_chroms.size(); k++)
421  {
422  for (Size i = 0; i < picked_chroms[k].size(); i++)
423  {
424  if (picked_chroms[k][i].getMZ() >= best_left && picked_chroms[k][i].getMZ() <= best_right)
425  {
426  //std::cout << "For Chrom " << k << " removing peak " << picked_chroms[k][i].getMZ() << " l/r : " << picked_chroms[k].getFloatDataArrays()[1][i] << " " <<
427  // picked_chroms[k].getFloatDataArrays()[2][i] << " with int " << picked_chroms[k][i].getIntensity() <<std::endl;
428  picked_chroms[k][i].setIntensity(0.0);
429  }
430  }
431  }
432 
433  // delete all seeds that overlap within the current seed
434  for (Size k = 0; k < picked_chroms.size(); k++)
435  {
436  for (Size i = 0; i < picked_chroms[k].size(); i++)
437  {
438  if (picked_chroms[k][i].getIntensity() <= 0.0) {continue; }
439 
440  double left = picked_chroms[k].getFloatDataArrays()[1][i];
441  double right = picked_chroms[k].getFloatDataArrays()[2][i];
442  if ((left > best_left && left < best_right)
443  || (right > best_left && right < best_right))
444  {
445  //std::cout << "= For Chrom " << k << " removing contained peak " << picked_chroms[k][i].getMZ() << " l/r : " << picked_chroms[k].getFloatDataArrays()[1][i] << " " <<
446  // picked_chroms[k].getFloatDataArrays()[2][i] << " with int " << picked_chroms[k][i].getIntensity() <<std::endl;
447  picked_chroms[k][i].setIntensity(0.0);
448  }
449  }
450  }
451  }
452 
454  void findLargestPeak(std::vector<RichPeakChromatogram>& picked_chroms, int& chr_idx, int& peak_idx);
455 
456 protected:
457 
459  void updateMembers_();
460 
462  MRMTransitionGroupPicker& operator=(const MRMTransitionGroupPicker& rhs);
463 
480  template <typename SpectrumT, typename TransitionT>
482  std::vector<SpectrumT>& picked_chroms, const int chr_idx,
483  const double best_left, const double best_right, String& outlier)
484  {
485 
486  // Resample all chromatograms around the current estimated peak and
487  // collect the raw intensities. For resampling, use a bit more on either
488  // side to correctly identify shoulders etc.
489  double resample_boundary = 15.0; // sample 15 seconds more on each side
490  SpectrumT master_peak_container;
491  const SpectrumT& ref_chromatogram = transition_group.getChromatogram(picked_chroms[chr_idx].getNativeID());
492  prepareMasterContainer_(ref_chromatogram, master_peak_container, best_left - resample_boundary, best_right + resample_boundary);
493  std::vector<std::vector<double> > all_ints;
494  for (Size k = 0; k < picked_chroms.size(); k++)
495  {
496  const SpectrumT& chromatogram = transition_group.getChromatogram(picked_chroms[k].getNativeID());
497  const SpectrumT used_chromatogram = resampleChromatogram_(chromatogram,
498  master_peak_container, best_left - resample_boundary, best_right + resample_boundary);
499 
500  std::vector<double> int_here;
501  for (Size i = 0; i < used_chromatogram.size(); i++)
502  {
503  int_here.push_back(used_chromatogram[i].getIntensity());
504  }
505  all_ints.push_back(int_here);
506  }
507 
508  // Compute the cross-correlation for the collected intensities
509  std::vector<double> mean_shapes;
510  std::vector<double> mean_coel;
511  for (Size k = 0; k < all_ints.size(); k++)
512  {
513  std::vector<double> shapes;
514  std::vector<double> coel;
515  for (Size i = 0; i < all_ints.size(); i++)
516  {
517  if (i == k) {continue;}
518  std::map<int, double> res = OpenSwath::Scoring::normalizedCrossCorrelation(
519  all_ints[k], all_ints[i], boost::numeric_cast<int>(all_ints[i].size()), 1);
520 
521  // the first value is the x-axis (retention time) and should be an int -> it show the lag between the two
522  double res_coelution = std::abs(OpenSwath::Scoring::xcorrArrayGetMaxPeak(res)->first);
523  double res_shape = std::abs(OpenSwath::Scoring::xcorrArrayGetMaxPeak(res)->second);
524 
525  shapes.push_back(res_shape);
526  coel.push_back(res_coelution);
527  }
528 
529  // We have computed the cross-correlation of chromatogram k against
530  // all others. Use the mean of these computations as the value for k.
532  msc = std::for_each(shapes.begin(), shapes.end(), msc);
533  double shapes_mean = msc.mean();
534  msc = std::for_each(coel.begin(), coel.end(), msc);
535  double coel_mean = msc.mean();
536 
537  // mean shape scores below 0.5-0.6 should be a real sign of trouble ... !
538  // mean coel scores above 3.5 should be a real sign of trouble ... !
539  mean_shapes.push_back(shapes_mean);
540  mean_coel.push_back(coel_mean);
541  }
542 
543  // find the chromatogram with the minimal shape score and the maximal
544  // coelution score -> if it is the same chromatogram, the chance is
545  // pretty good that it is different from the others...
546  int min_index_shape = std::distance(mean_shapes.begin(), std::min_element(mean_shapes.begin(), mean_shapes.end()));
547  int max_index_coel = std::distance(mean_coel.begin(), std::max_element(mean_coel.begin(), mean_coel.end()));
548 
549  // Look at the picked peaks that are within the current left/right borders
550  int missing_peaks = 0;
551  int multiple_peaks = 0;
552 
553  // collect all seeds that lie within the current seed
554  std::vector<double> left_borders;
555  std::vector<double> right_borders;
556  for (Size k = 0; k < picked_chroms.size(); k++)
557  {
558  double l_tmp;
559  double r_tmp;
560  double max_int = -1;
561 
562  int pfound = 0;
563  l_tmp = -1;
564  r_tmp = -1;
565  for (Size i = 0; i < picked_chroms[k].size(); i++)
566  {
567  if (picked_chroms[k][i].getMZ() >= best_left && picked_chroms[k][i].getMZ() <= best_right)
568  {
569  pfound++;
570  if (picked_chroms[k][i].getIntensity() > max_int)
571  {
572  max_int = picked_chroms[k][i].getIntensity() > max_int;
573  l_tmp = picked_chroms[k].getFloatDataArrays()[1][i];
574  r_tmp = picked_chroms[k].getFloatDataArrays()[2][i];
575  }
576  }
577  }
578 
579  if (l_tmp > 0.0) left_borders.push_back(l_tmp);
580  if (r_tmp > 0.0) right_borders.push_back(r_tmp);
581 
582  if (pfound == 0) missing_peaks++;
583  if (pfound > 1) multiple_peaks++;
584  }
585 
586  // Check how many chromatograms had exactly one peak picked between our
587  // current left/right borders -> this would be a sign of consistency.
588  LOG_DEBUG << " Overall found missing : " << missing_peaks << " and multiple : " << multiple_peaks << std::endl;
589 
591 
592  // Is there one transitions that is very different from the rest (e.g.
593  // the same element has a bad shape and a bad coelution score) -> potential outlier
594  if (min_index_shape == max_index_coel)
595  {
596  LOG_DEBUG << " element " << min_index_shape << " is a candidate for removal ... " << std::endl;
597  outlier = String(transition_group.getTransitions()[min_index_shape].getNativeID());
598  }
599  else
600  {
601  outlier = "none";
602  }
603 
604  // For the final score (larger is better), consider these scores:
605  // - missing_peaks (the more peaks are missing, the worse)
606  // - multiple_peaks
607  // - mean of the shapes (1 is very good, 0 is bad)
608  // - mean of the co-elution scores (0 is good, 1 is ok, above 1 is pretty bad)
609  double shape_score = std::accumulate(mean_shapes.begin(), mean_shapes.end(), 0.0) / mean_shapes.size();
610  double coel_score = std::accumulate(mean_coel.begin(), mean_coel.end(), 0.0) / mean_coel.size();
611  coel_score = (coel_score - 1.0) / 2.0;
612 
613  double score = shape_score - coel_score - 1.0 * missing_peaks / picked_chroms.size();
614 
615  LOG_DEBUG << " computed score " << score << " (from " << shape_score <<
616  " - " << coel_score << " - " << 1.0 * missing_peaks / picked_chroms.size() << ")" << std::endl;
617 
618  return score;
619  }
620 
630  template <typename SpectrumT>
631  void recalculatePeakBorders_(std::vector<SpectrumT>& picked_chroms, double& best_left, double& best_right, double max_z)
632  {
633  // 1. Collect all seeds that lie within the current seed
634  // - Per chromatogram only the most intense one counts, otherwise very
635  // - low intense peaks can contribute disproportionally to the voting
636  // - procedure.
637  std::vector<double> left_borders;
638  std::vector<double> right_borders;
639  for (Size k = 0; k < picked_chroms.size(); k++)
640  {
641  double max_int = -1;
642  double left = -1;
643  double right = -1;
644  for (Size i = 0; i < picked_chroms[k].size(); i++)
645  {
646  if (picked_chroms[k][i].getMZ() >= best_left && picked_chroms[k][i].getMZ() <= best_right)
647  {
648  if (picked_chroms[k].getFloatDataArrays()[0][i] > max_int)
649  {
650  max_int = picked_chroms[k].getFloatDataArrays()[0][i];
651  left = picked_chroms[k].getFloatDataArrays()[1][i];
652  right = picked_chroms[k].getFloatDataArrays()[2][i];
653  }
654  }
655  }
656  if (max_int > -1 )
657  {
658  left_borders.push_back(left);
659  right_borders.push_back(right);
660  LOG_DEBUG << " * " << k << " left boundary " << left_borders.back() << " with int " << max_int << std::endl;
661  LOG_DEBUG << " * " << k << " right boundary " << right_borders.back() << " with int " << max_int << std::endl;
662  }
663  }
664 
665  // Return for empty peak list
666  if (right_borders.empty())
667  {
668  return;
669  }
670 
671  // FEATURE IDEA: instead of Z-score use modified Z-score for small data sets
672  // http://d-scholarship.pitt.edu/7948/1/Seo.pdf
673  // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h.htm
674  // 1. calculate median
675  // 2. MAD = calculate difference to median for each value -> take median of that
676  // 3. Mi = 0.6745*(xi - median) / MAD
677 
678  // 2. Calculate mean and standard deviation
679  // If the coefficient of variation is too large for one border, we use a
680  // "pseudo-median" instead of the border of the most intense peak.
681  double mean, stdev;
682 
683  // Right borders
684  mean = std::accumulate(right_borders.begin(), right_borders.end(), 0.0) / (double) right_borders.size();
685  stdev = std::sqrt(std::inner_product(right_borders.begin(), right_borders.end(), right_borders.begin(), 0.0)
686  / right_borders.size() - mean * mean);
687  std::sort(right_borders.begin(), right_borders.end());
688 
689  LOG_DEBUG << " - Recalculating right peak boundaries " << mean << " mean / best "
690  << best_right << " std " << stdev << " : " << std::fabs(best_right - mean) / stdev
691  << " coefficient of variation" << std::endl;
692 
693  // Compare right borders of best transition with the mean
694  if (std::fabs(best_right - mean) / stdev > max_z)
695  {
696  best_right = right_borders[right_borders.size() / 2]; // pseudo median
697  LOG_DEBUG << " - Setting right boundary to " << best_right << std::endl;
698  }
699 
700  // Left borders
701  mean = std::accumulate(left_borders.begin(), left_borders.end(), 0.0) / (double) left_borders.size();
702  stdev = std::sqrt(std::inner_product(left_borders.begin(), left_borders.end(), left_borders.begin(), 0.0)
703  / left_borders.size() - mean * mean);
704  std::sort(left_borders.begin(), left_borders.end());
705 
706  LOG_DEBUG << " - Recalculating left peak boundaries " << mean << " mean / best "
707  << best_left << " std " << stdev << " : " << std::fabs(best_left - mean) / stdev
708  << " coefficient of variation" << std::endl;
709 
710  // Compare left borders of best transition with the mean
711  if (std::fabs(best_left - mean) / stdev > max_z)
712  {
713  best_left = left_borders[left_borders.size() / 2]; // pseudo median
714  LOG_DEBUG << " - Setting left boundary to " << best_left << std::endl;
715  }
716 
717  }
718 
720 
721  template <typename SpectrumT>
723  void prepareMasterContainer_(const SpectrumT& ref_chromatogram,
724  SpectrumT& master_peak_container, double best_left, double best_right)
725  {
726  // search for begin / end of the reference chromatogram (and add one more point)
727  typename SpectrumT::const_iterator begin = ref_chromatogram.begin();
728  while (begin != ref_chromatogram.end() && begin->getMZ() < best_left) {begin++; }
729  if (begin != ref_chromatogram.begin()) {begin--; }
730 
731  typename SpectrumT::const_iterator end = begin;
732  while (end != ref_chromatogram.end() && end->getMZ() < best_right) {end++; }
733  if (end != ref_chromatogram.end()) {end++; }
734 
735  // resize the master container and set the m/z values to the ones of the master container
736  master_peak_container.resize(distance(begin, end));
737  typename SpectrumT::iterator it = master_peak_container.begin();
738  for (typename SpectrumT::const_iterator chrom_it = begin; chrom_it != end; chrom_it++, it++)
739  {
740  it->setMZ(chrom_it->getMZ());
741  }
742  }
743 
745  template <typename SpectrumT>
746  SpectrumT resampleChromatogram_(const SpectrumT& chromatogram,
747  SpectrumT& master_peak_container, double best_left, double best_right)
748  {
749  // get the start / end point of this chromatogram => go one past
750  // best_left / best_right to make the resampling accurate also at the
751  // edge.
752  typename SpectrumT::const_iterator begin = chromatogram.begin();
753  while (begin != chromatogram.end() && begin->getMZ() < best_left) {begin++; }
754  if (begin != chromatogram.begin()) {begin--; }
755 
756  typename SpectrumT::const_iterator end = begin;
757  while (end != chromatogram.end() && end->getMZ() < best_right) {end++; }
758  if (end != chromatogram.end()) {end++; }
759 
760  SpectrumT resampled_peak_container = master_peak_container; // copy the master container, which contains the RT values
761  LinearResamplerAlign lresampler;
762  lresampler.raster(begin, end, resampled_peak_container.begin(), resampled_peak_container.end());
763 
764  return resampled_peak_container;
765  }
766 
768 
775  double calculateBgEstimation_(const RichPeakChromatogram& chromatogram, double best_left, double best_right);
776 
777  // Members
781  double min_qual_;
782 
787  };
788 }
789 
790 #endif // OPENMS_ANALYSIS_OPENSWATH_MRMTRANSITIONGROUPPICKER_H
791 
const double k
const DataValue & getMetaValue(const String &name) const
Returns the value corresponding to a string (or DataValue::EMPTY if not found)
void raster(SpecT< PeakType > &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:75
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
double min_qual_
Definition: MRMTransitionGroupPicker.h:781
String background_subtraction_
Definition: MRMTransitionGroupPicker.h:778
const std::vector< TransitionType > & getTransitions() const
Definition: MRMTransitionGroup.h:125
double recalculate_peaks_max_z_
Definition: MRMTransitionGroupPicker.h:786
A more convenient string class.
Definition: String.h:57
bool compute_peak_quality_
Definition: MRMTransitionGroupPicker.h:780
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:203
void sortByPosition()
Lexicographically sorts the peaks by their position.
Definition: MSSpectrum.h:419
IntensityType getIntensity() const
Definition: Peak2D.h:167
bool hasTransition(String key)
Definition: MRMTransitionGroup.h:146
OPENSWATHALGO_DLLAPI XCorrArrayType normalizedCrossCorrelation(std::vector< double > &data1, std::vector< double > &data2, int maxdelay, int lag)
const std::vector< SpectrumType > & getChromatograms() const
Definition: MRMTransitionGroup.h:151
SpectrumType & getPrecursorChromatogram(String key)
Definition: MRMTransitionGroup.h:193
std::vector< PointType > PointArrayType
Definition: ConvexHull2D.h:77
bool isSorted() const
Checks if all peaks are sorted with respect to ascending m/z.
Definition: MSSpectrum.h:481
void prepareMasterContainer_(const SpectrumT &ref_chromatogram, SpectrumT &master_peak_container, double best_left, double best_right)
create an empty master peak container that has the correct mz / RT values set
Definition: MRMTransitionGroupPicker.h:723
The PeakPickerMRM finds peaks a single chromatogram.
Definition: PeakPickerMRM.h:63
int stop_after_feature_
Definition: MRMTransitionGroupPicker.h:783
double mean() const
Definition: StatsHelpers.h:208
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void setParameters(const Param &param)
Sets the parameters.
A 2-dimensional hull representation in [counter]clockwise direction - depending on axis labelling...
Definition: ConvexHull2D.h:73
#define LOG_DEBUG
Macro for general debugging information.
Definition: LogStream.h:459
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:131
void setIntensity(IntensityType intensity)
Non-mutable access to the data point intensity (height)
Definition: Peak2D.h:173
void addFeature(Feature &feature, const String &key)
Adds an feature from a single chromatogram into the feature.
double min_peak_width_
Definition: MRMTransitionGroupPicker.h:785
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:215
The MRMTransitionGroupPicker finds peaks in chromatograms that belong to the same precursors...
Definition: MRMTransitionGroupPicker.h:81
bool recalculate_peaks_
Definition: MRMTransitionGroupPicker.h:779
const std::vector< ConvexHull2D > & getConvexHulls() const
Non-mutable access to the convex hulls.
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
Definition: MSSpectrum.h:342
void setQuality(Size index, QualityType q)
Set the quality in dimension c.
Linear Resampling of raw data with alignment.
Definition: LinearResamplerAlign.h:58
void pickChromatogram(const RichPeakChromatogram &chromatogram, RichPeakChromatogram &picked_chrom)
Finds peaks in a single chromatogram and annotates left/right borders.
void setHullPoints(const PointArrayType &points)
accessor for the outer(!) points (no checking is performed if this is actually a convex hull) ...
double computeQuality_(MRMTransitionGroup< SpectrumT, TransitionT > &transition_group, std::vector< SpectrumT > &picked_chroms, const int chr_idx, const double best_left, const double best_right, String &outlier)
Compute transition group quality (higher score is better)
Definition: MRMTransitionGroupPicker.h:481
An LC-MS feature.
Definition: Feature.h:70
functor to compute the mean and stddev of sequence using the std::foreach algorithm ...
Definition: StatsHelpers.h:170
void addFeature(MRMFeature &feature)
Definition: MRMTransitionGroup.h:213
void addPrecursorFeature(Feature &feature, const String &key)
Adds a precursor feature from a single chromatogram into the feature.
void setOverallQuality(QualityType q)
Set the overall quality.
bool hasPrecursorChromatogram(String key) const
Definition: MRMTransitionGroup.h:198
SpectrumT resampleChromatogram_(const SpectrumT &chromatogram, SpectrumT &master_peak_container, double best_left, double best_right)
use the master container from above to resample a chromatogram at those points stored in the master c...
Definition: MRMTransitionGroupPicker.h:746
SpectrumType & getChromatogram(String key)
Definition: MRMTransitionGroup.h:167
void pickTransitionGroup(MRMTransitionGroup< SpectrumT, TransitionT > &transition_group)
Pick a group of chromatograms belonging to the same peptide.
Definition: MRMTransitionGroupPicker.h:118
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
MRMFeature createMRMFeature(MRMTransitionGroup< SpectrumT, TransitionT > &transition_group, std::vector< SpectrumT > &picked_chroms, int &chr_idx, int &peak_idx)
Create feature from a vector of chromatograms and a specified peak.
Definition: MRMTransitionGroupPicker.h:195
void remove_overlapping_features(std::vector< SpectrumT > &picked_chroms, double best_left, double best_right)
Remove overlapping features.
Definition: MRMTransitionGroupPicker.h:416
OPENSWATHALGO_DLLAPI XCorrArrayType::iterator xcorrArrayGetMaxPeak(XCorrArrayType &array)
Find best peak in an cross-correlation (highest apex)
double stop_after_intensity_ratio_
Definition: MRMTransitionGroupPicker.h:784
A multi-chromatogram MRM feature.
Definition: MRMFeature.h:50
const String & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software. ...
void recalculatePeakBorders_(std::vector< SpectrumT > &picked_chroms, double &best_left, double &best_right, double max_z)
Recalculate the borders of the peak.
Definition: MRMTransitionGroupPicker.h:631
MSSpectrum< ChromatogramPeak > RichPeakChromatogram
Definition: MRMTransitionGroupPicker.h:88
const String & getTransitionGroupID() const
Definition: MRMTransitionGroup.h:115
Not implemented exception.
Definition: Exception.h:437
Size ensureUniqueId()
Assigns a valid unique id, but only if the present one is invalid. Returns 1 if the unique id was cha...
Definition: UniqueIdInterface.h:159
const TransitionType & getTransition(String key)
Definition: MRMTransitionGroup.h:141

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