Debian Codegraph @

src/dr/evomodelxml/continuous/hmc/FullyConjugateTreeTipsPotentialDerivativeParser.java

raw symbols SCIP
1 /*
2 * FullyConjugateTreeTipsPotentialDerivativeParser.java
3 *
4 * Copyright (c) 2002-2017 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.continuous.hmc;
27
28 import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood;
29 import dr.evomodel.continuous.hmc.FullyConjugateTreeTipsPotentialDerivative;
30 import dr.evomodel.treedatalikelihood.DataLikelihoodDelegate;
31 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
32 import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate;
33 import dr.evomodel.treedatalikelihood.continuous.TreeTipGradient;
34 import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities;
35 import dr.inference.model.Parameter;
36 import dr.inferencexml.model.MaskedParameterParser;
37 import dr.xml.*;
38
39 import static dr.evomodelxml.treelikelihood.TreeTraitParserUtilities.DEFAULT_TRAIT_NAME;
40
41 /**
42 * @author Max Tolkoff
43 * @author Marc A. Suchard
44 */
45
46 public class FullyConjugateTreeTipsPotentialDerivativeParser extends AbstractXMLObjectParser {
47
48 private final static String FULLY_CONJUGATE_TREE_TIPS_POTENTIAL_DERIVATIVE = "fullyConjugateTreeTipsPotentialDerivative";
49 private final static String FULLY_CONJUGATE_TREE_TIPS_POTENTIAL_DERIVATIVE2 = "traitGradientOnTree";
50 public static final String TRAIT_NAME = TreeTraitParserUtilities.TRAIT_NAME;
51 private static final String MASKING = MaskedParameterParser.MASKING;
52
53 @Override
54 public String getParserName() {
55 return FULLY_CONJUGATE_TREE_TIPS_POTENTIAL_DERIVATIVE;
56 }
57
58 @Override
59 public String[] getParserNames() {
60 return new String[] { FULLY_CONJUGATE_TREE_TIPS_POTENTIAL_DERIVATIVE, FULLY_CONJUGATE_TREE_TIPS_POTENTIAL_DERIVATIVE2 };
61 }
62
63 @Override
64 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
65
66 // String name = xo.hasId() ? xo.getId() : FULLY_CONJUGATE_TREE_TIPS_POTENTIAL_DERIVATIVE2;
67 String traitName = xo.getAttribute(TRAIT_NAME, DEFAULT_TRAIT_NAME);
68 // Object co = xo.getChild(0);
69
70 final FullyConjugateMultivariateTraitLikelihood fcTreeLikelihood = (FullyConjugateMultivariateTraitLikelihood) xo.getChild(FullyConjugateMultivariateTraitLikelihood.class);
71 final TreeDataLikelihood treeDataLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
72
73 Parameter mask = null;
74 if (xo.hasChildNamed(MASKING)) {
75 mask = (Parameter) xo.getElementFirstChild(MASKING);
76 }
77
78 if (fcTreeLikelihood != null) {
79
80 return new FullyConjugateTreeTipsPotentialDerivative(fcTreeLikelihood, mask);
81
82 } else if (treeDataLikelihood != null){
83
84 DataLikelihoodDelegate delegate = treeDataLikelihood.getDataLikelihoodDelegate();
85
86 if (!(delegate instanceof ContinuousDataLikelihoodDelegate)) {
87 throw new XMLParseException("May not provide a sequence data likelihood to compute tip trait gradient");
88 }
89
90 final ContinuousDataLikelihoodDelegate continuousData = (ContinuousDataLikelihoodDelegate) delegate;
91
92 return new TreeTipGradient(traitName, treeDataLikelihood, continuousData, mask);
93 } else {
94 throw new XMLParseException("Must provide a tree likelihood");
95 }
96 }
97
98 @Override
99 public XMLSyntaxRule[] getSyntaxRules() {
100 return rules;
101 }
102
103 private final XMLSyntaxRule[] rules = {
104 new XORRule(
105 new ElementRule(FullyConjugateMultivariateTraitLikelihood.class),
106 new ElementRule(TreeDataLikelihood.class)
107 ),
108 new ElementRule(MASKING,
109 new XMLSyntaxRule[]{
110 new ElementRule(Parameter.class)
111 }, true),
112 new ElementRule(Parameter.class, true),
113 };
114
115 @Override
116 public String getParserDescription() {
117 return null;
118 }
119
120 @Override
121 public Class getReturnType() {
122 return FullyConjugateTreeTipsPotentialDerivative.class;
123 }
124 }