Debian Codegraph @

src/dr/evomodel/coalescent/GMRFDensityComponent.java

raw symbols SCIP
1 /*
2 * GMRFDensityComponent.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.coalescent;
27
28 import dr.inference.model.Likelihood;
29 import dr.inference.model.Model;
30 import dr.inference.loggers.LogColumn;
31 import dr.inference.loggers.NumberColumn;
32 import dr.xml.*;
33
34 import java.util.Arrays;
35 import java.util.HashSet;
36 import java.util.Set;
37
38 /**
39 * @author Marc Suchard
40 */
41 public class GMRFDensityComponent implements Likelihood {
42
43 public static final String PARSER_NAME = "gmrfDensityComponent";
44 public static final String COMPONENT = "component";
45 public static final String COALESCENT_TAG = "coalescent";
46 public static final String GMRF_TAG = "gmrf";
47
48 public GMRFDensityComponent(GMRFSkyrideLikelihood skyride, boolean returnCoalescent) {
49 this.skyride = skyride;
50 this.returnCoalescent = returnCoalescent;
51 if ( returnCoalescent ) {
52 tag = "." + COALESCENT_TAG;
53 } else {
54 tag = "." + GMRF_TAG;
55 }
56 }
57
58 public Model getModel() {
59 return skyride;
60 }
61
62 public double getLogLikelihood() {
63
64 double skyrideLogLikelihood = skyride.getLogLikelihood(); // Handles updating flags, etc.
65
66 if ( returnCoalescent ) {
67 skyrideLogLikelihood -= skyride.peakLogFieldLikelihood();
68 } else {
69 skyrideLogLikelihood -= skyride.peakLogCoalescentLikelihood();
70 }
71 return skyrideLogLikelihood;
72 }
73
74 public boolean evaluateEarly() {
75 return false;
76 }
77
78 public void makeDirty() {
79 skyride.makeDirty();
80 }
81
82 public String prettyName() {
83 return skyride.prettyName() + tag;
84 }
85
86 @Override
87 public Set<Likelihood> getLikelihoodSet() {
88 return new HashSet<Likelihood>(Arrays.asList(this));
89 }
90
91 public boolean isUsed() {
92 return skyride.isUsed();
93 }
94
95 public void setUsed() {
96 skyride.setUsed();
97 }
98
99 public LogColumn[] getColumns() {
100 return new LogColumn[]{
101 new LikelihoodColumn(getId())
102 };
103 }
104
105 protected class LikelihoodColumn extends NumberColumn {
106 public LikelihoodColumn(String label) {
107 super(label);
108 }
109
110 public double getDoubleValue() {
111 return getLogLikelihood();
112 }
113 }
114
115 public String getId() {
116 return skyride.getId() + tag;
117 }
118
119 public void setId(String id) {
120 skyride.setId(id);
121 }
122
123 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
124
125 public String getParserName() {
126 return PARSER_NAME;
127 }
128
129 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
130
131 GMRFSkyrideLikelihood skyride = (GMRFSkyrideLikelihood) xo.getChild(GMRFSkyrideLikelihood.class);
132 String componentString = (String) xo.getAttribute(COMPONENT);
133 boolean returnCoalescent;
134 if (componentString.compareToIgnoreCase(COALESCENT_TAG) == 0) {
135 returnCoalescent = true;
136 } else if (componentString.compareToIgnoreCase(GMRF_TAG) == 0) {
137 returnCoalescent = false;
138 } else {
139 throw new XMLParseException("Unknown component of GMRF Skyride");
140 }
141
142 return new GMRFDensityComponent(skyride,returnCoalescent);
143 }
144
145 //************************************************************************
146 // AbstractXMLObjectParser implementation
147 //************************************************************************
148
149 public String getParserDescription() {
150 return "This element represents a single component (coalescent or field) of the GMRF Skyride.";
151 }
152
153 public Class getReturnType() {
154 return Likelihood.class;
155 }
156
157 public XMLSyntaxRule[] getSyntaxRules() {
158 return rules;
159 }
160
161 private final XMLSyntaxRule[] rules = {
162 AttributeRule.newStringRule(COMPONENT),
163 new ElementRule(GMRFSkyrideLikelihood.class),
164 };
165 };
166
167
168 private GMRFSkyrideLikelihood skyride;
169 private boolean returnCoalescent;
170 private String tag;
171 }