shared.nodes: Getting shared nodes between several phylogenies

View source: R/shared_nodes.R

shared.nodesR Documentation

Getting shared nodes between several phylogenies

Description

This function returns, for each phylogeny, a list of logical vectors for the nodes being shared between different topologies

Usage

shared.nodes(trees, own = NULL, shared = NULL)

Arguments

trees

The initial variable

own

Optional. A list giving a subset of retained taxa for each phylogeny, if one does not want to consider all phylogenetic tips

shared

Optional. A vector giving the common taxa between phylogenies to be retained. Automatically computed if not given as being all shared tips.

Examples

require(ape)
set.seed(1)
orig.tree<-rtree(50)
if(!require(foreach)){
  subsets<-list()
  trees<-list()
  for(i in 1:4){
    subsets[[i]]<-sample(orig.tree$tip.label,30,FALSE)
    trees[[i]]<-keep.tip(orig.tree,subsets[[i]])
  }
} else {
  subsets<-foreach(i=1:4)%do%{sample(orig.tree$tip.label,30,FALSE)}
  trees<-foreach(i=1:4)%do%{keep.tip(orig.tree,subsets[[i]])}}
Reduce(intersect,subsets) # Common species - only 7 species, that's convenient to see common nodes
par(mfrow=c(2,2),mar=c(3.1,2.1,2.1,0.1))
for(i in 1:4){ # Let's now plot the trees, and highlight the common species, the edges leading to them, and the nodes involved
  if(require(RRphylo)){
    inv.nodes<-unlist(sapply(Reduce(intersect,subsets),function(x){RRphylo::getMommy(trees[[i]],which(trees[[i]]$tip.label%in%x))}))
    plot(trees[[i]],show.tip.label=FALSE,edge.color=binarize(trees[[i]]$edge[,2],zero=c(unique(inv.nodes),which(trees[[i]]$tip.label%in%Reduce(intersect,subsets))),output=c("red","black")))
  } else {plot(trees[[i]],show.tip.label=FALSE)}
  tiplabels(trees[[i]]$tip.label[trees[[i]]$tip.label%in%Reduce(intersect,subsets)],
            which(trees[[i]]$tip.label%in%Reduce(intersect,subsets)),adj=-0.1,col="red",frame="none",font=3,cex=0.7)
  tiplabels(trees[[i]]$tip.label[!trees[[i]]$tip.label%in%Reduce(intersect,subsets)],
            which(!trees[[i]]$tip.label%in%Reduce(intersect,subsets)),adj=-0.1,col="black",frame="none",font=3,cex=0.7)
  if(require(RRphylo)){
    inv.dic.nodes<-unique(inv.nodes)[sapply(unique(inv.nodes),function(x){length(which(trees[[i]]$edge[trees[[i]]$edge[,1]==x,2]%in%c(unique(inv.nodes),which(trees[[i]]$tip.label%in%Reduce(intersect,subsets)))))})>1]
    nodelabels(as.character((Ntip(trees[[i]])+1):(Ntip(trees[[i]])+Nnode(trees[[i]])))[inv.dic.nodes-Ntip(trees[[i]])],
             c((Ntip(trees[[i]])+1):(Ntip(trees[[i]])+Nnode(trees[[i]])))[inv.dic.nodes-Ntip(trees[[i]])],col="red",cex=0.7,adj=-0.5,frame="none")
    nodelabels(as.character((Ntip(trees[[i]])+1):(Ntip(trees[[i]])+Nnode(trees[[i]])))[-(inv.dic.nodes-Ntip(trees[[i]]))],
             c((Ntip(trees[[i]])+1):(Ntip(trees[[i]])+Nnode(trees[[i]])))[-(inv.dic.nodes-Ntip(trees[[i]]))],col="black",cex=0.7,adj=-0.5,frame="none")
  } else{nodelabels(frame="none",col="black",cex=0.7,adj=-0.5)}
}
shared.nodes(trees) # Here is the direct output of the function, with logicals for nodes of each tree
for(i in 1:4){print(as.character(c((Ntip(trees[[i]])+1):(Ntip(trees[[i]])+Nnode(trees[[i]]))))[shared.nodes(trees)[[i]]])} # Here are the node numbers retained for each tree (compare with the plot)


jacobmaugoust/ULT documentation built on May 16, 2023, 1:29 p.m.