82 defaults_.setValue(
"rt_tolerance", 10.0,
"Maximal RT distance (in [s]) for two spectra's precursors.");
83 defaults_.setValue(
"mz_tolerance", 1.0,
"Maximal m/z distance (in Da) for two spectra's precursors.");
89 rt_max_ = (
double) param_.getValue(
"rt_tolerance");
90 mz_max_ = (
double) param_.getValue(
"mz_tolerance");
98 return 1 - ((d_rt / rt_max_ + d_mz / mz_max_) / 2);
105 double d_rt = fabs(first.
getRT() - second.
getRT());
106 double d_mz = fabs(first.
getMZ() - second.
getMZ());
108 if (d_rt > rt_max_ || d_mz > mz_max_)
114 double sim = getSimilarity(d_rt, d_mz);
154 template <
typename MapType>
157 IntList ms_levels = param_.getValue(
"block_method:ms_levels");
158 Int rt_block_size(param_.getValue(
"block_method:rt_block_size"));
159 double rt_max_length = (param_.getValue(
"block_method:rt_max_length"));
161 if (rt_max_length == 0)
163 rt_max_length = (std::numeric_limits<double>::max)();
166 for (IntList::iterator it_mslevel = ms_levels.begin(); it_mslevel < ms_levels.end(); ++it_mslevel)
170 SignedSize block_size_count(rt_block_size + 1);
171 Size idx_spectrum(0);
174 if (
Int(it1->getMSLevel()) == *it_mslevel)
177 if (++block_size_count >= rt_block_size ||
178 exp[idx_spectrum].getRT() - exp[idx_block].getRT() > rt_max_length)
180 block_size_count = 0;
181 idx_block = idx_spectrum;
185 spectra_to_merge[idx_block].push_back(idx_spectrum);
192 if (block_size_count == 0)
194 spectra_to_merge[idx_block] = std::vector<Size>();
198 mergeSpectra_(exp, spectra_to_merge, *it_mslevel);
207 template <
typename MapType>
213 std::vector<BinaryTreeNode> tree;
217 std::vector<BaseFeature> data;
219 for (
Size i = 0; i < exp.
size(); ++i)
221 if (exp[i].getMSLevel() != 2)
227 index_mapping[data.size()] = i;
231 bf.
setRT(exp[i].getRT());
232 std::vector<Precursor> pcs = exp[i].getPrecursors();
239 LOG_WARN <<
"More than one precursor found. Using first one!" << std::endl;
241 bf.
setMZ(pcs[0].getMZ());
244 data_size = data.size();
259 std::vector<std::vector<Size> > clusters;
262 for (
Size ii = 0; ii < tree.size(); ++ii)
264 if (tree[ii].distance >= 1)
266 tree[ii].distance = -1;
268 if (tree[ii].distance != -1)
273 ca.
cut(data_size - node_count, tree, clusters);
281 for (
Size i_outer = 0; i_outer < clusters.size(); ++i_outer)
283 if (clusters[i_outer].size() <= 1)
288 Size cl_index0 = clusters[i_outer][0];
289 spectra_to_merge[index_mapping[cl_index0]] = std::vector<Size>();
291 for (
Size i_inner = 1; i_inner < clusters[i_outer].size(); ++i_inner)
293 Size cl_index = clusters[i_outer][i_inner];
294 spectra_to_merge[index_mapping[cl_index0]].push_back(index_mapping[cl_index]);
299 mergeSpectra_(exp, spectra_to_merge, 2);
312 template <
typename MapType>
316 int ms_level = param_.getValue(
"average_gaussian:ms_level");
317 if (average_type ==
"tophat")
319 ms_level = param_.getValue(
"average_tophat:ms_level");
323 String spectrum_type = param_.getValue(
"average_gaussian:spectrum_type");
324 if (average_type ==
"tophat")
326 spectrum_type = param_.getValue(
"average_tophat:spectrum_type");
330 double fwhm(param_.getValue(
"average_gaussian:rt_FWHM"));
331 double factor = -4 * log(2.0) / (fwhm * fwhm);
332 double cutoff(param_.getValue(
"average_gaussian:cutoff"));
335 bool unit(param_.getValue(
"average_tophat:rt_unit") ==
"scans");
336 double range(param_.getValue(
"average_tophat:rt_range"));
337 double range_seconds = range / 2;
338 int range_scans = range;
339 if ((range_scans % 2) == 0)
343 range_scans = (range_scans - 1) / 2;
351 if (
Int(it_rt->getMSLevel()) == ms_level)
362 terminate_now =
false;
363 while (it_rt_2 != exp.
end() && !terminate_now)
365 if (
Int(it_rt_2->getMSLevel()) == ms_level)
368 if (average_type ==
"gaussian")
370 weight = std::exp(factor * pow(it_rt_2->getRT() - it_rt->getRT(), 2));
372 std::pair<Size, double> p(m, weight);
373 spectra_to_average_over[n].push_back(p);
376 if (average_type ==
"gaussian")
379 terminate_now = std::exp(factor * pow(it_rt_2->getRT() - it_rt->getRT(), 2)) < cutoff;
384 terminate_now = (steps > range_scans);
389 terminate_now = (std::abs(it_rt_2->getRT() - it_rt->getRT()) > range_seconds);
399 terminate_now =
false;
400 while (it_rt_2 != exp.
begin() && !terminate_now)
402 if (
Int(it_rt_2->getMSLevel()) == ms_level)
405 if (average_type ==
"gaussian")
407 weight = std::exp(factor * pow(it_rt_2->getRT() - it_rt->getRT(), 2));
409 std::pair<Size, double> p(m, weight);
410 spectra_to_average_over[n].push_back(p);
413 if (average_type ==
"gaussian")
416 terminate_now = std::exp(factor * pow(it_rt_2->getRT() - it_rt->getRT(), 2)) < cutoff;
421 terminate_now = (steps > range_scans);
426 terminate_now = (std::abs(it_rt_2->getRT() - it_rt->getRT()) > range_seconds);
440 for (std::vector<std::pair<Size, double> >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
445 for (std::vector<std::pair<Size, double> >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
447 (*it2).second /=
sum;
453 if (spectrum_type ==
"automatic")
455 Size idx = spectra_to_average_over.begin()->first;
456 type = exp[idx].getType(
true);
458 else if (spectrum_type ==
"profile")
462 else if (spectrum_type ==
"centroid")
470 averageCentroidSpectra_(exp, spectra_to_average_over, ms_level);
474 averageProfileSpectra_(exp, spectra_to_average_over, ms_level);
496 template <
typename MapType>
499 double mz_binning_width(param_.getValue(
"mz_binning_width"));
500 String mz_binning_unit(param_.getValue(
"mz_binning_width_unit"));
506 std::set<Size> merged_indices;
511 p.
setValue(
"tolerance", mz_binning_width);
512 if (!(mz_binning_unit ==
"Da" || mz_binning_unit ==
"ppm"))
517 p.
setValue(
"is_relative_tolerance", mz_binning_unit ==
"Da" ?
"false" :
"true");
519 std::vector<std::pair<Size, Size> > alignment;
521 Size count_peaks_aligned(0);
522 Size count_peaks_overall(0);
525 for (
auto it = spectra_to_merge.begin(); it != spectra_to_merge.end(); ++it)
527 ++cluster_sizes[it->second.size() + 1];
533 merged_indices.insert(it->first);
536 double rt_average = consensus_spec.
getRT();
537 double precursor_mz_average = 0.0;
538 Size precursor_count(0);
541 precursor_mz_average = consensus_spec.
getPrecursors()[0].getMZ();
545 count_peaks_overall += consensus_spec.size();
548 for (
auto sit = it->second.begin(); sit != it->second.end(); ++sit)
550 consensus_spec.
unify(exp[*sit]);
551 merged_indices.insert(*sit);
553 rt_average += exp[*sit].getRT();
554 if (ms_level >= 2 && exp[*sit].getPrecursors().size() > 0)
556 precursor_mz_average += exp[*sit].getPrecursors()[0].getMZ();
563 count_peaks_aligned += alignment.size();
564 count_peaks_overall += exp[*sit].
size();
567 Size spec_b_index(0);
570 Size spec_a = consensus_spec.size(), spec_b = exp[*sit].
size(), align_size = alignment.size();
571 for (
auto pit = exp[*sit].begin(); pit != exp[*sit].
end(); ++pit)
573 if (alignment.size() == 0 || alignment[align_index].second != spec_b_index)
576 consensus_spec.push_back(*pit);
582 Size copy_of_align_index(align_index);
584 while (alignment.size() > 0 &&
585 copy_of_align_index < alignment.size() &&
586 alignment[copy_of_align_index].second == spec_b_index)
588 ++copy_of_align_index;
592 while (alignment.size() > 0 &&
593 align_index < alignment.size() &&
594 alignment[align_index].second == spec_b_index)
596 consensus_spec[alignment[align_index].first].setIntensity(consensus_spec[alignment[align_index].first].getIntensity() +
597 (pit->getIntensity() / (
double)counter));
599 if (align_index == alignment.size())
604 align_size = align_size + 1 - counter;
609 if (spec_a + spec_b - align_size != consensus_spec.size())
611 LOG_WARN <<
"wrong number of features after merge. Expected: " << spec_a + spec_b - align_size <<
" got: " << consensus_spec.size() <<
"\n";
614 rt_average /= it->second.size() + 1;
615 consensus_spec.
setRT(rt_average);
621 precursor_mz_average /= precursor_count;
626 pcs[0].setMZ(precursor_mz_average);
630 if (consensus_spec.empty())
636 merged_spectra.addSpectrum(consensus_spec);
643 LOG_INFO <<
" size " << it->first <<
": " << it->second <<
"x\n";
647 sprintf(buffer,
"%d/%d (%.2f %%) of blocked spectra", (
int)count_peaks_aligned,
648 (
int)count_peaks_overall,
float(count_peaks_aligned) /
float(count_peaks_overall) * 100.);
654 for (
Size i = 0; i < exp.
size(); ++i)
656 if (merged_indices.count(i) == 0)
671 exp.
getSpectra().insert(exp.
end(), merged_spectra.begin(), merged_spectra.end());
695 template <
typename MapType>
700 double mz_binning_width(param_.getValue(
"mz_binning_width"));
701 String mz_binning_unit(param_.getValue(
"mz_binning_width_unit"));
703 unsigned progress = 0;
704 std::stringstream progress_message;
705 progress_message <<
"averaging profile spectra of MS level " << ms_level;
706 startProgress(0, spectra_to_average_over.size(), progress_message.str());
711 setProgress(++progress);
714 std::vector<double> mz_positions_all;
715 for (std::vector<std::pair<Size, double> >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
720 mz_positions_all.push_back(it_mz->getMZ());
724 sort(mz_positions_all.begin(), mz_positions_all.end());
726 std::vector<double> mz_positions;
727 std::vector<double> intensities;
728 double last_mz = std::numeric_limits<double>::min();
729 double delta_mz(mz_binning_width);
730 for (std::vector<double>::iterator it_mz = mz_positions_all.begin(); it_mz < mz_positions_all.end(); ++it_mz)
732 if (mz_binning_unit ==
"ppm")
734 delta_mz = mz_binning_width * (*it_mz) / 1000000;
737 if (((*it_mz) - last_mz) > delta_mz)
739 mz_positions.push_back(*it_mz);
740 intensities.push_back(0.0);
746 for (std::vector<std::pair<Size, double> >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
752 for (
Size i = 0; i < mz_positions.size(); ++i)
756 intensities[i] += nav.
eval(mz_positions[i]) * (it2->second);
763 average_spec.
clear(
false);
767 for (
Size i = 0; i < mz_positions.size(); ++i)
770 peak.
setMZ(mz_positions[i]);
772 average_spec.push_back(peak);
786 exp[it->first] = exp_tmp[n];
808 template <
typename MapType>
813 double mz_binning_width(param_.getValue(
"mz_binning_width"));
814 String mz_binning_unit(param_.getValue(
"mz_binning_width_unit"));
816 unsigned progress = 0;
818 std::stringstream progress_message;
819 progress_message <<
"averaging centroid spectra of MS level " << ms_level;
820 logger.
startProgress(0, spectra_to_average_over.size(), progress_message.str());
829 std::vector<std::pair<double, double> > mz_intensity_all;
830 for (std::vector<std::pair<Size, double> >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
835 std::pair<double, double> mz_intensity(it_mz->getMZ(), (it_mz->getIntensity() * it2->second));
836 mz_intensity_all.push_back(mz_intensity);
843 std::vector<double> mz_new;
844 std::vector<double> intensity_new;
845 double last_mz = std::numeric_limits<double>::min();
846 double delta_mz = mz_binning_width;
848 double sum_intensity(0);
850 for (std::vector<std::pair<double, double> >::const_iterator it_mz = mz_intensity_all.begin(); it_mz != mz_intensity_all.end(); ++it_mz)
852 if (mz_binning_unit ==
"ppm")
854 delta_mz = mz_binning_width * (it_mz->first) / 1000000;
857 if (((it_mz->first - last_mz) > delta_mz) && (count > 0))
859 mz_new.push_back(sum_mz / count);
860 intensity_new.push_back(sum_intensity);
865 last_mz = it_mz->first;
869 sum_mz += it_mz->first;
870 sum_intensity += it_mz->second;
875 mz_new.push_back(sum_mz / count);
876 intensity_new.push_back(sum_intensity);
881 average_spec.
clear(
false);
885 for (
Size i = 0; i < mz_new.size(); ++i)
888 peak.
setMZ(mz_new[i]);
890 average_spec.push_back(peak);
904 exp[it->first] = exp_tmp[n];
913 bool static compareByFirst(std::pair<double, double> i, std::pair<double, double> j)
915 return i.first < j.first;
Map< Size, std::vector< std::pair< Size, double > > > AverageBlocks
blocks of spectra (master-spectrum index to update to spectra to average over)
Definition: SpectraMerger.h:131
void setValue(const String &key, const DataValue &value, const String &description="", const StringList &tags=StringList())
Sets a value.
A more convenient string class.
Definition: String.h:58
Base::iterator Iterator
Definition: Map.h:80
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:202
#define LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:456
void sortByPosition()
Lexicographically sorts the peaks by their position.
void addSpectrum(const MSSpectrum &spectrum)
adds a spectrum to the list
Bundles analyzing tools for a clustering (given as sequence of BinaryTreeNode's)
Definition: ClusterAnalyzer.h:51
void endProgress() const
Ends the progress display.
SpectrumType
Spectrum peak type.
Definition: SpectrumSettings.h:70
static bool compareByFirst(std::pair< double, double > i, std::pair< double, double > j)
comparator for sorting peaks (m/z, intensity)
Definition: SpectraMerger.h:913
void mergeSpectra_(MapType &exp, const MergeBlocks &spectra_to_merge, const UInt ms_level)
merges blocks of spectra of a certain level
Definition: SpectraMerger.h:497
A two-dimensional distance matrix, similar to OpenMS::Matrix.
Definition: DistanceMatrix.h:67
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
SplineInterpolatedPeaks::Navigator getNavigator(double scaling=0.7)
returns an iterator for access of spline packages
profile data
Definition: SpectrumSettings.h:74
void mergeSpectraBlockWise(MapType &exp)
Definition: SpectraMerger.h:155
Iterator begin()
Definition: MSExperiment.h:157
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:58
Merges blocks of MS or MS2 spectra.
Definition: SpectraMerger.h:62
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:134
void sortSpectra(bool sort_mz=true)
Sorts the data points by retention time.
Base::const_iterator const_iterator
Definition: MSExperiment.h:125
Size size() const
Definition: MSExperiment.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
void setMZ(CoordinateType mz)
Mutable access to m/z.
Definition: Peak1D.h:119
void setIntensity(IntensityType intensity)
Mutable access to the data point intensity (height)
Definition: Peak1D.h:110
void setParameters(const Param ¶m)
Sets the parameters.
SpectraDistance_()
Definition: SpectraMerger.h:79
A basic LC-MS feature.
Definition: BaseFeature.h:55
#define LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged...
Definition: LogStream.h:452
Iterator end()
Definition: MSExperiment.h:167
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method...
Definition: SpectraMerger.h:87
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:214
CoordinateType getMZ() const
Returns the m/z coordinate (index 1)
Definition: Peak2D.h:196
double getPosMax() const
returns the maximum m/z (or RT) of the spectrum
void getSpectrumAlignment(std::vector< std::pair< Size, Size > > &alignment, const SpectrumType1 &s1, const SpectrumType2 &s2) const
Definition: SpectrumAlignment.h:86
double rt_max_
Definition: SpectraMerger.h:120
double operator()(const BaseFeature &first, const BaseFeature &second) const
Definition: SpectraMerger.h:102
double getSimilarity(const double d_rt, const double d_mz) const
Definition: SpectraMerger.h:95
double eval(double pos)
returns spline interpolated intensity at this position (fast access since we can start search from la...
void setProgress(SignedSize value) const
Sets the current progress.
A 1-dimensional raw data point or peak.
Definition: Peak1D.h:54
void average(MapType &exp, String average_type)
average over neighbouring spectra
Definition: SpectraMerger.h:313
void setMSLevel(UInt ms_level)
Sets the MS level.
Definition: SpectraMerger.h:75
Aligns the peaks of two sorted spectra Method 1: Using a banded (width via 'tolerance' parameter) ali...
Definition: SpectrumAlignment.h:65
Base::const_iterator ConstIterator
Definition: Map.h:81
void clear(bool clear_meta_data)
Clears all data and meta data.
void setRT(double rt)
Sets the absolute retention time (in seconds)
Management and storage of parameters / INI files.
Definition: Param.h:74
Map< Size, std::vector< Size > > MergeBlocks
blocks of spectra (master-spectrum index to sacrifice-spectra(the ones being merged into the master-s...
Definition: SpectraMerger.h:128
void averageCentroidSpectra_(MapType &exp, const AverageBlocks &spectra_to_average_over, const UInt ms_level)
average spectra (centroid mode)
Definition: SpectraMerger.h:809
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
CoordinateType getRT() const
Returns the RT coordinate (index 0)
Definition: Peak2D.h:208
SingleLinkage ClusterMethod.
Definition: SingleLinkage.h:57
double getPosMin() const
returns the minimum m/z (or RT) of the spectrum
void unify(const SpectrumSettings &rhs)
merge another spectrum setting into this one (data is usually appended, except for spectrum type whic...
const std::vector< Precursor > & getPrecursors() const
returns a const reference to the precursors
Data structure for spline interpolation of MS1 spectra and chromatograms.
Definition: SplineInterpolatedPeaks.h:61
Illegal self operation exception.
Definition: Exception.h:378
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:113
void setPrecursors(const std::vector< Precursor > &precursors)
sets the precursors
void startProgress(SignedSize begin, SignedSize end, const String &label) const
Initializes the progress display.
double mz_max_
Definition: SpectraMerger.h:121
iterator class for access of spline packages
Definition: SplineInterpolatedPeaks.h:111
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
void clear(bool clear_meta_data)
Clears all data and meta data.
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
void averageProfileSpectra_(MapType &exp, const AverageBlocks &spectra_to_average_over, const UInt ms_level)
average spectra (profile mode)
Definition: SpectraMerger.h:696
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
void cut(const Size cluster_quantity, const std::vector< BinaryTreeNode > &tree, std::vector< std::vector< Size > > &clusters)
Method to calculate a partition resulting from a certain step in clustering given by the number of cl...
void mergeSpectraPrecursors(MapType &exp)
merges spectra with similar precursors (must have MS2 level)
Definition: SpectraMerger.h:208
const std::vector< MSSpectrum > & getSpectra() const
returns the spectrum list
Hierarchical clustering with generic clustering functions.
Definition: ClusterHierarchical.h:63
centroid data or stick data
Definition: SpectrumSettings.h:73
int Int
Signed integer type.
Definition: Types.h:102
Map class based on the STL map (containing several convenience functions)
Definition: Map.h:50
void cluster(std::vector< Data > &data, const SimilarityComparator &comparator, const ClusterFunctor &clusterer, std::vector< BinaryTreeNode > &cluster_tree, DistanceMatrix< float > &original_distance)
Clustering function.
Definition: ClusterHierarchical.h:112