R Markdown

#This script shows Step 10 of the Unit 4 project
#Download from Canvas and test with example.nwk
#Then change ALL comments and code to read in your tree.nwk file
library(ape) #remember to install package first
## Warning: package 'ape' was built under R version 4.0.3
#Create Newick file of the tree from file downloaded from NCBI Virus
myTree <- read.tree('example.nwk')

#Check that root is the reference genome, "NC_045512.2||China|2019-12"
rerootTree <- root(myTree, myTree$tip.label[9])

#Add formatting to the tree for the different groups
#Create vector to store colors you want for each branch
#Tips 4, 10, and 11 are A.1, blue
#Tips 2, 3 are A.2, cyan
#Tips 8,9 are B, red
#Tip 5 is B.1 orange
#Tip 1 is B.1.320 green
#Tip 6 is B.1.610 darkorange3
#Tip 7 is B.3 gold

#Plot tree with labels to match tip labels and edge labels
#Comment out after you look up numbers
# plot.phylo(rerootTree, "radial",
#            main="Phylogenetic tree of COVID-19 strains",
#            show.tip.label = TRUE, 
#            font = .5,
#            cex = .5)

#edgelabels() 
#tiplabels()

edgecol <- c()
edgecol[1:20] = "gray" #start with all edges gray
edgecol[c(8,9,10,14)] = "blue" #set A.1 branches blue
edgecol[11:13] = "cyan" #set A.2 branches cyan
edgecol[18:19]="red" #change color to red for edge lines connected to B variants
edgecol[15] = "orange" #set B.1 branch orange
edgecol[7] = "green" #set B.1.320 branch green
edgecol[16] = "darkorange3" #set B.1.610 branch darkorange3
edgecol[17] = "gold" #set B.3 branch gold

#Create vector to store colors you want for each label
#Reference is black
#Utah is red
#Spain in purple
tipcol = c("red","purple","purple","red","purple","purple","red","purple","black","red","red")

#Rename tips in rerooted tree
rerootTree$tip.label
##  [1] "MT334526.1_||USA|2020-03-07"   "MT292571.1_||Spain|2020-03-09"
##  [3] "MT292574.1_||Spain|2020-03-02" "MT334533.1_||USA|2020-03-13"  
##  [5] "MT292570.1_||Spain|2020-03-10" "MT292569.1_||Spain|2020-03-09"
##  [7] "MT334529.1_||USA|2020-03-10"   "MT292572.1_||Spain|2020-03-10"
##  [9] "NC_045512.2||China|2019-12"    "MT334523.1_||USA|2020-03-05"  
## [11] "MT334531.1_||USA|2020-03-12"
rerootTree$tip.label <- c('Utah_Mar7','Spain_Mar9','Spain_Mar2',
                      'Utah_Mar13','Spain_Mar10','Spain_Mar9_2',
                      'Utah_Mar10','Spain_Mar10_2','Reference',
                      'Utah_Mar5','Utah_Mar12')

Including Plots

#Use plot.phylo function to create radial style tree
plot.phylo(rerootTree, "fan",
           main="Phylogenetic tree of COVID-19 strains",
           edge.color = edgecol,
           tip.color = tipcol,
           show.tip.label = TRUE, 
           cex = .4)

legend("topleft",
       legend = c("Utah", "Spain", "Reference"), #Name of groups
       fill = c("red","purple","black"),       # Color of the squares
       border = "black", # Color of the border of the squares
       cex =.6, #sets legend size
       xpd= TRUE) #places outside plot area
legend("topright",
       legend = c("A.1","A.2","B","B.1","B.1.320","B.1.610","B.3"), #Name of groups
       col = c("blue","cyan","red","orange","green","darkorange3","gold"),# Color of the squares
       border = "black", # Color of the border of the squares
       cex =.6,#sets legend size
       lwd=2, #sets line weight
       xpd=TRUE) #places outside plot area