From edd8b9d9392a99e0b1527507de3a4ca8fabfc1c6 Mon Sep 17 00:00:00 2001 From: pschloss Date: Wed, 9 Jun 2010 14:51:50 +0000 Subject: [PATCH] added weightedlinkage.cpp --- Mothur.xcodeproj/project.pbxproj | 2 ++ cluster.cpp | 4 +-- cluster.hpp | 13 ++++++++ clustercommand.cpp | 5 +-- makefile | 8 ++++- weightedlinkage.cpp | 56 ++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 weightedlinkage.cpp diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index ab546df..2f5722c 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXFileReference section */ + 7EC61A0911BEA6AF00F668D9 /* weightedlinkage.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = weightedlinkage.cpp; sourceTree = ""; }; A703FE931194645F002C397E /* makegroupcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = makegroupcommand.h; sourceTree = SOURCE_ROOT; }; A703FE941194645F002C397E /* makegroupcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = makegroupcommand.cpp; sourceTree = SOURCE_ROOT; }; A71D924211AEB42400D00CBC /* clustersplitcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clustersplitcommand.cpp; sourceTree = ""; }; @@ -640,6 +641,7 @@ A7DA2170113FECD400BF472F /* uvest.h */, A7DA2179113FECD400BF472F /* weighted.cpp */, A7DA217A113FECD400BF472F /* weighted.h */, + 7EC61A0911BEA6AF00F668D9 /* weightedlinkage.cpp */, A7DA217B113FECD400BF472F /* whittaker.cpp */, A7DA217C113FECD400BF472F /* whittaker.h */, ); diff --git a/cluster.cpp b/cluster.cpp index 96ca440..40537fe 100644 --- a/cluster.cpp +++ b/cluster.cpp @@ -235,7 +235,7 @@ void Cluster::update(double& cutOFF){ } } //if not merged it you need it for warning - if ((!merged) && (method == "average")) { + if ((!merged) && (method == "average" || method == "weighted")) { //m->mothurOut("Warning: trying to merge cell " + toString(rowCells[i]->row+1) + " " + toString(rowCells[i]->column+1) + " distance " + toString(rowCells[i]->dist) + " with value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); m->mothurOutEndLine(); if (cutOFF > rowCells[i]->dist) { cutOFF = rowCells[i]->dist; @@ -254,7 +254,7 @@ void Cluster::update(double& cutOFF){ // could be avoided for (int i=nColCells-1;i>=0;i--) { if (foundCol[i] == 0) { - if (method == "average") { + if (method == "average" || method == "weighted") { if (!((colCells[i]->row == smallRow) && (colCells[i]->column == smallCol))) { //m->mothurOut("Warning: merging cell " + toString(colCells[i]->row+1) + " " + toString(colCells[i]->column+1) + " distance " + toString(colCells[i]->dist) + " value above cutoff. Results may vary from using cutoff at cluster command instead of read.dist."); m->mothurOutEndLine(); if (cutOFF > colCells[i]->dist) { diff --git a/cluster.hpp b/cluster.hpp index 72d3611..aa517a0 100644 --- a/cluster.hpp +++ b/cluster.hpp @@ -94,4 +94,17 @@ private: /***********************************************************************/ +class WeightedLinkage : public Cluster { +public: + WeightedLinkage(RAbundVector*, ListVector*, SparseMatrix*, float, string); + bool updateDistance(MatData& colCell, MatData& rowCell); + string getTag(); + +private: + int saveRow; + int saveCol; +}; + +/***********************************************************************/ + #endif diff --git a/clustercommand.cpp b/clustercommand.cpp index 30c45e1..e9df82f 100644 --- a/clustercommand.cpp +++ b/clustercommand.cpp @@ -67,8 +67,8 @@ ClusterCommand::ClusterCommand(string option) { method = validParameter.validFile(parameters, "method", false); if (method == "not found") { method = "furthest"; } - if ((method == "furthest") || (method == "nearest") || (method == "average")) { } - else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest or average."); m->mothurOutEndLine(); abort = true; } + if ((method == "furthest") || (method == "nearest") || (method == "average") || (method == "weighted")) { } + else { m->mothurOut("Not a valid clustering method. Valid clustering algorithms are furthest, nearest, average, and weighted."); m->mothurOutEndLine(); abort = true; } showabund = validParameter.validFile(parameters, "showabund", false); if (showabund == "not found") { showabund = "T"; } @@ -91,6 +91,7 @@ ClusterCommand::ClusterCommand(string option) { if (method == "furthest") { cluster = new CompleteLinkage(rabund, list, matrix, cutoff, method); } else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff, method); } else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff, method); } + else if(method == "weighted"){ cluster = new WeightedLinkage(rabund, list, matrix, cutoff, method); } tag = cluster->getTag(); if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); } diff --git a/makefile b/makefile index 91a269c..8c33659 100644 --- a/makefile +++ b/makefile @@ -153,6 +153,7 @@ mothur : \ ./splitabundcommand.o\ ./splitmatrix.o\ ./clustersplitcommand.o\ + ./weightedlinkage.o\ ./inputdata.o\ ./jackknife.o\ ./kmer.o\ @@ -362,6 +363,7 @@ mothur : \ ./splitabundcommand.o\ ./splitmatrix.o\ ./clustersplitcommand.o\ + ./weightedlinkage.o\ ./inputdata.o\ ./jackknife.o\ ./kmer.o\ @@ -461,7 +463,7 @@ mothur : \ ./logsd.o\ ./geom.o\ ./setlogfilecommand.o\ - -o ../Release/mothur + -o mothur clean : rm \ @@ -574,6 +576,7 @@ clean : ./splitabundcommand.o\ ./splitmatrix.o\ ./clustersplitcommand.o\ + ./weightedlinkage.o\ ./inputdata.o\ ./jackknife.o\ ./kmer.o\ @@ -1705,5 +1708,8 @@ install : mothur ./classifyotucommand.o : classifyotucommand.cpp $(CC) $(CC_OPTIONS) classifyotucommand.cpp -c $(INCLUDE) -o ./classifyotucommand.o +# Item # 209 -- splitmatrix -- +./weightedlinkage.o : weightedlinkage.cpp + $(CC) $(CC_OPTIONS) weightedlinkage.cpp -c $(INCLUDE) -o ./weightedlinkage.o ##### END RUN #### diff --git a/weightedlinkage.cpp b/weightedlinkage.cpp new file mode 100644 index 0000000..0851bd7 --- /dev/null +++ b/weightedlinkage.cpp @@ -0,0 +1,56 @@ +#ifndef WEIGHTEDLINKAGE_H +#define WEIGHTEDLINKAGE_H + + +#include "mothur.h" +#include "cluster.hpp" +#include "rabundvector.hpp" +#include "sparsematrix.hpp" + +/* This class implements the WPGMA, weighted average neighbor clustering algorithm */ + +/***********************************************************************/ + +WeightedLinkage::WeightedLinkage(RAbundVector* rav, ListVector* lv, SparseMatrix* dm, float c, string s) : + Cluster(rav, lv, dm, c, s) +{ + saveRow = -1; + saveCol = -1; +} + + +/***********************************************************************/ +//This function returns the tag of the method. +string WeightedLinkage::getTag() { + return("wn"); +} + + +/***********************************************************************/ +//This function updates the distance based on the average linkage method. +bool WeightedLinkage::updateDistance(MatData& colCell, MatData& rowCell) { + try { + if ((saveRow != smallRow) || (saveCol != smallCol)) { +// rowBin = rabund->get(smallRow); +// colBin = rabund->get(smallCol); +// totalBin = rowBin + colBin; + saveRow = smallRow; + saveCol = smallCol; + } + + colCell->dist = (colCell->dist + rowCell->dist) / 2.0; + + return(true); + } + catch(exception& e) { + m->errorOut(e, "WeightedLinkage", "updateDistance"); + exit(1); + } +} + +/***********************************************************************/ + + +/***********************************************************************/ + +#endif -- 2.39.2