Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / clustalw / src / tree / UPGMA / RootedTreeOutput.cpp
1 /**
2  * Author: Mark Larkin
3  * 
4  * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
5  */
6 #ifdef HAVE_CONFIG_H
7     #include "config.h"
8 #endif
9 #include "RootedTreeOutput.h"
10
11 namespace clustalw
12 {
13
14 RootedTreeOutput::RootedTreeOutput(SeqInfo* seqInfo)
15 {
16     firstSeq = seqInfo->firstSeq;
17     lastSeq = seqInfo->lastSeq;
18     numSeqs = seqInfo->numSeqs;  
19 }
20
21 void RootedTreeOutput::printPhylipTree(RootedGuideTree* tree, ofstream* ptrToFile, Alignment *alignPtr,
22                                     DistMatrix* distMat)
23 {
24     if(!ptrToFile || !ptrToFile->is_open())
25     {
26         return;
27     }
28     
29     //cerr << "\n******************* DEBUG AW RootedTreeOutput::printPhylipTree: we only get here when using UPGMA with >2 seqs. Why?\n";
30
31     
32     // If we have only 2 sequences, use the distances in the distMat
33     if (lastSeq - firstSeq + 1 == 2)
34     {
35         (*ptrToFile) << "(" << alignPtr->getName(firstSeq) << ":" << fixed << setprecision(5) 
36         << (*distMat)(firstSeq, firstSeq + 1) << "," << alignPtr->getName(firstSeq + 1) 
37                      << ":" << fixed << setprecision(5)  << (*distMat)(firstSeq, firstSeq + 1)
38             ;
39         // << ")";
40         // AW 2009-05-15: final ")" added here, but not tested
41         // when is this ever used? tried
42         // infile=test.aln -clustering=nj|upgma
43         // on pairwise input, but this is only called for multiple
44         // input. why?
45     } else {
46         // AW 2009-05-08: fixed bug 155 by removing unnecessary, extra
47         // outer parenthesis from output
48         phylipTraverse(ptrToFile, alignPtr, tree->getRoot());
49     }
50     (*ptrToFile) << ";\n";
51 }
52
53 void RootedTreeOutput::printNexusTree(RootedGuideTree* tree, ofstream* ptrToFile, 
54                                       Alignment *alignPtr, DistMatrix* distMat)
55 {
56     if(!ptrToFile || !ptrToFile->is_open())
57     {
58         return;
59     }
60         
61     (*ptrToFile) << "#NEXUS\n\n";
62
63     (*ptrToFile) << "BEGIN TREES;\n\n";
64     (*ptrToFile) << "\tTRANSLATE\n";
65             
66     for(int j = 1; j < numSeqs; j++) 
67     {
68         (*ptrToFile) << "\t\t" << j << "\t" << alignPtr->getName(j) <<",\n";
69     }
70     (*ptrToFile) << "\t\t" << numSeqs << "\t" << alignPtr->getName(numSeqs) << "\n";
71     (*ptrToFile) << "\t\t;\n";
72
73     (*ptrToFile) << "\tUTREE PAUP_1= ";
74     
75     // IF we have only 2 seqs
76     if (lastSeq - firstSeq + 1 == 2)
77     {
78         (*ptrToFile) << "(" << alignPtr->getName(firstSeq) << ":" << fixed << setprecision(5) 
79                      << (*distMat)(firstSeq, firstSeq + 1) << "," << alignPtr->getName(firstSeq + 1) 
80                      << ":" << fixed << setprecision(5)  << (*distMat)(firstSeq, firstSeq + 1);
81     }
82     else
83     {                        
84         (*ptrToFile) << "(";
85         nexusTraverse(ptrToFile, alignPtr, tree->getRoot());
86     }
87     (*ptrToFile) << ");\n";
88     (*ptrToFile) << "\nENDBLOCK;\n";
89 }
90
91 /**
92  * PRIVATE FUNCTIONS
93  */ 
94  
95 void RootedTreeOutput::phylipTraverse(ofstream* ptrToFile, Alignment *alignPtr, Node* t)
96 {
97     if(!ptrToFile)
98     {
99         return;
100     }    
101     if(t != 0)
102     {
103         if(t->isLeafNode())
104         {
105             if(alignPtr)
106             {
107                 (*ptrToFile) << alignPtr->getName(t->getSeqNum()) << ":" << t->getHeight();
108             }
109             else
110             {
111                 (*ptrToFile) << t->getSeqNum() << ":" << t->getHeight();
112             }
113         }
114         else // Internal node
115         {
116             (*ptrToFile) << "(\n";
117             phylipTraverse(ptrToFile, alignPtr, t->getLeft());
118             (*ptrToFile) << ",\n";
119             phylipTraverse(ptrToFile, alignPtr, t->getRight());
120             (*ptrToFile) << "):" << t->getHeight();
121         }
122     }
123 }
124
125 void RootedTreeOutput::nexusTraverse(ofstream* ptrToFile, Alignment *alignPtr, Node* t)
126 {
127     if(t != 0)
128     {
129         if(!t->isLeafNode()) // Internal node
130         {
131             (*ptrToFile) << "(";
132             nexusTraverse(ptrToFile, alignPtr, t->getLeft());
133             (*ptrToFile) << ",";
134             nexusTraverse(ptrToFile, alignPtr, t->getRight());
135             (*ptrToFile) << "):" << t->getHeight();                    
136         }
137         else // Leaf node
138         {
139             if(alignPtr)
140             {
141                 (*ptrToFile) << alignPtr->getName(t->getSeqNum()) << ":" << t->getHeight();
142             }
143             else
144             {
145                 (*ptrToFile) << t->getSeqNum() << ":" << t->getHeight();
146             }
147         }
148     }
149
150 }
151
152 }