-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtraitDependent_functions.R
More file actions
641 lines (452 loc) · 15.2 KB
/
traitDependent_functions.R
File metadata and controls
641 lines (452 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# This function checks trees to see if they pass ape ultrametricity test.
# If not, it computes the differential root-to-tip distance across all tips.
# It adds the appropriate quantity to each terminal branch length to ensure that
# tree passes ultrametric test.
# Note: this is only a valid method of making trees ultrametric when the
# non-ultrametricity is due to small numerical discrepancies, e.g.,
# rounding or other floating point issues during phylogeny construction.
#
check_and_fix_ultrametric <- function(phy){
if (!is.ultrametric(phy)){
vv <- vcv.phylo(phy)
dx <- diag(vv)
mxx <- max(dx) - dx
for (i in 1:length(mxx)){
phy$edge.length[phy$edge[,2] == i] <- phy$edge.length[phy$edge[,2] == i] + mxx[i]
}
if (!is.ultrametric(phy)){
stop("Ultrametric fix failed\n")
}
}
return(phy)
}
logit <- function(x, min=0, max=1){
p <- (x-min)/(max-min)
log(p/(1-p))
}
invlogit <- function(x, min=0, max=1)
{
p <- exp(x)/(1+exp(x))
p <- ifelse( is.na(p) & !is.na(x), 1, p ) # fix problems with +Inf
p * (max-min) + min
}
# Gets a vector of initial parameters
# for a bisse, birth-death, or mk2 model
# using the likelihood functions created
# in diversitree with make.mk2, make.bisse, or make.bd
# These can be plugged directly into the corresponding
# likelihood function. However, they are not guaranteed
# to generate finite log-likelihoods.
# Arguments:
# fx: the diversitree likelihood function
# lmin: the minimum value across all parameters
# lmax: the maximum value across all parameters
getStartingParamsDiversitree <- function(fx, lmin, lmax){
lamset <- runif(3, lmin, lmax)
names(lamset) <- c('lambda', paste('lambda', 0:1, sep=''))
muset <- runif(3, 0, 1) * lamset
names(muset) <- c('mu', paste('mu', 0:1, sep=''))
qset <- runif(4, lmin, lmax * 0.2)
names(qset) <- c('q01', 'q10', 'q12', 'q21')
parvec <- c(lamset, muset, qset)
if (length(setdiff(argnames(fx), names(parvec))) > 0){
stop("Invalid argnames from function\n")
}
parset <- intersect(names(parvec), argnames(fx))
return(parvec[parset])
}
# A general purpose optimization function
# that optimizes parameters of a diversitree likelihood function.
# The likelihood function must correspond to one of the following models:
# a) BiSSE (or any constrained submodel)
# b) birth-death
# c) mk2 (2 state character only model)
fitDiversitree <- function(fx, nopt=1, lmin = 0.0001, lmax=20.0, MAXBAD = 1000, initscale = 0.1){
for (i in 1:nopt){
badcount <- 0
iv <- getStartingParamsDiversitree(fx, lmin=lmin, lmax=lmax*initscale)
resx <- try(optim(iv ,fx, method='L-BFGS-B', control=list(maxit=1000, fnscale=-1), lower=lmin, upper=lmax), silent=T)
while (class(resx) == 'try-error'){
iv <- getStartingParamsDiversitree(fx, lmin=lmin, lmax=lmax*initscale)
resx <- try(optim(iv , fx, method='L-BFGS-B', control=list(maxit=1000, fnscale=-1), lower=lmin, upper=lmax), silent=T)
badcount <- badcount + 1
if (badcount > MAXBAD){
stop("Too many fails in fitDiversitree\n")
}
}
if (i == 1){
best <- resx
}else{
if (best$value < resx$value){
best <- resx
}
}
}
fres <- list(pars=best$par, loglik=best$value)
fres$AIC <- -2*fres$loglik + 2*length(argnames(fx))
fres$counts <- best$counts
#fres$like_function <- fx
fres$convergence <- best$convergence
fres$message <- best$message
return(fres)
}
fitDiversitree_allmodels <- function(tree, traits, index=1, nopt=5){
# lam0, lam1, mu0, mu1, q01, q10
# lam0, lam1, mu0, q01, q10
# lam0, lam1, mu0 q01, q10
# lam0, mu0, q01, q10
lfx6 <- make.bisse(tree, traits)
lfx5lv <- constrain(lfx6, formulae=list(mu0 ~ mu1))
lfx5mv <- constrain(lfx6, formulae = list(lambda0 ~ lambda1))
lfx4 <- constrain(lfx5lv, formulae= list(lambda0 ~ lambda1))
# assume max lambda is 30x pb rate
lpb <- 30* (length(tree$tip.label) - 2 ) / sum(tree$edge.length)
fitx6 <- try(fitDiversitree(lfx6, nopt=nopt, lmax=lpb))
fitx5lv <- try(fitDiversitree(lfx5lv, nopt=nopt, lmax=lpb))
fitx5mv <- try(fitDiversitree(lfx5mv, nopt = nopt, lmax=lpb))
fitx4 <- try(fitDiversitree(lfx4, nopt = nopt, lmax = lpb))
resmat <- matrix(NA, nrow=4, ncol=12)
colnames(resmat) <- c("index", "loglik", "AIC", "conv", "upperbound", "lambda0", "lambda1", "mu0", "mu1", "q01", "q10", "pval_lrt")
zf <- function(x){
if (class(x) != "try-error"){
return(TRUE)
}else{
return(FALSE)
}
}
if (zf(fitx6) & zf(fitx5lv) & zf(fitx5mv) & zf(fitx4)){
x <- fitx6
resmat[1, 1:5] <- c(index, x$loglik, x$AIC, x$convergence, lpb)
resmat[1, names(x$pars)] <- x$pars
x <- fitx5lv
resmat[2, 1:5] <- c(index, x$loglik, x$AIC, x$convergence, lpb)
resmat[2, names(x$pars)] <- x$pars
x <- fitx5mv
resmat[3, 1:5] <- c(index, x$loglik, x$AIC, x$convergence, lpb)
resmat[3, names(x$pars)] <- x$pars
x <- fitx4
resmat[4, 1:5] <- c(index, x$loglik, x$AIC, x$convergence, lpb)
resmat[4, names(x$pars)] <- x$pars
# lrt vs state-independent
dpvec <- c(2,1,1)
for (i in 1:3){
ll <- 2 * (resmat[i,"loglik"] - resmat[4,"loglik"])
if (ll < 0 ){
resmat[i,"pval_lrt"] <- 1
}else{
resmat[i,"pval_lrt"] <- 1 - pchisq(ll, df=dpvec[i])
}
}
}else{
resmat[1:4,1] <- index
resmat[1:4, 2:12] <- NA
}
return(resmat)
}
DR_statistic <- function(x, return.mean = FALSE){
rootnode <- length(x$tip.label) + 1
sprates <- numeric(length(x$tip.label))
for (i in 1:length(sprates)){
node <- i
index <- 1
qx <- 0
while (node != rootnode){
el <- x$edge.length[x$edge[,2] == node]
node <- x$edge[,1][x$edge[,2] == node]
qx <- qx + el* (1 / 2^(index-1))
index <- index + 1
}
sprates[i] <- 1/qx
}
if (return.mean){
return(mean(sprates))
}else{
names(sprates) <- x$tip.label
return(sprates)
}
}
# Simulate trees, conditional on getting at least minf frequency of
# the derived character state
simTreeBiSSE <- function(pars, tmax = 50, minf = 0, maxf = 1, nmin = 50, n_fixed=NULL){
MAXBAD <- 500
badcount <- 0
while (1){
if (is.null(n_fixed)){
tmp <- tree.bisse(pars, max.t=tmax, x0=0)
}else{
tmp <- tree.bisse(pars, max.taxa = n_fixed, x0=0)
nmin <- n_fixed
}
if (!is.null(tmp)){
ff <- sum(tmp$tip.state == 1) / length(tmp$tip.state)
if (ff > minf & ff < maxf & length(tmp$tip.label) >= nmin){
break
}else if (badcount > MAXBAD){
stop("Parameters cannot generate good tree/char combination\n")
}
badcount <- badcount + 1
}
}
return(tmp)
}
asPhyloBaseClass <- function(phy){
obj <- list(edge=phy$edge, Nnode = phy$Nnode, tip.label = phy$tip.label, edge.length = phy$edge.length)
class(obj) <- "phylo"
return(obj)
}
countParsimonyChanges <- function(phy, s){
phy <- asPhyloBaseClass(phy)
if (length(unique(s)) == 1){
return(0)
}
if (sum(names(s) == phy$tip.label) != length(phy$tip.label)){
stop("Mismatch: state names vs tip labels\n")
}
# This fails occasionally.
#phdata <- phyDat(s, type = "USER", levels = c(0,1))
chars <- c("A", "C", "T", "G")
if (sum(s == 0) > 0){
s <- s + 1
}
cvec <- as.matrix(chars[s], ncol=1)
rownames(cvec) <- names(s)
phdata <- phyDat(cvec, type = "DNA")
return(parsimony(phy, phdata ))
}
getWeightedDR <- function(phy, s, DRvec){
phy <- asPhyloBaseClass(phy)
if (length(unique(s)) == 1){
return(0)
}
if (sum(names(s) == phy$tip.label) != length(phy$tip.label)){
stop("Mismatch: state names vs tip labels\n")
}
# This fails occasionally.
#phdata <- phyDat(s, type = "USER", levels = c(0,1))
chars <- c("A", "C", "T", "G")
if (sum(s == 0) > 0){
s <- s + 1
}
cvec <- as.matrix(chars[s], ncol=1)
rownames(cvec) <- names(s)
phdata <- phyDat(cvec, type = "DNA")
at <- acctran(phy, phdata)
rootnode <- length(phy$tip.label) + 1
curr_state <- s[phy$tip.label[1]]
which_edge <- which(phy$edge[,2] == 1)
parent <- phy$edge[which_edge, 1]
while(parent != rootnode){
if (at$edge.length[which_edge] == 1){
curr_state <- abs(curr_state - 1)
}
which_edge <- which(phy$edge[,2] == parent)
parent <- phy$edge[which_edge, 1]
}
at$rootstate <- curr_state
tipset <- phy$tip.label
ratesums <- numeric(2)
countsums <- numeric(2)
# Do this directly w vcv matrix:
vv <- cophenetic.phylo(at)
# First get rid of all terminal changes:
while (length(tipset) > 0){
curtip <- which(rownames(vv) == tipset[1])
in_group <- which(vv[,curtip] == 0)
tips <- rownames(vv)[in_group]
index <- s[tipset[1]]
ratesums[ index ] <- ratesums[ index] + sum(DRvec[tips]) / length(tips)
countsums[ index] <- countsums[ index ] + 1
tipset <- setdiff(tipset, tips)
}
ll <- list(rates = ratesums/countsums, ratesums = ratesums, countsums = countsums)
return(ll)
}
simTraitsParsimonyCriterion <- function(phy, par, changes, tol, fail_tol = 5000){
states <- 0
changes_min <- changes - round(changes * tol)
changes_max <- changes + round(changes * tol)
counter <- 0
while(1){
states <- sim.character(phy, par, x0=sample(c(1,0), 1), model="mk2")
cx <- countParsimonyChanges(phy, states)
if (cx >= changes_min & cx <= changes_max){
break
}
counter <- counter + 1
if (counter > fail_tol){
cat("Too many failed attempts in simTraitsParsimonyCriterion\n")
cat("It appears that transition rates are mismatched to tree and trait data\n")
stop("See tol and fail_tol arguments to this function")
}
}
return(states)
}
fitMK1 <- function(phy, states, mx = 10)
{
lfx <- make.mk2(phy, states)
lfx <- constrain(lfx, formulae = list(q01 ~ q10))
qres <- optimize(lfx, interval=c(0.00001, mx), maximum=T)
return(qres$maximum)
}
fitMK2 <- function(phy, states, nopt = 1){
lfx <- make.mk2(phy, states)
lfx2 <- function(pars){
pars <- exp(pars)
return( lfx(pars) )
}
for (i in 1:nopt){
badcount <- 0
iv <- log(runif(2, 0, 0.5))
resx <- try(optim(iv , lfx2, control=list(fnscale=-1)))
while (class(resx) == 'try-error'){
iv <- log(runif(2, 0, 0.5))
resx <- try(optim(iv , lfx2, control=list(fnscale=-1)))
badcount <- badcount + 1
if (badcount > MAXBAD){
stop("Too many fails in fitDiversitree\n")
}
}
if (i == 1){
best <- resx
}else{
if (best$value < resx$value){
best <- resx
}
}
}
fres <- list(pars=exp(best$par), loglik=best$value)
fres$AIC <- -2*fres$loglik + 2*2
fres$counts <- best$counts
fres$convergence <- best$convergence
fres$message <- best$message
return(fres)
}
# Fast Intuitive analysis of State-dependent Speciation Extinction rates
# incomplete = TRUE : allows trait data to be subset of tree, e.g., incomplete sampling for trait data
#
FISSE.binary <- function(phy, states, reps = 1000, tol=0.1, qratetype = "mk", incomplete = TRUE, ...){
mism <- setdiff(names(states), phy$tip.label)
if (length(mism) > 0){
stop("Error: Trait data includes taxa that are not present in tree\n")
}
if (! incomplete){
if (length(intersect(names(states), phy$tip.label)) != length(phy$tip.label) ){
stop("error in names matching between tree tips and state vector")
}
}
# The ES measures:
dx <- DR_statistic(phy)
# now drop tree to same set of taxa in states dataset:
phy <- drop.tip(phy, tip = setdiff(phy$tip.label, names(states)))
dx <- dx[phy$tip.label]
states <- states[phy$tip.label]
lam0 <- dx[names(states)[states == 0] ]
lam1 <- dx[names(states)[states == 1] ]
nc <- countParsimonyChanges(phy, states)
qq <- 0
if (qratetype == "parsimony"){
qq <- nc / sum(phy$edge.length)
}else if (qratetype == "mk"){
qq <- fitMK1(phy, states)
}else{
stop("Unsupported or invalid option to estimate q")
}
#mk2res <- fitMK2(phy, states)
mm <- matrix(NA, nrow=reps, ncol=2)
for (i in 1:reps){
#tset <- simTraitsParsimonyCriterion(phy, mk2res$pars, nc)
tset <- try(simTraitsParsimonyCriterion(phy, c(qq,qq), nc, tol, ...))
if (class(tset) != "try-error"){
mm[i,1] <- mean(dx[names(tset)[tset == 0]])
mm[i,2] <- mean(dx[names(tset)[tset == 1]])
}else{
obj <- list(lambda0 = mean(lam0), lambda1 = mean(lam1), pval = NA, null_mean_diff = NA, null_sd = NA, nchanges_parsimony = nc, qpars = qq)
return(obj)
}
}
delta <- mm[,2] - mm[,1]
delta_true <- mean(lam1) - mean(lam0)
pval <- sum(delta_true > delta) / (reps + 1)
obj <- list(lambda0 = mean(lam0), lambda1 = mean(lam1), pval = pval, null_mean_diff = mean(delta), null_sd = sd(delta), nchanges_parsimony = nc, qpars = qq)
return(obj)
}
simulateCharacter <- function(phy, qval, minf){
maxbadcount <- 0
good <- FALSE
while (!good){
if (maxbadcount > 25000){
stop("maxbadcount exceeded\n")
}
states <- sim.character(phy, c(qval, qval), x0 = sample(c(0,1), 1), model = "mk2")
states <- states[phy$tip.label]
tx <- table(states)
if (length(tx) > 1 & (min(tx) > minf*length(phy$tip.label))){
good <- TRUE
}
maxbadcount <- maxbadcount + 1
}
return(states)
}
##########################
# Batch processing function
runAnalyses <- function(treefile, traitfile, id = "xxx", bisse_opt = 5){
v <- read.tree(treefile)
t1 <- read.csv(traitfile, header=F, stringsAsFactors=F)
states <- t1[,2]
names(states) <- t1[,1]
ff <- FISSE.binary(v, states, reps=2000)
bisse <- fitDiversitree_allmodels(v, states, nopt=bisse_opt)
ntips = length(v$tip.label)
f0 <- sum(states == 0) / ntips
f1 <- sum(states == 1) / ntips
fisse <- c(ntips=ntips, f0=f0, f1=f1, unlist(ff))
res <- list(id=id, traits=traitfile, tree=treefile, fisse=fisse, bisse=bisse)
return(res)
}
# Fast Intuitive analysis of State-dependent Speciation Extinction rates
# Weighted version
# Rates by state are weighted such that each parsimony origin of a state counts as a single
# point, regardless of how many tips are included.
#
# This function was not used in Rabosky & Goldberg, Evolution, 2017
# Has relatively low power but is more robust to phylogenetic pseudoreplication
#
FISSE.weighted.binary <- function(phy, states, reps = 1000, tol=0.1, qratetype = "mk", ...){
if (length(intersect(names(states), phy$tip.label)) != length(phy$tip.label) ){
stop("error in names matching between tree tips and state vector")
}
states <- states[phy$tip.label]
# The ES measures:
dx <- DR_statistic(phy)
tmp <- getWeightedDR(phy, states, dx)
lam0 <- tmp$rates[1]
lam1 <- tmp$rates[2]
nc <- countParsimonyChanges(phy, states)
qq <- 0
if (qratetype == "parsimony"){
qq <- nc / sum(phy$edge.length)
}else if (qratetype == "mk"){
qq <- fitMK1(phy, states)
}else{
stop("Unsupported or invalid option to estimate q")
}
#mk2res <- fitMK2(phy, states)
mm <- matrix(NA, nrow=reps, ncol=2)
for (i in 1:reps){
#tset <- simTraitsParsimonyCriterion(phy, mk2res$pars, nc)
tset <- try(simTraitsParsimonyCriterion(phy, c(qq,qq), nc, ...))
if (class(tset) != "try-error"){
tmp <- getWeightedDR(phy, tset, dx)
mm[i,1] <- tmp$rates[1]
mm[i,2] <- tmp$rates[2]
}else{
obj <- list(lambda0 = lam0, lambda1 = lam1, pval = NA, null_mean_diff = NA, null_sd = NA, nchanges_parsimony = nc, qpars = qq)
return(obj)
}
}
delta <- mm[,2] - mm[,1]
delta_true <- lam1 - lam0
pval <- sum(delta_true > delta) / (reps + 1)
obj <- list(lambda0 = lam0, lambda1 = lam1, pval = pval, null_mean_diff = mean(delta), null_sd = sd(delta), nchanges_parsimony = nc, qpars = qq)
return(obj)
}