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 #ifndef OPENMS_COMPARISON_CLUSTERING_CLUSTERHIERARCHICAL_H
42 #define OPENMS_COMPARISON_CLUSTERING_CLUSTERHIERARCHICAL_H
43 
52 
53 #include <vector>
54 
55 namespace OpenMS
56 {
57 
64  class OPENMS_DLLAPI ClusterHierarchical
65  {
66 private:
67 
69  double threshold_;
70 
71 public:
74  threshold_(1.0)
75  {
76  }
77 
80  threshold_(source.threshold_)
81  {
82  }
83 
86  {
87  }
88 
107  template <typename Data, typename SimilarityComparator>
108  void cluster(std::vector<Data> & data, const SimilarityComparator & comparator, const ClusterFunctor & clusterer, std::vector<BinaryTreeNode> & cluster_tree, DistanceMatrix<float> & original_distance)
109  {
110  if (original_distance.dimensionsize() != data.size())
111  {
112  //create distancematrix for data with comparator
113  original_distance.clear();
114  original_distance.resize(data.size(), 1);
115  for (Size i = 0; i < data.size(); i++)
116  {
117  for (Size j = 0; j < i; j++)
118  {
119  //distance value is 1-similarity value, since similarity is in range of [0,1]
120  original_distance.setValueQuick(i, j, 1 - comparator(data[i], data[j]));
121  }
122  }
123  }
124 
125  //~ std::cout << "done" << std::endl; //maybe progress handler?
126  // create clustering with ClusterMethod, DistanceMatrix and Data
127  clusterer(original_distance, cluster_tree, threshold_);
128  }
129 
146  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)
147  {
148 
149  std::vector<BinnedSpectrum> binned_data;
150  binned_data.reserve(data.size());
151 
152  //transform each PeakSpectrum to a corresponding BinnedSpectrum with given settings of size and spread
153  for (Size i = 0; i < data.size(); i++)
154  {
155  //double sz(2), UInt sp(1);
156  binned_data.push_back(BinnedSpectrum(sz, sp, data[i]));
157  }
158 
159  //create distancematrix for data with comparator
160  original_distance.clear();
161  original_distance.resize(data.size(), 1);
162 
163  for (Size i = 0; i < binned_data.size(); i++)
164  {
165  for (Size j = 0; j < i; j++)
166  {
167  //distance value is 1-similarity value, since similarity is in range of [0,1]
168  original_distance.setValue(i, j, 1 - comparator(binned_data[i], binned_data[j]));
169  }
170  }
171 
172  // create Clustering with ClusterMethod, DistanceMatrix and Data
173  clusterer(original_distance, cluster_tree, threshold_);
174  }
175 
177  double getThreshold()
178  {
179  return threshold_;
180  }
181 
185  void setThreshold(double x)
186  {
187  threshold_ = x;
188  }
189 
190  };
191 
197  class OPENMS_DLLAPI UnnormalizedComparator :
199  {
200 public:
201  UnnormalizedComparator(const char * file, int line, const char * function, const char * message
202  = "Clustering with unnormalized similarity measurement requested, normalized is mandatory") throw();
203  virtual ~UnnormalizedComparator() throw();
204  };
205 
206 }
207 #endif //OPENMS_COMPARISON_CLUSTERING_CLUSTERHIERARCHICAL_H
Base class for compare functors of BinnedSpectra.
Definition: BinnedSpectrumCompareFunctor.h:57
virtual ~ClusterHierarchical()
destructor
Definition: ClusterHierarchical.h:85
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:146
void resize(SizeType dimensionsize, Value value=Value())
resizing the container
Definition: DistanceMatrix.h:350
A two-dimensional distance matrix, similar to OpenMS::Matrix.
Definition: DistanceMatrix.h:68
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
double getThreshold()
get the threshold
Definition: ClusterHierarchical.h:177
Exception thrown if clustering is attempted without a normalized compare functor. ...
Definition: ClusterHierarchical.h:197
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void clear()
reset all
Definition: DistanceMatrix.h:328
void setValueQuick(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:310
void setThreshold(double x)
Definition: ClusterHierarchical.h:185
ClusterHierarchical(const ClusterHierarchical &source)
copy constructor
Definition: ClusterHierarchical.h:79
ClusterHierarchical()
default constructor
Definition: ClusterHierarchical.h:73
double threshold_
the threshold given to the ClusterFunctor
Definition: ClusterHierarchical.h:69
Exception base class.
Definition: Exception.h:90
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:65
void setValue(SizeType i, SizeType j, ValueType value)
sets a value at a given position:
Definition: DistanceMatrix.h:264
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
SizeType dimensionsize() const
gives the number of rows (i.e. number of columns)
Definition: DistanceMatrix.h:419
Hierarchical clustering with generic clustering functions.
Definition: ClusterHierarchical.h:64
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:108
Base class for cluster functors.
Definition: ClusterFunctor.h:54

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:21:59 using doxygen 1.8.13