Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

ClusterHierarchical.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 //
00031 
00032 //  You should have received a copy of the GNU Lesser General Public
00033 //  License along with this library; if not, write to the Free Software
00034 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00035 //
00036 // --------------------------------------------------------------------------
00037 // $Maintainer: Mathias Walzer $
00038 // $Authors: $
00039 // --------------------------------------------------------------------------
00040 //
00041 #ifndef OPENMS_COMPARISON_CLUSTERING_CLUSTERHIERARCHICAL_H
00042 #define OPENMS_COMPARISON_CLUSTERING_CLUSTERHIERARCHICAL_H
00043 
00044 #include <OpenMS/KERNEL/StandardTypes.h>
00045 #include <OpenMS/DATASTRUCTURES/DistanceMatrix.h>
00046 #include <OpenMS/COMPARISON/CLUSTERING/ClusterFunctor.h>
00047 #include <OpenMS/COMPARISON/CLUSTERING/ClusterAnalyzer.h>
00048 #include <OpenMS/COMPARISON/SPECTRA/PeakSpectrumCompareFunctor.h>
00049 #include <OpenMS/COMPARISON/SPECTRA/BinnedSpectrum.h>
00050 #include <OpenMS/COMPARISON/SPECTRA/BinnedSpectrumCompareFunctor.h>
00051 #include <OpenMS/CONCEPT/Exception.h>
00052 
00053 #include <vector>
00054 
00055 namespace OpenMS
00056 {
00057 
00064   class OPENMS_DLLAPI ClusterHierarchical
00065   {
00066 private:
00067 
00069     double threshold_;
00070 
00071 public:
00073     ClusterHierarchical() :
00074       threshold_(1.0)
00075     {
00076     }
00077 
00079     ClusterHierarchical(const ClusterHierarchical & source) :
00080       threshold_(source.threshold_)
00081     {
00082     }
00083 
00085     virtual ~ClusterHierarchical()
00086     {
00087     }
00088 
00107     template <typename Data, typename SimilarityComparator>
00108     void cluster(std::vector<Data> & data, const SimilarityComparator & comparator, const ClusterFunctor & clusterer, std::vector<BinaryTreeNode> & cluster_tree, DistanceMatrix<Real> & original_distance)
00109     {
00110       if (original_distance.dimensionsize() != data.size())
00111       {
00112         //create distancematrix for data with comparator
00113         original_distance.clear();
00114         original_distance.resize(data.size(), 1);
00115         for (Size i = 0; i < data.size(); i++)
00116         {
00117           for (Size j = 0; j < i; j++)
00118           {
00119             //distance value is 1-similarity value, since similarity is in range of [0,1]
00120             original_distance.setValueQuick(i, j, 1 - comparator(data[i], data[j]));
00121           }
00122         }
00123       }
00124 
00125       //~ std::cout << "done" << std::endl; //maybe progress handler?
00126       // create clustering with ClusterMethod, DistanceMatrix and Data
00127       clusterer(original_distance, cluster_tree, threshold_);
00128     }
00129 
00146     void cluster(std::vector<PeakSpectrum> & data, const BinnedSpectrumCompareFunctor & comparator, double sz, UInt sp, const ClusterFunctor & clusterer, std::vector<BinaryTreeNode> & cluster_tree, DistanceMatrix<Real> & original_distance)
00147     {
00148 
00149       std::vector<BinnedSpectrum> binned_data;
00150       binned_data.reserve(data.size());
00151 
00152       //transform each PeakSpectrum to a corresponding BinnedSpectrum with given settings of size and spread
00153       for (Size i = 0; i < data.size(); i++)
00154       {
00155         //double sz(2), UInt sp(1);
00156         binned_data.push_back(BinnedSpectrum(sz, sp, data[i]));
00157       }
00158 
00159       //create distancematrix for data with comparator
00160       original_distance.clear();
00161       original_distance.resize(data.size(), 1);
00162 
00163       for (Size i = 0; i < binned_data.size(); i++)
00164       {
00165         for (Size j = 0; j < i; j++)
00166         {
00167           //distance value is 1-similarity value, since similarity is in range of [0,1]
00168           original_distance.setValue(i, j, 1 - comparator(binned_data[i], binned_data[j]));
00169         }
00170       }
00171 
00172       // create Clustering with ClusterMethod, DistanceMatrix and Data
00173       clusterer(original_distance, cluster_tree, threshold_);
00174     }
00175 
00177     double getThreshold()
00178     {
00179       return threshold_;
00180     }
00181 
00185     void setThreshold(double x)
00186     {
00187       threshold_ = x;
00188     }
00189 
00190   };
00191 
00197   class OPENMS_DLLAPI UnnormalizedComparator :
00198     public Exception::BaseException
00199   {
00200 public:
00201     UnnormalizedComparator(const char * file, int line, const char * function, const char * message
00202                              = "Clustering with unnormalized similarity measurement requested, normalized is mandatory") throw();
00203     virtual ~UnnormalizedComparator() throw();
00204   };
00205 
00206 }
00207 #endif //OPENMS_COMPARISON_CLUSTERING_CLUSTERHIERARCHICAL_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:37 using doxygen 1.7.1