library(MatrixCorrelation)

# read in generated matrices (this is tomato samples from manuscript - Dong et al., 2017)
load("SRR5748725_matrix_simple_norm.Rdata")
mtx1 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748725_matrix_simple_norm_rep.Rdata")
mtx1b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748726_matrix_simple_norm.Rdata")
mtx2 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748726_matrix_simple_norm_rep.Rdata")
mtx2b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748729_matrix_simple_norm.Rdata")
mtx3 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748729_matrix_simple_norm_rep.Rdata")
mtx3b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748730_matrix_simple_norm.Rdata")
mtx4 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748730_matrix_simple_norm_rep.Rdata")
mtx4b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748731_matrix_simple_norm.Rdata")
mtx5 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748731_matrix_simple_norm_rep.Rdata")
mtx5b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748732_matrix_simple_norm.Rdata")
mtx6 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748732_matrix_simple_norm_rep.Rdata")
mtx6b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748733_matrix_simple_norm.Rdata")
mtx7 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748733_matrix_simple_norm_rep.Rdata")
mtx7b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

load("SRR5748734_matrix_simple_norm.Rdata")
mtx8 <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))
load("SRR5748734_matrix_simple_norm_rep.Rdata")
mtx8b <- matrix(mtx, nrow=length(mtx[,1]), ncol=length(mtx[1,]), dimnames=list(rownames(mtx),colnames(mtx)))

# cleanup
remove(mtx)

# get matrices ready for bulk analysis
mtxs <- list(mtx1,mtx1b,mtx2,mtx2b,mtx3,mtx3b,mtx4,mtx4b,mtx5,mtx5b,mtx6,mtx6b,mtx7,mtx7b,mtx8,mtx8b)

# reduce rows and columns to only those common to all matrices
minrow <- Reduce(intersect, lapply(mtxs, rownames))
mincol <- Reduce(intersect, lapply(mtxs, colnames))
min <- intersect(mincol,minrow)
mtxs <- lapply(mtxs, function(x) x[min,])
mtxs <- lapply(mtxs, function(x) x[,min])

# create a distance matrix using the PSI metric from the MatrixCorrelation package
d = matrix(rep(0,16*16), nrow=16, ncol=16, dimnames=list(c("mtx1","mtx1b","mtx2","mtx2b","mtx3","mtx3b","mtx4","mtx4b","mtx5","mtx5b","mtx6","mtx6b","mtx7","mtx7b","mtx8","mtx8b"),c("mtx1","mtx1b","mtx2","mtx2b","mtx3","mtx3b","mtx4","mtx4b","mtx5","mtx5b","mtx6","mtx6b","mtx7","mtx7b","mtx8","mtx8b")))
for(i in 1:16) {
  for(j in 1:16) {
    print(c(i,j))
    # converting rather arbitrarily PSI similarity to distance here
    d[i,j] = 1-PSI(mtxs[[i]],mtxs[[j]],center=FALSE)^2
  }
}

# plot dendrogram
dd <- as.dist(d)
hc <- hclust(dd, method = "single")
plot(hc, labels = NULL, hang = 0.1, 
     main = "Heatmap similarity dendrogram", sub = NULL,
     xlab = "Heatmap version", ylab = "PSI distance")


