File: ancestralReconstructStates.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (220 lines) | stat: -rw-r--r-- 10,020 bytes parent folder | download | duplicates (5)
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
/*
Copyright (C) 2011 Tal Pupko  TalP@tauex.tau.ac.il.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "ancestralReconstructStates.h"
#include <cmath>

using namespace std;

/********************************************************************************************
ancestralReconstructStates
*********************************************************************************************/
ancestralReconstructStates::ancestralReconstructStates(const tree &tr, const sequenceContainer &sc, stochasticProcess *sp):
_tr(tr), _sc(sc){
	if(!sp){
		errorMsg::reportError("error in the constructor ancestralReconstructStates sp argument is NULL");
	}
	else{
		_sp = sp;
	}
	_statesV.resize(_sc.seqLen());
	for (int pos = 0; pos <_sc.seqLen(); ++pos){
		initializeStatesVector(pos);
	}
}
void ancestralReconstructStates::initializeStatesVector(int pos){
	_statesV[pos].resize(_tr.getNodesNum(),-1000);
	checkThatNamesInTreeAreSameAsNamesInSequenceContainer(_tr,_sc);
	seqContainerTreeMap scTreeMap(_sc,_tr);
	vector <tree::nodeP> leaves;
	_tr.getAllLeaves(leaves,_tr.getRoot());
	for (unsigned int i=0; i< leaves.size();i++){
		int myleafId = (leaves[i])->id();
		int mySeqId = scTreeMap.seqIdOfNodeI(myleafId);
		_statesV[pos][myleafId] = _sc[mySeqId][pos];
	}
}
/********************************************************************************************
upL[node][letter] = max(letter_here){P(letter->letter_here)*upL[son1][letter_here]*upL[son2][letter_here]} for letter at father node.
backtrack[node][letter] = argmax of above
*********************************************************************************************/
void ancestralReconstructStates::traverseUpML(VVVdouble &upL, VVVint &backtrack){ // input as empty vector to be filled
	LOGnOUT(4,<<"traverseUpML..."<<endl);
	upL.resize(_sc.seqLen());
	backtrack.resize(_sc.seqLen());
	for (int pos = 0; pos <_sc.seqLen(); ++pos){
		traverseUpML(upL[pos], backtrack[pos], pos);
	}
}
/********************************************************************************************
*********************************************************************************************/
void ancestralReconstructStates::traverseUpML(VVdouble &upL, VVint &backtrack, int pos){ // input as empty vector to be filled
	computePijGam pi;
	pi.fillPij(_tr,*_sp);
	upL.resize(_tr.getNodesNum());
	for (unsigned int i = 0; i < upL.size(); i++)
		upL[i].resize(_sp->alphabetSize());
	backtrack.resize(_tr.getNodesNum());
	for (unsigned int i = 0; i < backtrack.size(); i++)
		backtrack[i].resize(_sp->alphabetSize());
	treeIterDownTopConst tIt(_tr);
	for (tree::nodeP mynode = tIt.first(); mynode != tIt.end(); mynode = tIt.next()) {
		int father_state = 0;
		if (mynode->isLeaf()) {
			for (father_state=0; father_state<_sp->alphabetSize();father_state++){ // looping over states at father
				int myState = _statesV[pos][mynode->id()];
				if(myState == _sc.getAlphabet()->unknown()){
					myState = father_state; // same as relations=1, for missing data
				}
				for (int i=0; i < _sp->categories();++i) {
					upL[mynode->id()][father_state] += pi.getPij(i,mynode->id(),father_state,myState)*_sp->ratesProb(i);
				}
				backtrack[mynode->id()][father_state]=myState;
			}
		}
		else if (!(mynode->isRoot())) {
			for (father_state=0; father_state<_sp->alphabetSize();father_state++){ // looping over states at father
				MDOUBLE myMax = -1;
				int myArgMax=-1;
				for (int my_state=0;my_state<_sp->alphabetSize();my_state++){ // loop to find max over current node
					//MDOUBLE val=_sp->Pij_t(father_state,my_state,mynode->dis2father());
					MDOUBLE val=0;
					for (int i=0; i < _sp->categories();++i) {
						val += pi.getPij(i,mynode->id(),father_state,my_state)*_sp->ratesProb(i);
					}
					for (int son=0;son<mynode->getNumberOfSons();son++)
						val*=upL[mynode->getSon(son)->id()][my_state];
					if (val>myMax){
						myMax=val;
						myArgMax=my_state;
					}
				}
				if ((myMax<0) || (myArgMax<0))
					errorMsg::reportError("Error in traverseUpML: cannot find maximum");
				upL[mynode->id()][father_state]=myMax;
				backtrack[mynode->id()][father_state]=myArgMax;
			}
		}
		else {// root
			for (int root_state=0; root_state<_sp->alphabetSize();root_state++){
				MDOUBLE val=_sp->freq(root_state);
				for (int son=0;son<mynode->getNumberOfSons();son++)
					val*=upL[mynode->getSon(son)->id()][root_state];
				upL[mynode->id()][root_state]=val;
			}
		}
	}
}

