OpenMS
ClusterHierarchical.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 //
5 
6 // You should have received a copy of the GNU Lesser General Public
7 // License along with this library; if not, write to the Free Software
8 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9 //
10 // --------------------------------------------------------------------------
11 // $Maintainer: Mathias Walzer $
12 // $Authors: $
13 // --------------------------------------------------------------------------
14 //
15 #pragma once
16 
25 
26 #include <vector>
27 
28 namespace OpenMS
29 {
30 
37  class OPENMS_DLLAPI ClusterHierarchical
38  {
39 private:
40 
42  double threshold_;
43 
44 public:
47  threshold_(1.0)
48  {
49  }
50 
53  threshold_(source.threshold_)
54  {
55  }
56 
59  {
60  }
61 
85  template <typename Data, typename SimilarityComparator>
86  void cluster(std::vector<Data> & data,
87  const SimilarityComparator & comparator,
88  const ClusterFunctor & clusterer,
89  std::vector<BinaryTreeNode> & cluster_tree,
90  DistanceMatrix<float> & original_distance)
91  {
92  if (original_distance.dimensionsize() != data.size())
93  {
94  // create distance matrix for data using comparator
95  original_distance.clear();
96  original_distance.resize(data.size(), 1);
97  for (Size i = 0; i < data.size(); i++)
98  {
99  for (Size j = 0; j < i; j++)
100  {
101  // distance value is 1-similarity value, since similarity is in range of [0,1]
102  original_distance.setValueQuick(i, j, 1 - comparator(data[i], data[j]));
103  }
104  }
105  }
106 
107  // create clustering with ClusterMethod, DistanceMatrix and Data
108  clusterer(original_distance, cluster_tree, threshold_);
109  }
110 
128  void cluster(std::vector<PeakSpectrum> & data,
129  const BinnedSpectrumCompareFunctor & comparator,
130  double sz,
131  UInt sp,
132  float offset,
133  const ClusterFunctor & clusterer,
134  std::vector<BinaryTreeNode> & cluster_tree,
135  DistanceMatrix<float> & original_distance) const
136  {
137  std::vector<BinnedSpectrum> binned_data;
138  binned_data.reserve(data.size());
139 
140  //transform each PeakSpectrum to a corresponding BinnedSpectrum with given settings of size and spread
141  for (Size i = 0; i < data.size(); i++)
142  {
143  //double sz(2), UInt sp(1);
144  binned_data.emplace_back(data[i], sz, false, sp, offset);
145  }
146 
147  //create distancematrix for data with comparator
148  original_distance.clear();
149  original_distance.resize(data.size(), 1);
150 
151  for (Size i = 0; i < binned_data.size(); i++)
152  {
153  for (Size j = 0; j < i; j++)
154  {
155  //distance value is 1-similarity value, since similarity is in range of [0,1]
156  original_distance.setValue(i, j, 1 - comparator(binned_data[i], binned_data[j]));
157  }
158  }
159 
160  // create Clustering with ClusterMethod, DistanceMatrix and Data
161  clusterer(original_distance, cluster_tree, threshold_);
162  }
163 
165  double getThreshold() const
166  {
167  return threshold_;
168  }
169 
173  void setThreshold(double x)
174  {
175  threshold_ = x;
176  }
177 
178  };
179 
185  class OPENMS_DLLAPI UnnormalizedComparator :
187  {
188 public:
189  UnnormalizedComparator(const char * file, int line, const char * function, const char * message
190  = "Clustering with unnormalized similarity measurement requested, normalized is mandatory") throw();
191  ~UnnormalizedComparator() throw() override;
192  };
193 
194 }
195 
Base class for compare functors of BinnedSpectra.
Definition: BinnedSpectrumCompareFunctor.h:32
Base class for cluster functors.
Definition: ClusterFunctor.h:28
Hierarchical clustering with generic clustering functions.
Definition: ClusterHierarchical.h:38
void setThreshold(double x)
Definition: ClusterHierarchical.h:173
virtual ~ClusterHierarchical()
destructor
Definition: ClusterHierarchical.h:58
double getThreshold() const
get the threshold
Definition: ClusterHierarchical.h:165
ClusterHierarchical(const ClusterHierarchical &source)
copy constructor
Definition: ClusterHierarchical.h:52
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:86
double threshold_
the threshold given to the ClusterFunctor
Definition: ClusterHierarchical.h:42
ClusterHierarchical()
default constructor
Definition: ClusterHierarchical.h:46
A two-dimensional distance matrix, similar to OpenMS::Matrix.
Definition: DistanceMatrix.h:42
SizeType dimensionsize() const
gives the number of rows (i.e. number of columns)
Definition: DistanceMatrix.h:392
void setValue(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:237
void setValueQuick(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:283
void clear()
reset all
Definition: DistanceMatrix.h:301
void resize(SizeType dimensionsize, Value value=Value())
resizing the container
Definition: DistanceMatrix.h:323
Exception base class.
Definition: Exception.h:65
Exception thrown if clustering is attempted without a normalized compare functor.
Definition: ClusterHierarchical.h:187
UnnormalizedComparator(const char *file, int line, const char *function, const char *message="Clustering with unnormalized similarity measurement requested, normalized is mandatory")
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
void cluster(std::vector< PeakSpectrum > &data, const BinnedSpectrumCompareFunctor &comparator, double sz, UInt sp, float offset, const ClusterFunctor &clusterer, std::vector< BinaryTreeNode > &cluster_tree, DistanceMatrix< float > &original_distance) const
clustering function for binned PeakSpectrum
Definition: ClusterHierarchical.h:128
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22