Debian Codegraph @

src/dr/oldevomodel/treelikelihood/NodePosteriorTreeLikelihood.java

raw symbols SCIP
1 /*
2 * NodePosteriorTreeLikelihood.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.oldevomodel.treelikelihood;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.tree.TreeTrait;
32 import dr.evolution.tree.TreeTraitProvider;
33 import dr.evomodel.branchratemodel.BranchRateModel;
34 import dr.oldevomodel.sitemodel.SiteModel;
35 import dr.oldevomodel.substmodel.SubstitutionModel;
36 import dr.evomodel.tipstatesmodel.TipStatesModel;
37 import dr.evomodel.tree.TreeModel;
38 import dr.oldevomodelxml.treelikelihood.TreeLikelihoodParser;
39 import dr.inference.model.Likelihood;
40 import dr.xml.*;
41
42 /**
43 * Package: dr.evomodel.treelikelihood
44 * Description:
45 * <p/>
46 * <p/>
47 * Created by
48 * avaleks ([email protected])
49 * Date: 01-Aug-2008
50 * Time: 10:36:39
51 */
52 @Deprecated // Switching to BEAGLE
53 public class NodePosteriorTreeLikelihood extends TreeLikelihood implements TreeTraitProvider {
54
55 protected double[][] nodePosteriors;
56 protected double[][] forwardProbs;
57 protected double[] likes;
58 boolean posteriorsKnown;
59 private double[] childPartials;
60 private double[] partialLikelihood;
61
62 public NodePosteriorTreeLikelihood(PatternList patternList, TreeModel treeModel, SiteModel siteModel, BranchRateModel branchRateModel, TipStatesModel tipStatesModel, boolean useAmbiguities, boolean allowMissingTaxa, boolean storePartials, boolean forceJavaCore) {
63 super(patternList, treeModel, siteModel, branchRateModel, tipStatesModel, useAmbiguities, allowMissingTaxa, storePartials, forceJavaCore, false);
64 // TreeLikelihood does not initialize the partials for tips, we'll do it ourselves
65 int extNodeCount = treeModel.getExternalNodeCount();
66 for (int i = 0; i < extNodeCount; i++) {
67 String id = treeModel.getTaxonId(i);
68 int index = patternList.getTaxonIndex(id);
69 setPartials(likelihoodCore, patternList, categoryCount, index, i);
70 }
71 childPartials = new double[stateCount * patternCount];
72 partialLikelihood = new double[stateCount * patternCount];
73 posteriorsKnown = false;
74 }
75
76 TreeTrait posteriors = new TreeTrait.DA() {
77 public String getTraitName() {
78 return "posteriors";
79 }
80
81 public Intent getIntent() {
82 return Intent.NODE;
83 }
84
85 public double[] getTrait(Tree tree, NodeRef node) {
86 if (tree != treeModel) {
87 throw new RuntimeException("Can only calculate node posteriors on treeModel given to constructor");
88 }
89 if (!posteriorsKnown) {
90 calculatePosteriors();
91 }
92 return nodePosteriors[node.getNumber()];
93 }
94
95 };
96
97 public TreeTrait[] getTreeTraits() {
98 return new TreeTrait[] { posteriors };
99 }
100
101 public TreeTrait getTreeTrait(String key) {
102 // ignore the key - it must be the one they wanted, no?
103 return posteriors;
104 }
105
106 public double[] getPosteriors(int nodeNum) {
107 if (!posteriorsKnown) {
108 calculatePosteriors();
109 }
110 return nodePosteriors[nodeNum];
111 }
112
113 public void getNodeMatrix(int nodeNum, double[] probabilities) {
114 ((AbstractLikelihoodCore) likelihoodCore).getNodeMatrix(nodeNum, 0, probabilities);
115 }
116
117 public void calculatePosteriors() {
118 int nodeCount = treeModel.getNodeCount();
119 traverseForward(treeModel, treeModel.getRoot());
120 for (int k = 0; k < nodeCount; ++k) {
121 for (int i = 0; i < patternCount; ++i) {
122 for (int j = 0; j < stateCount; ++j) {
123 nodePosteriors[k][stateCount * i + j] /= likes[i];
124 }
125 }
126 }
127 posteriorsKnown = true;
128 }
129
130 protected double calculateLogLikelihood() {
131 posteriorsKnown = false;
132
133 return super.calculateLogLikelihood();
134 }
135
136
137 public void traverseForward(TreeModel tree, NodeRef node) {
138 if (nodePosteriors == null) {
139 nodePosteriors = new double[tree.getNodeCount()][patternCount * stateCount];
140 }
141 if (forwardProbs == null) {
142 forwardProbs = new double[tree.getNodeCount()][patternCount * stateCount];
143 }
144 int nodeNum = node.getNumber();
145
146 NodeRef parent = tree.getParent(node);
147
148
149 if (parent == null) {/* this is the root */
150 double[] rootFreqs = frequencyModel.getFrequencies();
151 double[] rootPartials = getRootPartials();
152 if (likes == null) {
153 likes = new double[patternCount];
154 }
155
156 for (int j = 0; j < patternCount; j++) {
157 System.arraycopy(rootFreqs, 0, forwardProbs[nodeNum], j * stateCount, stateCount);
158 likes[j] = 0.0;
159 for (int k = 0; k < stateCount; ++k) {
160 nodePosteriors[nodeNum][stateCount * j + k] = forwardProbs[nodeNum][stateCount * j + k] * rootPartials[j * stateCount + k];
161 likes[j] = likes[j] + rootPartials[stateCount * j + k] * rootFreqs[k];
162 }
163 }
164
165 } else { /* regular internal node */
166 int parentNum = parent.getNumber();
167 int numChildren = tree.getChildCount(parent);
168
169 System.arraycopy(forwardProbs[parentNum], 0, forwardProbs[nodeNum], 0, stateCount * patternCount);
170
171 for (int child = 0; child < numChildren; ++child) {
172 NodeRef childNode = tree.getChild(parent, child);
173 int childNum = childNode.getNumber();
174 if (childNum != nodeNum) {
175 getNodeMatrix(childNum, probabilities);
176 likelihoodCore.getPartials(childNum, childPartials);
177 accumulateMatrixMultiply(probabilities, childPartials, forwardProbs[nodeNum]);
178 }
179 }
180
181 getNodeMatrix(nodeNum, probabilities);
182 likelihoodCore.getPartials(nodeNum, partialLikelihood);
183
184 matrixMultiplyBackward(probabilities, forwardProbs[nodeNum], nodePosteriors[nodeNum]);
185 for (int i = 0; i < patternCount * stateCount; ++i) {
186 forwardProbs[nodeNum][i] = nodePosteriors[nodeNum][i];
187 nodePosteriors[nodeNum][i] = nodePosteriors[nodeNum][i] * partialLikelihood[i];
188 }
189 }
190 if (!tree.isExternal(node)) { /* looking at internal nodes here */
191 for (int child = 0; child < tree.getChildCount(node); ++child) {
192 NodeRef child1 = tree.getChild(node, child);
193 traverseForward(tree, child1);
194 }
195 }
196 }
197
198 public void accumulateMatrixMultiply(double[] probabilities, double[] partials, double[] out) {
199 int u = 0;
200 int v = 0;
201
202 for (int k = 0; k < patternCount; ++k) {
203 int w = 0;
204 for (int i = 0; i < stateCount; ++i) {
205 double sum1 = 0.0;
206 for (int j = 0; j < stateCount; ++j) {
207 sum1 += probabilities[w] * partials[v + j];
208 w++;
209 }
210 out[u] = sum1 * out[u];
211 u++;
212 }
213 v += stateCount;
214 }
215 }
216
217 public void matrixMultiplyBackward(double[] probabilities, double[] partials, double[] out) {
218 int u = 0;
219 int v = 0;
220 for (int k = 0; k < patternCount; ++k) {
221 for (int i = 0; i < stateCount; ++i) {
222 int w = i;
223 double sum1 = 0.0;
224 for (int j = 0; j < stateCount; ++j) {
225 sum1 += probabilities[w] * partials[v + j];
226 w += stateCount;
227 }
228 out[u] = sum1;
229 u++;
230 }
231 v += stateCount;
232 }
233 }
234
235 /**
236 * The XML parser
237 */
238
239 public static final String NODE_POSTERIOR_LIKELIHOOD = "nodePosteriorLikelihood";
240 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
241
242 public String getParserName() {
243 return NODE_POSTERIOR_LIKELIHOOD;
244 }
245
246 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
247
248 boolean useAmbiguities = xo.getAttribute(TreeLikelihoodParser.USE_AMBIGUITIES, false);
249 boolean storePartials = xo.getAttribute(TreeLikelihoodParser.STORE_PARTIALS, true);
250
251 PatternList patternList = (PatternList) xo.getChild(PatternList.class);
252 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
253 SiteModel siteModel = (SiteModel) xo.getChild(SiteModel.class);
254
255 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
256
257 return new NodePosteriorTreeLikelihood(patternList, treeModel, siteModel,
258 branchRateModel, null, useAmbiguities, false, storePartials, false);
259 }
260
261 //************************************************************************
262 // AbstractXMLObjectParser implementation
263 //************************************************************************
264
265 public String getParserDescription() {
266 return "This element represents the likelihood of a patternlist on a tree given the site model.";
267 }
268
269 public Class getReturnType() {
270 return Likelihood.class;
271 }
272
273 public XMLSyntaxRule[] getSyntaxRules() {
274 return rules;
275 }
276
277 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
278 AttributeRule.newBooleanRule(TreeLikelihoodParser.USE_AMBIGUITIES, true),
279 AttributeRule.newBooleanRule(TreeLikelihoodParser.ALLOW_MISSING_TAXA, true),
280 new ElementRule(PatternList.class),
281 new ElementRule(TreeModel.class),
282 new ElementRule(SiteModel.class),
283 new ElementRule(BranchRateModel.class, true),
284 new ElementRule(SubstitutionModel.class)
285 };
286 };
287
288 }
289