/
pmlPart.R
976 lines (911 loc) · 29.3 KB
/
pmlPart.R
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
#
# pmlPart + pmlCluster
#
optimPartQGeneral <- function(object, Q = c(1, 1, 1, 1, 1, 1),
subs = rep(1, length(Q)), ...) {
m <- length(Q)
n <- max(subs)
ab <- numeric(n)
for (i in 1:n) ab[i] <- log(Q[which(subs == i)[1]])
fn <- function(ab, object, m, n, subs, ...) {
Q <- numeric(m)
for (i in 1:n) Q[subs == i] <- ab[i]
Q <- exp(Q)
result <- 0
for (i in seq_along(object)) result <- result + update(object[[i]],
Q = Q, ...)$logLik
result
}
res <- optim(par = ab, fn = fn, gr = NULL, method = "L-BFGS-B",
lower = -Inf, upper = Inf, control = list(fnscale = -1,
maxit = 25), object = object, m = m, n = n, subs = subs, ...)
Q <- rep(1, m)
for (i in 1:n) Q[subs == i] <- exp(res[[1]][i])
res[[1]] <- Q
res
}
optimPartBf <- function(object, bf = c(0.25, 0.25, 0.25, 0.25), ...) {
l <- length(bf)
nenner <- 1 / bf[l]
lbf <- log(bf * nenner)
lbf <- lbf[-l]
fn <- function(lbf, object, ...) {
result <- 0
bf <- exp(c(lbf, 0))
bf <- bf / sum(bf)
n <- length(object)
for (i in 1:n) result <- result + update(object[[i]],
bf = bf, ...)$logLik
result
}
res <- optim(par = lbf, fn = fn, gr = NULL, method = "Nelder-Mead",
control = list(fnscale = -1, maxit = 500), object, ...)
bf <- exp(c(res[[1]], 0))
bf <- bf / sum(bf)
}
optimPartInv <- function(object, inv = 0.01, ...) {
fn <- function(inv, object, ...) {
result <- 0
n <- length(object)
for (i in 1:n) result <- result + update(object[[i]], inv = inv,
...)$logLik
result
}
res <- optimize(f = fn, interval = c(0, 1), lower = 0, upper = 1,
maximum = TRUE, tol = 1e-04, object, ...)
res[[1]]
}
optimPartGamma <- function(object, shape = 1, ...) {
fn <- function(shape, object, ...) {
result <- 0
n <- length(object)
for (i in 1:n) result <- result + update(object[[i]], shape = shape,
...)$logLik
result
}
res <- optimize(f = fn, interval = c(0, 100), lower = 0, upper = 100,
maximum = TRUE, tol = 0.01, object, ...)
res
}
dltmp <- function(fit, i = 1, transform = transform) {
tree <- fit$tree
data <- getCols(fit$data, tree$tip.label)
if (is.null(attr(tree, "order")) || attr(tree, "order") != "postorder")
tree <- reorder(tree, "postorder")
q <- length(tree$tip.label)
node <- tree$edge[, 1]
edge <- tree$edge[, 2]
m <- max(edge)
dat <- vector(mode = "list", length = m)
eig <- fit$eig
w <- fit$w[i]
g <- fit$g[i]
bf <- fit$bf
el <- tree$edge.length
P <- getP(el, eig, g)
nr <- as.integer(attr(data, "nr"))
nc <- as.integer(attr(data, "nc"))
node <- as.integer(node - min(node))
edge <- as.integer(edge - 1)
nTips <- as.integer(length(tree$tip.label))
mNodes <- as.integer(max(node) + 1)
contrast <- attr(data, "contrast")
nco <- as.integer(dim(contrast)[1])
dat[(q + 1):m] <- .Call("LogLik2", data, P, nr, nc, node, edge, nTips,
mNodes, contrast, nco)
parent <- tree$edge[, 1]
child <- tree$edge[, 2]
nTips <- min(parent) - 1
datp <- vector("list", m)
el <- tree$edge.length
if (transform) dP <- getdP(tree$edge.length, eig, g)
else dP <- getdP2(tree$edge.length, eig, g)
datp[(nTips + 1)] <- dat[(nTips + 1)]
l <- length(child)
dl <- matrix(0, nr, l)
for (j in (m - 1):1) {
# tips have factor format, internal edges are matrices
if (child[j] > nTips) {
tmp2 <- (datp[[parent[j]]] / (dat[[child[j]]] %*% P[[j]]))
dl[, j] <- (tmp2 * (dat[[child[j]]] %*% dP[[j]])) %*% (w * bf)
datp[[child[j]]] <- (tmp2 %*% P[[j]]) * dat[[child[j]]]
}
else {
tmp2 <- datp[[parent[j]]] / ((contrast %*% P[[j]])[data[[child[j]]], ])
dl[, j] <- (tmp2 * ((contrast %*% dP[[j]])[data[[child[j]]], ])) %*%
(w * bf)
}
}
dl
}
dl <- function(x, transform = TRUE) {
l <- length(x$w)
dl <- dltmp(x, 1, transform)
i <- 2
while (i < (l + 1)) {
dl <- dl + dltmp(x, i, transform)
i <- i + 1
}
dl
}
# add control and change edge
optimPartEdge <- function(object, ...) {
tree <- object[[1]]$tree
theta <- tree$edge.length
theta <- pmax(theta, 1e-8)
tree$edge.length <- theta
tmptree <- tree
n <- length(object)
l <- length(theta)
nrv <- numeric(n)
for (i in 1:n) nrv[i] <- attr(object[[i]]$data, "nr")
cnr <- cumsum(c(0, nrv))
weight <- numeric(sum(nrv))
dl <- matrix(NA, sum(nrv), l)
for (i in 1:n) weight[(cnr[i] + 1):cnr[i + 1]] <- attr(object[[i]]$data,
"weight")
ll0 <- 0
for (i in 1:n) object[[i]] <- update(object[[i]], tree = tree)
for (i in 1:n) ll0 <- ll0 + object[[i]]$logLik
eps <- 1
scalep <- 1
k <- 1
while (eps > 0.001 & k < 50) {
if (scalep == 1) {
for (i in 1:n) {
lv <- drop(exp(object[[i]]$siteLik))
dl[(cnr[i] + 1):cnr[i + 1], ] <- dl(object[[i]], TRUE) / lv
}
sc <- colSums(weight * dl)
F <- crossprod(dl * weight, dl) + diag(l) * 1e-10
# add small ridge penalty for numerical stability
}
thetaNew <- log(theta) + scalep * solve(F, sc)
thetaNew <- pmax(thetaNew, log(1e-8))
tmptree$edge.length <- as.numeric(exp(thetaNew))
for (i in 1:n) object[[i]] <- update(object[[i]], tree = tmptree)
ll1 <- 0
for (i in 1:n) ll1 <- ll1 + object[[i]]$logLik
eps <- ll1 - ll0
if (eps < 0 || is.nan(eps)) {
scalep <- scalep / 2
eps <- 1
thetaNew <- log(theta)
ll1 <- ll0
}
else {
scalep <- 1
tree <- tmptree
}
theta <- exp(thetaNew)
theta <- pmax(theta, 1e-8)
ll0 <- ll1
k <- k + 1
}
for (i in 1:n) object[[i]] <- update(object[[i]], tree = tree)
object
}
makePart <- function(fit, rooted, weight = ~index + genes) {
if (inherits(fit, "phyDat")) {
x <- fit
dm <- dist.ml(x)
if (!rooted) tree <- NJ(dm)
else tree <- upgma(dm)
fit <- pml(tree, x, k = 4)
}
dat <- fit$data
if (class(weight)[1] == "formula")
weight <- xtabs(weight, data = attr(dat, "index"))
fits <- NULL
for (i in 1:dim(weight)[2]) {
ind <- which(weight[, i] > 0)
dat2 <- getRows(dat, ind)
attr(dat2, "weight") <- weight[ind, i]
fits[[i]] <- update(fit, data = dat2)
}
names(fits) <- colnames(fits)
fits
}
#' @rdname pmlPart
#' @export
multiphyDat2pmlPart <- function(x, method="unrooted", tip.dates=NULL, ...) {
if(is.list(x) && all(sapply(x,inherits, "phyDat"))) seq <- x
else if(inherits(x, "multiphyDat")) seq <- x@seq
else stop("x must be of class multiphyDat or a list of phyDat objects")
shared_tree <- TRUE
if (shared_tree) {
concatenate_x <- do.call(cbind.phyDat, seq)
tree <- candidate_tree(concatenate_x, method=method, tip.dates=tip.dates)
}
else tree <- NULL
fun <- function(x, method, tip.dates, tree, ...) {
if (is.null(tree)) {
tree <- candidate_tree(x, method=method, tip.dates=tip.dates)
}
pml(tree, x, ...)
}
fits <- lapply(seq, fun, tree = tree, ...)
fits
}
#' @rdname pmlPart
#' @export
pmlPart2multiPhylo <- function(x) {
res <- lapply(x$fits, FUN = function(x) x$tree)
class(res) <- "multiPhylo"
res
}
#' @export
plot.pmlPart <- function(x, ...) {
plot(pmlPart2multiPhylo(x), ...)
}
#' Partition model.
#'
#' Model to estimate phylogenies for partitioned data.
#'
#' The \code{formula} object allows to specify which parameter get optimized.
#' The formula is generally of the form \code{edge + bf + Q ~ rate + shape +
#' \dots{}}, on the left side are the parameters which get optimized over all
#' partitions, on the right the parameter which are optimized specific to each
#' partition. The parameters available are \code{"nni", "bf", "Q", "inv",
#' "shape", "edge", "rate"}. Each parameters can be used only once in the
#' formula. \code{"rate"} is only available for the right side of the formula.
#'
#' For partitions with different edge weights, but same topology, \code{pmlPen}
#' can try to find more parsimonious models (see example).
#'
#' \code{pmlPart2multiPhylo} is a convenience function to extract the trees out
#' of a \code{pmlPart} object.
#'
#' @aliases pmlPart
#' @param formula a formula object (see details).
#' @param object an object of class \code{pml} or a list of objects of class
#' \code{pml} .
#' @param control A list of parameters for controlling the fitting process.
#' @param model A vector containing the models containing a model for each
#' partition.
#' @param method One of "unrooted", "ultrametric" or "tiplabeled". Only unrooted
#' is properly supported right now.
#' @param tip.dates A named vector of sampling times associated to the
#' tips/sequences. Leave empty if not estimating tip dated phylogenies.
#' @param \dots Further arguments passed to or from other methods.
#' @param x an object of class \code{pmlPart}
#' @return \code{kcluster} returns a list with elements
#' \item{logLik}{log-likelihood of the fit} \item{trees}{a list of all trees
#' during the optimization.} \item{object}{an object of class \code{"pml"} or
#' \code{"pmlPart"}}
#' @author Klaus Schliep \email{klaus.schliep@@gmail.com}
#' @seealso
#' \code{\link{pml}},\code{\link{pmlCluster}},\code{\link{pmlMix}},
#' \code{\link{SH.test}}
#' @keywords cluster
#' @examples
#'
#' data(yeast)
#' dm <- dist.logDet(yeast)
#' tree <- NJ(dm)
#' fit <- pml(tree,yeast)
#' fits <- optim.pml(fit)
#'
#' weight=xtabs(~ index+genes,attr(yeast, "index"))[,1:10]
#'
#' sp <- pmlPart(edge ~ rate + inv, fits, weight=weight)
#' sp
#'
#' \dontrun{
#' sp2 <- pmlPart(~ edge + inv, fits, weight=weight)
#' sp2
#' AIC(sp2)
#'
#' sp3 <- pmlPen(sp2, lambda = 2)
#' AIC(sp3)
#' }
#'
#' @rdname pmlPart
#' @export pmlPart
pmlPart <- function(formula, object, control = pml.control(epsilon = 1e-8,
maxit = 10, trace = 1), model = NULL, method="unrooted", ...) {
method <- match.arg(method, c("unrooted", "ultrametric", "tipdated"))
if(method=="unrooted") rooted <- FALSE
else rooted <- TRUE
call <- match.call()
form <- phangornParseFormula(formula)
opt <- c("nni", "bf", "Q", "inv", "shape", "edge", "rate")
optAll <- match(opt, form$left)
optPart <- match(opt, form$right)
AllNNI <- !is.na(optAll[1])
AllBf <- !is.na(optAll[2])
AllQ <- !is.na(optAll[3])
AllInv <- !is.na(optAll[4])
AllGamma <- !is.na(optAll[5])
AllEdge <- !is.na(optAll[6])
PartNni <- !is.na(optPart[1])
PartBf <- !is.na(optPart[2])
PartQ <- !is.na(optPart[3])
PartInv <- !is.na(optPart[4])
PartGamma <- !is.na(optPart[5])
PartEdge <- !is.na(optPart[6])
PartRate <- !is.na(optPart[7])
if (PartNni) PartEdge <- TRUE
if(AllNNI) AllEdge <- TRUE
if (inherits(object, "multiphyDat")) {
if (AllNNI || AllEdge) object <- do.call(cbind.phyDat, object@seq)
else fits <- multiphyDat2pmlPart(object, rooted = rooted, ...)
}
if (inherits(object, "pml")) fits <- makePart(object, rooted = rooted, ...)
if (inherits(object, "phyDat")) fits <- makePart(object, rooted = rooted, ...)
if (inherits(object, "pmlPart")) fits <- object$fits
if (inherits(object, "list")) fits <- object
if(AllNNI) if(Ntip(fits[[1]]$tree) < (3 + !rooted)) AllNNI <- FALSE
trace <- control$trace
epsilon <- control$epsilon
maxit <- control$maxit
p <- length(fits)
# if(length(model)<p) model = rep(model, length = p)
if (AllQ) {
Q <- fits[[1]]$Q
for (i in 1:p) fits[[i]] <- update(fits[[i]], Q = Q)
}
if (AllBf) {
bf <- fits[[1]]$bf
for (i in 1:p) fits[[i]] <- update(fits[[i]], bf = bf)
}
if (AllInv) {
inv <- fits[[1]]$inv
for (i in 1:p) fits[[i]] <- update(fits[[i]], inv = inv)
}
if (AllGamma) {
shape <- fits[[1]]$shape
for (i in 1:p) fits[[i]] <- update(fits[[i]], shape = shape)
}
if (AllEdge || AllNNI) {
tree <- fits[[1]]$tree
tree$edge.length <- pmax(tree$edge.length, 1e-6)
for (i in 1:p) fits[[i]] <- update(fits[[i]], tree = tree)
}
m <- 1
logLik <- 0
for (i in 1:p) logLik <- logLik + fits[[i]]$log
eps <- 10
on.exit({
df <- matrix(1, 6, 2)
colnames(df) <- c("#df", "group")
rownames(df) <- c("Edge", "Shape", "Inv", "Bf", "Q", "Rate")
df[1, 1] <- length(fits[[1]]$tree$edge.length)
df[2, 1] <- fits[[1]]$k > 1
df[3, 1] <- fits[[1]]$inv > 0
df[4, 1] <- length(unique(fits[[1]]$bf)) - 1
df[5, 1] <- length(unique(fits[[1]]$Q)) - 1
df[6, 1] <- 0 # rates
if (PartEdge) df[1, 2] <- p
if (PartGamma) df[2, 2] <- p
if (PartInv) df[3, 2] <- p
if (PartBf) df[4, 2] <- p
if (PartQ) df[5, 2] <- p
if (PartRate) df[6, 1] <- p - 1
attr(logLik, "df") <- sum(df[, 1] * df[, 2])
object <- list(logLik = logLik, fits = fits, call = call, df = df)
class(object) <- "pmlPart"
return(object)
})
while (eps > epsilon & m < maxit) {
loli <- 0
# if (AllEdge) fits <- optimPartEdge(fits)
if (any(c(PartNni, PartBf, PartInv, PartQ, PartGamma, PartEdge, PartRate))){
for (i in 1:p) {
fits[[i]] <- optim.pml(fits[[i]], optNni = PartNni, optBf = PartBf,
optQ = PartQ, optInv = PartInv, optGamma = PartGamma,
optEdge = PartEdge, optRate = PartRate, optRooted = rooted,
control = pml.control(maxit = 3, epsilon = 1e-8, trace - 1),
model = model[i])
}
}
if (AllQ) {
Q <- fits[[1]]$Q
subs <- c(1:(length(Q) - 1), 0)
newQ <- optimPartQGeneral(fits, Q = Q, subs = subs)
for (i in 1:p) fits[[i]] <- update(fits[[i]], Q = newQ[[1]])
}
if (AllBf) {
bf <- fits[[1]]$bf
newBf <- optimPartBf(fits, bf = bf)
for (i in 1:p) fits[[i]] <- update(fits[[i]], bf = newBf)
}
if (AllInv) {
inv <- fits[[1]]$inv
newInv <- optimPartInv(fits, inv = inv)
for (i in 1:p) fits[[i]] <- update(fits[[i]], inv = newInv)
}
if (AllGamma) {
shape <- fits[[1]]$shape
newGamma <- optimPartGamma(fits, shape = shape)[[1]]
for (i in 1:p) fits[[i]] <- update(fits[[i]], shape = newGamma)
}
if (AllNNI) {
fits <- optimPartEdge(fits)
fits <- optimPartNNI(fits, AllEdge)
if (trace > 0) cat(attr(fits, "swap"), " NNI operations performed")
if(attr(fits, "swap") == 0) AllNNI <- FALSE
}
if (AllEdge) fits <- optimPartEdge(fits)
if (PartRate) {
tree <- fits[[1]]$tree
rate <- numeric(p)
wp <- numeric(p)
for (i in 1:p) {
wp[i] <- sum(fits[[i]]$weight)
rate[i] <- fits[[i]]$rate
}
ratemult <- sum(wp) / sum(wp * rate)
tree$edge.length <- tree$edge.length / ratemult
for (i in 1:p) fits[[i]] <- update(fits[[i]], tree = tree,
rate = rate[i] * ratemult)
}
loli <- 0
for (i in 1:p) loli <- loli + fits[[i]]$log
eps <- (logLik - loli) / loli
if (trace > 0) cat("loglik:", logLik, "-->", loli, "\n")
logLik <- loli
m <- m + 1
}
}
#
# pmlCluster
#
pmlCluster.fit <- function(formula, fit, weight, p = 4, part = NULL,
control = pml.control(epsilon = 1e-8, maxit = 10,
trace = 1), ...) {
call <- match.call()
form <- phangornParseFormula(formula)
opt <- c("nni", "bf", "Q", "inv", "shape", "edge", "rate")
optAll <- match(opt, form$left)
optPart <- match(opt, form$right)
AllNNI <- !is.na(optAll[1])
AllBf <- !is.na(optAll[2])
AllQ <- !is.na(optAll[3])
AllInv <- !is.na(optAll[4])
AllGamma <- !is.na(optAll[5])
AllEdge <- !is.na(optAll[6])
PartNni <- !is.na(optPart[1])
PartBf <- !is.na(optPart[2])
PartQ <- !is.na(optPart[3])
PartInv <- !is.na(optPart[4])
PartGamma <- !is.na(optPart[5])
PartEdge <- !is.na(optPart[6])
PartRate <- !is.na(optPart[7])
if (PartNni) PartEdge <- TRUE
nrw <- dim(weight)[1]
ncw <- dim(weight)[2]
if (is.null(part)) {
part <- rep(1:p, length = ncw)
part <- sample(part)
}
Part <- part
Gtrees <- vector("list", p)
dat <- fit$data
attr(fit$orig.data, "index") <- attr(dat, "index") <- NULL
for (i in 1:p) Gtrees[[i]] <- fit$tree
fits <- vector("list", p)
for (i in 1:p) fits[[i]] <- fit
trace <- control$trace
eps <- 0
m <- 1
logLik <- fit$log
trees <- list()
weights <- matrix(0, nrw, p)
lls <- matrix(0, nrw, p)
loli <- fit$log
oldpart <- part
eps2 <- 1
iter <- 0
swap <- 1
on.exit({
df <- matrix(1, 6, 2)
colnames(df) <- c("#df", "group")
rownames(df) <- c("Edge", "Shape", "Inv", "Bf", "Q", "Rate")
df[1, 1] <- length(fits[[1]]$tree$edge.length)
df[2, 1] <- fits[[1]]$k - 1
df[3, 1] <- fits[[1]]$inv > 0
df[4, 1] <- length(unique(fits[[1]]$bf)) - 1
df[5, 1] <- length(unique(fits[[1]]$Q)) - 1
df[6, 1] <- 0
if (PartEdge)
df[1, 2] <- p
if (PartGamma)
df[2, 2] <- p
if (PartInv)
df[3, 2] <- p
if (PartBf)
df[4, 2] <- p
if (PartQ)
df[5, 2] <- p
if (PartRate)
df[6, 1] <- p - 1
attr(logLik, "df") <- sum(df[, 1] * df[, 2])
res <- list(logLik = logLik, Partition = Part, trees = trees)
result <- list(logLik = loli, fits = fits, Partition = part, df = df,
res = res, call = call)
class(result) <- c("pmlPart")
return(result)
})
while (eps < ncw || abs(eps2) > control$eps) {
df2 <- 0
if (any(c(PartNni, PartBf, PartInv, PartQ, PartGamma, PartEdge, PartRate))){
for (i in 1:p) {
weights[, i] <- rowSums(weight[, which(part == i),
drop = FALSE])
ind <- which(weights[, i] > 0)
dat2 <- getRows(dat, ind)
attr(dat2, "weight") <- weights[ind, i]
fits[[i]] <- update(fits[[i]], data = dat2)
fits[[i]] <- optim.pml(fits[[i]], PartNni, PartBf,
PartQ, PartInv, PartGamma, PartEdge, PartRate,
control = pml.control(epsilon = 1e-8, maxit = 3, trace - 1))
lls[, i] <- update(fits[[i]], data = dat)$siteLik
Gtrees[[i]] <- fits[[i]]$tree
}
}
if (AllQ) {
Q <- fits[[1]]$Q
subs <- c(1:(length(Q) - 1), 0)
newQ <- optimPartQGeneral(fits, Q = Q, subs = subs)[[1]]
for (i in 1:p) fits[[i]] <- update(fits[[i]], Q = newQ)
df2 <- df2 + length(unique(newQ)) - 1
}
if (AllBf) {
bf <- fits[[1]]$bf
newBf <- optimPartBf(fits, bf = bf)
for (i in 1:p) fits[[i]] <- update(fits[[i]], bf = newBf)
df2 <- df2 + length(unique(newBf)) - 1
}
if (AllInv) {
inv <- fits[[1]]$inv
newInv <- optimPartInv(fits, inv = inv)
for (i in 1:p) fits[[i]] <- update(fits[[i]], inv = newInv)
# there was an Error
df2 <- df2 + 1
}
if (AllGamma) {
shape <- fits[[1]]$shape
newGamma <- optimPartGamma(fits, shape = shape)[[1]]
for (i in 1:p) fits[[i]] <- update(fits[[i]], shape = newGamma)
df2 <- df2 + 1
}
if (AllNNI) {
fits <- optimPartEdge(fits)
fits <- optimPartNNI(fits, AllEdge)
if (trace > 0) cat(attr(fits, "swap"), " NNI operations performed")
swap <- attr(fits, "swap")
}
if (AllEdge) {
fits <- optimPartEdge(fits)
df2 <- df2 + length(fits[[1]]$tree$edge.length)
}
if (PartRate) {
tree <- fits[[1]]$tree
rate <- numeric(p)
wp <- numeric(p)
for (i in 1:p) {
wp[i] <- sum(fits[[i]]$weight)
rate[i] <- fits[[i]]$rate
}
ratemult <- sum(wp) / sum(wp * rate)
tree$edge.length <- tree$edge.length / ratemult
for (i in 1:p) fits[[i]] <- update(fits[[i]], tree = tree,
rate = rate[i] * ratemult)
}
for (i in 1:p) lls[, i] <- update(fits[[i]], data = dat)$siteLik
trees[[m]] <- Gtrees
LL <- t(weight) %*% lls
# choose partitions which change
tmp <- (LL[cbind(1:ncw, part)] - apply(LL, 1, max)) / colSums(weight)
fixi <- numeric(p)
for (i in 1:p) {
tmpi <- which(part == i)
fixi[i] <- tmpi[which.max(tmp[tmpi])]
}
oldpart <- part
# restrict the number of elements changing groups
# If more than 25% would change, only the 25% with the highest increase per
# site change
if (sum(tmp == 0) / length(tmp) < .75) {
medtmp <- quantile(tmp, .25)
medind <- which(tmp <= medtmp)
part[medind] <- max.col(LL[medind, ])
}
else part <- max.col(LL)
# else part <- apply(LL, 1, which.max)
# force groups to have at least one member
part[fixi] <- 1:p
Part <- cbind(Part, part)
eps <- sum(diag(table(part, oldpart)))
eps2 <- loli
loli <- sum(apply(LL, 1, max))
eps2 <- (eps2 - loli) / loli
logLik <- c(logLik, loli)
if (trace > 0) print(loli)
Part <- cbind(Part, part)
df2 <- df2 + df2
if (eps == ncw & swap == 0)
AllNNI <- FALSE
m <- m + 1
if (eps == ncw)
iter <- iter + 1
if (iter == 3)
break
}
}
#' Stochastic Partitioning
#'
#' Stochastic Partitioning of genes into p cluster.
#'
#' The \code{formula} object allows to specify which parameter get optimized.
#' The formula is generally of the form \code{edge + bf + Q ~ rate + shape +
#' \dots{}}, on the left side are the parameters which get optimized over all
#' cluster, on the right the parameter which are optimized specific to each
#' cluster. The parameters available are \code{"nni", "bf", "Q", "inv",
#' "shape", "edge", "rate"}. Each parameter can be used only once in the
#' formula. There are also some restriction on the combinations how parameters
#' can get used. \code{"rate"} is only available for the right side. When
#' \code{"rate"} is specified on the left hand side \code{"edge"} has to be
#' specified (on either side), if \code{"rate"} is specified on the right hand
#' side it follows directly that \code{edge} is too.
#'
#' @param formula a formula object (see details).
#' @param fit an object of class \code{pml}.
#' @param weight \code{weight} is matrix of frequency of site patterns for all
#' genes.
#' @param p number of clusters.
#' @param part starting partition, otherwise a random partition is generated.
#' @param nrep number of replicates for each p.
#' @param control A list of parameters for controlling the fitting process.
#' @param \dots Further arguments passed to or from other methods.
#' @return \code{pmlCluster} returns a list with elements
#' \item{logLik}{log-likelihood of the fit} \item{trees}{a list of all trees
#' during the optimization.} \item{fits}{fits for the final partitions}
#' @author Klaus Schliep \email{klaus.schliep@@gmail.com}
#' @seealso
#' \code{\link{pml}},\code{\link{pmlPart}},\code{\link{pmlMix}},
#' \code{\link{SH.test}}
#' @references K. P. Schliep (2009). Some Applications of statistical
#' phylogenetics (PhD Thesis)
#'
#' Lanfear, R., Calcott, B., Ho, S.Y.W. and Guindon, S. (2012) PartitionFinder:
#' Combined Selection of Partitioning Schemes and Substitution Models for
#' Phylogenetic Analyses. \emph{Molecular Biology and Evolution}, \bold{29(6)},
#' 1695-1701
#' @keywords cluster
#' @examples
#'
#' \dontrun{
#' data(yeast)
#' dm <- dist.logDet(yeast)
#' tree <- NJ(dm)
#' fit <- pml(tree,yeast)
#' fit <- optim.pml(fit)
#'
#' weight <- xtabs(~ index+genes,attr(yeast, "index"))
#' set.seed(1)
#'
#' sp <- pmlCluster(edge~rate, fit, weight, p=1:4)
#' sp
#' SH.test(sp)
#' }
#'
#' @export pmlCluster
pmlCluster <- function(formula, fit, weight, p = 1:5, part = NULL, nrep = 10,
control = pml.control(epsilon = 1e-08, maxit = 10,
trace = 1), ...) {
call <- match.call()
form <- phangornParseFormula(formula)
if (any(p == 1)) {
opt2 <- c("nni", "bf", "Q", "inv", "shape", "edge")
tmp1 <- opt2 %in% form$left
tmp1 <- tmp1 | (opt2 %in% form$right)
fit <- optim.pml(fit, tmp1[1], tmp1[2], tmp1[3], tmp1[4],
tmp1[5], tmp1[6], control = control)
}
p <- p[p != 1]
if (length(p) == 0) return(fit)
n <- sum(weight)
k <- 2
BIC <- matrix(0, length(p) + 1, nrep)
BIC[1, ] <- AIC(fit, k = log(n))
LL <- matrix(NA, length(p) + 1, nrep)
LL[1, ] <- logLik(fit)
P <- array(dim = c(length(p) + 1, nrep, dim(weight)[2]))
tmpBIC <- Inf
choice <- c(1, 1)
for (j in p) {
tmp <- NULL
for (i in 1:nrep) {
tmp <- pmlCluster.fit(formula, fit, weight, p = j, part = part,
control = control, ...)
P[k, i, ] <- tmp$Partition
BIC[k, i] <- AIC(tmp, k = log(n))
LL[k, i] <- logLik(tmp)
if (BIC[k, i] < tmpBIC) {
tmpBIC <- BIC[k, i]
result <- tmp
choice <- c(k, i)
}
}
k <- k + 1
}
p <- c(1, p)
result$choice <- choice
result$BIC <- BIC
result$AllPartitions <- P
result$AllLL <- LL
result$p <- p
class(result) <- c("pmlCluster", "pmlPart")
result
}
#' @export
plot.pmlCluster <- function(x, which = c(1L:3L), caption =
list("BIC", "log-likelihood", "Partitions"), ...) {
show <- rep(FALSE, 3)
show[which] <- TRUE
choice <- x$choice
if (show[1]) {
X <- x$AllPartitions[choice[1], , ]
d <- dim(X)
ind <- order(X[choice[2], ])
im <- matrix(0, d[2], d[2])
for (j in 1:d[1]) {
for (i in 1:d[2]) im[i, ] <- im[i, ] + (X[j, ] == X[j, i])
}
image(im[ind, ind], ...)
}
if (show[1]) matplot(x$p, x$BIC, ylab = "BIC", xlab = "number of clusters")
if (show[1]) matplot(x$p, x$AllLL, ylab = "log-likelihood",
xlab = "number of clusters")
}
#' @export
print.pmlPart <- function(x, ...) {
levels <- attr(x$fits[[1]]$data, "levels")
r <- length(x$fits)
nc <- attr(x$fits[[1]]$data, "nc")
k <- x$fits[[1]]$k
lbf <- x$df["Bf", 2]
bf <- matrix(0, lbf, nc)
if (lbf > 1) dimnames(bf) <- list(1:r, levels)
lQ <- x$df["Q", 2]
Q <- matrix(0, lQ, nc * (nc - 1) / 2)
if (lQ > 1) dimnames(Q) <- list(1:r, NULL)
type <- attr(x$fits[[1]]$data, "type")
loli <- numeric(r)
rate <- numeric(r)
shape <- numeric(r)
sizes <- numeric(r)
inv <- numeric(r)
for (i in 1:r) {
loli[i] <- x$fits[[i]]$logLik
if (i <= lbf) bf[i, ] <- x$fits[[i]]$bf
if (i <= lQ) Q[i, ] <- x$fits[[i]]$Q
rate[i] <- x$fits[[i]]$rate
shape[i] <- x$fits[[i]]$shape
inv[i] <- x$fits[[i]]$inv
sizes[i] <- sum(attr(x$fits[[i]]$data, "weight"))
}
cat("\nloglikelihood:", x$logLik, "\n")
cat("\nloglikelihood of partitions:\n ", loli, "\n")
cat("AIC: ", AIC(x), " BIC: ", AIC(x, k = log(sum(sizes))), "\n\n")
cat("Proportion of invariant sites:", inv, "\n")
cat("\nRates:\n")
cat(rate, "\n")
if (k > 1) {
cat("\nShape parameter:\n")
cat(shape, "\n")
}
if (type == "AA") cat("Rate matrix:", x$fits[[1]]$model, "\n")
else {
cat("\nBase frequencies: \n")
print(bf)
cat("\nRate matrix:\n")
print(Q)
}
}
#' @export
logLik.pmlPart <- function(object, ...) {
res <- object$logLik
attr(res, "df") <- sum(object$df[, 1] * object$df[, 2])
class(res) <- "logLik"
res
}
optNNI <- function(fit, INDEX) {
tree <- fit$tree
ll.0 <- fit$ll.0
bf <- fit$bf
eig <- fit$eig
# k <- fit$k
w <- fit$w
g <- fit$g
rootEdges <- attr(INDEX, "root")
.dat <- NULL
parent <- tree$edge[, 1]
child <- tree$edge[, 2]
data <- getCols(fit$data, tree$tip.label)
datp <- rnodes(tree, data, w, g, eig, bf)
tmp <- length(tree$tip.label)
for (i in seq_along(w)) .dat[i, 1:tmp] <- new2old.phyDat(data)
evector <- numeric(max(parent))
evector[child] <- tree$edge.length
m <- dim(INDEX)[1]
loglik <- numeric(2 * m)
edgeMatrix <- matrix(0, 2 * m, 5)
for (i in 1:m) {
ei <- INDEX[i, ]
el0 <- evector[INDEX[i, ]]
l <- length(datp[, 1])
weight <- fit$weight
datn <- vector("list", 4 * l)
attr(datn, "dim") <- c(l, 4)
datn <- .dat[, ei[1:4], drop = FALSE]
if (!(ei[5] %in% rootEdges))
datn[, 1] <- datp[, ei[1], drop = FALSE]
new1 <- optim.quartet(el0[c(1, 3, 2, 4, 5)],
eig, bf, datn[, c(1, 3, 2, 4), drop = FALSE], g,
w, weight, ll.0, llcomp = fit$log)
new2 <- optim.quartet(el0[c(1, 4, 3, 2, 5)],
eig, bf, datn[, c(1, 4, 3, 2), drop = FALSE], g,
w, weight, ll.0, llcomp = fit$log)
loglik[(2 * i) - 1] <- new1[[2]]
loglik[(2 * i)] <- new2[[2]]
edgeMatrix[(2 * i) - 1, ] <- new1[[1]]
edgeMatrix[(2 * i), ] <- new2[[1]]
}
list(loglik = loglik, edges = edgeMatrix)
}
optimPartNNI <- function(object, AllEdge = TRUE, trace=0, ...) {
tree <- object[[1]]$tree
INDEX <- indexNNI(tree)
l <- length(object)
loglik0 <- 0
for (i in 1:l) loglik0 <- loglik0 + logLik(object[[i]])
l <- length(object)
TMP <- vector("list", l)
for (i in 1:l) {
TMP[[i]] <- optNNI(object[[i]], INDEX)
}
loglik <- TMP[[1]][[1]]
for (i in 2:l) loglik <- loglik + TMP[[i]][[1]]
swap <- 0
candidates <- loglik > loglik0
while (any(candidates)) {
ind <- which.max(loglik)
loglik[ind] <- -Inf
if (ind %% 2)
swap.edge <- c(2, 3)
else swap.edge <- c(2, 4)
tree2 <- changeEdge(tree, INDEX[(ind + 1) %/% 2, swap.edge],
INDEX[(ind + 1) %/% 2, ], TMP[[1]][[2]][ind, ])
tmpll <- 0
for (i in 1:l) {
if (!AllEdge) tree2 <- changeEdge(object[[i]]$tree, INDEX[(ind + 1) %/% 2,
swap.edge], INDEX[(ind + 1) %/% 2, ], TMP[[i]][[2]][ind, ])
tmpll <- tmpll + update(object[[i]], tree = tree2)$logLik
}
if (tmpll < loglik0)
candidates[ind] <- FALSE
if (tmpll > loglik0 + 1e-8) {
swap <- swap + 1
tree <- tree2
indi <- which(rep(colSums(apply(INDEX, 1, match,
INDEX[(ind + 1) %/% 2, ], nomatch = 0)) > 0, each = 2))
candidates[indi] <- FALSE
loglik[indi] <- -Inf
for (i in 1:l) {
if (!AllEdge) tree2 <- changeEdge(object[[i]]$tree,
INDEX[(ind + 1) %/% 2, swap.edge],
INDEX[(ind + 1) %/% 2, ], TMP[[i]][[2]][ind, ])
object[[i]] <- update(object[[i]], tree = tree2)
}
loglik0 <- 0
for (i in 1:l) loglik0 <- loglik0 + logLik(object[[i]])
if(trace>0) cat(loglik0, "\n")
}
}
if (AllEdge) object <- optimPartEdge(object)
attr(object, "swap") <- swap
object
}