Created
October 25, 2015 16:36
-
-
Save flashton2003/7fdc77f125b7067fadb7 to your computer and use it in GitHub Desktop.
Read in a pair wise distance matrix as a list, and convert to matrix and do hierarchical clustering and draw a tree and export it as a newick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(reshape) | |
library(ape) | |
# read in the data | |
f <- read.delim("~/Dropbox/mash_project/2015.09.23.all_vs_all.txt", header=F) | |
# use reshape's cast function to change to matrix | |
m <- cast(f, V1 ~ V2) | |
# set the row names | |
rownames(m) <- m[,1] | |
# get rid of a couple of rows | |
m <- m[,-2] | |
# convert any 0s that were read in as strings to integers | |
m <- apply(m, 2, as.numeric ) | |
# change the matrix to a distance matrix | |
d <- dist(m) | |
# do hierarchical clustering | |
h <- hclust(d) | |
# plot the dendrogram | |
plot(h) | |
# use ape's as phylo function | |
tree <- as.phylo(h) | |
# export as newick for viewing in figtree | |
write.tree(phy=tree, file = 'exported.tree') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I get the sample file or any dummy data (2015.09.23.all_vs_all.txt) to try?.