Debian Codegraph @

src/test/dr/evomodel/alloppnet/speciation/AlloppSpeciesNetworkModelTEST.java

raw symbols SCIP
1 /*
2 * AlloppSpeciesNetworkModelTEST.java
3 *
4 * Copyright (c) 2002-2017 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 test.dr.evomodel.alloppnet.speciation;
27
28 import dr.evomodel.alloppnet.speciation.AlloppMulLabTree;
29 import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings;
30 import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings.ApSpInfo;
31 import dr.math.MathUtils;
32 import dr.math.distributions.GammaDistribution;
33 import org.junit.Before;
34 import org.junit.Test;
35 import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings.Individual;
36 import dr.evomodel.alloppnet.speciation.AlloppSpeciesNetworkModel;
37
38
39
40 import static org.junit.Assert.assertEquals;
41
42
43 /**
44 *
45 * @author Graham Jones
46 * Date: 01/06/2011
47 */
48
49
50 public class AlloppSpeciesNetworkModelTEST {
51
52 @Before
53 public void setUp() throws Exception {
54 }
55
56
57
58
59 @Test
60 public void testAlloppSpeciesNetworkModel() {
61
62 //testGammaQuantile();
63 testNetworkToMulLabTree();
64 testLhoodMulLabTree();
65 // grjtodo-soon would like a test of gene tree in network compatibility.
66 }
67
68
69 /*
70 * ****************** TESTING MulLabTree conversion ******************
71 *
72 */
73
74
75 // trying to reproduce weird bug when quantile returned 4.9e-324
76 public void testGammaQuantile() {
77 MathUtils.setSeed(42);
78 for (int i = 0; i < 1000000; i++) {
79 double s = MathUtils.uniform(.999, 1.001);
80 double b = MathUtils.uniform(6e-5, 7e-5);
81 final GammaDistribution gamma = new GammaDistribution(s,b);
82 double q = MathUtils.uniform(3e-7, 4e-7);
83 double x = gamma.quantile(q);
84 assert x > 1e-20;
85 }
86 }
87
88
89 public void testNetworkToMulLabTree() {
90 ApSpInfo[] apspecies = new ApSpInfo[5];
91 apspecies[0] = new ApSpInfo("a", 2, new Individual[0]);
92 apspecies[1] = new ApSpInfo("b", 4, new Individual[0]);
93 apspecies[2] = new ApSpInfo("c", 4, new Individual[0]);
94 apspecies[3] = new ApSpInfo("d", 4, new Individual[0]);
95 apspecies[4] = new ApSpInfo("e", 2, new Individual[0]);
96
97 AlloppSpeciesBindings testASB = new AlloppSpeciesBindings(apspecies);
98 AlloppSpeciesNetworkModel testASNM = new AlloppSpeciesNetworkModel(testASB);
99
100 System.out.println("Tests of Network To MulLabTree conversion with dips a,e and tets b,c,d");
101 String newick;
102 newick = testASNM.testExampleNetworkToMulLabTree(1);
103 System.out.println(newick + "\n\n");
104 assertEquals(0, newick.compareTo("((((b0,c0),d0),a),(((b1,c1),d1),e))"));
105 newick = testASNM.testExampleNetworkToMulLabTree(2);
106 System.out.println(newick + "\n\n");
107 assertEquals(0, newick.compareTo("(((((b0,c0),d0),a),((b1,c1),d1)),e)"));
108 newick = testASNM.testExampleNetworkToMulLabTree(3);
109 System.out.println(newick + "\n\n");
110 assertEquals(0, newick.compareTo("(((((b0,c0),d0),((b1,c1),d1)),a),e)"));
111 newick = testASNM.testExampleNetworkToMulLabTree(4);
112 System.out.println(newick + "\n\n");
113 assertEquals(0, newick.compareTo("((((b0,c0),a),(d0,d1)),((b1,c1),e))"));
114 newick = testASNM.testExampleNetworkToMulLabTree(5);
115 System.out.println(newick + "\n\n");
116 assertEquals(0, newick.compareTo("(((((a,b0),c0),c1),(d0,d1)),(b1,e))"));
117 System.out.println("");
118 System.out.println("");
119 }
120
121
122 public void testLhoodMulLabTree() {
123 ApSpInfo[] apspecies = new ApSpInfo[3];
124 apspecies[0] = new ApSpInfo("a", 2, new Individual[0]);
125 apspecies[1] = new ApSpInfo("b", 2, new Individual[0]);
126 apspecies[2] = new ApSpInfo("z", 4, new Individual[0]);
127
128 AlloppSpeciesBindings testASB = new AlloppSpeciesBindings(apspecies);
129 AlloppMulLabTree testamlt = new AlloppMulLabTree(testASB);
130 double llhood = testamlt.testGeneTreeInMULTreeLogLikelihood();
131 assertEquals(llhood, -13.562218135041552713, 1e-10);
132 System.out.println(llhood);
133 }
134
135
136 /* This R code produces -13.562218135041552713
137
138 # 0 2 3 1
139 # a z0 z1 b b'
140 # | # | # | # | |
141 # | # | + | # | | hyb 0.005
142 # | 4 | # | # | | (a,z0) in multree 0.01
143 # \ / # | # | |
144 # v # | # | | (a,z0) in gene tree 0.015
145 # | # | 5 | | (z1,b) in multree 0.02
146 # | # \ / /
147 # | # v / (z1,b) in gene tree 0.025
148 # | # | |
149 # \ # / /
150 # | 6 | | root multree 0.03
151 # | |/ (z1,b),b' 0.035
152 # | /
153 # \ /
154 # v root gene tree 0.045
155 # |
156
157
158 # here, numbers 1-9 show branch or part-branch indices
159 # a z0 z1 b b'
160 # | # 2 # 3 # | |
161 # 1 # | + | # | | hyb 0.005
162 # | # 4 # 5 # |6| (a,z0) in multree 0.01
163 # \ / # | # | |
164 # v # | # | | (a,z0) in gene tree 0.015
165 # | # | # | | (z1,b) in multree 0.02
166 # | # \ / /
167 # 7| # v / (z1,b) in gene tree 0.025
168 # | # 8| |
169 # \ # / /
170 # | # | | root multree 0.03
171 # | |/ (z1,b),b' 0.035
172 # | 9 /
173 # \ /
174 # v root gene tree 0.045
175 # |
176
177
178
179
180 brs <- matrix(0, nrow=9, ncol=3)
181 tms <- matrix(0, nrow=9, ncol=4)
182 brs[1,] <- c(.003, .001, 1); tms[1,] <- c(.0, .01, -1,-1)
183 brs[2,] <- c(.003, .001, 1); tms[2,] <- c(.00, .005, -1,-1)
184 brs[3,] <- c(.003, .001, 1); tms[3,] <- c(.00, .005, -1,-1)
185 brs[4,] <- c(.001, .001, 1); tms[4,] <- c(.005, .01, -1,-1)
186 brs[5,] <- c(.001, .001, 1); tms[5,] <- c(.005, .01, -1,-1)
187 brs[6,] <- c(.003, .001, 2); tms[6,] <- c(.0, .02, -1,-1)
188 brs[7,] <- c(.002, .001, 2); tms[7,] <- c(.01, .015, .03, -1)
189 brs[8,] <- c(.002, .001, 3); tms[8,] <- c(.02, .025, .03, -1)
190 brs[9,] <- c(.002, .002, 3); tms[9,] <- c(.03, .035, .045, 999)
191
192
193
194 branchllhood <- function(b, tm) {
195 llhood <- 0
196 tm <- tm[tm>=0]
197 tm <- tm - tm[1]
198 k <- length(tm)
199 n <- b[3]
200 p0 <- b[1]
201 pk <- b[2]
202 for (i in 1:(k-1)) {
203 pi0 <- linear.interp.pop(tm[1], tm[k], p0, pk, tm[i])
204 pi1 <- linear.interp.pop(tm[1], tm[k], p0, pk, tm[i+1])
205 if (i < k-1) {
206 x <- onecoalllhood(n, tm[i], tm[i+1], pi0, pi1)
207 n <- n-1
208 } else {
209 x <- nocoalllhood(n, tm[i], tm[i+1], pi0, pi1)
210 }
211 llhood <- llhood + x
212 }
213 llhood
214 }
215
216 linear.interp.pop <- function(t0, t1, p0, p1, x)
217 {
218 (p0*(t1-x) + p1*(x-t0))/(t1-t0)
219 }
220
221 onecoalllhood <- function(n, t0, t1, p0, p1) {
222 x <- -log(p1) - integrate(pop.integrand, n=n, t0=t0, t1=t1, p0=p0, p1=p1, lower=t0, upper=t1)$value
223 cat("onecoalllhood", "n", n, "t0", t0, "t1", t1, "p0", p0, "p1", p1, "llhood", x, "\n")
224 x
225 }
226
227
228 nocoalllhood <- function(n, t0, t1, p0, p1) {
229 x <- - integrate(pop.integrand, n=n, t0=t0, t1=t1, p0=p0, p1=p1, lower=t0, upper=t1)$value
230 cat("nocoalllhood", "n", n, "t0", t0, "t1", t1, "p0", p0, "p1", p1, "llhood", x, "\n")
231 x
232 }
233
234
235 pop.integrand <- function(x, n, t0, t1, p0, p1)
236 {
237 (n*(n-1)/2) * (t1-t0) / (p0*(t1-x) + p1*(x-t0))
238 }
239
240
241 llhood <- 0
242 for (i in 1:dim(brs)[1]) {
243 x <- branchllhood(brs[i,], tms[i,])
244 cat("branch", i, "llhood", x, "\n")
245 llhood <- llhood + x
246 }
247 llhood
248
249 */
250
251 }
252