Search in sources :

Example 1 with TreeTraitParserUtilities

use of dr.evomodelxml.treelikelihood.TreeTraitParserUtilities in project beast-mcmc by beast-dev.

the class DataFromTreeTipsParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    TreeTraitParserUtilities utilities = new TreeTraitParserUtilities();
    String traitName = (String) xo.getAttribute(TreeTraitParserUtilities.TRAIT_NAME);
    MutableTreeModel treeModel = (MutableTreeModel) xo.getChild(MutableTreeModel.class);
    TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = utilities.parseTraitsFromTaxonAttributes(xo, traitName, treeModel, true);
    MatrixParameter dataParameter = MatrixParameter.recast(returnValue.traitParameter.getId(), returnValue.traitParameter);
    if (xo.hasChildNamed(TreeTraitParserUtilities.MISSING)) {
        Parameter missing = (Parameter) xo.getChild(TreeTraitParserUtilities.MISSING).getChild(Parameter.class);
        missing.setDimension(dataParameter.getDimension());
        for (int i = 0; i < missing.getDimension(); i++) {
            if (returnValue.missingIndices.contains(i)) {
                missing.setParameterValue(i, 1);
            } else {
                missing.setParameterValue(i, 0);
            }
        }
    }
    return dataParameter;
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) TreeTraitParserUtilities(dr.evomodelxml.treelikelihood.TreeTraitParserUtilities) MutableTreeModel(dr.evolution.tree.MutableTreeModel) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter)

Example 2 with TreeTraitParserUtilities

use of dr.evomodelxml.treelikelihood.TreeTraitParserUtilities in project beast-mcmc by beast-dev.

