Answers.Body: <p>there are nice features in R <strong><a href="https://yulab-smu.github.io/treedata-book/chapter5.html#visualizing-and-annotating-tree-using-grammar-of-graphics" rel="nofollow noreferrer">ggtree</a></strong> package. You may use the <em>geom_cladelabel()</em> function for your purposes. But I haven't find yet a way to automatize clade annotation. To do it, you have to be sure of the clade homogeneity before removing tiplabs. here are some commands to do clade label annotation:</p>
  
  <pre><code>annot&lt;-read.delim("annota.txt", header = T)
  
  ggtr&lt;-as.treedata(pars.a)
  g&lt;-ggtree(ggtr)
  #find the node number corresponding to the common ancestor of the top clade in you tree
  MRCA(ggtr, "AAL40400", "AAF25519")
  #here, check that all accessions belong to the same organism/group
  subset(annot, label=="AAL40400" | label=="AAF25519")
  #in this case, sequences are related to bovine.
  g&lt;-g + geom_cladelabel(node=89, label="Bovine", align=F, fontsize=4)
  plot(g)
  
  #another example
  MRCA(ggtr, "AAA46456", "AAF97738")
  subset(annot, label=="AAA46456" | label=="AAF97738")
  g&lt;-g+ geom_cladelabel(node=73, label="Rodent", align=F, fontsize=4)
  plot(g)
  </code></pre>
  
  <p><a href="https://i.sstatic.net/UbbRG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UbbRG.png" alt="build with ggtree"></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T08:28:35.157Z
