Debian Codegraph @

src/dr/evomodel/arg/coalescent/ARGCoalescentLikelihood.java

raw symbols SCIP
1 /*
2 * ARGCoalescentLikelihood.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.arg.coalescent;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evomodel.arg.ARGModel;
30 import dr.inference.model.Model;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.ArrayList;
35 import java.util.logging.Logger;
36
37 public class ARGCoalescentLikelihood extends VeryOldCoalescentLikelihood {
38
39 public static final String ARG_COALESCENT_MODEL = "argCoalescentLikelihood";
40 public static final String RECOMBINATION_RATE = "recombinationRate";
41 public static final String POPULATION_SIZE = "populationSize";
42 public static final String ARG_MODEL = "argModel";
43 public static final String MAX_REASSORTMENTS = "maxReassortments";
44 public static final String ANCESTRAL_RESTRICTION = "ancestralRestriction";
45
46 public static final int RECOMBINATION = 3;
47
48 private Parameter popSize;
49 private Parameter recomRate;
50 protected ARGModel arg;
51 private int taxaNumber;
52 protected int maxReassortments;
53 private boolean ancestralRestriction = false;
54
55 private ArrayList<CoalescentInterval> intervals;
56 private ArrayList<CoalescentInterval> storedIntervals;
57
58 public ARGCoalescentLikelihood(String name, ARGModel arg, int max) {
59 super(name);
60 this.arg = arg;
61
62 intervals = new ArrayList<CoalescentInterval>();
63
64 taxaNumber = arg.getExternalNodeCount();
65 this.maxReassortments = max;
66 }
67
68 public ARGCoalescentLikelihood(Parameter popSize, Parameter recomRate,
69 ARGModel arg, boolean setupIntervals, int maxReassort, boolean ancestralRestriction) {
70 super(ARG_COALESCENT_MODEL);
71
72 this.popSize = popSize;
73 this.recomRate = recomRate;
74 this.arg = arg;
75 this.ancestralRestriction = ancestralRestriction;
76 addVariable(popSize);
77 addVariable(recomRate);
78
79 addModel(arg);
80 intervals = new ArrayList<CoalescentInterval>(arg.getNodeCount());
81 intervalsKnown = false;
82 likelihoodKnown = false;
83
84 if (setupIntervals) {
85 intervalsKnown = true;
86 calculateIntervals();
87 }
88
89 taxaNumber = arg.getExternalNodeCount();
90 this.maxReassortments = maxReassort;
91 }
92
93 public void calculateIntervals() {
94 intervals.clear();
95 intervals.ensureCapacity(arg.getNodeCount());
96
97 NodeRef x;
98 for (int i = 0; i < arg.getInternalNodeCount(); i++) {
99 x = arg.getInternalNode(i);
100 if (arg.isReassortment(x)) {
101 intervals.add(new CoalescentInterval(arg.getNodeHeight(x), RECOMBINATION));
102 } else {
103 intervals.add(new CoalescentInterval(arg.getNodeHeight(x), COALESCENT));
104 }
105 }
106 for (int i = 0; i < arg.getExternalNodeCount(); i++) {
107 x = arg.getExternalNode(i);
108 if (arg.getNodeHeight(x) > 0.0) {
109 intervals.add(new CoalescentInterval(arg.getNodeHeight(x), NEW_SAMPLE));
110 }
111 }
112 dr.util.HeapSort.sort(intervals);
113
114
115 double a = 0, b = 0;
116 for (int i = 0; i < intervals.size(); i++) {
117 b = intervals.get(i).length;
118 intervals.get(i).length = intervals.get(i).length - a;
119 a = b;
120 }
121
122 intervalsKnown = true;
123 }
124
125 public void handleModelChangedEvent(Model model, Object object, int index) {
126 if (model == arg) {
127 intervalsKnown = false;
128 }
129 likelihoodKnown = false;
130 }
131
132 public void handleParameterChangedEvent(Parameter parameter, int index) {
133 likelihoodKnown = false;
134 }
135
136 public void storeState() {
137 storedIntervals = new ArrayList<CoalescentInterval>(intervals.size());
138 for (CoalescentInterval interval : intervals) {
139 storedIntervals.add(interval.clone());
140 }
141
142 intervalsKnown = likelihoodKnown = false;
143
144
145 storedIntervalsKnown = intervalsKnown;
146 storedLikelihoodKnown = likelihoodKnown;
147 storedLogLikelihood = logLikelihood;
148
149 }
150
151 public void restoreState() {
152 intervals = storedIntervals;
153 storedIntervals.clear();
154 intervalsKnown = storedIntervalsKnown;
155 likelihoodKnown = storedLikelihoodKnown;
156 logLikelihood = storedLogLikelihood;
157
158 intervalsKnown = likelihoodKnown = false;
159 // if (!intervalsKnown) {
160 // likelihoodKnown = false;
161 // }
162 }
163
164 public boolean currentARGValid(boolean allowDoubleParents) {
165 if (!intervalsKnown) {
166 calculateIntervals();
167 }
168 int taxa = taxaNumber;
169
170 for (CoalescentInterval x : intervals) {
171 if (taxa == 1)
172 return false;
173 if (x.type == COALESCENT)
174 taxa--;
175 else if (x.type == RECOMBINATION)
176 taxa++;
177 else
178 throw new RuntimeException("Not implemented yet");
179 }
180 if (!allowDoubleParents) {
181 for (int i = 0, n = arg.getNodeCount(); i < n; i++) {
182 NodeRef x = arg.getNode(i);
183 if (arg.isReassortment(x) && arg.getParent(x, 0) == arg.getParent(x, 1)) {
184 return false;
185 }
186 }
187 }
188
189 return true;
190 }
191
192 public double getLogLikelihood() {
193 if (likelihoodKnown)
194 return logLikelihood;
195 if (!intervalsKnown)
196 calculateIntervals();
197
198 likelihoodKnown = true;
199
200 if (arg.getReassortmentNodeCount() > maxReassortments){
201 logLikelihood = Double.NEGATIVE_INFINITY;
202 }else if(ancestralRestriction && !arg.isAncestral()){
203 logLikelihood = Double.NEGATIVE_INFINITY;
204 }else{
205 logLikelihood = calculateLogLikelihood(
206 popSize.getParameterValue(0),
207 recomRate.getParameterValue(0));
208 }
209
210
211 return logLikelihood;
212 }
213
214 private double chooseTwo(int n) {
215 return n * (n - 1) / 2.0;
216 }
217
218 private double calculateLogLikelihood(double pSize, double rRate) {
219
220 double logLike = 0.0;
221 int numberOfTaxa = taxaNumber;
222
223 for (CoalescentInterval interval : intervals) {
224
225 if (numberOfTaxa == 1)
226 return Double.NEGATIVE_INFINITY;
227
228 double rate = (double) numberOfTaxa *
229 (numberOfTaxa - 1 + rRate) / (2.0 * pSize);
230
231 logLike += Math.log(rate) - rate * interval.length;
232
233 if (interval.type == COALESCENT) {
234 logLike += Math.log((double) (numberOfTaxa - 1) /
235 (numberOfTaxa - 1 + rRate))
236 - Math.log(chooseTwo(numberOfTaxa));
237 numberOfTaxa--;
238 } else if (interval.type == RECOMBINATION) {
239 logLike += Math.log(rRate / (numberOfTaxa - 1 + rRate))
240 - Math.log((double) numberOfTaxa);
241
242 numberOfTaxa++;
243 } else {
244 throw new RuntimeException("Not implemented yet");
245 }
246 }
247
248 assert numberOfTaxa == 1;
249
250 return logLike;
251 }
252
253 private class CoalescentInterval implements Comparable<CoalescentInterval>,
254 Cloneable {
255 public int type;
256 public double length;
257
258 public CoalescentInterval(double length, int type) {
259 this.length = length;
260 this.type = type;
261 }
262
263 public int compareTo(CoalescentInterval a) {
264
265 if (a.length > this.length) {
266 return -1;
267 } else if (a.length == this.length) {
268 Logger.getLogger("dr.evomodel.coalescent").severe(
269 "The current ARG Model has 2 internal nodes " +
270 "at the same height");
271 return 0;
272 }
273 return 1;
274 }
275
276 public String toString() {
277 if (type == 0) {
278 return "(" + length + ", Coalescent)";
279 }
280 return "(" + length + ", Recombination)";
281 }
282
283 public CoalescentInterval clone() {
284 return new CoalescentInterval(length, type);
285 }
286
287 }
288
289 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
290
291 public String getParserDescription() {
292 return "A coalescent likelihood for an ARG model";
293 }
294
295 public Class getReturnType() {
296 return ARGCoalescentLikelihood.class;
297 }
298
299 public String getParserName() {
300 return ARG_COALESCENT_MODEL;
301 }
302
303 public XMLSyntaxRule[] getSyntaxRules() {
304 return rules;
305 }
306
307 private final XMLSyntaxRule[] rules = {
308 new ElementRule(POPULATION_SIZE,
309 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
310 new ElementRule(RECOMBINATION_RATE,
311 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
312 new ElementRule(ARG_MODEL,
313 new XMLSyntaxRule[]{new ElementRule(ARGModel.class)}),
314 AttributeRule.newBooleanRule(ANCESTRAL_RESTRICTION,true),
315 };
316
317 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
318 XMLObject cxo = xo.getChild(RECOMBINATION_RATE);
319 Parameter rRate = (Parameter) cxo.getChild(Parameter.class);
320
321 cxo = xo.getChild(POPULATION_SIZE);
322 Parameter pSize = (Parameter) cxo.getChild(Parameter.class);
323
324 cxo = xo.getChild(ARG_MODEL);
325 ARGModel argModel = (ARGModel) cxo.getChild(ARGModel.class);
326
327 int maxreassort = Integer.MAX_VALUE;
328 if (xo.hasAttribute(MAX_REASSORTMENTS)) {
329 maxreassort = xo.getIntegerAttribute(MAX_REASSORTMENTS);
330 }
331
332 boolean ancestral = false;
333 if(xo.hasAttribute(ANCESTRAL_RESTRICTION))
334 ancestral = xo.getBooleanAttribute(ANCESTRAL_RESTRICTION);
335
336 return new ARGCoalescentLikelihood(pSize, rRate, argModel, false, maxreassort, ancestral);
337 }
338
339
340 };
341
342 public String toString() {
343 return getClass().getSimpleName() + " " + super.toString();
344 }
345
346
347 }