Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 
153  void cluster(std::vector<PeakSpectrum> & data, const BinnedSpectrumCompareFunctor & comparator, double sz, UInt sp, const ClusterFunctor & clusterer, std::vector<BinaryTreeNode> & cluster_tree, DistanceMatrix<float> & original_distance)
154  {
155 
156  std::vector<BinnedSpectrum> binned_data;
157  binned_data.reserve(data.size());
158 
159  //transform each PeakSpectrum to a corresponding BinnedSpectrum with given settings of size and spread
160  for (Size i = 0; i < data.size(); i++)
161  {
162  //double sz(2), UInt sp(1);
163  binned_data.push_back(BinnedSpectrum(data[i], sz, false, sp));
164  }
165 
166  //create distancematrix for data with comparator
167  original_distance.clear();
168  original_distance.resize(data.size(), 1);
169 
170  for (Size i = 0; i < binned_data.size(); i++)
171  {
172  for (Size j = 0; j < i; j++)
173  {
174  //distance value is 1-similarity value, since similarity is in range of [0,1]
175  original_distance.setValue(i, j, 1 - comparator(binned_data[i], binned_data[j]));
176  }
177  }
178 
179  // create Clustering with ClusterMethod, DistanceMatrix and Data
180  clusterer(original_distance, cluster_tree, threshold_);
181  }
182 
184  double getThreshold()
185  {
186  return threshold_;
187  }
188 
192  void setThreshold(double x)
193  {
194  threshold_ = x;
195  }
196 
197  };
198 
204  class OPENMS_DLLAPI UnnormalizedComparator :
206  {
207 public:
208  UnnormalizedComparator(const char * file, int line, const char * function, const char * message
209  = "Clustering with unnormalized similarity measurement requested, normalized is mandatory") throw();
210  ~UnnormalizedComparator() throw() override;
211  };
212 
213 }
214 
Base class for compare functors of BinnedSpectra.
Definition: BinnedSpectrumCompareFunctor.h:56
virtual ~ClusterHierarchical()
destructor
Definition: ClusterHierarchical.h:84
void cluster(std::vector< PeakSpectrum > &data, const BinnedSpectrumCompareFunctor &comparator, double sz, UInt sp, const ClusterFunctor &clusterer, std::vector< BinaryTreeNode > &cluster_tree, DistanceMatrix< float > &original_distance)
clustering function for binned PeakSpectrum
Definition: ClusterHierarchical.h:153
void resize(SizeType dimensionsize, Value value=Value())
resizing the container
Definition: DistanceMatrix.h:349
A two-dimensional distance matrix, similar to OpenMS::Matrix.
Definition: DistanceMatrix.h:67
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
double getThreshold()
get the threshold
Definition: ClusterHierarchical.h:184
Exception thrown if clustering is attempted without a normalized compare functor. ...
Definition: ClusterHierarchical.h:204
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
void clear()
reset all
Definition: DistanceMatrix.h:327
void setValueQuick(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:309
void setThreshold(double x)
Definition: ClusterHierarchical.h:192
ClusterHierarchical(const ClusterHierarchical &source)
copy constructor
Definition: ClusterHierarchical.h:78
ClusterHierarchical()
default constructor
Definition: ClusterHierarchical.h:72
double threshold_
the threshold given to the ClusterFunctor
Definition: ClusterHierarchical.h:68
Exception base class.
Definition: Exception.h:89
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:75
void setValue(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:263
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
SizeType dimensionsize() const
gives the number of rows (i.e. number of columns)
Definition: DistanceMatrix.h:418
Hierarchical clustering with generic clustering functions.
Definition: ClusterHierarchical.h:63
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
Base class for cluster functors.
Definition: ClusterFunctor.h:53

OpenMS / TOPP release 2.3.0 Documentation generated on Wed Apr 18 2018 19:29:04 using doxygen 1.8.14