Debian Codegraph @

src/dr/evolution/coalescent/structure/StructuredCoalescent.java

raw symbols SCIP
1 /*
2 * StructuredCoalescent.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.evolution.coalescent.structure;
27
28 import dr.evolution.colouring.ColourChangeMatrix;
29 import dr.evolution.colouring.TreeColouring;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32
33 /**
34 * @author Alexei Drummond
35 * @author Gerton Lunter
36 * @author Andrew Rambaut
37 * @version $Id: StructuredCoalescent.java,v 1.17 2006/09/11 09:33:01 gerton Exp $
38 */
39 public class StructuredCoalescent {
40
41 static double tinyTime = 1.0e-6; // make sure rounding errors don't influence population sizes (for Bayesian Skyline)
42
43 public StructuredCoalescent() {
44
45 }
46
47 /**
48 * @param intervals
49 * @return the log likelihood
50 */
51 // public double calculateLogLikelihood(TreeColouring treeColouring, StructuredIntervalList intervals, ColourChangeMatrix mm, double[] N) {
52 public double calculateLogLikelihood(TreeColouring treeColouring, StructuredIntervalList intervals, ColourChangeMatrix mm, MetaPopulation mp) {
53
54 double logL = 0;
55 Tree tree = treeColouring.getTree();
56
57 double _totalIntegratedRate = 0.0; /* debugging */
58
59 // include the probability of the leaf colouring
60 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
61
62 NodeRef leafNode = tree.getExternalNode(i);
63 logL += Math.log(mm.getEquilibrium(treeColouring.getNodeColour(leafNode)));
64
65 }
66
67 double currentTime = 0.0;
68
69 // walk up the tree
70 for (int i = 0; i < intervals.getIntervalCount(); i++) {
71
72 // integral of exit rate over time interval
73 double intensity = 0.0;
74
75 // get length of current interval
76 double time = intervals.getInterval(i);
77
78 // calculate the integrated exit rate
79 for (int j = 0; j < intervals.getPopulationCount(); j++) {
80
81 int lineages = intervals.getLineageCount(i, j);
82
83 // coalescent contribution
84 //exitRate += lineages * (lineages-1) / (2*N[j]);
85
86 intensity += (lineages * (lineages - 1) / 2.0) * mp.getIntegral(currentTime, currentTime + time, j);
87
88 // migration contribution
89 intensity += (-mm.getBackwardRate(j, j)) * lineages * time;
90 }
91
92 _totalIntegratedRate += intensity;
93
94 //System.out.print(" LL: interv="+i+"\trate="+df.format(exitRate)+"\tt="+df.format(time)+"\tintrate="+ df.format(time*exitRate) + "\tsurv="+df.format(survivalProbability));
95
96 double pointDensity = 0.0;
97 Event event = intervals.getEvent(i);
98
99 // update current time
100 currentTime += time;
101 //currentTime = event.time;
102
103 switch (event.getType()) {
104
105 case COALESCENT: {
106
107 if (!(event.getAboveColour() == event.getBelowColour())) {
108 throw new Error("Coalescent event changes colour");
109 }
110 // old version:
111 //pointDensity = 1.0 / N[event.aboveColour];
112
113 // Make sure that rounding errors do not influence the population size (relevant for
114 // non-continuous population models, e.g. piecewise constant models for the Bayesian skyline)
115 pointDensity = 1.0 / mp.getDemographic(currentTime - tinyTime, event.getAboveColour());
116
117 //System.out.println(" Coalescent; density="+pointDensity);
118
119 }
120 break;
121
122 case MIGRATION: {
123
124 if (!(event.getAboveColour() != event.getBelowColour())) {
125 throw new Error("Colour-change event fails to change colour");
126 }
127 pointDensity = mm.getBackwardRate(event.getBelowColour(), event.getAboveColour());
128
129 //System.out.println(" Migration; density="+pointDensity);
130
131 }
132 break;
133 case SAMPLE: {
134
135 pointDensity = 1.0;
136 //System.out.println(" Sample");
137 }
138 break;
139 }
140
141 logL += Math.log(pointDensity) - intensity;
142 }
143
144 /*
145 System.out.println("Full likelihood "+logL);
146 System.out.println("Integrated exit rate "+_totalIntegratedRate);
147 */
148
149 return logL;
150 }
151
152 /**
153 * Calculate the (2-population) structured coalescent likelihood for the given tree,
154 * tip-colors, population sizes and migration rates.
155 * @param tree
156 * @param initial the probability of being in population 0 at the tips
157 * @param N the population sizes of the two populations
158 * @param m the migration rates between the two populations
159 */
160 /*public double calculateLogLikelihood(Tree tree, double[] initial, double[] N, double[] m) {
161
162 double logL = 0.0;
163
164 // get the coalescent and add sample intervals
165 TreeIntervals intervals = new TreeIntervals(tree);
166
167 // the partial probs at all nodes.
168 double[] p = new double[tree.getNodeCount()];
169
170 // initialize the partials at all external nodes.
171 // index of initial array should correspond to the index in external node list of tree.
172 for (int i = 0; i < initial.length; i++) {
173 p[i] = initial[i];
174 }
175
176 // the coalescence rates among all pairs of lineages
177 double[][] cl = new double[tree.getNodeCount()][tree.getNodeCount()];
178
179 // loop through intervals, updating partials
180 for (int i = 0; i < intervals.getIntervalCount(); i++) {
181
182 double ti = intervals.getInterval(i);
183 List lineages = intervals.getLineages(i);
184
185 // calculate new partials after time ti for all lineages in the current interval
186 for (int j = 0; j < lineages.size(); j++) {
187
188 NodeRef lineage = (NodeRef)lineages.get(j);
189 int li = lineage.getNumber();
190
191 // update the partial for node j
192 p[li] = timeEvolution(p[li], ti);
193 }
194
195 // if this interval is a coalescent then calculate coalescent contribution to likelihood
196 if (intervals.getIntervalType(i) == IntervalType.COALESCENT) {
197
198 // calculate coalescence rates for all pairs of current lineages
199 for (int j = 0; j < lineages.size(); j++) {
200
201 NodeRef lineage1 = (NodeRef)lineages.get(j);
202 int i1 = lineage1.getNumber();
203
204 for (int k = j+1; k < lineages.size(); k++) {
205
206 NodeRef lineage2 = (NodeRef)lineages.get(k);
207 int i2 = lineage2.getNumber();
208
209 double p0 = p[i1]*p[i2]; // probability both lineages in population 0
210 double p1 = (1-p[i1])*(1-p[i2]); // probability both lineages in population 1
211 double psame = p0 + p1; // probability both lineages i1 and i2 in same population
212 double pdiff = 1.0 - psame;
213
214 cl[i1][i2] = cl[i2][i1] = p0/N[0] + p1/N[1];
215 }
216 }
217
218 // do something with cl to calculate coalescent density
219
220 // get nodes involved in coalescent event
221 NodeRef parent = intervals.getCoalescentNode(i);
222 int pi = parent.getNumber();
223 NodeRef left = tree.getChild(parent,0);
224 NodeRef right = tree.getChild(parent,1);
225 int i1 = left.getNumber();
226 int i2 = right.getNumber();
227
228 // calculate the partial prob for the beginning of the coalesced lineage
229 double p0 = p[i1]*p[i2]; // probability both lineages in population 0
230 double p1 = (1-p[i1])*(1-p[i2]); // probability both lineages in population 1
231 double psame = p0 + p1; // probability both lineages in same population
232
233 p[pi] = p0 / psame; // re-normalized probability coalescent event in population 0
234
235 // add the likelihood contribution from co-location probability psame
236 // need to account for different coalescent rates here as well!
237 logL += Math.log(psame);
238
239 } else {
240 // interval ended in a sample event -- nothing left to do
241 }
242
243 // at this point all p values have been updated for the start of the next coalescent interval.
244 }
245
246 return logL;
247 } */
248
249 /*
250 *//**
251 * @return the probability of being in population 0 after time t, starting from prob p
252 *//*
253 private double timeEvolution(double p, double t) {
254 throw new RuntimeException("Not implemented");
255 }*/
256 }