| 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 |
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 |
|
| 44 |
|
| 45 |
@Deprecated |
| 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 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 77 |
if (parent != null && updateNode[nodeNum]) { |
| 78 |
|
| 79 |
final double branchRate = branchRateModel.getBranchRate(tree, node); |
| 80 |
|
| 81 |
|
| 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 |
|
| 103 |
if (!tree.isExternal(node)) { |
| 104 |
|
| 105 |
|
| 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 |
|
| 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 |
|
| 128 |
|
| 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 |
|
| 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 |
|
| 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 |
} |