Debian Codegraph @

src/dr/evomodel/operators/MicrosatelliteSingleAncestralStateGibbsOperator.java

raw symbols SCIP
1 /*
2 * MsatSingleAncestralStateGibbsOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.operators;
27
28 import dr.inference.model.Parameter;
29 import dr.evomodel.tree.MicrosatelliteSamplerTreeModel;
30 import dr.evomodel.tree.TreeModel;
31 import dr.oldevomodel.substmodel.MicrosatelliteModel;
32 import dr.evomodel.branchratemodel.BranchRateModel;
33 import dr.evolution.tree.NodeRef;
34 import dr.inference.operators.GibbsOperator;
35 import dr.inference.operators.SimpleMCMCOperator;
36 import dr.math.MathUtils;
37
38 /**
39 * @author Chieh-Hsi Wu
40 *
41 *
42 */
43 public class MicrosatelliteSingleAncestralStateGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
44 public static final String MSAT_SINGLE_ANCESTAL_STATE_GIBBS_OPERATOR = "MsatSingleAncestralStateGibbsOperator";
45 private Parameter parameter;
46 private MicrosatelliteSamplerTreeModel msatSamplerTreeModel;
47 private MicrosatelliteModel msatModel;
48 private BranchRateModel branchRateModel;
49
50 public MicrosatelliteSingleAncestralStateGibbsOperator(
51 Parameter parameter,
52 MicrosatelliteSamplerTreeModel msatSamplerTreeModel,
53 MicrosatelliteModel msatModel,
54 BranchRateModel branchRateModel,
55 double weight){
56
57 super();
58 this.parameter = parameter;
59 this.msatSamplerTreeModel = msatSamplerTreeModel;
60 this.msatModel = msatModel;
61 this.branchRateModel = branchRateModel;
62 setWeight(weight);
63 }
64
65 public double doOperation() {
66 TreeModel tree = msatSamplerTreeModel.getTreeModel();
67
68
69 int index = MathUtils.nextInt(parameter.getDimension());
70
71 //double logq=0.0;
72
73
74 //getLikelihoodGiven the children
75 NodeRef node = tree.getNode(msatSamplerTreeModel.getNodeNumberFromParameterIndex(index));
76 NodeRef lc = tree.getChild(node,0);
77 NodeRef rc = tree.getChild(node,1);
78
79
80 int lcState = msatSamplerTreeModel.getNodeValue(lc);
81 int rcState = msatSamplerTreeModel.getNodeValue(rc);
82
83
84 double branchLeftLength = tree.getBranchLength(lc)*branchRateModel.getBranchRate(tree,lc);
85 double branchRightLength = tree.getBranchLength(rc)*branchRateModel.getBranchRate(tree,rc);
86
87
88 double[] probLbranch = msatModel.getColTransitionProbabilities(branchLeftLength, lcState);
89 double[] probRbranch = msatModel.getColTransitionProbabilities(branchRightLength, rcState);
90
91 double[] lik = new double[msatModel.getDataType().getStateCount()];
92 //int currState = (int)parameter.getParameterValue(index);
93 //if node = root node
94
95 if(tree.isRoot(node)){
96 //likelihood of root state also depends on the stationary distribution
97 double[] statDist = msatModel.getStationaryDistribution();
98 for(int j = 0; j < lik.length; j++){
99 lik[j] = probLbranch[j]*probRbranch[j]*statDist[j];
100 }
101 }else{
102 NodeRef parent = tree.getParent(node);
103 int pState = msatSamplerTreeModel.getNodeValue(parent);
104 double branchParentLength = tree.getBranchLength(node)*branchRateModel.getBranchRate(tree,node);
105 double[] probPbranch = msatModel.getRowTransitionProbabilities(branchParentLength,pState);
106 for(int j = 0; j < lik.length; j++){
107 lik[j] = probLbranch[j]*probRbranch[j]*probPbranch[j];
108 }
109 }
110
111 int sampledState = MathUtils.randomChoicePDF(lik);
112 //logq = logq + Math.log(lik[currState]) - Math.log(lik[sampledState]);
113 parameter.setParameterValue(index,sampledState);
114 return 0.0;
115 }
116
117 public String getPerformanceSuggestion(){
118 return "None";
119 }
120 public String getOperatorName(){
121 return MSAT_SINGLE_ANCESTAL_STATE_GIBBS_OPERATOR;
122 }
123 public int getStepCount(){
124 return 1;
125 }
126
127 }