Answers.Text: looks bit more complicated due to this "have to be sure of the clade homogeneity"  if there is a mix up then its difficult
Answers.UserDisplayName: kcm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T08:31:04.780Z
Answers.Text: you may keep tip labels where clade are heterogeneous. Or you should consider smaller clades or consider larger taxonomic groups (eg I summarized mice and rats as rodents in the second example). In any case, i think it deserve a visual/manual inspection to ensure the correctness.
Answers.UserDisplayName: thomas duge de bernonville
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T08:41:28.577Z
Answers.Text: okay so the better idea is to combine the larger taxa group.So first i have to find the node number then combine. Is it possible to give separate color to the clade? such as bovine one color then rodent other etc ..?okay the color can be done i saw the manual
Answers.UserDisplayName: kcm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T08:44:10.660Z
Answers.Text: yes it is fully customizable. Finding the appropriate number is the more fastidious step I think. There is probably a way to automatize it however.
Answers.UserDisplayName: thomas duge de bernonville
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T08:46:14.950Z
Answers.Text: nodelabels(cex=0.4) this is how i can label the node in base R which i was doing it is the same for the object which is made using this "g<-ggtree(ggtr)"
Answers.UserDisplayName: kcm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T08:52:55.747Z
Answers.Text: this a good thing to identify interesting node numbers!
Answers.UserDisplayName: thomas duge de bernonville
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T09:18:27.610Z
Answers.Text: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/108577/discussion-between-krushnach-chandra-and-thomas-duge-de-bernonville).
Answers.UserDisplayName: kcm
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-28T07:18:11.710Z
Answers.LastActivityDate: 2020-05-28T07:18:11.710Z
Answers.UserDisplayName: thomas duge de bernonville
Body: <p>I was looking to overlay annotation on my phylogeny made,which was solved here <a href="https://bioinformatics.stackexchange.com/questions/13377/comparing-phylogeny-in-r">question asked</a> where i was looking to compare two different phylogeny which i could do.</p>
  
  <p>Now i want to label the tips into various groups such as if its human or canine etc.One of the possible solution i found is this <a href="https://stackoverflow.com/questions/58554889/in-r-how-can-i-color-the-labels-from-my-phylogenetic-tree-using-bionj-from-ape">one</a> not sure how to implement it.</p>
  
  <p>alignment  <a href="https://drive.google.com/open?id=1ErIpdS99Vu_tsWtvNQCKftzFmpJAUKuu" rel="nofollow noreferrer">file</a>
  annotation <a href="https://drive.google.com/file/d/1YQve01NEy1pyNeRNdvLAB9FVqHGNL37D/view?usp=sharing" rel="nofollow noreferrer">file</a></p>
  
  <pre><code>library(dendextend)
  library(seqinr)
  library(phytools)
  library(phangorn)
  
  a&lt;-read.alignment("clean_dup_align_fast.fas", format="fasta") 
  a.phydat&lt;-as.phyDat(a)
  dist.a.phydat&lt;-dist.dna(as.DNAbin(a.phydat))
  upgma.a&lt;-upgma(dist.a.phydat)
  parsimony(upgma.a,a.phydat)
  pars.a &lt;- optim.parsimony(upgma.a, a.phydat)
  pars.a&lt;-acctran(pars.a, a.phydat)
  pars.a.rooted&lt;-root(pars.a, outgroup="AAA64460", resolve.root=T)
  pars.a.rooted.dd&lt;-as.dendrogram(force.ultrametric(pars.a.rooted))
  </code></pre>
  
  <p>i want to label according to the start column which is my source in my annotation file 
  Any suggestion or help would be really appreciated </p>
  
  <p>My working solution using <a href="http://rforbiochemists.blogspot.com/2017/01/playing-with-phylogenetic-tree.html" rel="nofollow noreferrer">this</a> , this works as I did the alignment of sequences using full header name which kind of help to look for the pattern and grep it.
  The data <a href="https://drive.google.com/file/d/1M7Mq0gKWvauqd-VKJCrNJ8KLMSXxx1Mu/view?usp=sharing" rel="nofollow noreferrer">file</a></p>
  
  <p>I would still like to see the ggtree way instead of putting lots of label in the plot I would only like to see if I can annotate all the tips which have one organism source as one color rather putting all the labels </p>
  
  <pre><code>pars.a
  # "Phylogenetic tree with 516 tips and 514 internal nodes"
  
  # by using the class() function
  class(pars.a)
  # "phylo"
  
  # or by using the str() structure function
  str(tree) 
  str(pars.a)
  # "List of 4"
  # this list includes <span class="math-container">$edge, $</span>Nnode, <span class="math-container">$ tip.label and $</span>edge.length
  # the tree<span class="math-container">$tip.label includes family designation
  #tree$</span>tip.label  # 516 of these
  pars.a$tip.label
  # from the Science paper, we have seven kinase families:
  # kinase categories... TK, TKL, STE, CK1, AGC, CAMK, CMGC
  # with the following colours
  # "red", "green", "paleblue", "orange", "yellow", "purple", "pink", "green" 
  
  
  # by using the grep()function on the tree<span class="math-container">$tip.label part of the object
  # we can find the tip labels that include "TK/" - i.e. tyrosine kinases
  #grep("TK/", tree$</span>tip.label)  # gives a list of numbers with "TK/" in tip label
  #length(grep("TK/", tree$tip.label))
  
  grep("Human",pars.a<span class="math-container">$tip.label)
  length(grep("Human",ml.a.rooted$</span>tip.label)
  )
  # thus there are 94 tip labels with that are designated TK (not TKL tyrosine kinase like)
  
  # make a vector for each tip.label called tipcol with black on all of these...
  #tipcol &lt;- rep('black', length(tree<span class="math-container">$tip.label))
  tipcol &lt;- rep('black',length(pars.a$</span>tip.label))
  
  # make a vector with our list of kinase categories
  #kinaseCats &lt;- c("TK/", "TKL", "STE", "CK1", "AGC", "CAMK", "CMGC", "RGC")
  kinaseCats &lt;- c("Human","Rat","Murine","Bovine","Canine","Feline","Porcine","Gallus","Bat","Infectious","Transmissible")
  
  # make a vector of color we want:
  colorsList &lt;-c("red", "darkolivegreen3", "blue", "orange", "blueviolet", "purple", "pink", "green","brown","navyblue","purple4")
  
  # replace colours where grep gives "TK" as red, etc in a loop
  #for(i in 1:length(kinaseCats)){
   # tipcol[grep(kinaseCats[i], tree$tip.label)] &lt;- colorsList[i]
  #}
  
  
  for(i in 1:length(kinaseCats)){
    tipcol[grep(kinaseCats[i], pars.a$tip.label)] &lt;- colorsList[i]
  }
  
  
  
  # plot with edge length false to see nodes better
  #plot(tree, 
   #    use.edge.length = FALSE,
    #   tip.color=tipcol, 
     #  cex = 0.25)
  
  
  plot(pars.a, 
       use.edge.length = FALSE,
       tip.color=tipcol, 
       cex = 0.5)
  # slow to draw due to text - a bit annoying!
  nodelabels(cex=0.4)
  </code></pre>
  
  <p>figure <a href="https://i.sstatic.net/IQCJJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/IQCJJ.png" alt="plot "></a> </p>
  
