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
|
#include "computePosteriorExpectationOfSubstitutions.h"
#include "definitions.h"
#include "computeDownAlg.h"
#include "computeUpAlg.h"
#include "matrixUtils.h"
#include "treeIt.h"
#include "likelihoodComputation.h"
using namespace std;
/********************************************************************************************
computePosteriorExpectationOfSubstitutions
*********************************************************************************************/
computePosteriorExpectationOfSubstitutions::computePosteriorExpectationOfSubstitutions(const tree &tr, const sequenceContainer &sc, const stochasticProcess *sp):
_tr(tr), _sc(sc){
if(!sp){
errorMsg::reportError("error in the constructor computePosteriorExpectationOfSubstitutions sp argument is NULL");
}
else{
_sp = sp;
}
}
/********************************************************************************************
Expectation of number of substitutions from character u to v --- =
sum over all substitutions x,y:
Posterior(Node=x,Father=y|D)*Exp(substitutions u to v|Node=x,Father=y)
The second term is given to the function as input (can be obtained via simulations)
*********************************************************************************************/
VVdouble computePosteriorExpectationOfSubstitutions::computeExpectationAcrossTree(
simulateJumpsAbstract &sim, //input given from simulation studies
const VVVdouble &posteriorProbs,
VVVdouble &expForBranch)
{
//int numNodes = _tr.getNodesNum();
int alphabetSize = _sp->alphabetSize();
VVdouble res;
resizeMatrix(res,alphabetSize,alphabetSize);
treeIterTopDownConst tIt(_tr);
for (tree::nodeP mynode = tIt.first(); mynode != tIt.end(); mynode = tIt.next()) {
for (int fromState=0;fromState<alphabetSize;++fromState)
{
for (int toState=0;toState<alphabetSize;++toState)
{
if (fromState==toState)
continue;
expForBranch[mynode->id()][fromState][toState] = computeExpectationOfChangePerBranch(sim,posteriorProbs,mynode,fromState,toState);
res[fromState][toState] +=expForBranch[mynode->id()][fromState][toState];
}
}
}
return res;
}
/********************************************************************************************
Posterior probabilities computed across entire tree, for all substitutions from character u to v
*********************************************************************************************/
VVdouble computePosteriorExpectationOfSubstitutions::computePosteriorAcrossTree(
simulateJumpsAbstract &sim, //input given from simulation studies
const VVVdouble &posteriorProbsGivenTerminals,VVVdouble &probsForBranch)
{
//int numNodes = _tr.getNodesNum();
int alphabetSize = _sp->alphabetSize();
// N: resized before
//probsForBranch.resize(numNodes);
//for (int n=0;n<numNodes;++n)
// resizeMatrix(probsForBranch[n],alphabetSize,alphabetSize);
VVdouble res;
resizeMatrix(res,alphabetSize,alphabetSize);
treeIterTopDownConst tIt(_tr);
for (tree::nodeP mynode = tIt.first(); mynode != tIt.end(); mynode = tIt.next()) {
for (int fromState=0;fromState<alphabetSize;++fromState)
{
for (int toState=0;toState<alphabetSize;++toState)
{
if (fromState==toState)
continue;
probsForBranch[mynode->id()][fromState][toState]= computePosteriorOfChangePerBranch(sim,posteriorProbsGivenTerminals,mynode,fromState,toState);
res[fromState][toState] +=probsForBranch[mynode->id()][fromState][toState];
}
}
}
return res;
}
/********************************************************************************************
*********************************************************************************************/
MDOUBLE computePosteriorExpectationOfSubstitutions::computePosteriorOfChangePerBranch(simulateJumpsAbstract &sim, //input given from simulation studies
const VVVdouble &posteriorProbs,
tree::nodeP node,
int fromState, int toState)
{
int alphabetSize = _sp->alphabetSize();
MDOUBLE res = 0;
for (int x=0;x<alphabetSize;++x)
{
for (int y=0;y<alphabetSize;++y)
{
res+=sim.getProb(node->name(),x,y,fromState,toState)*posteriorProbs[node->id()][x][y];
}
}
return res;
}
/********************************************************************************************
Posterior of observing a certain state substitution along a branch:
P(Node=x,Father=y|D) = P(D,Node=x,Father=y)/P(D)
usage: posteriorPerNodePer2States[mynode->id()][fatherState][sonState]
*********************************************************************************************/
void computePosteriorExpectationOfSubstitutions::computePosteriorOfChangeGivenTerminals(VVVdouble &posteriorPerNodePer2States, int pos){
int numNodes = _tr.getNodesNum();
int alphabetSize = _sp->alphabetSize();
posteriorPerNodePer2States.resize(numNodes);
for (int n=0;n<posteriorPerNodePer2States.size();++n)
resizeMatrix(posteriorPerNodePer2States[n],alphabetSize,alphabetSize);
suffStatGlobalHomPos sscUp;
suffStatGlobalHomPos sscDown; //for a reversible model
sscUp.allocatePlace(numNodes,alphabetSize);
computePijHom pi;
pi.fillPij(_tr,*_sp);
computeUpAlg comp_Up;
computeDownAlg comp_Down;
comp_Up.fillComputeUp(_tr,_sc,pos,pi,sscUp);
comp_Down.fillComputeDown(_tr,_sc,pos,pi,sscDown,sscUp);
treeIterTopDownConst tIt(_tr);
MDOUBLE ll = convert(likelihoodComputation::getLofPos(pos,_tr,_sc,pi,*_sp));
for (tree::nodeP mynode = tIt.first(); mynode != tIt.end(); mynode = tIt.next()) {
for (int sonState = 0; sonState<alphabetSize; ++sonState){
for (int fatherState = 0; fatherState<alphabetSize; ++fatherState){
posteriorPerNodePer2States[mynode->id()][fatherState][sonState]= computePosterioGivenTerminalsPerBranch(mynode->id(),sonState,fatherState,sscUp,sscDown, pi,ll,mynode->name());
}
}
}
}
/********************************************************************************************
Posterior of observing a certain state substitution along a branch:
P(Node=sonState,Father=fatherState|D) = P(D,Node=sonState,Father=fatherState)/P(D)
usage: posteriorPerNodePer2States[mynode->id()][fatherState][sonState]
*********************************************************************************************/
MDOUBLE computePosteriorExpectationOfSubstitutions::computePosterioGivenTerminalsPerBranch
(int nodeId,int sonState, int fatherState,suffStatGlobalHomPos &sscUp,
suffStatGlobalHomPos &sscDown,computePijHom &pi, MDOUBLE &LLData, const string nodeName)
{
MDOUBLE res, Down, Up, pij;
Down = convert(sscDown.get(nodeId,fatherState));
Up = convert(sscUp.get(nodeId,sonState));
pij = pi.getPij(nodeId,fatherState,sonState);
res=_sp->freq(fatherState)*Down*Up*pij;
res/=LLData;
// if(gainLossOptions::_printDEBUGinfo)
// LOG(3,<<nodeName<<" son "<<sonState<<" Down "<<Down<<" father "<<fatherState<<" Up "<<Up<<" pij "<<pij<<" resDXY "<<resDXY<<" LLData "<<LLData<<" prob "<<res<<endl);
if (res > 1 + 1e-4){
LOGnOUT(3,<<nodeId<<" son "<<sonState<<" Down "<<Down<<" father "<<fatherState<<" Up "<<Up<<" pij "<<pij<<" res "<<res<<" LLData "<<LLData<<endl);
res = 1;
}
if (res<-1e-4){
LOGnOUT(3,<<nodeId<<" son "<<sonState<<" Down "<<Down<<" father "<<fatherState<<" Up "<<Up<<" pij "<<pij<<" res "<<res<<" LLData "<<LLData<<endl);
res = 0;
}
if ((res > 1 + 0.000001) || (res<-0.000001)){
string err = "Error in computePosteriorExpectationOfSubstitutions::computePosterioGivenTerminalsPerBranch, non probability value ";
err+=double2string(res);
err+=" at node ";
err+=int2string(nodeId);
err+= " sonState ";
err+= int2string(sonState);
err+= " fatherState ";
err+= int2string(fatherState);
errorMsg::reportError(err);
}
return res;
}
/********************************************************************************************
*********************************************************************************************/
MDOUBLE computePosteriorExpectationOfSubstitutions::computeExpectationOfChangePerBranch(
simulateJumpsAbstract &sim, //input given from simulation studies
const VVVdouble &posteriorProbsGivenTerminals,
tree::nodeP node,int fromState, int toState)
{
int alphabetSize = _sp->alphabetSize();
MDOUBLE nodeExpectation = 0;
for (int x = 0; x<alphabetSize; ++x){
for (int y = 0; y<alphabetSize; ++y){
nodeExpectation+=(posteriorProbsGivenTerminals[node->id()][x][y]*
sim.getExpectation(node->name(),x,y,fromState,toState));
//DEBUG
LOG(6,<<"node "<<node->id()<<endl);
LOG(6,<<"from "<<fromState<<" to "<<toState<<" given "<<x<<" and "<<y
<<" post= "<<posteriorProbsGivenTerminals[node->id()][x][y]<<" sim= "<< sim.getExpectation(node->name(),x,y,fromState,toState)<<endl);
}
}
return nodeExpectation;
}
|