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.
Add a comment