the class ContinuousDataLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Tree treeModel = (Tree) xo.getChild(Tree.class);
    MultivariateDiffusionModel diffusionModel = (MultivariateDiffusionModel) xo.getChild(MultivariateDiffusionModel.class);
    BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
    boolean useTreeLength = xo.getAttribute(USE_TREE_LENGTH, false);
    boolean scaleByTime = xo.getAttribute(SCALE_BY_TIME, false);
    boolean reciprocalRates = xo.getAttribute(RECIPROCAL_RATES, false);
    if (reciprocalRates) {
        throw new XMLParseException("Reciprocal rates are not yet implemented.");
    }
    if (rateModel == null) {
        rateModel = new DefaultBranchRateModel();
    }
    ContinuousRateTransformation rateTransformation = new ContinuousRateTransformation.Default(treeModel, scaleByTime, useTreeLength);
    final int dim = diffusionModel.getPrecisionmatrix().length;
    String traitName = TreeTraitParserUtilities.DEFAULT_TRAIT_NAME;
    List<Integer> missingIndices;
    // Parameter sampleMissingParameter = null;
    ContinuousTraitPartialsProvider dataModel;
    boolean useMissingIndices = true;
    boolean integratedProcess = xo.getAttribute(INTEGRATED_PROCESS, false);
    if (xo.hasChildNamed(TreeTraitParserUtilities.TRAIT_PARAMETER)) {
        TreeTraitParserUtilities utilities = new TreeTraitParserUtilities();
        TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = utilities.parseTraitsFromTaxonAttributes(xo, traitName, treeModel, true);
        CompoundParameter traitParameter = returnValue.traitParameter;
        missingIndices = returnValue.missingIndices;
        // sampleMissingParameter = returnValue.sampleMissingParameter;
        traitName = returnValue.traitName;
        useMissingIndices = returnValue.useMissingIndices;
        PrecisionType precisionType = PrecisionType.SCALAR;
        if (xo.getAttribute(FORCE_FULL_PRECISION, false) || (useMissingIndices && !xo.getAttribute(FORCE_COMPLETELY_MISSING, false))) {
            precisionType = PrecisionType.FULL;
        }
        if (xo.hasChildNamed(TreeTraitParserUtilities.JITTER)) {
            utilities.jitter(xo, diffusionModel.getPrecisionmatrix().length, missingIndices);
        }
        if (!integratedProcess) {
            dataModel = new ContinuousTraitDataModel(traitName, traitParameter, missingIndices, useMissingIndices, dim, precisionType);
        } else {
            dataModel = new IntegratedProcessTraitDataModel(traitName, traitParameter, missingIndices, useMissingIndices, dim, precisionType);
        }
    } else {
        // Has ContinuousTraitPartialsProvider
        dataModel = (ContinuousTraitPartialsProvider) xo.getChild(ContinuousTraitPartialsProvider.class);
    }
    ConjugateRootTraitPrior rootPrior = ConjugateRootTraitPrior.parseConjugateRootTraitPrior(xo, dataModel.getTraitDimension());
    final boolean allowSingular;
    if (dataModel instanceof IntegratedFactorAnalysisLikelihood) {
        if (traitName == TreeTraitParserUtilities.DEFAULT_TRAIT_NAME) {
            traitName = FACTOR_NAME;
        }
        if (xo.hasAttribute(ALLOW_SINGULAR)) {
            allowSingular = xo.getAttribute(ALLOW_SINGULAR, false);
        } else {
            allowSingular = true;
        }
    } else if (dataModel instanceof RepeatedMeasuresTraitDataModel) {
        traitName = ((RepeatedMeasuresTraitDataModel) dataModel).getTraitName();
        allowSingular = xo.getAttribute(ALLOW_SINGULAR, false);
    } else {
        allowSingular = xo.getAttribute(ALLOW_SINGULAR, false);
    }
    List<BranchRateModel> driftModels = AbstractMultivariateTraitLikelihood.parseDriftModels(xo, diffusionModel);
    List<BranchRateModel> optimalTraitsModels = AbstractMultivariateTraitLikelihood.parseOptimalValuesModels(xo, diffusionModel);
    MultivariateElasticModel elasticModel = null;
    if (xo.hasChildNamed(STRENGTH_OF_SELECTION_MATRIX)) {
        XMLObject cxo = xo.getChild(STRENGTH_OF_SELECTION_MATRIX);
        MatrixParameterInterface strengthOfSelectionMatrixParam;
        strengthOfSelectionMatrixParam = (MatrixParameterInterface) cxo.getChild(MatrixParameterInterface.class);
        if (strengthOfSelectionMatrixParam != null) {
            elasticModel = new MultivariateElasticModel(strengthOfSelectionMatrixParam);
        }
    }
    DiffusionProcessDelegate diffusionProcessDelegate;
    if ((optimalTraitsModels != null && elasticModel != null) || xo.getAttribute(FORCE_OU, false)) {
        if (!integratedProcess) {
            diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, elasticModel);
        } else {
            diffusionProcessDelegate = new IntegratedOUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, elasticModel);
        }
    } else {
        if (driftModels != null || xo.getAttribute(FORCE_DRIFT, false)) {
            diffusionProcessDelegate = new DriftDiffusionModelDelegate(treeModel, diffusionModel, driftModels);
        } else {
            diffusionProcessDelegate = new HomogeneousDiffusionModelDelegate(treeModel, diffusionModel);
        }
    }
    ContinuousDataLikelihoodDelegate delegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, allowSingular);
    if (dataModel instanceof IntegratedFactorAnalysisLikelihood) {
        ((IntegratedFactorAnalysisLikelihood) dataModel).setLikelihoodDelegate(delegate);
    }
    TreeDataLikelihood treeDataLikelihood = new TreeDataLikelihood(delegate, treeModel, rateModel);
    boolean reconstructTraits = xo.getAttribute(RECONSTRUCT_TRAITS, true);
    if (reconstructTraits) {
        // if (missingIndices != null && missingIndices.size() == 0) {
        if (!useMissingIndices) {
            ProcessSimulationDelegate simulationDelegate = delegate.getPrecisionType() == PrecisionType.SCALAR ? new ConditionalOnTipsRealizedDelegate(traitName, treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, delegate) : new MultivariateConditionalOnTipsRealizedDelegate(traitName, treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, delegate);
            TreeTraitProvider traitProvider = new ProcessSimulation(treeDataLikelihood, simulationDelegate);
            treeDataLikelihood.addTraits(traitProvider.getTreeTraits());
        } else {
            ProcessSimulationDelegate simulationDelegate = delegate.getPrecisionType() == PrecisionType.SCALAR ? new ConditionalOnTipsRealizedDelegate(traitName, treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, delegate) : new MultivariateConditionalOnTipsRealizedDelegate(traitName, treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, delegate);
            TreeTraitProvider traitProvider = new ProcessSimulation(treeDataLikelihood, simulationDelegate);
            treeDataLikelihood.addTraits(traitProvider.getTreeTraits());
            ProcessSimulationDelegate fullConditionalDelegate = new TipRealizedValuesViaFullConditionalDelegate(traitName, treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, delegate);
            treeDataLikelihood.addTraits(new ProcessSimulation(treeDataLikelihood, fullConditionalDelegate).getTreeTraits());
        // String partialTraitName = getPartiallyMissingTraitName(traitName);
        // 
        // ProcessSimulationDelegate partialSimulationDelegate = new ProcessSimulationDelegate.ConditionalOnPartiallyMissingTipsDelegate(partialTraitName,
        // treeModel, diffusionModel, dataModel, rootPrior, rateTransformation, rateModel, delegate);
        // 
        // TreeTraitProvider partialTraitProvider = new ProcessSimulation(partialTraitName,
        // treeDataLikelihood, partialSimulationDelegate);
        // 
        // treeDataLikelihood.addTraits(partialTraitProvider.getTreeTraits());
        }
    }
    return treeDataLikelihood;
}
Also used : MultivariateConditionalOnTipsRealizedDelegate(dr.evomodel.treedatalikelihood.preorder.MultivariateConditionalOnTipsRealizedDelegate) MultivariateElasticModel(dr.evomodel.continuous.MultivariateElasticModel) PrecisionType(dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) CompoundParameter(dr.inference.model.CompoundParameter) MultivariateDiffusionModel(dr.evomodel.continuous.MultivariateDiffusionModel) Tree(dr.evolution.tree.Tree) TreeTraitProvider(dr.evolution.tree.TreeTraitProvider) MatrixParameterInterface(dr.inference.model.MatrixParameterInterface) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) ProcessSimulation(dr.evomodel.treedatalikelihood.ProcessSimulation) TipRealizedValuesViaFullConditionalDelegate(dr.evomodel.treedatalikelihood.preorder.TipRealizedValuesViaFullConditionalDelegate) TreeTraitParserUtilities(dr.evomodelxml.treelikelihood.TreeTraitParserUtilities) ConditionalOnTipsRealizedDelegate(dr.evomodel.treedatalikelihood.preorder.ConditionalOnTipsRealizedDelegate) MultivariateConditionalOnTipsRealizedDelegate(dr.evomodel.treedatalikelihood.preorder.MultivariateConditionalOnTipsRealizedDelegate) ProcessSimulationDelegate(dr.evomodel.treedatalikelihood.preorder.ProcessSimulationDelegate)

