Debian Codegraph @

src/dr/evomodel/continuous/GibbsSampleMissingTraitsOperator.java

raw symbols SCIP
1 /*
2 * GibbsSampleMissingTraitsOperator.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.evomodel.continuous;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTrait;
31 import dr.evomodel.treedatalikelihood.DataLikelihoodDelegate;
32 import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
33 import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate;
34 import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities;
35 import dr.inference.model.Parameter;
36 import dr.inference.operators.MCMCOperator;
37 //import dr.inference.operators.OperatorFailedException;
38 import dr.inference.operators.SimpleMCMCOperator;
39 import dr.xml.*;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 //import static dr.evomodel.treedatalikelihood.preorder.ConditionalOnPartiallyMissingTipsRealizedDelegate.PARTIAL;
45 import static dr.evomodelxml.treelikelihood.TreeTraitParserUtilities.MISSING;
46 import static dr.evomodelxml.treelikelihood.TreeTraitParserUtilities.TRAIT_NAME;
47
48 /**
49 * @author Marc A. Suchard
50 */
51 public class GibbsSampleMissingTraitsOperator extends SimpleMCMCOperator
52 // implements GibbsOperator
53 {
54
55
56 private static final String PARTIAL = "partial";
57
58 final private TreeDataLikelihood treeLikelihood;
59 final private TreeTrait treeTrait;
60 final private Parameter parameter;
61 final private Parameter missing;
62
63 public GibbsSampleMissingTraitsOperator(TreeDataLikelihood treeLikelihood,
64 TreeTrait treeTrait, Parameter parameter, Parameter missing) {
65 super();
66
67 this.treeLikelihood = treeLikelihood;
68 this.treeTrait = treeTrait;
69
70 this.parameter = parameter;
71 this.missing = missing;
72 }
73
74 @Override
75 public double doOperation() {//throws OperatorFailedException {
76
77 final Tree tree = treeLikelihood.getTree();
78
79 for (int i = 0; i < tree.getExternalNodeCount(); ++i) {
80 final NodeRef node = tree.getExternalNode(i);
81
82 treeTrait.getTrait(tree, node);
83 }
84
85 double[] tipValues = (double[]) treeTrait.getTrait(tree, null);
86 // System.err.println(new dr.math.matrixAlgebra.Vector(tipValues));
87
88 assert (tipValues.length == parameter.getDimension());
89
90 for (int i = 0; i < parameter.getDimension(); ++i) {
91 boolean fill = missing.getParameterValue(i) == 1;
92 if (fill) {
93 parameter.setParameterValue(i, tipValues[i]); // TODO Update silently?
94 }
95 }
96
97 return 0; // TODO Not yet known ...
98 }
99
100 @Override
101 public String getOperatorName() {
102 return OPERATOR_NAME + "(" + "TODO" + ")";
103 }
104
105 // @Override
106 // public int getStepCount() {
107 // return 1;
108 // }
109
110 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
111
112 public String getParserName() {
113 return OPERATOR_NAME;
114 }
115
116 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
117
118 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
119
120 TreeDataLikelihood treeLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
121 // ContinuousDataLikelihoodDelegate traitDelegate = parseContinuousDataLikelihoodDelegate(xo);
122
123 TreeTrait treeTrait = parseTreeTrait(xo, PARTIAL);
124
125 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
126 Parameter missing = (Parameter) xo.getElementFirstChild(MISSING);
127
128 if (parameter.getDimension() != missing.getDimension()) {
129 throw new XMLParseException("Unequal parameter lengths");
130 }
131
132 GibbsSampleMissingTraitsOperator operator = new GibbsSampleMissingTraitsOperator(treeLikelihood,
133 treeTrait,
134 parameter, missing);
135
136 operator.setWeight(weight);
137
138 return operator;
139
140 }
141
142 //************************************************************************
143 // AbstractXMLObjectParser implementation
144 //************************************************************************
145
146 public XMLSyntaxRule[] getSyntaxRules() {
147 return rules;
148 }
149
150 private final XMLSyntaxRule[] rules = {
151 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
152 new ElementRule(TreeDataLikelihood.class),
153 new ElementRule(Parameter.class),
154 new ElementRule(TreeTraitParserUtilities.MISSING,
155 new XMLSyntaxRule[] {
156 new ElementRule(Parameter.class),
157 }),
158 AttributeRule.newStringRule(TRAIT_NAME, true),
159 };
160
161 public String getParserDescription() {
162 return "This element returns an independence coalescent sampler from a demographic model.";
163 }
164
165 public Class getReturnType() {
166 return GibbsSampleMissingTraitsOperator.class;
167 }
168
169 };
170
171 public static ContinuousDataLikelihoodDelegate parseContinuousDataLikelihoodDelegate(XMLObject xo)
172 throws XMLParseException {
173
174 TreeDataLikelihood treeLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
175 DataLikelihoodDelegate delegate = treeLikelihood.getDataLikelihoodDelegate();
176
177 if (!(delegate instanceof ContinuousDataLikelihoodDelegate)) {
178 throw new XMLParseException("Only implemented for multivariate trait diffusion models");
179 }
180
181 return (ContinuousDataLikelihoodDelegate) delegate;
182 }
183
184 private static TreeTrait parseTreeTrait(XMLObject xo, String prefix) throws XMLParseException {
185
186 TreeDataLikelihood treeLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
187
188 String traitName = null;
189 if (xo.hasAttribute(TRAIT_NAME)) {
190 traitName = (String) xo.getAttribute(TRAIT_NAME);
191 } else {
192 TreeTrait[] traits = treeLikelihood.getTreeTraits();
193 List<String> traitNames = matchedTraitNames(traits, prefix + ".");
194
195 if (traitNames.size() == 1) {
196 traitName = traitNames.get(0);
197 }
198 }
199
200 TreeTrait treeTrait = treeLikelihood.getTreeTrait(traitName);
201
202 if (treeTrait == null) {
203 throw new XMLParseException("Unknown partially observed tree trait");
204 }
205
206 return treeTrait;
207 }
208
209 private static List<String> matchedTraitNames(final TreeTrait[] traits, final String prefix) {
210 List<String> names = new ArrayList<String>();
211 for (TreeTrait trait : traits) {
212 if (trait.getTraitName().startsWith(prefix)) {
213 names.add(trait.getTraitName());
214 }
215 }
216 return names;
217 }
218
219 public static final String OPERATOR_NAME = "gibbsSampleMissingTraitsOperator";
220
221 }