/spacodiR/R/phylogeneticUtilities.R

http://github.com/eastman/spacodiR · R · 148 lines · 111 code · 27 blank · 10 comment · 29 complexity · 68f20a7ea80c162859cf6ef68bafe361 MD5 · raw file

  1. #general statistical function for comparing two vectors on non-independent samples
  2. #author: JM EASTMAN 2010; updated 03.06.2011 to remove NA
  3. resamp.test <-
  4. function(obs=obs, exp=exp, mu=0, iter=10000, two.tailed=FALSE, na.rm=TRUE){
  5. if(na.rm) {
  6. oe=lapply(list(obs,exp), function(x) return(x[!is.na(x)]))
  7. obs=oe[[1]]
  8. exp=oe[[2]]
  9. }
  10. O=as.numeric(obs)[sample(1:length(obs), iter, replace=TRUE)]
  11. E=as.numeric(exp)[sample(1:length(exp), iter, replace=TRUE)]
  12. result=c(O-(E+mu))
  13. p=round(sum(result>=0)/iter, digits=nchar(iter))
  14. q=1-p
  15. if(two.tailed) {
  16. res=list(diffs=result, 2*(c(p,q)[which(c(p,q)==min(c(p,q)))]))
  17. } else {
  18. res=list(diffs=result, greater=p, lesser=q)
  19. }
  20. return(res)
  21. }
  22. #general phylogenetic utility for returning the first (usually, unless a polytomy exists) two descendants the supplied node
  23. #author: JM EASTMAN 2010
  24. get.desc.of.node <-
  25. function(node, phy) {
  26. return(phy$edge[which(phy$edge[,1]==node),2])
  27. }
  28. #general phylogenetic utility for returning all descendants (listed as given in phy$edge[,2]) of a node (excluding the supplied node)
  29. #author: JM EASTMAN 2010, based on GEIGER:::node.leaves()
  30. get.descendants.of.node <-
  31. function (node, phy, tips=FALSE)
  32. {
  33. node <- as.numeric(node)
  34. n <- Ntip(phy)
  35. if(node <= n) return(NULL)
  36. l <- c()
  37. d <- get.desc.of.node(node, phy)
  38. for (j in d) {
  39. l <- c(l, j)
  40. if (j > n) {
  41. l <- c(l, get.descendants.of.node(j, phy))
  42. }
  43. }
  44. if(tips) return(l[l<=n]) else return(l)
  45. }
  46. #determines if a value falls within a range [min,max]
  47. #author: JM Eastman 2010
  48. withinrange <-
  49. function(x, min, max) {
  50. a=sign(x-min)
  51. b=sign(x-max)
  52. if(abs(a+b)==2) return(FALSE) else return(TRUE)
  53. }
  54. ######
  55. phy.deresolve=function(phy, time.range=c(0,0), relative=TRUE)
  56. {
  57. if (is.null(phy$edge.length))
  58. stop("The tree does not appear to have branch lengths")
  59. if (class(phy)!="phylo")
  60. stop("The tree does not appear to be a valid phylo object")
  61. if(length(time.range)>2)
  62. stop("Cannot interpret the range of time with more than two elements")
  63. if(length(time.range)==1)
  64. time.range=c(0,time.range)
  65. time.range=time.range[order(time.range)]
  66. bb <- branching.times(phy)
  67. if(relative) bb=bb/max(bb)
  68. inr=sapply(bb, function(x) withinrange(x, time.range[1], time.range[2]))
  69. ind <- as.numeric(names(bb[inr]))
  70. if(any(ind==Ntip(phy)+1)) ind=ind[-which(ind==Ntip(phy)+1)]
  71. n <- length(ind)
  72. if (!n) {
  73. return(phy)
  74. } else {
  75. ind.tmp = match(ind, phy$edge[,2])
  76. ind = ind.tmp[!is.na(ind.tmp)]
  77. }
  78. orig.edge=phy$edge
  79. orig.phy=phy
  80. ntips=Ntip(phy)
  81. reedge <- function(ancestor, des.to.drop) {
  82. wh <- which(phy$edge[, 1] == des.to.drop)
  83. dd <- which(orig.edge[, 2] == des.to.drop)
  84. dropped.branch <- phy$edge.length[dd]
  85. d.d <- c(get.desc.of.node(des.to.drop, orig.phy))
  86. if(length(d.d)) phy$edge.length[match(d.d, orig.edge[,2])]<<-phy$edge.length[match(d.d, orig.edge[,2])]+dropped.branch
  87. for (k in wh) {
  88. if (phy$edge[k, 2] %in% node.to.drop) {
  89. reedge(ancestor, phy$edge[k, 2])
  90. } else {
  91. phy$edge[k, 1] <<- ancestor
  92. }
  93. }
  94. }
  95. node.to.drop <- phy$edge[ind, 2]
  96. anc <- phy$edge[ind, 1]
  97. for (i in 1:n) {
  98. if (anc[i] %in% node.to.drop)
  99. next
  100. reedge(anc[i], node.to.drop[i])
  101. }
  102. phy$edge <- phy$edge[-ind, ]
  103. phy$edge.length <- phy$edge.length[-ind]
  104. phy$Nnode <- phy$Nnode - n
  105. sel <- phy$edge > min(node.to.drop)
  106. for (i in which(sel)) phy$edge[i] <- phy$edge[i] - sum(node.to.drop < phy$edge[i])
  107. if (!is.null(phy$node.label))
  108. phy$node.label <- phy$node.label[-(node.to.drop - length(phy$tip.label))]
  109. phy
  110. }
  111. ######
  112. phy.nodetimes <-
  113. function(phy, time.range=c(0,0), proportion=TRUE) {
  114. N=Ntip(phy)
  115. phy$node.label=NULL
  116. xx = c(rep(0,N), branching.times(phy))
  117. names(xx)[1:N]=1:N
  118. if(proportion) xx=xx/max(branching.times(phy))
  119. tt=sapply(xx,function(x) withinrange(x,min(time.range),max(time.range)))
  120. return(xx[tt])
  121. }