File: RootedGuideTree.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,324 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 16
file content (164 lines) | stat: -rw-r--r-- 3,665 bytes parent folder | download | duplicates (11)
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
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include <iostream>
#include "RootedGuideTree.h"
#include "../../general/userparams.h"
using namespace std;

namespace clustalw
{

RootedGuideTree::RootedGuideTree()
 : root(0){ }

RootedGuideTree::RootedGuideTree(Node* r)
 : root(r) { }

RootedGuideTree::~RootedGuideTree()
{
    root->makeEmpty();
}

void RootedGuideTree::setRoot(Node* r)
{
    makeEmpty();
    
    root = r;
}
 
void RootedGuideTree::makeEmpty()
{
    root->makeEmpty();
}

void RootedGuideTree::orderNodes()
{
    calcOrderNode(root);
}

int RootedGuideTree::calcOrderNode(Node* node)
{
    if(node != 0)
    {
        if(node->getLeft() == 0 && node->getRight() == 0) // Leaf Node
        {
            node->setOrder(1);
            return 1;
        }
        else // Internal node
        {
            node->setOrder(calcOrderNode(node->getLeft()) + calcOrderNode(node->getRight()));
            return node->getOrder();            
        }
    }
    return 0;
}

void RootedGuideTree::calcSeqWeights(int firstSeq, int lastSeq, vector<int>* seqWeights)
{
    if((int)seqWeights->size() < lastSeq - 1)
    {
        seqWeights->resize(lastSeq - 1);
    }
    
    int i = 0, _nSeqs = 0;
    int temp = 0, sum = 0;
    //
    // If there are more than three sequences....
    //
    _nSeqs = lastSeq - firstSeq;
    if ((_nSeqs >= 2) && (userParameters->getDistanceTree() == true) && 
        (userParameters->getNoWeights() == false))
    {
        //
        // Calculate sequence weights based on Phylip tree.
        //
        orderNodes();
        calcWeights(seqWeights);

        //
        // Normalise the weights, such that the sum of the weights = INT_SCALE_FACTOR
        //

        sum = 0;
        for (i = firstSeq; i < lastSeq; i++)
        {
            sum += (*seqWeights)[i];
        }

        if (sum == 0)
        {
            for (i = firstSeq; i < lastSeq; i++)
            {
                (*seqWeights)[i] = 1;
            }
            sum = i;
        }

        for (i = firstSeq; i < lastSeq; i++)
        {
            (*seqWeights)[i] = ((*seqWeights)[i] * INT_SCALE_FACTOR) / sum;
            if ((*seqWeights)[i] < 1)
            {
                (*seqWeights)[i] = 1;
            }
        }
    }
    else
    {
        //
        // Otherwise, use identity weights.
        //
        temp = INT_SCALE_FACTOR / _nSeqs;
        // AW 2009-07-09: goes wrong if we have more than
        // INT_SCALE_FACTOR seqs. if so, set to 1, just as above
        // same as in Tree.cpp
        if (temp < 1)
            temp = 1;

        
        for (i = firstSeq; i < lastSeq; i++)
        {
            (*seqWeights)[i] = temp;
        }
    }
}

void RootedGuideTree::calcWeights(vector<int>* seqWeights)
{
    vector<float> weights;
    int sizeSeqWeights = seqWeights->size();
    weights.resize(sizeSeqWeights, 0.0);
    
    doWeightCalc(0.0, &weights, root);

    for(int i = 0; i < sizeSeqWeights; i++)
    {
        (*seqWeights)[i] = static_cast<int>(weights[i] * 100);        
    }
}

void RootedGuideTree::doWeightCalc(float weightSoFar, vector<float>* weights, Node* t)
{
    if(t != 0)
    {
        if(t->getLeft() == 0 && t->getRight() == 0) // Leaf Node
        {
            (*weights)[t->getSeqNum() - 1] = weightSoFar;
        }
        else // Internal node
        {
            float w = weightSoFar + (t->getHeight() / t->getOrder());
            doWeightCalc(w, weights, t->getLeft());
            doWeightCalc(w, weights, t->getRight());            
        }
    }
}

}