Debian Codegraph @

src/dr/evomodel/operators/MicrosatelliteFullAncestryImportanceSamplingOperator.java

raw symbols SCIP
1 /*
2 * MsatFullAncestryImportanceSamplingOperator.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.evolution.tree.TreeUtils;
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.inference.model.Parameter;
34 import dr.evolution.tree.NodeRef;
35 import dr.inference.operators.SimpleMCMCOperator;
36 import dr.math.MathUtils;
37
38 /**
39 * @author Chieh-Hsi Wu
40 *
41 * Produce an importance sample of the ancestry given a msat pattern and a tree.
42 */
43 public class MicrosatelliteFullAncestryImportanceSamplingOperator extends SimpleMCMCOperator {
44
45 public static final String MSAT_FULL_ANCESTRY_IMPORTANCE_SAMPLING_OPERATOR = "MsatFullAncestryImportanceSamplingOperator";
46 private Parameter parameter;
47 private MicrosatelliteSamplerTreeModel msatSamplerTreeModel;
48 private MicrosatelliteModel msatModel;
49 private BranchRateModel branchRateModel;
50
51
52 public MicrosatelliteFullAncestryImportanceSamplingOperator(
53 Parameter parameter,
54 MicrosatelliteSamplerTreeModel msatSamplerTreeModel,
55 MicrosatelliteModel msatModel,
56 BranchRateModel branchRateModel,
57 double weight){
58
59 super();
60 this.parameter = parameter;
61 this.msatSamplerTreeModel = msatSamplerTreeModel;
62 this.msatModel = msatModel;
63 this.branchRateModel = branchRateModel;
64 setWeight(weight);
65 }
66
67 public double doOperation() {
68 TreeModel tree = msatSamplerTreeModel.getTreeModel();
69
70 //get postOrder
71 int[] postOrder = new int[tree.getNodeCount()];
72 TreeUtils.postOrderTraversalList(tree,postOrder);
73
74 int extNodeCount = tree.getExternalNodeCount();
75 double logq=0.0;
76 for(int i = 0; i < postOrder.length; i ++){
77
78 //if it's an internal node
79 if(postOrder[i] >= extNodeCount){
80
81 //getLikelihoodGiven the children
82 NodeRef node = tree.getNode(postOrder[i]);
83 NodeRef lc = tree.getChild(node,0);
84 NodeRef rc = tree.getChild(node,1);
85 int lcState = msatSamplerTreeModel.getNodeValue(lc);
86 int rcState = msatSamplerTreeModel.getNodeValue(rc);
87 double branchLeftLength = tree.getBranchLength(lc)*branchRateModel.getBranchRate(tree,lc);
88 double branchRightLength = tree.getBranchLength(rc)*branchRateModel.getBranchRate(tree,rc);
89 double[] probLbranch = msatModel.getColTransitionProbabilities(branchLeftLength, lcState);
90 double[] probRbranch = msatModel.getColTransitionProbabilities(branchRightLength, rcState);
91 double[] lik = new double[msatModel.getDataType().getStateCount()];
92 int currState = (int)parameter.getParameterValue(msatSamplerTreeModel.getParameterIndexFromNodeNumber(postOrder[i]));
93 //if node = root node
94 if(i == postOrder.length -1){
95 //likelihood of root state also depends on the stationary distribution
96 double[] statDist = msatModel.getStationaryDistribution();
97 for(int j = 0; j < lik.length; j++){
98 lik[j] = probLbranch[j]*probRbranch[j]*statDist[j];
99 }
100
101 }else{
102
103 for(int j = 0; j < lik.length; j++){
104 lik[j] = probLbranch[j]*probRbranch[j];
105 }
106
107 }
108
109 int sampledState = MathUtils.randomChoicePDF(lik);
110 logq = logq + Math.log(lik[currState]) - Math.log(lik[sampledState]);
111 parameter.setParameterValue(msatSamplerTreeModel.getParameterIndexFromNodeNumber(postOrder[i]),sampledState);
112 }
113 }
114
115 return logq;
116 }
117
118 public String getPerformanceSuggestion(){
119 return "None";
120 }
121 public String getOperatorName(){
122 return MSAT_FULL_ANCESTRY_IMPORTANCE_SAMPLING_OPERATOR;
123 }
124 public int getStepCount(){
125 return 1;
126 }
127 }