File: RootedClusterTree.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,868 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 15
file content (222 lines) | stat: -rw-r--r-- 6,776 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include "RootedClusterTree.h"
#include "../../general/OutputFile.h"
#include "UPGMAAlgorithm.h"
#include "RootedTreeOutput.h"
//#include "../RandomGenerator.h"
namespace clustalw
{

auto_ptr<AlignmentSteps> RootedClusterTree::treeFromDistMatrix(RootedGuideTree* phyloTree,DistMatrix* distMat, Alignment *alignPtr, 
                                           int seq1, int nSeqs, string& phylipName)
{
    OutputFile phylipPhyTreeFile;
    auto_ptr<AlignmentSteps> progSteps;
    try
    {
        // Test to see if the inputs are valid
        if(seq1 < 1 || nSeqs < 1)
        {
            cerr << "Invalid inputs into treeFromDistMatrix \n"
                 << "seq1 = " << seq1 << " nSeqs = " << nSeqs << "\n"
                 << "Need to end program!\n";
            exit(1); 
            return progSteps;
        }

        float dist;
        string path;
        verbose = false;
        firstSeq = seq1;
        lastSeq = firstSeq + nSeqs - 1;
    
        SeqInfo info;
        info.firstSeq = firstSeq;
        info.lastSeq = lastSeq;
        info.numSeqs = nSeqs;

        utilityObject->getPath(userParameters->getSeqName(), &path);
        
        if(nSeqs >= 2)
        {
            string name = phylipName;
            if(!phylipPhyTreeFile.openFile(&name, 
                             "\nEnter name for new GUIDE TREE           file  ", &path, "dnd",
                             "Guide tree"))
            {
                return progSteps;
            }
            phylipName = name;                    
        }
        else
        {
            return progSteps;
        }
                
        RootedTreeOutput outputTree(&info);
        
        ofstream* ptrToFile = phylipPhyTreeFile.getPtrToFile();
        
        if (nSeqs == 2)
        {
            dist = (*distMat)(firstSeq, firstSeq + 1) / 2.0;
            if(ptrToFile->is_open())
            {
                (*ptrToFile) <<  "(" << alignPtr->getName(firstSeq) << ":" 
                             << setprecision(5)
                             << dist << "," << alignPtr->getName(firstSeq + 1) << ":" 
                             << setprecision(5) << dist <<");\n";
            }
            progSteps.reset(new AlignmentSteps);
            vector<int> groups;
            groups.resize(nSeqs + 1, 0);
            groups[1] = 1;
            groups[2] = 2;
        }
        else
        {
            UPGMAAlgorithm clusAlgorithm;
            progSteps = clusAlgorithm.generateTree(phyloTree, distMat, &info, false);
            outputTree.printPhylipTree(phyloTree, ptrToFile, alignPtr, distMat);
        }
        return progSteps;
    }
    catch(const exception &ex)
    {
        cerr << "ERROR: Error has occurred in treeFromDistMatrix. " 
             << "Need to terminate program.\n"
             << ex.what();
        exit(1);   
    }
    catch(...)
    {
        cerr << "ERROR: Error has occurred in treeFromDistMatrix. " 
             << "Need to terminate program.\n";      
        exit(1);  
    }
}

void RootedClusterTree::treeFromAlignment(TreeNames* treeNames, Alignment *alignPtr)
{
    try
    {
        OutputFile phylipPhyTreeFile;   
        OutputFile clustalPhyTreeFile;
        OutputFile distancesPhyTreeFile;
        OutputFile nexusPhyTreeFile;
        OutputFile pimFile;
        
        RootedGuideTree phyloTree;
        
        string path;
        int j;
        int overspill = 0;
        int totalDists;
        numSeqs = alignPtr->getNumSeqs(); // NOTE class variable
        
        /**
         * Check if numSeqs is ok
         */
        if(!checkIfConditionsMet(numSeqs, 2))
        {
            return;
        }
              
        firstSeq = 1;
        lastSeq = numSeqs;
        
        // The SeqInfo struct is passed to reduce the number of parameters passed!
        SeqInfo info;
        info.firstSeq = firstSeq;
        info.lastSeq = lastSeq;
        info.numSeqs = numSeqs;
    
        RootedTreeOutput outputTree(&info); // No bootstrap!

        utilityObject->getPath(userParameters->getSeqName(), &path);

        /**
         * Open the required output files.
         */
        if(!openFilesForTreeFromAlignment(&clustalPhyTreeFile, &phylipPhyTreeFile, 
                        &distancesPhyTreeFile, &nexusPhyTreeFile, &pimFile, treeNames, &path))
        {
            return; // Problem opeing one of the files, cannot continue!
        } 
    
        int _lenFirstSeq = alignPtr->getSeqLength(firstSeq);
    
        bootPositions.clear();
        bootPositions.resize(_lenFirstSeq + 2);

        for (j = 1; j <= _lenFirstSeq; ++j)
        {
            bootPositions[j] = j;
        }

        /**
         * Calculate quickDist and overspill
         */
        overspill = calcQuickDistMatForAll(clustalPhyTreeFile.getPtrToFile(),
                       phylipPhyTreeFile.getPtrToFile(), nexusPhyTreeFile.getPtrToFile(),
                       pimFile.getPtrToFile(), distancesPhyTreeFile.getPtrToFile(), alignPtr);

        // check if any distances overflowed the distance corrections 
        if (overspill > 0)
        {
            totalDists = (numSeqs *(numSeqs - 1)) / 2;
            overspillMessage(overspill, totalDists);
        }

        if (userParameters->getOutputTreeClustal())
        {
            verbose = true;
        }
        // Turn on file output 
    

        if (userParameters->getOutputTreeClustal() ||
            userParameters->getOutputTreePhylip() 
            || userParameters->getOutputTreeNexus())
        {
            UPGMAAlgorithm clusAlgorithm;
            clusAlgorithm.setVerbose(true);
            clusAlgorithm.generateTree(&phyloTree, quickDistMat.get(), &info, false,
                                       clustalPhyTreeFile.getPtrToFile());
            clusAlgorithm.setVerbose(false);
        }

        if (userParameters->getOutputTreePhylip())
        {
            outputTree.printPhylipTree(&phyloTree, phylipPhyTreeFile.getPtrToFile(), alignPtr,
                                        quickDistMat.get());
        }

        if (userParameters->getOutputTreeNexus())
        {
            outputTree.printNexusTree(&phyloTree, nexusPhyTreeFile.getPtrToFile(), alignPtr, 
                                       quickDistMat.get());
        }
    
        /** Free up resources!!!!! */
    
        treeGaps.clear();
        bootPositions.clear();
          
    }
    catch(const exception& ex)
    {
        cerr << ex.what() << endl;
        utilityObject->error("Terminating program. Cannot continue\n");
        exit(1);    
    }
}
                                
}