4 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
9 #include "ClusterTreeOutput.h"
14 ClusterTreeOutput::ClusterTreeOutput(SeqInfo* seqInfo, int boot)
17 firstSeq = seqInfo->firstSeq;
18 lastSeq = seqInfo->lastSeq;
19 numSeqs = seqInfo->numSeqs;
22 void ClusterTreeOutput::printTreeDesc(PhyloTree* phyloTree)
24 for(int i = 1; i <= numSeqs; i++)
26 for(int j = 1; j <= numSeqs; j++)
28 cout << " " << phyloTree->treeDesc[i][j];
36 * The function printPhylipTree is used to print out the unrooted clustered tree in
38 * @param phyloTree A pointer to the PhyloTree struct that contains the description of the
40 * @param tree The file to print the phylip tree to. Must be open!
41 * @param alignPtr The alignment object. Needed for the names.
42 * @param distMat The distance matrix that was used for the generation of the cluster.
43 * @param bootTotals Holds the bootstrap values. Only used if the tree has been bootstrapped
45 void ClusterTreeOutput::printPhylipTree(PhyloTree* phyloTree, ofstream* tree,
46 Alignment *alignPtr, DistMatrix* distMat, vector<int>* bootTotals)
49 if (lastSeq - firstSeq + 1 == 2)
51 (*tree) << "(" << alignPtr->getName(firstSeq) << ":" << fixed <<setprecision(5)
52 << (*distMat)(firstSeq, firstSeq + 1) << "," << alignPtr->getName(firstSeq + 1)
53 << ":" << fixed << setprecision(5) << (*distMat)(firstSeq, firstSeq + 1)
60 oldRow = twoWaySplit(phyloTree, tree, lastSeq - firstSeq + 1 - 2,
61 1, alignPtr, bootTotals);
63 (*tree) << ":" << fixed << setprecision(5)
64 << phyloTree->leftBranch[lastSeq - firstSeq + 1 - 2];
66 if ((bootstrap == BS_BRANCH_LABELS) && (oldRow > 0) &&
67 ((*bootTotals)[oldRow] > 0))
69 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
73 oldRow = twoWaySplit(phyloTree, tree, lastSeq - firstSeq + 1 - 2,
74 2, alignPtr, bootTotals);
76 (*tree) << ":" << fixed << setprecision(5)
77 << phyloTree->leftBranch[lastSeq - firstSeq + 1 - 1];
79 if ((bootstrap == BS_BRANCH_LABELS) && (oldRow > 0) &&
80 ((*bootTotals)[oldRow] > 0))
82 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
86 oldRow = twoWaySplit(phyloTree, tree, lastSeq - firstSeq + 1 - 2,
87 3, alignPtr, bootTotals);
89 (*tree) << ":" << fixed << setprecision(5)
90 << phyloTree->leftBranch[lastSeq - firstSeq + 1];
92 if ((bootstrap == BS_BRANCH_LABELS) && (oldRow > 0) &&
93 ((*bootTotals)[oldRow] > 0))
95 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
99 if (bootstrap == BS_NODE_LABELS)
101 (*tree) << "TRICHOTOMY";
106 int ClusterTreeOutput::twoWaySplit(PhyloTree* phyloTree, ofstream* tree,
107 int startRow, int flag, Alignment *alignPtr, vector<int>* bootTotals)
109 int row, newRow = 0, oldRow, col, testCol = 0;
112 if (startRow != lastSeq - firstSeq + 1-2)
117 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
119 if (phyloTree->treeDesc[startRow][col] == flag)
127 for (row = startRow - 1; row >= 1; row--)
128 if (phyloTree->treeDesc[row][testCol] == 1)
137 phyloTree->treeDesc[startRow][testCol] = 0;
138 (*tree) << alignPtr->getName(testCol + firstSeq - 1);
139 if (startRow == lastSeq - firstSeq + 1 - 2)
144 (*tree) << ":" << fixed << setprecision(5) << phyloTree->leftBranch[startRow]
149 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
151 if ((phyloTree->treeDesc[startRow][col] == 1) &&
152 (phyloTree->treeDesc[newRow][col] == 1))
154 phyloTree->treeDesc[startRow][col] = 0;
157 oldRow = twoWaySplit(phyloTree, tree, newRow, 1, alignPtr, bootTotals);
159 if (startRow == lastSeq - firstSeq + 1-2)
164 (*tree) << ":" << fixed << setprecision(5) << phyloTree->leftBranch[startRow];
166 if ((bootstrap == BS_BRANCH_LABELS) && ((*bootTotals)[oldRow] > 0))
168 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
175 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
176 if (phyloTree->treeDesc[startRow][col] == flag)
184 for (row = startRow - 1; row >= 1; row--)
185 if (phyloTree->treeDesc[row][testCol] == 1)
194 phyloTree->treeDesc[startRow][testCol] = 0;
195 (*tree) << alignPtr->getName(testCol + firstSeq - 1);
196 (*tree) << ":" << fixed << setprecision(5) << phyloTree->rightBranch[startRow]
201 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
203 if ((phyloTree->treeDesc[startRow][col] == 1) &&
204 (phyloTree->treeDesc[newRow][col] == 1))
206 phyloTree->treeDesc[startRow][col] = 0;
209 oldRow = twoWaySplit(phyloTree, tree, newRow, 1, alignPtr, bootTotals);
210 (*tree) << ":" << fixed << setprecision(5) << phyloTree->rightBranch[startRow];
212 if ((bootstrap == BS_BRANCH_LABELS) && ((*bootTotals)[oldRow] > 0))
214 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
219 if ((bootstrap == BS_NODE_LABELS) && ((*bootTotals)[startRow] > 0))
221 (*tree) << (*bootTotals)[startRow];
227 void ClusterTreeOutput::printNexusTree(PhyloTree* phyloTree, ofstream* tree,
228 Alignment *alignPtr, DistMatrix* distMat, vector<int>* bootTotals)
233 (*tree) << "#NEXUS\n\n";
235 (*tree) << "BEGIN TREES;\n\n";
236 (*tree) << "\tTRANSLATE\n";
238 for(i = 1; i < numSeqs; i++)
240 (*tree) << "\t\t" << i << "\t" << alignPtr->getName(i) <<",\n";
242 (*tree) << "\t\t" << numSeqs << "\t" << alignPtr->getName(numSeqs) <<"\n";
243 (*tree) << "\t\t;\n";
245 (*tree) << "\tUTREE PAUP_1= ";
247 if(lastSeq - firstSeq + 1 == 2)
249 (*tree) << "(" << firstSeq << ":" << fixed << setprecision(5)
250 << (*distMat)(firstSeq, firstSeq + 1) << "," << firstSeq + 1 << ":"
251 << fixed << setprecision(5) << (*distMat)(firstSeq, firstSeq + 1) << ")";
258 oldRow = twoWaySplitNexus(phyloTree, tree,
259 lastSeq - firstSeq + 1 - 2, 1, alignPtr, bootTotals);
261 (*tree) << ":" << fixed << setprecision(5)
262 << phyloTree->leftBranch[lastSeq-firstSeq+1-2];
264 if ((bootstrap == BS_BRANCH_LABELS) && (oldRow > 0) && ((*bootTotals)[oldRow]>0))
266 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
271 oldRow = twoWaySplitNexus(phyloTree, tree, lastSeq - firstSeq + 1 - 2, 2,
272 alignPtr, bootTotals);
273 (*tree) << ":" << fixed << setprecision(5)
274 << phyloTree->leftBranch[lastSeq-firstSeq+1-1];
276 if ((bootstrap==BS_BRANCH_LABELS) && (oldRow>0) && ((*bootTotals)[oldRow]>0))
278 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
283 oldRow = twoWaySplitNexus(phyloTree, tree,
284 lastSeq-firstSeq+1-2, 3, alignPtr, bootTotals);
286 (*tree) << ":" << fixed << setprecision(5)
287 << phyloTree->leftBranch[lastSeq-firstSeq+1];
289 if ((bootstrap==BS_BRANCH_LABELS) && (oldRow>0) && ((*bootTotals)[oldRow]>0))
291 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
296 if (bootstrap == BS_NODE_LABELS)
298 (*tree) << "TRICHOTOMY";
302 (*tree) << "\nENDBLOCK;\n";
305 int ClusterTreeOutput::twoWaySplitNexus(PhyloTree* phyloTree, ofstream* tree,
306 int startRow, int flag, Alignment *alignPtr, vector<int>* bootTotals)
308 int row, newRow = 0, oldRow, col, testCol = 0;
311 if (startRow != lastSeq - firstSeq + 1 - 2)
316 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
318 if (phyloTree->treeDesc[startRow][col] == flag)
326 for (row = startRow - 1; row >= 1; row--)
327 if (phyloTree->treeDesc[row][testCol] == 1)
336 phyloTree->treeDesc[startRow][testCol] = 0;
337 (*tree) << testCol + firstSeq - 1;;
338 if (startRow == lastSeq - firstSeq + 1-2)
343 (*tree) << ":" << fixed << setprecision(5) << phyloTree->leftBranch[startRow] << ",";
347 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
349 if ((phyloTree->treeDesc[startRow][col] == 1) &&
350 (phyloTree->treeDesc[newRow][col] == 1))
352 phyloTree->treeDesc[startRow][col] = 0;
355 oldRow = twoWaySplitNexus(phyloTree, tree, newRow, 1, alignPtr, bootTotals);
357 if (startRow == lastSeq - firstSeq + 1-2)
362 (*tree) << ":" << fixed << setprecision(5) << phyloTree->leftBranch[startRow];
363 if ((bootstrap == BS_BRANCH_LABELS) && ((*bootTotals)[oldRow] > 0))
365 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
372 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
373 if (phyloTree->treeDesc[startRow][col] == flag)
381 for (row = startRow - 1; row >= 1; row--)
382 if (phyloTree->treeDesc[row][testCol] == 1)
391 phyloTree->treeDesc[startRow][testCol] = 0;
392 (*tree) << testCol + firstSeq - 1;
393 (*tree) << ":" << fixed << setprecision(5) << phyloTree->rightBranch[startRow] << ")";
397 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
399 if ((phyloTree->treeDesc[startRow][col] == 1) &&
400 (phyloTree->treeDesc[newRow][col] == 1))
402 phyloTree->treeDesc[startRow][col] = 0;
405 oldRow = twoWaySplitNexus(phyloTree, tree, newRow, 1, alignPtr, bootTotals);
407 (*tree) << ":" << fixed << setprecision(5) << phyloTree->rightBranch[startRow];
408 if ((bootstrap == BS_BRANCH_LABELS) && ((*bootTotals)[oldRow] > 0))
410 (*tree) << "[" << (*bootTotals)[oldRow] << "]";
415 if ((bootstrap == BS_NODE_LABELS) && ((*bootTotals)[startRow] > 0))
417 (*tree) << (*bootTotals)[startRow];
423 void ClusterTreeOutput::printTree(PhyloTree* phyloTree, ofstream* tree,
430 for (row = 1; row <= lastSeq - firstSeq + 1 - 3; row++)
433 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
435 if (phyloTree->treeDesc[row][col] == 0)
444 if ((*totals)[row] > 0)
446 (*tree) << setw(7) << (*totals)[row];
450 for (col = 1; col <= lastSeq - firstSeq + 1; col++)
452 (*tree) << setw(1) << phyloTree->treeDesc[lastSeq - firstSeq + 1 -2][col];