Tags: <r><phylogeny>
Title: Adding external annotation to the phylogeny tree
ThreadText: # Adding external annotation to the phylogeny tree
  
  (Asked by: username_0 on 2020-05-27)
  
  I was looking to overlay annotation on my phylogeny made,which was solved here [question asked](https://bioinformatics.stackexchange.com/questions/13377/comparing-phylogeny-in-r) where i was looking to compare two different phylogeny which i could do.
  
  Now i want to label the tips into various groups such as if its human or canine etc.One of the possible solution i found is this [one](https://stackoverflow.com/questions/58554889/in-r-how-can-i-color-the-labels-from-my-phylogenetic-tree-using-bionj-from-ape) not sure how to implement it.
  
  alignment [file](https://drive.google.com/open?id=1ErIpdS99Vu_tsWtvNQCKftzFmpJAUKuu)
  annotation [file](https://drive.google.com/file/d/1YQve01NEy1pyNeRNdvLAB9FVqHGNL37D/view?usp=sharing)
  
  ```
  library(dendextend)
  library(seqinr)
  library(phytools)
  library(phangorn)
  
  a<-read.alignment("clean_dup_align_fast.fas", format="fasta") 
  a.phydat<-as.phyDat(a)
  dist.a.phydat<-dist.dna(as.DNAbin(a.phydat))
  upgma.a<-upgma(dist.a.phydat)
  parsimony(upgma.a,a.phydat)
  pars.a <- optim.parsimony(upgma.a, a.phydat)
  pars.a<-acctran(pars.a, a.phydat)
  pars.a.rooted<-root(pars.a, outgroup="AAA64460", resolve.root=T)
  pars.a.rooted.dd<-as.dendrogram(force.ultrametric(pars.a.rooted))
  
  ```
  
  i want to label according to the start column which is my source in my annotation file
  Any suggestion or help would be really appreciated
  
  My working solution using [this](http://rforbiochemists.blogspot.com/2017/01/playing-with-phylogenetic-tree.html) , this works as I did the alignment of sequences using full header name which kind of help to look for the pattern and grep it.
  The data [file](https://drive.google.com/file/d/1M7Mq0gKWvauqd-VKJCrNJ8KLMSXxx1Mu/view?usp=sharing)
  
  I would still like to see the ggtree way instead of putting lots of label in the plot I would only like to see if I can annotate all the tips which have one organism source as one color rather putting all the labels
  
  ```
  pars.a
  # "Phylogenetic tree with 516 tips and 514 internal nodes"
  
  # by using the class() function
  class(pars.a)
  # "phylo"
  
  # or by using the str() structure function
  str(tree) 
  str(pars.a)
  # "List of 4"
  # this list includes $edge, $Nnode, $ tip.label and $edge.length
  # the tree$tip.label includes family designation
  #tree$tip.label  # 516 of these
  pars.a$tip.label
  # from the Science paper, we have seven kinase families:
  # kinase categories... TK, TKL, STE, CK1, AGC, CAMK, CMGC
  # with the following colours
  # "red", "green", "paleblue", "orange", "yellow", "purple", "pink", "green" 
  
  # by using the grep()function on the tree$tip.label part of the object
  # we can find the tip labels that include "TK/" - i.e. tyrosine kinases
  #grep("TK/", tree$tip.label)  # gives a list of numbers with "TK/" in tip label
  #length(grep("TK/", tree$tip.label))
  
  grep("Human",pars.a$tip.label)
  length(grep("Human",ml.a.rooted$tip.label)
  )
  # thus there are 94 tip labels with that are designated TK (not TKL tyrosine kinase like)
  
  # make a vector for each tip.label called tipcol with black on all of these...
  #tipcol <- rep('black', length(tree$tip.label))
  tipcol <- rep('black',length(pars.a$tip.label))
  
  # make a vector with our list of kinase categories
  #kinaseCats <- c("TK/", "TKL", "STE", "CK1", "AGC", "CAMK", "CMGC", "RGC")
  kinaseCats <- c("Human","Rat","Murine","Bovine","Canine","Feline","Porcine","Gallus","Bat","Infectious","Transmissible")
  
  # make a vector of color we want:
  colorsList <-c("red", "darkolivegreen3", "blue", "orange", "blueviolet", "purple", "pink", "green","brown","navyblue","purple4")
  
  # replace colours where grep gives "TK" as red, etc in a loop
  #for(i in 1:length(kinaseCats)){
   # tipcol[grep(kinaseCats[i], tree$tip.label)] <- colorsList[i]
  #}
  
  for(i in 1:length(kinaseCats)){
    tipcol[grep(kinaseCats[i], pars.a$tip.label)] <- colorsList[i]
  }
  
  # plot with edge length false to see nodes better
  #plot(tree, 
   #    use.edge.length = FALSE,
    #   tip.color=tipcol, 
     #  cex = 0.25)
  
  plot(pars.a, 
       use.edge.length = FALSE,
       tip.color=tipcol, 
       cex = 0.5)
  # slow to draw due to text - a bit annoying!
  nodelabels(cex=0.4)
  
  ```
  
  figure [![plot ](https://i.sstatic.net/IQCJJ.png)](https://i.sstatic.net/IQCJJ.png)
  
  **Tags:** r, phylogeny
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-05-28. Score: 1 | Accepted answer
  
  there are nice features in R **[ggtree](https://yulab-smu.github.io/treedata-book/chapter5.html#visualizing-and-annotating-tree-using-grammar-of-graphics)** package. You may use the *geom\_cladelabel()* function for your purposes. But I haven't find yet a way to automatize clade annotation. To do it, you have to be sure of the clade homogeneity before removing tiplabs. here are some commands to do clade label annotation:
  
  ```
  annot<-read.delim("annota.txt", header = T)
  
  ggtr<-as.treedata(pars.a)
  g<-ggtree(ggtr)
  #find the node number corresponding to the common ancestor of the top clade in you tree
  MRCA(ggtr, "AAL40400", "AAF25519")
  #here, check that all accessions belong to the same organism/group
  subset(annot, label=="AAL40400" | label=="AAF25519")
  #in this case, sequences are related to bovine.
  g<-g + geom_cladelabel(node=89, label="Bovine", align=F, fontsize=4)
  plot(g)
  
  #another example
  MRCA(ggtr, "AAA46456", "AAF97738")
  subset(annot, label=="AAA46456" | label=="AAF97738")
  g<-g+ geom_cladelabel(node=73, label="Rodent", align=F, fontsize=4)
  plot(g)
  
  ```
  
  [![build with ggtree](https://i.sstatic.net/UbbRG.png)](https://i.sstatic.net/UbbRG.png)
  
  > **Comments:**
  > **username_0**: looks bit more complicated due to this "have to be sure of the clade homogeneity"  if there is a mix up then its difficult  
  > **username_1**: you may keep tip labels where clade are heterogeneous. Or you should consider smaller clades or consider larger taxonomic groups (eg I summarized mice and rats as rodents in the second example). In any case, i think it deserve a visual/manual inspection to ensure the correctness.  
  > **username_0**: okay so the better idea is to combine the larger taxa group.So first i have to find the node number then combine. Is it possible to give separate color to the clade? such as bovine one color then rodent other etc ..?okay the color can be done i saw the manual  
  > **username_1**: yes it is fully customizable. Finding the appropriate number is the more fastidious step I think. There is probably a way to automatize it however.  
  > **username_0**: nodelabels(cex=0.4) this is how i can label the node in base R which i was doing it is the same for the object which is made using this "g<-ggtree(ggtr)"  
  > **username_1**: this a good thing to identify interesting node numbers!  
  > **username_0**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/108577/discussion-between-krushnach-chandra-and-username_1).  
  
  ---
  
  