/********************************************************************************************
return likelihood of max joint reconstruction
*********************************************************************************************/
Vdouble ancestralReconstructStates::traverseDownML(VVVdouble &upL, VVVint &backtrack,VVVint &transitionTypeCount) { // input as already filled vector
	LOGnOUT(4,<<"traverseDownML..."<<endl);
	Vdouble LofJointV;
	LofJointV.resize(_sc.seqLen());
	transitionTypeCount.resize(_sc.seqLen());

	for (int pos = 0; pos <_sc.seqLen(); ++pos){
		LofJointV[pos] = traverseDownML(upL[pos], backtrack[pos],transitionTypeCount[pos], pos);
	}
	return LofJointV;
}

/********************************************************************************************
fill _statesV, transitionTypeCount
*********************************************************************************************/
MDOUBLE ancestralReconstructStates::traverseDownML(VVdouble &upL, VVint &backtrack,VVint &transitionTypeCount, int pos) { // input as already filled vector
	if (backtrack.size() == 0)
		errorMsg::reportError("error in ancestralReconstruct::traverseDownML, input vector backtrack must be filled (call traverseUpML() first)");
	MDOUBLE LofJoint;
	int stateOfRoot;
	findMaxInVector(upL[(_tr.getRoot())->id()], LofJoint, stateOfRoot);
	_statesV[pos][(_tr.getRoot())->id()] = stateOfRoot;
	transitionTypeCount.resize(_sp->alphabetSize());
	for (unsigned int i = 0; i < transitionTypeCount.size(); i++)
		transitionTypeCount[i].resize(_sp->alphabetSize(),0);
	treeIterTopDownConst tIt(_tr);
	for (tree::nodeP mynode = tIt.first(); mynode != tIt.end(); mynode = tIt.next()) {
		if (mynode->isRoot()) continue;
		int myId = mynode->id();
		int stateAtFather = _statesV[pos][mynode->father()->id()];
		int myState = _statesV[pos][mynode->id()];
		if(myState == _sc.getAlphabet()->unknown()){
			myState = stateAtFather; // same as relations=1, for missing data
		}
		if (mynode->isLeaf()) {
			transitionTypeCount[stateAtFather][myState]++;
			if ((_statesV[pos][mynode->id()]!=stateAtFather))
				LOG(7,<<"switch from "<<mynode->father()->name()<<"("<<stateAtFather<<") to "<<mynode->name()<<"("<<_statesV[pos][mynode->id()]<<")"<<endl);
			continue;
		}
		if(_statesV[pos][mynode->id()] == -2)
			cout<<_statesV[pos][mynode->id()]<<" unKnown at pos="<<pos<<" node="<<mynode->id()<<endl;
		_statesV[pos][mynode->id()]=backtrack[myId][stateAtFather];
		transitionTypeCount[stateAtFather][_statesV[pos][mynode->id()]]++;
	}
	return log(LofJoint);
}



/********************************************************************************************
compute Prob(letter at Node N is x|Data): the posterior probabilities at ancestral states 
Use the pre-calculated joint posterior probability P(N=x, father(N)=y|D) and just sum over these probs:
Prob(N=x|Data) = sum{fatherState}[P(N=x, father(N)=y|D)]}
stores results in member VVVdouble[pos][node][state] _ancestralProbs
use VVVVdouble _probChanges_PosNodeXY == jointPost[pos][nodeID][fatherLetter][letter]- after computePosteriorOfChangeGivenTerminals
*********************************************************************************************/
void ancestralReconstructStates::computeAncestralPosterior(const VVVVdouble& jointPost)
{
	LOGnOUT(4,<<"computeAncestralPosterior (take into acount joint probabilty)..."<<endl);
	int numNodes = _tr.getNodesNum();
	int alphabetSize = _sp->alphabetSize();
	//int alphabetSizeForProbsSize = alphabetSize;
	//bool isThereMissingData = _sc.getAlphabetDistribution(true)[2]>0;
	//if(isThereMissingData)
	//	alphabetSizeForProbsSize++; // resize for one more

	_ancestralProbs.resize(_sc.seqLen());
	for (int pos = 0; pos <_sc.seqLen(); ++pos){
		resizeMatrix(_ancestralProbs[pos], numNodes, alphabetSize);
		treeIterTopDownConst tIt(_tr);
		int letter;
		for (tree::nodeP mynode = tIt.first(); mynode != tIt.end(); mynode = tIt.next()) {
			if (mynode->isRoot()) {
				//for(letter = 0; letter<alphabetSize; ++letter) // itay's version - the computed vals are all 0
				//	_ancestralProbs[pos][mynode->id()][letter] = jointPost[pos][mynode->id()][0][letter];
				for(letter = 0; letter < alphabetSize; ++letter) {
					MDOUBLE sum = 0.0;
					for(int sonLetter = 0; sonLetter < alphabetSize; ++sonLetter) {
						sum += jointPost[pos][mynode->getSon(0)->id()][letter][sonLetter];	// sum over the son joint prob (instead of father)
					}
					_ancestralProbs[pos][mynode->id()][letter] = sum;
				}
				continue;
			}
			for(letter = 0; letter < alphabetSize; ++letter) {
				MDOUBLE sum = 0.0;
				for(int fatherLetter = 0; fatherLetter < alphabetSize; ++fatherLetter) {
					sum += jointPost[pos][mynode->id()][fatherLetter][letter];
				}
				_ancestralProbs[pos][mynode->id()][letter] = sum;
			}
		}
	}	
}