Dear Romain,

the small function below returns the out group. I define he outgroup as the
clade with less taxa from the root. If both clades have the same number of
taxa in it  one clades is chosen randomly.

library(phangorn)

getOutgroup <- function(tree){
  tree = midpoint(tree) # you probably have done this beforehand
  tmp = Descendants(tree)
  kids = Children(tree, getRoot(tree))
  tmp = tmp[kids]
  x = sapply(tmp, length)
  tree$tip.label[tmp[[which.min(x)]]]
}

# example
trees = rmtree(10, 5)

outGroups <- lapply(trees, getOutgroup)
res = sapply(outGroups , function(x, y)any(y %in% x), y="t1")
sum(res) # number of outgroups which contain "t1"
which(res) # which trees have "t1" in the outgroup
which(res & (sapply(outGroups, length)==1)) # which trees have only "t1" in
the outgroup

trees = lapply(trees, midpoint)
class(trees) = "multiPhylo"
plot(trees)

Cheers,
Klaus


PS: midpoint is now part of the phangorn package and much faster for larger
trees.


On Thu, Oct 23, 2014 at 5:57 AM, romain <[email protected]> wrote:

> Dear Klauss,
>
> Following the topic below, I was wondering if there is function to get the
> name of the outgroup after having rooted the tree with the midpoint
> function.
> I can plot the tree and just check it by eyes. However I got > 1000 trees
> and I want to count how many times on particular species is placed as
> outgroup.
>
> Thank you for your help.
>
> Romain Blanc-Mathieu
> PhD INRA-CNRS-UNS Agrobiotech sophia Antipolis
> 06160 Antibes
> FRANCE
>
>
> Dear Robin,
>
> here is a function, that does midpoint rooting, you have to attach the
> phangorn package. It is likely to appear in one of the next ape
> versions.
>
> Cheers,
> Klaus
>
>
>
>
> midpoint <- function(tree){
>      dm = cophenetic(tree)
>      tree = unroot(tree)
>      rn = max(tree$edge)+1
>      maxdm = max(dm)
>      ind =  which(dm==maxdm,arr=TRUE)[1,]
>      tmproot = Ancestors(tree, ind[1], "parent")
>      tree = phangorn:::reroot(tree, tmproot)
>      edge = tree$edge
>      el = tree$edge.length
>      children = tree$edge[,2]
>      left = match(ind[1], children)
>      tmp = Ancestors(tree, ind[2], "all")
>      tmp= c(ind[2], tmp[-length(tmp)])
>      right = match(tmp, children)
>      if(el[left]>= (maxdm/2)){
>           edge = rbind(edge, c(rn, ind[1]))
>           edge[left,2] = rn
>           el[left] = el[left] - (maxdm/2)
>           el = c(el, maxdm/2)
>      }
>      else{
>          sel = cumsum(el[right])
>          i = which(sel>(maxdm/2))[1]
>          edge = rbind(edge, c(rn, tmp[i]))
>          edge[right[i],2] = rn
>          eltmp =  sel[i] - (maxdm/2)
> #        el = c(el, sel[i] - (maxdm/2))
>          el = c(el, el[right[i]] - eltmp)
>          el[right[i]] = eltmp
>      }
>      tree$edge.length = el
>      tree$edge=edge
>      tree$Nnode  = tree$Nnode+1
>      phangorn:::reorderPruning(phangorn:::reroot(tree, rn))
> }
>
>
>
>
> On 9/2/10, Velzen, Robin van <Robin.vanVelzen at wur.nl  <
> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo>> wrote:
> >/  Dear Ape authors and mailing list members,
> />/
> />/  I am new to R. and to Ape and very much impressed by the
> functionality the
> />/  platform offers. I have used Ape to construct distance-based trees
> using the
> />/  'read.dna', 'dist.dna' and 'nj' functions. Now I wish to root the
> resulting
> />/  Neighbor Joining tree using midpoint rooting, but have not been able
> to find
> />/  the right function.
> />/
> />/  Does anyone know if there is a function for midpoint rooting in Ape or
> />/  similar R. package, or if there is a smart way to define a midpoint
> root
> />/  outgroup that can be used with the 'root' function in Ape?
> />/
> />/  Any help or suggestion will be much appreciated!
> />/
> />/  Thanks,
> />/
> />/  Robin
> />/
> />/  Robin van Velzen
> />/  PhD student
> />/  Biosystematics Group
> />/  Wageningen University
> />/
> />/  Wageningen Campus, Radix building 107, Room W4.Aa.095
> />/  Droevendaalsesteeg 1, 6708 PB Wageningen, The Netherlands
> />/  PO Box 647, 6700 AP Wageningen, The Netherlands
> />/  Tel. +31 (0)317 483425
> />/  http://www.bis.wur.nl
> />/
> />/
> />/
> />/     [[alternative HTML version deleted]]
> />/
> />/  _______________________________________________
> />/  R-sig-phylo mailing list
> />/  R-sig-phylo at r-project.org  <
> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo>
> />/  https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> />/
> /
>
> --
> Klaus Schliep
> Universit� Paris 6 (Pierre et Marie Curie)
> 9, Quai Saint-Bernard, 75005 Paris
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-phylo mailing list - [email protected]
> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> Searchable archive at
> http://www.mail-archive.com/[email protected]/




-- 
Klaus Schliep
Postdoctoral Fellow
Revell Lab, University of Massachusetts Boston

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-phylo mailing list - [email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/[email protected]/

Reply via email to