Debian Codegraph @

src/dr/evomodelxml/treedatalikelihood/ContinuousDataLikelihoodParser.java

raw symbols SCIP
1 /*
2 * ContinuousDataLikelihoodParser.java
3 *
4 * Copyright (c) 2002-2016 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.evomodelxml.treedatalikelihood;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.tree.TreeTraitProvider;
30 import dr.evomodel.branchratemodel.BranchRateModel;
31 import dr.evomodel.branchratemodel.DefaultBranchRateModel;
32 import dr.evomodel.continuous.*;
33 import dr.evomodel.treedatalikelihood.ProcessSimulation;
34 import dr.evomodel.treedatalikelihood.preorder.ConditionalOnTipsRealizedDelegate;
35 import dr.evomodel.treedatalikelihood.preorder.MultivariateConditionalOnTipsRealizedDelegate;
36 import dr.evomodel.treedatalikelihood.preorder.ProcessSimulationDelegate;
37 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
38 import dr.evomodel.treedatalikelihood.continuous.*;
39 import dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType;
40 import dr.evomodel.treedatalikelihood.preorder.TipRealizedValuesViaFullConditionalDelegate;
41 import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities;
42 import dr.inference.model.CompoundParameter;
43 import dr.inference.model.Parameter;
44 import dr.xml.*;
45
46 import java.util.List;
47
48 /**
49 * @author Andrew Rambaut
50 * @author Marc Suchard
51 * @version $Id$
52 */
53 public class ContinuousDataLikelihoodParser extends AbstractXMLObjectParser {
54
55 public static final String CONJUGATE_ROOT_PRIOR = AbstractMultivariateTraitLikelihood.CONJUGATE_ROOT_PRIOR;
56 private static final String USE_TREE_LENGTH = AbstractMultivariateTraitLikelihood.USE_TREE_LENGTH;
57 private static final String SCALE_BY_TIME = AbstractMultivariateTraitLikelihood.SCALE_BY_TIME;
58 private static final String RECIPROCAL_RATES = AbstractMultivariateTraitLikelihood.RECIPROCAL_RATES;
59 private static final String DRIFT_MODELS = AbstractMultivariateTraitLikelihood.DRIFT_MODELS;
60
61 private static final String RECONSTRUCT_TRAITS = "reconstructTraits";
62 private static final String FORCE_COMPLETELY_MISSING = "forceCompletelyMissing";
63 private static final String ALLOW_SINGULAR = "allowSingular";
64 private static final String FORCE_FULL_PRECISION = "forceFullPrecision";
65 private static final String FORCE_DRIFT = "forceDrift";
66
67 private static final String CONTINUOUS_DATA_LIKELIHOOD = "traitDataLikelihood";
68
69 public String getParserName() {
70 return CONTINUOUS_DATA_LIKELIHOOD;
71 }
72
73 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
74
75 Tree treeModel = (Tree) xo.getChild(Tree.class);
76 MultivariateDiffusionModel diffusionModel = (MultivariateDiffusionModel) xo.getChild(MultivariateDiffusionModel.class);
77 BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
78
79 boolean useTreeLength = xo.getAttribute(USE_TREE_LENGTH, false);
80 boolean scaleByTime = xo.getAttribute(SCALE_BY_TIME, false);
81 boolean reciprocalRates = xo.getAttribute(RECIPROCAL_RATES, false);
82
83 if (reciprocalRates) {
84 throw new XMLParseException("Reciprocal rates are not yet implemented.");
85 }
86
87 if (rateModel == null) {
88 rateModel = new DefaultBranchRateModel();
89 }
90
91 ContinuousRateTransformation rateTransformation = new ContinuousRateTransformation.Default(
92 treeModel, scaleByTime, useTreeLength);
93
94 final int dim = diffusionModel.getPrecisionmatrix().length;
95 ConjugateRootTraitPrior rootPrior = ConjugateRootTraitPrior.parseConjugateRootTraitPrior(xo, dim);
96
97 String traitName = TreeTraitParserUtilities.DEFAULT_TRAIT_NAME;
98 List<Integer> missingIndices;
99 // Parameter sampleMissingParameter = null;
100 ContinuousTraitPartialsProvider dataModel;
101 boolean useMissingIndices = true;
102
103 if (xo.hasChildNamed(TreeTraitParserUtilities.TRAIT_PARAMETER)) {
104 TreeTraitParserUtilities utilities = new TreeTraitParserUtilities();
105
106 TreeTraitParserUtilities.TraitsAndMissingIndices returnValue =
107 utilities.parseTraitsFromTaxonAttributes(xo, traitName, treeModel, true);
108 CompoundParameter traitParameter = returnValue.traitParameter;
109 missingIndices = returnValue.missingIndices;
110 // sampleMissingParameter = returnValue.sampleMissingParameter;
111 traitName = returnValue.traitName;
112 useMissingIndices = returnValue.useMissingIndices;
113
114 PrecisionType precisionType = PrecisionType.SCALAR;
115
116 if (xo.getAttribute(FORCE_FULL_PRECISION, false) ||
117 (useMissingIndices && !xo.getAttribute(FORCE_COMPLETELY_MISSING, false))) {
118 precisionType = PrecisionType.FULL;
119 }
120
121 // System.err.println("Using precisionType == " + precisionType + " for data model.");
122
123 dataModel = new ContinuousTraitDataModel(traitName,
124 traitParameter,
125 missingIndices, useMissingIndices,
126 dim, precisionType);
127 } else { // Has ContinuousTraitPartialsProvider
128 dataModel = (ContinuousTraitPartialsProvider) xo.getChild(ContinuousTraitPartialsProvider.class);
129 }
130
131 final boolean allowSingular;
132 if (dataModel instanceof IntegratedFactorAnalysisLikelihood) {
133 if (xo.hasAttribute(ALLOW_SINGULAR)) {
134 allowSingular = xo.getAttribute(ALLOW_SINGULAR, false);
135 } else {
136 allowSingular = true;
137 }
138 } else {
139 allowSingular = xo.getAttribute(ALLOW_SINGULAR, false);
140 }
141
142 List<BranchRateModel> driftModels = AbstractMultivariateTraitLikelihood.parseDriftModels(xo, diffusionModel);
143
144 DiffusionProcessDelegate diffusionProcessDelegate;
145 if (driftModels != null || xo.getAttribute(FORCE_DRIFT, false)) {
146 diffusionProcessDelegate = new DriftDiffusionModelDelegate(treeModel, diffusionModel, driftModels);
147 } else {
148 diffusionProcessDelegate = new HomogeneousDiffusionModelDelegate(treeModel, diffusionModel);
149 }
150
151 ContinuousDataLikelihoodDelegate delegate = new ContinuousDataLikelihoodDelegate(treeModel,
152 diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, allowSingular);
153
154 if (dataModel instanceof IntegratedFactorAnalysisLikelihood) {
155 ((IntegratedFactorAnalysisLikelihood)dataModel).setLikelihoodDelegate(delegate);
156 }
157
158 TreeDataLikelihood treeDataLikelihood = new TreeDataLikelihood(delegate, treeModel,
159 rateModel);
160
161 boolean reconstructTraits = xo.getAttribute(RECONSTRUCT_TRAITS, true);
162 if (reconstructTraits) {
163
164 // if (missingIndices != null && missingIndices.size() == 0) {
165 if (!useMissingIndices) {
166
167 ProcessSimulationDelegate simulationDelegate =
168 delegate.getPrecisionType()== PrecisionType.SCALAR ?
169 new ConditionalOnTipsRealizedDelegate(traitName, treeModel,
170 diffusionModel, dataModel, rootPrior, rateTransformation, delegate) :
171 new MultivariateConditionalOnTipsRealizedDelegate(traitName, treeModel,
172 diffusionModel, dataModel, rootPrior, rateTransformation, delegate);
173
174 TreeTraitProvider traitProvider = new ProcessSimulation(treeDataLikelihood, simulationDelegate);
175
176 treeDataLikelihood.addTraits(traitProvider.getTreeTraits());
177
178 } else {
179
180 ProcessSimulationDelegate simulationDelegate =
181 delegate.getPrecisionType()== PrecisionType.SCALAR ?
182 new ConditionalOnTipsRealizedDelegate(traitName, treeModel,
183 diffusionModel, dataModel, rootPrior, rateTransformation, delegate) :
184 new MultivariateConditionalOnTipsRealizedDelegate(traitName, treeModel,
185 diffusionModel, dataModel, rootPrior, rateTransformation, delegate);
186
187 TreeTraitProvider traitProvider = new ProcessSimulation(treeDataLikelihood, simulationDelegate);
188
189 treeDataLikelihood.addTraits(traitProvider.getTreeTraits());
190
191 ProcessSimulationDelegate fullConditionalDelegate = new TipRealizedValuesViaFullConditionalDelegate(
192 traitName, treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, delegate);
193
194 treeDataLikelihood.addTraits(new ProcessSimulation(treeDataLikelihood, fullConditionalDelegate).getTreeTraits());
195
196 // String partialTraitName = getPartiallyMissingTraitName(traitName);
197 //
198 // ProcessSimulationDelegate partialSimulationDelegate = new ProcessSimulationDelegate.ConditionalOnPartiallyMissingTipsDelegate(partialTraitName,
199 // treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, rateModel, delegate);
200 //
201 // TreeTraitProvider partialTraitProvider = new ProcessSimulation(partialTraitName,
202 // treeDataLikelihood, partialSimulationDelegate);
203 //
204 // treeDataLikelihood.addTraits(partialTraitProvider.getTreeTraits());
205 }
206 }
207
208 return treeDataLikelihood;
209 }
210
211 //************************************************************************
212 // AbstractXMLObjectParser implementation
213 //************************************************************************
214
215 public String getParserDescription() {
216 return "This element represents the likelihood of trait data on a tree given a diffusion model.";
217 }
218
219 public Class getReturnType() {
220 return TreeDataLikelihood.class;
221 }
222
223 public static final XMLSyntaxRule[] rules = {
224 new ElementRule(Tree.class),
225 new ElementRule(MultivariateDiffusionModel.class),
226 new ElementRule(BranchRateModel.class, true),
227 new ElementRule(CONJUGATE_ROOT_PRIOR, ConjugateRootTraitPrior.rules),
228 new XORRule(
229 new ElementRule(TreeTraitParserUtilities.TRAIT_PARAMETER, new XMLSyntaxRule[] {
230 new ElementRule(Parameter.class),
231 }),
232 new ElementRule(ContinuousTraitPartialsProvider.class)
233 ),
234 new ElementRule(DRIFT_MODELS, new XMLSyntaxRule[]{
235 new ElementRule(BranchRateModel.class, 1, Integer.MAX_VALUE),
236 }, true),
237 AttributeRule.newBooleanRule(SCALE_BY_TIME, true),
238 AttributeRule.newBooleanRule(USE_TREE_LENGTH, true),
239 AttributeRule.newBooleanRule(RECIPROCAL_RATES, true),
240 AttributeRule.newBooleanRule(RECONSTRUCT_TRAITS, true),
241 AttributeRule.newBooleanRule(FORCE_COMPLETELY_MISSING, true),
242 AttributeRule.newBooleanRule(ALLOW_SINGULAR, true),
243 AttributeRule.newBooleanRule(FORCE_FULL_PRECISION, true),
244 };
245
246 public XMLSyntaxRule[] getSyntaxRules() {
247 return rules;
248 }
249 }