Example #1
0
        /// <summary>
        /// Construct clusters using different update methods
        /// </summary>
        /// <param name="distanceMatrix">IDistanceMatrix</param>
        /// <param name="updateDistanceMethodName">enum EUpdateDistanceMethods</param>
        public HierarchicalClustering(IDistanceMatrix distanceMatrix, UpdateDistanceMethodsTypes updateDistanceMethodName)
        {
            if (distanceMatrix.Dimension <= 0)
            {
                throw new Exception("Invalid distance matrix dimension");
            }

            try
            {
                // The number of nodes in the final tree is 2N-2:
                // N sequence nodes (leaves) and N-2 internal nodes
                // where N is the number of input sequences
                _nodes = new List <BinaryGuideTreeNode>(distanceMatrix.Dimension * 2 - 1);
                _edges = new List <BinaryGuideTreeEdge>(distanceMatrix.Dimension * 2 - 2);

                // The number of clusters is the number of leaves at the beginning
                // As the algorithm merges clusters, only one cluster remains.
                _clusters = new List <int>(distanceMatrix.Dimension);

                // Construct _indexToCluster
                _indexToCluster = new int[distanceMatrix.Dimension];
                for (int i = 0; i < distanceMatrix.Dimension; ++i)
                {
                    _indexToCluster[i] = i;
                }
            }
            catch (OutOfMemoryException ex)
            {
                throw new Exception("Out of memory", ex.InnerException);
            }

            // Choose a update-distance method
            switch (updateDistanceMethodName)
            {
            case (UpdateDistanceMethodsTypes.Average):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateAverage);
                break;

            case (UpdateDistanceMethodsTypes.Single):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateSingle);
                break;

            case (UpdateDistanceMethodsTypes.Complete):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateComplete);
                break;

            case (UpdateDistanceMethodsTypes.WeightedMAFFT):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateWeightedMAFFT);
                break;

            default:
                throw new Exception("invalid update method");
            }
        }
Example #2
0
        /// <summary>
        /// Construct clusters using different update methods
        /// </summary>
        /// <param name="distanceMatrix">IDistanceMatrix</param>
        /// <param name="updateDistanceMethodName">enum EUpdateDistanceMethods</param>
        public HierarchicalClusteringParallel(IDistanceMatrix distanceMatrix, UpdateDistanceMethodsTypes updateDistanceMethodName)
        {
            if (distanceMatrix.Dimension <= 0)
            {
                throw new Exception("Invalid distance matrix dimension");
            }

            // The number of nodes in the final tree is 2N-2:
            // N sequence nodes (leaves) and N-2 internal nodes
            // where N is the number of input sequences
            _nodes = new List <BinaryGuideTreeNode>(distanceMatrix.Dimension * 2 - 2);
            _edges = new List <BinaryGuideTreeEdge>();

            // The number of clusters is the number of leaves at the beginning
            // As the algorithm merges clusters, only one cluster remains.
            _clusters = new List <int>(distanceMatrix.Dimension);

            // Choose a update-distance method
            switch (updateDistanceMethodName)
            {
            case (UpdateDistanceMethodsTypes.Aaverage):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateAverage);
                break;

            case (UpdateDistanceMethodsTypes.Single):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateSingle);
                break;

            case (UpdateDistanceMethodsTypes.Complete):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateComplete);
                break;

            case (UpdateDistanceMethodsTypes.WeightedMAFFT):
                _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateWeightedMAFFT);
                break;

            default:
                throw new Exception("invalid update method");
            }

            // Initialize the clusters
            Initialize(distanceMatrix);

            // Clustering...
            while (_numberOfClusters > 1)
            {
                GetNextPairOfCluster(distanceMatrix);
                CreateCluster();
                UpdateDistance(distanceMatrix);
                UpdateClusters();
            }
        }
Example #3
0
        /// <summary>
        /// Construct clusters using different update methods
        /// </summary>
        /// <param name="distanceMatrix">IDistanceMatrix</param>
        /// <param name="updateDistanceMethodName">enum EUpdateDistanceMethods</param>
        public HierarchicalClustering(IDistanceMatrix distanceMatrix, UpdateDistanceMethodsTypes updateDistanceMethodName)
        {
            if (distanceMatrix.Dimension <= 0)
            {
                throw new Exception("Invalid distance matrix dimension");
            }

            try
            {
                // The number of nodes in the final tree is 2N-2:
                // N sequence nodes (leaves) and N-2 internal nodes
                // where N is the number of input sequences
                _nodes = new List<BinaryGuideTreeNode>(distanceMatrix.Dimension * 2 - 1);
                _edges = new List<BinaryGuideTreeEdge>(distanceMatrix.Dimension * 2 - 2);

                // The number of clusters is the number of leaves at the beginning
                // As the algorithm merges clusters, only one cluster remains.
                _clusters = new List<int>(distanceMatrix.Dimension);
                
                // Construct _indexToCluster
                _indexToCluster = new int[distanceMatrix.Dimension];
                for (int i = 0; i < distanceMatrix.Dimension; ++i)
                {
                    _indexToCluster[i] = i;
                }
            }
            catch (OutOfMemoryException ex)
            {
                throw new Exception("Out of memory", ex.InnerException);
            }

            // Choose a update-distance method
            switch(updateDistanceMethodName)
            {
                case(UpdateDistanceMethodsTypes.Average):
                    _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateAverage);
                    break;
                case(UpdateDistanceMethodsTypes.Single):
                    _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateSingle);
                    break;
                case(UpdateDistanceMethodsTypes.Complete):
                    _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateComplete);
                    break;
                case(UpdateDistanceMethodsTypes.WeightedMAFFT):
                    _updateDistanceMethod = new UpdateDistanceMethodSelector(UpdateWeightedMAFFT);
                    break;
                default:
                    throw new Exception("invalid update method");
            }
        }