Debian Codegraph @

src/dr/evomodel/continuous/ContinuousLikelihood.java

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