Skip to content

Instantly share code, notes, and snippets.

@flashton2003
Created October 25, 2015 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flashton2003/7fdc77f125b7067fadb7 to your computer and use it in GitHub Desktop.
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
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')
@Amrithasuresh
Copy link

Can I get the sample file or any dummy data (2015.09.23.all_vs_all.txt) to try?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment