| 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.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 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 122 |
|
| 123 |
dataModel = new ContinuousTraitDataModel(traitName, |
| 124 |
traitParameter, |
| 125 |
missingIndices, useMissingIndices, |
| 126 |
dim, precisionType); |
| 127 |
} else { |
| 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 |
|
| 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 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return treeDataLikelihood; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 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 |
} |