Debian Codegraph @

src/dr/oldevomodel/treelikelihood/EpochTreeLikelihood.java

raw symbols SCIP
1 /*
2 * EpochTreeLikelihood.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.evomodel.branchratemodel.BranchRateModel;
32 import dr.oldevomodel.sitemodel.SiteModel;
33 import dr.oldevomodel.substmodel.SubstitutionEpochModel;
34 import dr.evomodel.tipstatesmodel.TipStatesModel;
35 import dr.evomodel.tree.TreeModel;
36 import dr.oldevomodelxml.treelikelihood.TreeLikelihoodParser;
37 import dr.inference.model.Likelihood;
38 import dr.xml.*;
39
40 import java.util.logging.Logger;
41
42 /**
43 * @author Marc A. Suchard
44 */
45 @Deprecated // Switching to BEAGLE
46 public class EpochTreeLikelihood extends TreeLikelihood {
47 public static final String LIKE_NAME = "epochTreeLikelihood";
48
49 public EpochTreeLikelihood(PatternList patternList,
50 TreeModel treeModel,
51 SiteModel siteModel,
52 BranchRateModel branchRateModel,
53 TipStatesModel tipStatesModel,
54 boolean useAmbiguities,
55 boolean allowMissingTaxa,
56 boolean storePartials,
57 boolean forceJavaCore) {
58 super(patternList, treeModel, siteModel, branchRateModel, tipStatesModel,useAmbiguities,allowMissingTaxa,storePartials,forceJavaCore, false);
59
60 }
61
62
63 /**
64 * Traverse the tree calculating partial likelihoods.
65 *
66 * @return whether the partials for this node were recalculated.
67 */
68 protected boolean traverse(Tree tree, NodeRef node) {
69
70 boolean update = false;
71
72 int nodeNum = node.getNumber();
73
74 NodeRef parent = tree.getParent(node);
75
76 // First update the transition probability matrix(ices) for this branch
77 if (parent != null && updateNode[nodeNum]) {
78
79 final double branchRate = branchRateModel.getBranchRate(tree, node);
80
81 // Get the operational time of the branch
82 final double parentNodeHeight = tree.getNodeHeight(parent);
83 final double nodeHeight = tree.getNodeHeight(node);
84 final double branchTime = branchRate * (parentNodeHeight - nodeHeight);
85
86 if (branchTime < 0.0) {
87 throw new RuntimeException("Negative branch length: " + branchTime);
88 }
89
90 likelihoodCore.setNodeMatrixForUpdate(nodeNum);
91
92 for (int i = 0; i < categoryCount; i++) {
93
94 double branchLength = siteModel.getRateForCategory(i) * branchTime;
95 ((SubstitutionEpochModel)siteModel.getSubstitutionModel()).getTransitionProbabilities(nodeHeight, parentNodeHeight,branchLength, probabilities);
96 likelihoodCore.setNodeMatrix(nodeNum, i, probabilities);
97 }
98
99 update = true;
100 }
101
102 // If the node is internal, update the partial likelihoods.
103 if (!tree.isExternal(node)) {
104
105 // Traverse down the two child nodes
106 NodeRef child1 = tree.getChild(node, 0);
107 final boolean update1 = traverse(tree, child1);
108
109 NodeRef child2 = tree.getChild(node, 1);
110 final boolean update2 = traverse(tree, child2);
111
112 // If either child node was updated then update this node too
113 if (update1 || update2) {
114
115 final int childNum1 = child1.getNumber();
116 final int childNum2 = child2.getNumber();
117
118 likelihoodCore.setNodePartialsForUpdate(nodeNum);
119
120 if (integrateAcrossCategories) {
121 likelihoodCore.calculatePartials(childNum1, childNum2, nodeNum);
122 } else {
123 likelihoodCore.calculatePartials(childNum1, childNum2, nodeNum, siteCategories);
124 }
125
126 if (parent == null) {
127 // No parent this is the root of the tree -
128 // calculate the pattern likelihoods
129 double[] frequencies = frequencyModel.getFrequencies();
130
131 double[] partials = getRootPartials();
132
133 likelihoodCore.calculateLogLikelihoods(partials, frequencies, patternLogLikelihoods);
134 }
135
136 update = true;
137 }
138 }
139
140 return update;
141
142 }
143
144
145 /**
146 * The XML parser
147 */
148 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
149
150 public String getParserName() {
151 return LIKE_NAME;
152 }
153
154 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
155
156 boolean useAmbiguities = xo.getAttribute(TreeLikelihoodParser.USE_AMBIGUITIES, false);
157 boolean allowMissingTaxa = xo.getAttribute(TreeLikelihoodParser.ALLOW_MISSING_TAXA, false);
158 boolean storePartials = xo.getAttribute(TreeLikelihoodParser.STORE_PARTIALS, true);
159 boolean forceJavaCore = xo.getAttribute(TreeLikelihoodParser.FORCE_JAVA_CORE, false);
160
161 PatternList patternList = (PatternList) xo.getChild(PatternList.class);
162 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
163 SiteModel siteModel = (SiteModel) xo.getChild(SiteModel.class);
164
165 if (! (siteModel.getSubstitutionModel() instanceof SubstitutionEpochModel)) {
166 throw new XMLParseException("EpochTreeLikelihood only currently works with a SubstitutionEpochModel");
167 }
168
169 BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
170
171 TipStatesModel tipStatesModel = (TipStatesModel) xo.getChild(TipStatesModel.class);
172
173 Logger.getLogger("dr.evolution").info("\n ---------------------------------\nCreating EpochTreeLikelihood model.");
174 Logger.getLogger("dr.evolution").info("\tIf you publish results using substitution epoch likelihood, please reference" +
175 " Shapiro and Suchard (in preparation).\n---------------------------------\n");
176
177
178 return new EpochTreeLikelihood(
179 patternList,
180 treeModel,
181 siteModel,
182 branchRateModel,
183 tipStatesModel,
184 useAmbiguities, allowMissingTaxa, storePartials, forceJavaCore);
185 }
186
187 //************************************************************************
188 // AbstractXMLObjectParser implementation
189 //************************************************************************
190
191 public String getParserDescription() {
192 return "This element represents the likelihood of a patternlist on a tree given the site model.";
193 }
194
195 public Class getReturnType() {
196 return Likelihood.class;
197 }
198
199 public XMLSyntaxRule[] getSyntaxRules() {
200 return rules;
201 }
202
203 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
204 AttributeRule.newBooleanRule(TreeLikelihoodParser.USE_AMBIGUITIES, true),
205 AttributeRule.newBooleanRule(TreeLikelihoodParser.ALLOW_MISSING_TAXA, true),
206 AttributeRule.newBooleanRule(TreeLikelihoodParser.STORE_PARTIALS, true),
207 AttributeRule.newBooleanRule(TreeLikelihoodParser.FORCE_JAVA_CORE, true),
208 new ElementRule(PatternList.class),
209 new ElementRule(TreeModel.class),
210 new ElementRule(SiteModel.class),
211 new ElementRule(BranchRateModel.class, true),
212 new ElementRule(TipStatesModel.class, true)
213 };
214 };
215
216 }