An undergrad that I am working with generated a large phylogeny and because I don't know the group that well I wondered if the taxa were grouping in a sensible way.  I thought that it would be nice to put a prefix for family and tribe for each of the two hundred species.  With this it would be easy to see if something is way out of place.  So this is basically what I have for tip labels:

Tribolium_castaneum
Penthe_oblonga
Blaps_variolaris
Gnaptorina_cylindricollis
Atasthalomorpha_dentifrons
Bolitophagus_reticulatus
Eledona_agricola
Cylindronotus_laevioctostriatus
Nesotes_aethiops

But I have text file that has most of these species with  prefixes representing higher taxonomy like this:

TeTeBl_Blaps_variolaris
TeTeBl_Gnaptorina_cylindricollis
TeTeBo_Atasthalomorpha_dentifrons
TeTeBo_Bolitophagus_reticulatus
TeTeBo_Eledona_agricola
TeTeHe_Cylindronotus_laevioctostriatus
TeTeHe_Nesotes_aethiops


So what we need is an R function that will find the matches and replace them in the tree file.  Here is a function that will do that for us.

prefix.tips <- function(tree, names, prefix){    #defines the function variables
  for(i in 1:length(tree$tip.label)){            #counter for tips of the tree

    for(j in 1:length(names)){                   #counter the list of names
                                                 #renames appropriate tips
      if(tree$tip.label[i]==substring(names[j],prefix, nchar(names[j]))){tree$tip.label[i]<-names[j]   
      }
    }
  }
  return(tree)
  }


One caveat the way that I did this does require that the prefix length is the same in each name.



0

Add a comment

Great Blogs
Great Blogs
About Me
About Me
My Photo
I am broadly interested in the application and development of comparative methods to better understand genome evolution at all scales from nucleotides to chromosomes.
Subscribe
Subscribe
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.