annotate heatmap.r @ 55:680cf68098d5 draft

Uploaded
author guerler
date Thu, 05 Jun 2014 14:23:41 -0400
parents 5a478e171752
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
1 # load sparse matrix package
eafc7121f553 Uploaded
guerler
parents:
diff changeset
2 suppressPackageStartupMessages(library('Matrix'))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
3
eafc7121f553 Uploaded
guerler
parents:
diff changeset
4 # access a numeric column
eafc7121f553 Uploaded
guerler
parents:
diff changeset
5 get_numeric <- function(table, column_key) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
6 column <- as.numeric(column_key)
53
5a478e171752 Uploaded
guerler
parents: 37
diff changeset
7 column_data <- suppressWarnings(as.numeric(as.character(table[column][[1]])))
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
8 return (c(column_data))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
9 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
10
eafc7121f553 Uploaded
guerler
parents:
diff changeset
11 # access a label column
eafc7121f553 Uploaded
guerler
parents:
diff changeset
12 get_label <- function(table, column_key) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
13 column <- as.numeric(column_key)
55
680cf68098d5 Uploaded
guerler
parents: 53
diff changeset
14 column_data <- as.character(table[column][[1]])
680cf68098d5 Uploaded
guerler
parents: 53
diff changeset
15 return (c(column_data))
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
16 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
17
eafc7121f553 Uploaded
guerler
parents:
diff changeset
18 # inflate three columns into matrix
eafc7121f553 Uploaded
guerler
parents:
diff changeset
19 matrify <- function (data) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
20 if (ncol(data) != 3)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
21 stop('Data frame must have three column format')
eafc7121f553 Uploaded
guerler
parents:
diff changeset
22 plt <- data[, 1]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
23 spc <- data[, 2]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
24 abu <- data[, 3]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
25 plt.codes <- levels(factor(plt))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
26 spc.codes <- levels(factor(spc))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
27 taxa <- Matrix(0, nrow=length(plt.codes), ncol=length(spc.codes), sparse=TRUE)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
28 row <- match(plt, plt.codes)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
29 col <- match(spc, spc.codes)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
30 for (i in 1:length(abu)) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
31 taxa[row[i], col[i]] <- abu[i]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
32 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
33 colnames(taxa) <- spc.codes
eafc7121f553 Uploaded
guerler
parents:
diff changeset
34 rownames(taxa) <- plt.codes
eafc7121f553 Uploaded
guerler
parents:
diff changeset
35 taxa
eafc7121f553 Uploaded
guerler
parents:
diff changeset
36 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
38 # flatten data.frame into three column format
eafc7121f553 Uploaded
guerler
parents:
diff changeset
39 flatten <- function(my_matrix) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
40 summ <-summary(my_matrix)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
41 summ <- data.frame(i=rownames(my_matrix)[summ$i], j=colnames(my_matrix)[summ$j], x=summ$x)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
42 summ
eafc7121f553 Uploaded
guerler
parents:
diff changeset
43 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
44
eafc7121f553 Uploaded
guerler
parents:
diff changeset
45 # wrapper
eafc7121f553 Uploaded
guerler
parents:
diff changeset
46 wrapper <- function(table, columns, options) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
47
eafc7121f553 Uploaded
guerler
parents:
diff changeset
48 # initialize output list
eafc7121f553 Uploaded
guerler
parents:
diff changeset
49 l <- list()
eafc7121f553 Uploaded
guerler
parents:
diff changeset
50
eafc7121f553 Uploaded
guerler
parents:
diff changeset
51 # get number of columns
eafc7121f553 Uploaded
guerler
parents:
diff changeset
52 n = length(columns)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
53
eafc7121f553 Uploaded
guerler
parents:
diff changeset
54 # consistency check
eafc7121f553 Uploaded
guerler
parents:
diff changeset
55 if (n %% 3 != 0) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
56 print ('heatmap::wrapper() - Data not consistent (n mod 3 != 0)')
eafc7121f553 Uploaded
guerler
parents:
diff changeset
57 return (l)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
58 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
59
eafc7121f553 Uploaded
guerler
parents:
diff changeset
60 # create index sequence
eafc7121f553 Uploaded
guerler
parents:
diff changeset
61 index = seq(1, n, by=3)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
62
eafc7121f553 Uploaded
guerler
parents:
diff changeset
63 # get keys
eafc7121f553 Uploaded
guerler
parents:
diff changeset
64 keys = names(columns)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
65
eafc7121f553 Uploaded
guerler
parents:
diff changeset
66 # loop through blocks
eafc7121f553 Uploaded
guerler
parents:
diff changeset
67 for (i in index) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
68 # create columns
eafc7121f553 Uploaded
guerler
parents:
diff changeset
69 ci <- get_label(table, columns[keys[i]])
eafc7121f553 Uploaded
guerler
parents:
diff changeset
70 cj <- get_label(table, columns[keys[i+1]])
eafc7121f553 Uploaded
guerler
parents:
diff changeset
71 cx <- get_numeric(table, columns[keys[i+2]])
eafc7121f553 Uploaded
guerler
parents:
diff changeset
72
eafc7121f553 Uploaded
guerler
parents:
diff changeset
73 # create a frame from columns
eafc7121f553 Uploaded
guerler
parents:
diff changeset
74 my_frame <- data.frame(ci=ci, cj=cj, cx=cx)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
75
eafc7121f553 Uploaded
guerler
parents:
diff changeset
76 # create matrix out of the frame
eafc7121f553 Uploaded
guerler
parents:
diff changeset
77 my_matrix <- matrify(my_frame)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
78
eafc7121f553 Uploaded
guerler
parents:
diff changeset
79 # create/cluster matrix
eafc7121f553 Uploaded
guerler
parents:
diff changeset
80 row_order <- hclust(dist(my_matrix))$order
eafc7121f553 Uploaded
guerler
parents:
diff changeset
81 col_order <- hclust(dist(t(my_matrix)))$order
eafc7121f553 Uploaded
guerler
parents:
diff changeset
82
eafc7121f553 Uploaded
guerler
parents:
diff changeset
83 # reorder matrix
eafc7121f553 Uploaded
guerler
parents:
diff changeset
84 my_matrix <- my_matrix[row_order, col_order]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
85
eafc7121f553 Uploaded
guerler
parents:
diff changeset
86 # transform back to three columns
53
5a478e171752 Uploaded
guerler
parents: 37
diff changeset
87 my_flatmatrix = flatten(my_matrix)
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
88
eafc7121f553 Uploaded
guerler
parents:
diff changeset
89 # append to result list
eafc7121f553 Uploaded
guerler
parents:
diff changeset
90 l <- append(l, list(my_flatmatrix$i))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
91 l <- append(l, list(my_flatmatrix$j))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
92 l <- append(l, list(my_flatmatrix$x))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
93 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
94
eafc7121f553 Uploaded
guerler
parents:
diff changeset
95 # return
eafc7121f553 Uploaded
guerler
parents:
diff changeset
96 return (l)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
97 }