OpenMS
ClusterHierarchical.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, 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 #include <vector>
26 
27 namespace OpenMS
28 {
29 
36 class OPENMS_DLLAPI ClusterHierarchical
37 {
38 private:
40  double threshold_;
41 
42 public:
44  ClusterHierarchical(): threshold_(1.0)
45  {
46  }
47 
49  ClusterHierarchical(const ClusterHierarchical& source): threshold_(source.threshold_)
50  {
51  }
52 
55  {
56  }
57 
83  template<typename Data, typename SimilarityComparator>
84  void cluster(std::vector<Data>& data,
85  const SimilarityComparator& comparator,
86  const ClusterFunctor& clusterer,
87  std::vector<BinaryTreeNode>& cluster_tree,
88  DistanceMatrix<float>& original_distance)
89  {
90  if (original_distance.dimensionsize() != data.size())
91  {
92  // create distance matrix for data using comparator
93  original_distance.clear();
94  original_distance.resize(data.size(), 1);
95  for (Size i = 0; i < data.size(); i++)
96  {
97  for (Size j = 0; j < i; j++)
98  {
99  // distance value is 1-similarity value, since similarity is in range of [0,1]
100  original_distance.setValueQuick(i, j, 1 - comparator(data[i], data[j]));
101  }
102  }
103  }
104 
105  // create clustering with ClusterMethod, DistanceMatrix and Data
106  clusterer(original_distance, cluster_tree, threshold_);
107  }
108 
129  void cluster(std::vector<PeakSpectrum>& data,
130  const BinnedSpectrumCompareFunctor& comparator,
131  double sz,
132  UInt sp,
133  float offset,
134  const ClusterFunctor& clusterer,
135  std::vector<BinaryTreeNode>& cluster_tree,
136  DistanceMatrix<float>& original_distance) const
137  {
138  std::vector<BinnedSpectrum> binned_data;
139  binned_data.reserve(data.size());
140 
141  // transform each PeakSpectrum to a corresponding BinnedSpectrum with given settings of size and spread
142  for (Size i = 0; i < data.size(); i++)
143  {
144  // double sz(2), UInt sp(1);
145  binned_data.emplace_back(data[i], sz, false, sp, offset);
146  }
147 
148  // create distancematrix for data with comparator
149  original_distance.clear();
150  original_distance.resize(data.size(), 1);
151 
152  for (Size i = 0; i < binned_data.size(); i++)
153  {
154  for (Size j = 0; j < i; j++)
155  {
156  // distance value is 1-similarity value, since similarity is in range of [0,1]
157  original_distance.setValue(i, j, 1 - comparator(binned_data[i], binned_data[j]));
158  }
159  }
160 
161  // create Clustering with ClusterMethod, DistanceMatrix and Data
162  clusterer(original_distance, cluster_tree, threshold_);
163  }
164 
166  double getThreshold() const
167  {
168  return threshold_;
169  }
170 
174  void setThreshold(double x)
175  {
176  threshold_ = x;
177  }
178 };
179 
186 {
187 public:
188  UnnormalizedComparator(const char* file,
189  int line,
190  const char* function,
191  const char* message = "Clustering with unnormalized similarity measurement requested, normalized is mandatory") throw();
192  ~UnnormalizedComparator() throw() override;
193 };
194 
195 } // namespace OpenMS
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:37
void setThreshold(double x)
Definition: ClusterHierarchical.h:174
virtual ~ClusterHierarchical()
destructor
Definition: ClusterHierarchical.h:54
double getThreshold() const
get the threshold
Definition: ClusterHierarchical.h:166
ClusterHierarchical(const ClusterHierarchical &source)
copy constructor
Definition: ClusterHierarchical.h:49
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:84
double threshold_
the threshold given to the ClusterFunctor
Definition: ClusterHierarchical.h:40
ClusterHierarchical()
default constructor
Definition: ClusterHierarchical.h:44
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:360
void setValue(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:222
void setValueQuick(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:259
void clear()
reset all
Definition: DistanceMatrix.h:271
void resize(SizeType dimensionsize, Value value=Value())
resizing the container
Definition: DistanceMatrix.h:293
Exception base class.
Definition: Exception.h:63
Exception thrown if clustering is attempted without a normalized compare functor.
Definition: ClusterHierarchical.h:186
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:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
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:129
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19