-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 13.4 KB
/
.Rhistory
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
threshold <- 10
count <- 0
count
if (diff>=threshold) {count <- count+1}
count
count/length(intNodes)
nodeImbFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
intNodes <- (Ntips+1):(Ntips+tree$Nnode) # internal node numbers
count <- 0
for (i in intNodes) {
tmp <- nodeImb(tree,i)
diff <- abs(tmp[[1]]-tmp[[2]])
if (diff>=threshold) {count <- count+1}
}
return(count/length(intNodes))
}
tree <- rtree(20)
plot(tree)
nodeImbFrac(tree,threshold=5)
tree <- rtree(20)
plot(tree)
nodeImbFrac(tree,threshold=5)
tree <- rtree(20)
plot(tree)
nodeImbFrac(tree,threshold=5)
tree <- rtree(20)
plot(tree)
nodeImbFrac(tree,threshold=5)
nodeImbFrac(tree,threshold=10)
tree <- rtree(20)
plot(tree)
nodeImbFrac(tree,threshold=5)
tree <- rtree(20)
plot(tree)
nodeImbFrac(tree,threshold=5)
#' nodeImbFrac
#'
#' Calculate the fraction of internal nodes with an imbalance above a given threshold.
#'
#' @author Michelle Kendall \email{michelle.louise.kendall@@gmail.com}
#'
#' @param tree a tree of class \code{phylo} or \code{phylo4}.
#' @param threshold a threshold value for node imbalance.
#'
#' @return For any internal node, the function \code{\link{nodeImb}} gives the number of tip descendants of each of the node's descending branches. \code{nodeImbFrac} returns the fraction of internal nodes where the difference between these numbers is greater than or equal to the threshold.
#'
#' @import ape ****FILL IN
#'
#' @seealso \code{\link{nodeImb}}, \code{\link{nodeDepth}}
#'
#' @examples
#' ## Find the fraction of internal nodes with an imbalance of 5 or more, in a random tree with 20 tips:
#' tree <- rtree(20)
#' plot(tree)
#' nodeImbFrac(tree,threshold=5)
#'
#' @export
nodeImbFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
intNodes <- (Ntips+1):(Ntips+tree$Nnode) # internal node numbers
count <- 0
for (i in intNodes) {
tmp <- nodeImb(tree,i)
diff <- abs(tmp[[1]]-tmp[[2]])
if (diff>=threshold) {count <- count+1}
}
return(count/length(intNodes))
}
setwd("C:/Users/mlkendal/Dropbox/phyloTop")
devtools::document()
tree <- rtree(20)
plot(tree)
nodeDepthFrac(tree,threshold=5)
nodeDepthFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
intNodes <- (Ntips+1):(Ntips+tree$Nnode) # internal node numbers
count <- 0
for (i in intNodes) {
tmp <- nodeImb(tree,i)
diff <- abs(tmp[[1]]-tmp[[2]])
if (diff>=threshold) {count <- count+1}
}
return(count/length(intNodes))
}
nodeDepthFrac(tree,threshold=5)
tree <- rtree(20)
Ntips <- length(tree$tip.label) # number of tips
nodes <- 1:(Ntips+tree$Nnode) # node numbers
nodes
count <- 0
tmp <- nodeDepth(tree,i)
tmp
i <- 1
tmp <- nodeDepth(tree,i)
tmp
nodeDepthFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
nodes <- 1:(Ntips+tree$Nnode) # node numbers
count <- 0
for (i in nodes) {
tmp <- nodeDepth(tree,i)
if (tmp>=threshold) {count <- count+1}
}
return(count/length(intNodes))
}
tree <- rtree(20)
plot(tree)
nodeDepthFrac(tree,threshold=5)
nodes
tmp <- nodeDepth(tree,i)
tmp
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
nodes <- 1:(Ntips+tree$Nnode) # node numbers
nodes
count
for (i in nodes) {
tmp <- nodeDepth(tree,i)
if (tmp>=threshold) {count <- count+1}
}
tmp <- nodeDepth(tree,i)
i
i <- 1
tmp <- nodeDepth(tree,i)
tmp <- nodeDepth(tree,2)
tmp <- nodeDepth(tree,3)
tmp <- nodeDepth(tree,4)
tmp <- nodeDepth(tree,39)
tmp <- nodeDepth(tree,38)
nodelabel()
plot(tree)
nodelabel()
plot(tree)
nodelabels()
depths <- getDepths(tree)
depths
allDepths <- c(depths$tipDepths,depths$nodeDepths)
allDepths
l <- length(allDepths)
l
nodeDepth <- function(tree,node) {
depths <- getDepths(tree)
allDepths <- c(depths$tipDepths,depths$nodeDepths)
l <- length(allDepths)
if (node > l) stop(paste0("Please supply a valid node number between 1 and ",l))
return(allDepths[[node]])
}
#' nodeDepthFrac
#'
#' Calculate the fraction of nodes with a depth greater than or equal to a given threshold.
#'
#' @author Michelle Kendall \email{michelle.louise.kendall@@gmail.com}
#'
#' @param tree a tree of class \code{phylo} or \code{phylo4}.
#' @param threshold a threshold value for node depth
#'
#' @return The fraction of nodes with a depth greater than or equal to the threshold (see \code{\link{nodeDepth}} for more details on the meaning of node depth).
#'
#' @import ape
#'
#' @seealso \code{\link{nodeDepth}}
#'
#' @examples
#' ## Find the fraction of nodes with a depth of 5 or more, in a random tree with 20 tips:
#' tree <- rtree(20)
#' plot(tree)
#' nodelabels()
#' nodeDepthFrac(tree,threshold=5)
#'
#' @export
nodeDepthFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
nodes <- 1:(Ntips+tree$Nnode) # node numbers
count <- 0
for (i in nodes) {
tmp <- nodeDepth(tree,38)
if (tmp>=threshold) {count <- count+1}
}
return(count/length(intNodes))
}
tree <- rtree(20)
plot(tree)
nodelabels()
nodeDepthFrac(tree,threshold=5)
nodeDepthFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
nodes <- 1:(Ntips+tree$Nnode) # node numbers
count <- 0
for (i in nodes) {
tmp <- nodeDepth(tree,38)
if (tmp>=threshold) {count <- count+1}
}
return(count/length(nodes))
}
tree <- rtree(20)
plot(tree)
nodelabels()
nodeDepthFrac(tree,threshold=5)
tree <- rtree(20)
plot(tree)
nodelabels()
nodeDepthFrac(tree,threshold=5)
tree <- rtree(10)
plot(tree)
nodelabels()
nodeDepthFrac(tree,threshold=5)
tree <- rtree(10)
plot(tree)
nodeDepthFrac(tree,threshold=5)
nodeDepthFrac <- function(tree,threshold) {
# initial tree check
tree <- phyloCheck(tree)
Ntips <- length(tree$tip.label) # number of tips
nodes <- 1:(Ntips+tree$Nnode) # node numbers
count <- 0
for (i in nodes) {
tmp <- nodeDepth(tree,i)
if (tmp>=threshold) {count <- count+1}
}
return(count/length(nodes))
}
tree <- rtree(10)
plot(tree)
nodeDepthFrac(tree,threshold=5)
nodeDepthFrac(tree,threshold=4)
tree <- rtree(20)
tree$edge.length
tree$edge.length <- rep(1,38)
tree <- rtree(20)
plot(tree)
plot(tree, type="dendogram")
plot(tree, type="phylogram")
plot(tree, type="cladogram")
tree$edge.length <- rep(1,38) # so that the depths are easier to view in the plot
plot(tree)
nodeDepthFrac(tree,threshold=5)
10/39
14/39
tree <- rtree(20)
tree$edge.length <- rep(1,38) # so that the depths are easier to view in the plot
plot(tree)
nodeDepthFrac(tree,threshold=5)
28/39
tree <- rtree(20)
tree$edge.length <- rep(1,38) # so that the depths are easier to view in the plot
plot(tree)
nodeDepthFrac(tree,threshold=5)
18/39
tree <- rtree(20)
tree$edge.length <- rep(1,38) # so that the depths are easier to view in the plot
plot(tree)
nodeDepthFrac(tree,threshold=5)
25/39
24/39
nodeDepthFrac(tree,threshold=5)
nodeDepthFrac(tree,threshold=5) * 39
tree <- rtree(20)
tree$edge.length <- rep(1,38) # so that the depths are easier to view in the plot
plot(tree)
nodeDepthFrac(tree,threshold=5)
18/39
devtools::document()
devtools::document()
devtools::document()
devtools::load_all()
devtools::build()
setwd("C:/Users/mlkendal/Dropbox/phyloTop")
devtools::document()
library(installr)
updateR()
setwd("C:/Users/mlkendal/Dropbox/phyloTop")
devtools::document()
devtools::load_all()
devtools::check()
devtools::build()
phyloTopfuncs <- c("avgLadder",
"cherries",
"colless.phylo",
"configShow",
"getDepths",
"getLabGenealogy",
"ILnumber",
"ladderSizes",
"ladderShow",
"makeEpiRecord",
"makePhyloTree",
"makeTransTree",
"maxHeight",
"modelCreate",
"modelSummary",
"nConfig",
"nodeDepth",
"nodeDepthFrac",
"nodeImb",
"nodeImbFrac",
"phyloCheck",
"pitchforks",
"rtree4",
"sackin.phylo",
"splitTop",
"stairs",
"treeImb",
"widths"
)
?avgLadder
?avgLadder
tree <- rtree(20)
avgLadder(tree)
avgLadder(tree, normalise=TRUE)
plot(tree)
avgLadder(tree)
avgLadder(tree, normalise=TRUE)
?cherries
tree <- rtree(10)
plot(tree)
cherries(tree)
cherries(tree, normalise=TRUE)
?configShow
configShow(tree,2, edge.width=2)
tree <- rtree(10)
plot(tree)
cherries(tree)
configShow(tree,2, edge.width=2)
?colless.phylo
library(apTreeshape)
?colless
colless.phylo(rtree(10))
tree <- rtree(10)
plot(tree)
colless.phylo(tree)
?configShow
configShow(rtree(20),3, edge.width=2)
configShow(rtree(20),3, edge.width=2)
configShow(rtree(20),3, edge.width=2)
configShow(rtree(20),3, edge.width=2)
configShow(rtree(20),3, edge.width=2)
?getDepths
tree <- rtree(20)
treeDepths <- getDepths(tree)
tree$tip.label <- treeDepths$tipDepths
tree$node.label <- treeDepths$nodeDepths
plot(tree, show.node.label=TRUE)
?getLabGenealogy
myepirecord <- makeEpiRecord(c(1,2,3,4))
mygenealogy <- getLabGenealogy(myepirecord)
plot(mygenealogy)
?ILnumber
tree <- rtree(10)
plot(tree)
ILnumber(tree)
tree <- rtree(10)
plot(tree)
ILnumber(tree)
ILnumber(tree, normalise=TRUE)
?ladderSizes
## Find ladder sizes in a random tree with 20 tips:
tree <- rtree(20)
ladderSizes(tree)
tree <- rtree(20)
plot(tree)
ladderSizes(tree)
tree <- phyloCheck(tree)
ntip=length(tree$tip.label)
ntip
nn=tree$Nnode
nn
Ancs=(ntip+1):(ntip+nn) # assumes tips are 1:ntip, then internal nodes
Ancs
Pointers=t(vapply(Ancs, function(x) tree$edge[tree$edge[,1]==x,2], FUN.VALUE=c(1,2)))
Pointers
BrIsLadder=(rowSums(DP)==1) # which branches (1:nn) are ladders (1 or 0)
BrIsLadder
DP=Pointers
DP[DP<=ntip]=0
DP[DP>ntip]=1
DP
BrIsLadder=(rowSums(DP)==1) # which branches (1:nn) are ladders (1 or 0)
BrIsLadder
LadderBr=(1:nrow(DP))[BrIsLadder] # vector of only br that are part of a ladder
LadderBr
LadderBr <- which(rowSums(DP)==1)
LadderBr
LadderBr <- which(rowSums(DP)==1) BrIsLadder=(rowSums(DP)==1) # which branches are ladders
LadderBr <- which(rowSums(DP)==1)
LadderBr
BrIsLadder=(rowSums(DP)==1) # which branches are ladders
BrIsLadder
BrIsLadder=(rowSums(DP)==1) # which branches are ladders
BrIsLadder
LadderBr=(1:nrow(DP))[BrIsLadder] # vector of only br that are part of a ladder
LadderBr
LadderNodes=ntip+LadderBr # vector of NODES (orig node id) that are ladders
LadderNodes
allIsLadder=c(rep(0,ntip),BrIsLadder) # 0 for all tips, then 1 or 0 for internal nodes
allIsLadder
BrIsLadder=(rowSums(DP)==1) # which branches are ladders
BrIsLadder
LadderBr=(1:nrow(DP))[BrIsLadder] # vector of only br that are part of a ladder
LadderBr
EL=rbind( cbind(LadderNodes, Pointers[LadderBr,1]),cbind(LadderNodes,Pointers[LadderBr,2])) # Ancestor, Descendant where Ancestor is a Ladder Node (but fast as no finding in the edgelist).
EL
ToKeep=allIsLadder[EL[,2]] # 1 only where second col of EL is a ladder node
ToKeep
EdgeList=EL[ToKeep==1,] # only keep edges where ancestor and des are ladd
EdgeList
list(ladderSizes=0,ladderBr=NULL)
EdgeList <- t(as.data.frame(EdgeList)) # prevents problems when there is only one row
chEL=matrix(data="hi",nrow=nrow(EdgeList),ncol=2) # coerce to format for graph
chEL[,1]=paste("v",as.character(EdgeList[,1]),sep="")
chEL[,2]=paste("v",as.character(EdgeList[,2]),sep="")
gr=graph_from_edgelist(chEL)
ladderSizes <- components(gr)$csize # sizes of different ladder components
tree <- rtree(20)
plot(tree)
ladderSizes(tree)
?ladderShow
ladderShow(rtree(50), ladderCol="blue", edge.width=2)
tree <- rtree(50)
tree <- rtree(50)
ladderShow(tree, ladderCol="blue", edge.width=2)
ladderSizes(tree)
tree <- rtree(50)
ladderShow(tree, ladderCol="blue", edge.width=2)
ladderSizes(tree)
tree <- rtree(50)
plot(tree)
ladderSizes(tree)
tree <- rtree(500)
plot(tree)
ladderSizes(tree)
tree <- rtree(20)
plot(tree)
ladderSizes(tree)
?ladderShow
tree <- rtree(20)
plot(tree)
ladderSizes(tree)
ladderShow(tree)
?makeEpiRecord
setwd("C:/Users/mlkendal/Dropbox/phyloTop")
devtools::document()
library(treescape)
devtools::load_all(".")
pkgdown::build_site()
pkgdown::build_site()
```r
library(devtools)
install_github("michellekendall/phyloTop")
```
The stable version can be installed from CRAN using:
```r
install.packages("phyloTop")
```
install.packages("phyloTop")
```r
library("phyloTop")
```
```r
phyloTop(rmtree(10,50))
```
phyloTop(rmtree(10,50))
phyloTop(rmtree(10,50))
# Chunk 3: load
library("phyloTop")
set.seed(123)
phyloTop(rmtree(10,50))
subtreeShow(rtree(20),nodeList=c(23,35), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
subtreeShow(rtree(20),nodeList=c(25,35), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
set.seed(123)
phyloTop(rmtree(10,50))
subtreeShow(rtree(20),nodeList=c(25,35), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
set.seed(123)
phyloTop(rmtree(10,50))
subtreeShow(rtree(20),nodeList=c(25,35), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
subtreeShow(rtree(20),nodeList=c(25,32), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
set.seed(123)
phyloTop(rmtree(10,50))
subtreeShow(rtree(20),nodeList=c(25,32), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
set.seed(123)
phyloTop(rmtree(10,50))
subtreeShow(rtree(20),nodeList=c(25,33), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
# Chunk 3: load
library("phyloTop")
# Chunk 4: phyloTop_example
set.seed(123)
phyloTop(rmtree(10,50))
# Chunk 5: subtreeShow_example
subtreeShow(rtree(20),nodeList=c(25,33), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
pkgdown::build_site()
pkgdown::build_site()
library("phyloTop")
set.seed(123)
phyloTop(rmtree(10,50))
subtreeShow(rtree(20),nodeList=c(25,33), mainCol="navy", subtreeCol="cyan", nodeLabelCol="cyan", edge.width=2)
pgkdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()
pkgdown::build_site()