public void TestUpgma() { //decimal[,] distanceMatrix1 = //{ // { 0, 0, 0, 0, 0 }, // { 0, 0, 1, 1, 1 }, // { 0, 1, 0, 2, 2 }, // { 0, 1, 2, 0, 3 }, // { 0, 1, 2, 3, 0 }, //}; //var result = UpgmaClustering.Upgma(distanceMatrix1); decimal[,] distanceMatrix2 = { { 0, 19, 27, 8, 33, 18, 13 }, { 19, 0, 31, 18, 36, 1, 13 }, { 27, 31, 0, 26, 41, 32, 29 }, { 8, 18, 26, 0, 31, 17, 14 }, { 33, 36, 41, 31, 0, 35, 28 }, { 18, 1, 32, 17, 35, 0, 12 }, { 13, 13, 29, 14, 28, 12, 0 } }; var names = new List <string>(); for (var i = 0; i < distanceMatrix2.GetLength(0); i++) { names.Add("" + i); } List <string> vectorNames = new List <string>(); var maxVectorLength = distanceMatrix2.GetLength(0) > distanceMatrix2.GetLength(1) ? distanceMatrix2.GetLength(0) : distanceMatrix2.GetLength(1); for (var i = 0; i < maxVectorLength; i++) { vectorNames.Add(SpreadsheetFileHandler.AlphabetLetterRollOver(i) + i); } List <List <UpgmaNode> > nodeList; List <List <string> > treeList; var minimumOutputTreeLeafs = 1; List <string> finalTreeLeafOrderList; UpgmaClustering.Upgma(distanceMatrix2, vectorNames, minimumOutputTreeLeafs, out nodeList, out treeList, out finalTreeLeafOrderList); CheckUpgmaDistanceConsistency(nodeList[nodeList.Count - 1]); List <string> treeLeafOrderList; var tree = Newick.NewickTreeFormat(nodeList[nodeList.Count - 1].ToList <GenericNode>(), names, out treeLeafOrderList, minimumOutputTreeLeafs); Console.WriteLine(tree); }
public void TestUpgmaLowestDistanceIndexes() { decimal[,] distanceMatrix1 = { { 0, 1, 2, 3, 4 }, { 0, 1, 2, 3, 4 }, { 0, 1, 2, 3, 4 }, { 0, 1, 2, 3, 4 }, { 0, 1, 2, 3, 4 }, }; decimal[,] distanceMatrix2 = { { 10, 1, 2, 3, 4 }, { 10, 1, 2, 3, 4 }, { 10, 1, 2, 0, 4 }, { 10, 1, 2, 3, 4 }, { 10, 1, 2, 3, 4 }, }; decimal[,] distanceMatrix3 = { { 10, 1, 2, 3, 4 }, { 10, 1, 2, 3, 4 }, { 10, 1, 2, 3, 4 }, { 10, 1, 2, 3, 0 }, { 10, 1, 2, 0, 0 }, }; var result1 = UpgmaClustering.UpgmaLowestDistanceIndexes(ConvertTypes.Decimal2DArrayToDecimalListList(distanceMatrix1)); var result2 = UpgmaClustering.UpgmaLowestDistanceIndexes(ConvertTypes.Decimal2DArrayToDecimalListList(distanceMatrix2)); var result3 = UpgmaClustering.UpgmaLowestDistanceIndexes(ConvertTypes.Decimal2DArrayToDecimalListList(distanceMatrix3)); Assert.IsTrue(result1.X == 0 && result1.Y == 1); Assert.IsTrue(result2.X == 2 && result2.Y == 3); Assert.IsTrue(result3.X == 3 && result3.Y == 4); }
public void TestUpgmaDistanceMatrixNextIteration() { List <List <decimal> > distanceMatrix1 = new List <List <decimal> >() { new List <decimal>() { 0, 0, 0, 0, 0 }, new List <decimal>() { 0, 0, 1, 1, 1 }, new List <decimal>() { 0, 1, 0, 2, 2 }, new List <decimal>() { 0, 1, 2, 0, 3 }, new List <decimal>() { 0, 1, 2, 3, 0 }, }; var expectedResult = new List <List <List <decimal> > >() { new List <List <decimal> >() { new List <decimal>() { 0.0m, 0.5m, 0.5m, 0.5m }, new List <decimal>() { 0.5m, 0.0m, 2.0m, 2.0m }, new List <decimal>() { 0.5m, 2.0m, 0.0m, 3.0m }, new List <decimal>() { 0.5m, 2.0m, 3.0m, 0.0m } }, new List <List <decimal> >() { new List <decimal>() { 0.25m, 1.25m, 1.25m }, new List <decimal>() { 1.25m, 0.00m, 3.00m }, new List <decimal>() { 1.25m, 3.00m, 0.00m }, }, new List <List <decimal> >() { new List <decimal>() { 0.750m, 2.125m }, new List <decimal>() { 2.125m, 0.000m }, }, new List <List <decimal> >() { new List <decimal>() { 1.4375m }, } }; Console.WriteLine(); Console.WriteLine("Sample matrix:"); CommonMethods.PrintMatrix(distanceMatrix1); Console.WriteLine(); for (var i = 0; i < 4; i++) { Console.WriteLine("Upgma iteration #" + i); var ind1 = UpgmaClustering.UpgmaLowestDistanceIndexes(distanceMatrix1); var result1 = UpgmaClustering.UpgmaDistanceMatrixNextIteration(distanceMatrix1, ind1); Console.WriteLine(); Console.WriteLine("expected result:"); CommonMethods.PrintMatrix(expectedResult[i]); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("actual result:"); CommonMethods.PrintMatrix(result1); Console.WriteLine(); Console.WriteLine(); for (var x = 0; x < expectedResult[i].Count; x++) { for (var y = 0; y < expectedResult[i][x].Count; y++) { Assert.IsTrue(expectedResult[i][x][y] == result1[x][y]); } } } }