I'm generating NJ trees using the adegenet package in r and I want to
color code the branches. I expect this is fairly straight forward but
I'm missing something that I haven't been able to find through my online
searches. I can label points by referencing my annotation file but when it comes to generating colors for the branches based on
the same variable I have a problem.
packages = list("ape","pegas", "sequinr", "ggplot2", "adegenet", "phangorn")
install.packages("ape","pegas", "sequinr", "ggplot2", "adegenet", "phangorn")
library("ape")
library("pegas")
library("seqinr")
library("ggplot2")
library("adegenet")
library("phangorn")
setwd("/Users/ErinWilkus/Desktop/ErinsSNP1")
## Reading fasta file
dna <- fasta2DNAbin("SNP_ALLALL.fa")
dna
class(dna)
as.character(dna[1:5, 1:10])
## Annotation file to include seed source
annot <- read.csv("~/Desktop/ErinsSNP1/AnnotationsALL.csv")
head(annot)
#Checking that samples are identicle in both files
dim(dna)
dim(annot)
all(annot$accession==rownames(dna))
table(annot$Source)
### Calculating distance with JC69 (Jukes and Cantor (1969)). It assumes that all substitutions (i.e. a change of a base by another one) have the same probability. This probability is the same for all sites along the DNA sequence. Other distances are possible.
D <- dist.dna(dna, model = "JC69")
class(D)
length(D)
#Plotting distance matrix
temp<-as.data.frame(as.matrix(D))
table.paint(temp, cleg=0, clabel.row=.5, clabel.col=.5)
#transform data to generate distance matric image
temp<-t(as.matrix(D))
temp<- temp[,ncol(temp):1]
par(mar=c(1,5,5,1))
image(x=1:230, y=1:230, temp, col=rev(heat.colors(100)), xaxt="n", yaxt="n", xlab="", ylab="")
axis(side=2, at=1:230, lab=rownames(dna), las=2, cex.axis=.5)
axis(side=3, at=1:230, lab=rownames(dna), las=3, cex.axis=.5)
### Making a Neighbor-Joining Tree Estimation
tre <- nj(D)
class(tre)
tre<-ladderize(tre)
tre
#Plot simple neighbor joining tree with color codes for sources
# Why won't this work?
plot(tre, show.tip=FALSE)
title("Unrooted NJ tree")
myPal<- colorRampPalette(c("red", "yellow", "green", "blue"))
tiplabels(annot$Source, bg=num2col(annot$Source, col.pal=myPal), cex=.5)
temp<- pretty(M:'SEA 5', 28)
legend("bottomleft", fill=num2col (temp, col.pal=myPal), leg=temp, ncol=2)