OpenMS  2.7.0
ClusterHierarchical.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-2021.
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 
32 // You should have received a copy of the GNU Lesser General Public
33 // License along with this library; if not, write to the Free Software
34 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 //
36 // --------------------------------------------------------------------------
37 // $Maintainer: Mathias Walzer $
38 // $Authors: $
39 // --------------------------------------------------------------------------
40 //
41 #pragma once
42 
51 
52 #include <vector>
53 
54 namespace OpenMS
55 {
56 
63  class OPENMS_DLLAPI ClusterHierarchical
64  {
65 private:
66 
68  double threshold_;
69 
70 public:
73  threshold_(1.0)
74  {
75  }
76 
79  threshold_(source.threshold_)
80  {
81  }
82 
85  {
86  }
87 
111  template <typename Data, typename SimilarityComparator>
112  void cluster(std::vector<Data> & data,
113  const SimilarityComparator & comparator,
114  const ClusterFunctor & clusterer,
115  std::vector<BinaryTreeNode> & cluster_tree,
116  DistanceMatrix<float> & original_distance)
117  {
118  if (original_distance.dimensionsize() != data.size())
119  {
120  // create distance matrix for data using comparator
121  original_distance.clear();
122  original_distance.resize(data.size(), 1);
123  for (Size i = 0; i < data.size(); i++)
124  {
125  for (Size j = 0; j < i; j++)
126  {
127  // distance value is 1-similarity value, since similarity is in range of [0,1]
128  original_distance.setValueQuick(i, j, 1 - comparator(data[i], data[j]));
129  }
130  }
131  }
132 
133  // create clustering with ClusterMethod, DistanceMatrix and Data
134  clusterer(original_distance, cluster_tree, threshold_);
135  }
136 
154  void cluster(std::vector<PeakSpectrum> & data,
155  const BinnedSpectrumCompareFunctor & comparator,
156  double sz,
157  UInt sp,
158  float offset,
159  const ClusterFunctor & clusterer,
160  std::vector<BinaryTreeNode> & cluster_tree,
161  DistanceMatrix<float> & original_distance)
162  {
163  std::vector<BinnedSpectrum> binned_data;
164  binned_data.reserve(data.size());
165 
166  //transform each PeakSpectrum to a corresponding BinnedSpectrum with given settings of size and spread
167  for (Size i = 0; i < data.size(); i++)
168  {
169  //double sz(2), UInt sp(1);
170  binned_data.push_back(BinnedSpectrum(data[i], sz, false, sp, offset));
171  }
172 
173  //create distancematrix for data with comparator
174  original_distance.clear();
175  original_distance.resize(data.size(), 1);
176 
177  for (Size i = 0; i < binned_data.size(); i++)
178  {
179  for (Size j = 0; j < i; j++)
180  {
181  //distance value is 1-similarity value, since similarity is in range of [0,1]
182  original_distance.setValue(i, j, 1 - comparator(binned_data[i], binned_data[j]));
183  }
184  }
185 
186  // create Clustering with ClusterMethod, DistanceMatrix and Data
187  clusterer(original_distance, cluster_tree, threshold_);
188  }
189 
191  double getThreshold()
192  {
193  return threshold_;
194  }
195 
199  void setThreshold(double x)
200  {
201  threshold_ = x;
202  }
203 
204  };
205 
211  class OPENMS_DLLAPI UnnormalizedComparator :
213  {
214 public:
215  UnnormalizedComparator(const char * file, int line, const char * function, const char * message
216  = "Clustering with unnormalized similarity measurement requested, normalized is mandatory") throw();
217  ~UnnormalizedComparator() throw() override;
218  };
219 
220 }
221 
Base class for compare functors of BinnedSpectra.
Definition: BinnedSpectrumCompareFunctor.h:58
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:76
Base class for cluster functors.
Definition: ClusterFunctor.h:54
Hierarchical clustering with generic clustering functions.
Definition: ClusterHierarchical.h:64
void setThreshold(double x)
Definition: ClusterHierarchical.h:199
virtual ~ClusterHierarchical()
destructor
Definition: ClusterHierarchical.h:84
double getThreshold()
get the threshold
Definition: ClusterHierarchical.h:191
ClusterHierarchical(const ClusterHierarchical &source)
copy constructor
Definition: ClusterHierarchical.h:78
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
double threshold_
the threshold given to the ClusterFunctor
Definition: ClusterHierarchical.h:68
ClusterHierarchical()
default constructor
Definition: ClusterHierarchical.h:72
A two-dimensional distance matrix, similar to OpenMS::Matrix.
Definition: DistanceMatrix.h:68
SizeType dimensionsize() const
gives the number of rows (i.e. number of columns)
Definition: DistanceMatrix.h:418
void setValue(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:263
void setValueQuick(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:309
void clear()
reset all
Definition: DistanceMatrix.h:327
void resize(SizeType dimensionsize, Value value=Value())
resizing the container
Definition: DistanceMatrix.h:349
Exception base class.
Definition: Exception.h:92
Exception thrown if clustering is attempted without a normalized compare functor.
Definition: ClusterHierarchical.h:213
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:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
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)
clustering function for binned PeakSpectrum
Definition: ClusterHierarchical.h:154
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47