Debian Codegraph @

src/dr/evomodel/continuous/RootTraitMLE.java

raw symbols SCIP
1 /*
2 * RootTraitMLE.java
3 *
4 * Copyright (c) 2002-2015 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.continuous.Continuous;
29 import dr.evolution.continuous.ContinuousTraitLikelihood;
30 import dr.evolution.continuous.Contrastable;
31 import dr.evomodel.tree.TreeModel;
32 import dr.inference.model.Statistic;
33 import dr.xml.*;
34
35 import java.util.ArrayList;
36
37
38 /**
39 * Calculates the likelihood of a set of continuous attributes on a tree.
40 *
41 * @version $Id: RootTraitMLE.java,v 1.3 2006/06/18 16:20:58 alexei Exp $
42 *
43 * @author Alexei Drummond
44 */
45 public class RootTraitMLE extends Statistic.Abstract {
46
47 public static final String ROOT_TRAIT_MLE = "rootTraitMLE";
48 public static final String TRAIT = "trait";
49 public static final String NAME = "name";
50
51 public RootTraitMLE(TreeModel treeModel, String[] traitNames) {
52
53 this.treeModel = treeModel;
54 this.traitNames = traitNames;
55 mles = new Contrastable[traitNames.length];
56 }
57
58 /**
59 * Get MLE estimate of the given trait
60 * @return the MLE estimate of the given trait index.
61 */
62 public double getStatisticValue(int i) {
63
64 contTraitLikelihood.calculateLikelihood(treeModel, traitNames, mles, 1.0);
65
66 if (mles[i] instanceof Continuous) return ((Continuous)mles[i]).getValue();
67 return -1.0;
68 }
69
70 public int getDimension() {
71 return traitNames.length;
72 }
73
74 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
75
76 public String getParserName() { return ROOT_TRAIT_MLE; }
77
78 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
79
80 TreeModel treeModel = (TreeModel)xo.getChild(TreeModel.class);
81 ArrayList<String> traits = new ArrayList<String>();
82
83 for (int i = 0; i < xo.getChildCount(); i++) {
84
85 Object child = xo.getChild(i);
86 if (xo.getChild(i) instanceof XMLObject) {
87
88 XMLObject cxo = (XMLObject)xo.getChild(i);
89
90 if (cxo.getName().equals(TRAIT)) {
91 traits.add(cxo.getStringAttribute(NAME));
92 }
93 }
94 }
95
96 String[] traitNames = new String[traits.size()];
97 for (int i =0; i < traitNames.length; i++) { traitNames[i] = traits.get(i); }
98
99 RootTraitMLE rootTraitMLE = new RootTraitMLE(treeModel, traitNames);
100
101 return rootTraitMLE;
102 }
103
104 //************************************************************************
105 // AbstractXMLObjectParser implementation
106 //************************************************************************
107
108 public String getParserDescription() {
109 return "A maximum likelihood estimate of trait value at the root.";
110 }
111
112 public Class getReturnType() { return RootTraitMLE.class; }
113
114 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
115
116 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
117 new ElementRule(TreeModel.class )
118 };
119 };
120
121 private String[] traitNames = null;
122 private Contrastable[] mles = null;
123 private TreeModel treeModel = null;
124 private ContinuousTraitLikelihood contTraitLikelihood = new ContinuousTraitLikelihood();
125 }