Constructing a NJ tree with color coded branches

410 views
Skip to first unread message

Erin Wilkus

unread,
Mar 4, 2016, 3:56:55 PM3/4/16
to Davis R Users' Group
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)

Michael Levy

unread,
Mar 7, 2016, 12:09:14 AM3/7/16
to davi...@googlegroups.com

Hi Erin,

It’s a little hard to tell what’s going on without being able to run your code, but it looks to me like what you have should put colors underneath your tip labels, yes? If you want to color code the branches, you need a color for each segment (note that this is greater than the number of tips) and you can provide that vector of colors to the edge.color argument in the plot call, e.g. plot(tre, edge.color = myColors). Does that help?


-- 
Michael Levy
c: 304-376-4523


--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages