createLeafNode <- function(hclust, i) {
list(name = hclust$labels[[i]], order = hclust$order[[i]])
}
hclustToTree <- function(hclust) {
if (length(hclust$merge) == 0)
return(NULL)
merges <- list()
for (index in 1:nrow(hclust$merge)) {
left <- hclust$merge[index, 1]
right <- hclust$merge[index, 2]
if (left < 0)
left <- createLeafNode(hclust, -left) else left <- merges[[left]]
if (right < 0)
right <- createLeafNode(hclust, -right) else right <- merges[[right]]
if (left$order > right$order) {
tmp <- left
left <- right
right <- tmp
}
merges[[index]] <- list(children = list(left, right), order = left$order)
}
return(merges[nrow(hclust$merge)])
}
building the hclust
data_heatmap <- read.delim("~/Desktop/proteomicjs/exampleHeatmap/data_heatmap.tsv",
header = T)
fillmatrix <- function(df) {
m <- matrix(nrow = max(df$row_idx), ncol = max(df$col_idx))
for (i in 1:max(df$row_idx)) {
for (j in 1:max(df$col_idx)) {
m[i, j] <- df[which(df$row_idx == i & df$col_idx == j), 3]
}
}
m
}
resultM <- fillmatrix(data_heatmap)
# extracting the rowname json of js
rowLabel = c("1759080_s_at", "1759302_s_at", "1759502_s_at", "1759540_s_at",
"1759781_s_at", "1759828_s_at", "1759829_s_at", "1759906_s_at", "1760088_s_at",
"1760164_s_at", "1760453_s_at", "1760516_s_at", "1760594_s_at", "1760894_s_at",
"1760951_s_at", "1761030_s_at", "1761128_at", "1761145_s_at", "1761160_s_at",
"1761189_s_at", "1761222_s_at", "1761245_s_at", "1761277_s_at", "1761434_s_at",
"1761553_s_at", "1761620_s_at", "1761873_s_at", "1761884_s_at", "1761944_s_at",
"1762105_s_at", "1762118_s_at", "1762151_s_at", "1762388_s_at", "1762401_s_at",
"1762633_s_at", "1762701_s_at", "1762787_s_at", "1762819_s_at", "1762880_s_at",
"1762945_s_at", "1762983_s_at", "1763132_s_at", "1763138_s_at", "1763146_s_at",
"1763198_s_at", "1763383_at", "1763410_s_at", "1763426_s_at", "1763490_s_at",
"1763491_s_at")
colLabel = c("con1027", "con1028", "con1029", "con103", "con1030", "con1031",
"con1032", "con1033", "con1034", "con1035", "con1036", "con1037", "con1038",
"con1039", "con1040", "con1041", "con108", "con109", "con110", "con111",
"con112", "con125", "con126", "con127", "con128", "con129", "con130", "con131",
"con132", "con133", "con134", "con135", "con136", "con137", "con138", "con139",
"con14", "con15", "con150", "con151", "con152", "con153", "con16", "con17",
"con174", "con184", "con185", "con186", "con187", "con188", "con189", "con191",
"con192", "con193", "con194", "con199", "con2", "con200", "con201", "con21")
rownames(resultM) <- rowLabel
colnames(resultM) <- colLabel
plot(hclust(dist(resultM)))
library("jsonlite")
clustResult <- hclust(dist(resultM))
halfway <- hclustToTree(clustResult)
jsonTree <- toJSON(halfway)