Example 3 with TreeTraitParserUtilities

use of dr.evomodelxml.treelikelihood.TreeTraitParserUtilities in project beast-mcmc by beast-dev.

the class TraitValidationProviderParser method parseTraitValidationProvider.

public static TraitValidationProvider parseTraitValidationProvider(XMLObject xo) throws XMLParseException {
    String trueValuesName = xo.getStringAttribute(TreeTraitParserUtilities.TRAIT_NAME);
    String inferredValuesName = xo.getStringAttribute(INFERRED_NAME);
    TreeDataLikelihood treeLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
    ContinuousDataLikelihoodDelegate delegate = (ContinuousDataLikelihoodDelegate) treeLikelihood.getDataLikelihoodDelegate();
    ContinuousTraitPartialsProvider dataModel = delegate.getDataModel();
    Tree treeModel = treeLikelihood.getTree();
    TreeTraitParserUtilities utilities = new TreeTraitParserUtilities();
    TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = utilities.parseTraitsFromTaxonAttributes(xo, trueValuesName, treeModel, true);
    Parameter trueParameter = returnValue.traitParameter;
    List<Integer> trueMissing = returnValue.missingIndices;
    Parameter missingParameter = null;
    if (xo.hasChildNamed(MASK)) {
        missingParameter = (Parameter) xo.getElementFirstChild(MASK);
    }
    String id = xo.getId();
    TraitValidationProvider provider = new TraitValidationProvider(trueParameter, dataModel, treeModel, id, missingParameter, treeLikelihood, inferredValuesName, trueMissing);
    return provider;
}
Also used : TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) Tree(dr.evolution.tree.Tree) TreeTraitParserUtilities(dr.evomodelxml.treelikelihood.TreeTraitParserUtilities) Parameter(dr.inference.model.Parameter) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) ContinuousTraitPartialsProvider(dr.evomodel.treedatalikelihood.continuous.ContinuousTraitPartialsProvider) TraitValidationProvider(dr.inference.model.TraitValidationProvider)

Aggregations

TreeTraitParserUtilities (dr.evomodelxml.treelikelihood.TreeTraitParserUtilities)3 Tree (dr.evolution.tree.Tree)2 TreeDataLikelihood (dr.evomodel.treedatalikelihood.TreeDataLikelihood)2 Parameter (dr.inference.model.Parameter)2 MutableTreeModel (dr.evolution.tree.MutableTreeModel)1 TreeTraitProvider (dr.evolution.tree.TreeTraitProvider)1 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)1 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)1 MultivariateDiffusionModel (dr.evomodel.continuous.MultivariateDiffusionModel)1 MultivariateElasticModel (dr.evomodel.continuous.MultivariateElasticModel)1 ProcessSimulation (dr.evomodel.treedatalikelihood.ProcessSimulation)1 ContinuousDataLikelihoodDelegate (dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate)1 ContinuousTraitPartialsProvider (dr.evomodel.treedatalikelihood.continuous.ContinuousTraitPartialsProvider)1 PrecisionType (dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType)1 ConditionalOnTipsRealizedDelegate (dr.evomodel.treedatalikelihood.preorder.ConditionalOnTipsRealizedDelegate)1 MultivariateConditionalOnTipsRealizedDelegate (dr.evomodel.treedatalikelihood.preorder.MultivariateConditionalOnTipsRealizedDelegate)1 ProcessSimulationDelegate (dr.evomodel.treedatalikelihood.preorder.ProcessSimulationDelegate)1 TipRealizedValuesViaFullConditionalDelegate (dr.evomodel.treedatalikelihood.preorder.TipRealizedValuesViaFullConditionalDelegate)1 CompoundParameter (dr.inference.model.CompoundParameter)1 MatrixParameter (dr.inference.model.MatrixParameter)1