@sjcrh/proteinpaint-server 2.38.0 → 2.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils/hclust.R CHANGED
@@ -12,37 +12,16 @@
12
12
  # since the docker images lacks the packages, but the auto-install was prevented due to container safety (no internet query)
13
13
  # this script will not run, leading to hard to decipher crashing
14
14
 
15
- #ggplot2_path <- system.file(package='ggplot2')
16
- #if (nchar(ggplot2_path) == 0) {
17
- # install.packages("ggplot2", repos='https://cran.case.edu/')
18
- #}
19
- #
20
- #jsonlite_path <- system.file(package='jsonlite')
21
- #if (nchar(jsonlite_path) == 0) {
22
- # install.packages("jsonlite", repos='https://cran.case.edu/')
23
- #}
24
- #
25
- #dendextend_path <- system.file(package='dendextend')
26
- #if (nchar(dendextend_path) == 0) {
27
- # install.packages("dendextend", repos='https://cran.case.edu/')
28
- #}
29
- #
30
- #reshape_path <- system.file(package='reshape')
31
- #if (nchar(reshape_path) == 0) {
32
- # install.packages("reshape", repos='https://cran.case.edu/')
33
- #}
15
+ # To plot the heatmap uncomment line `library(ggplot2) and lines after "Visualization" comment
16
+
34
17
 
35
- #flashClust_path <- system.file(package='flashClust')
36
- #if (nchar(flashClust_path) == 0) {
37
- # install.packages("flashClust", repos='https://cran.case.edu/')
38
- #}
39
18
 
40
19
  suppressPackageStartupMessages(library(dendextend))
41
20
  library(jsonlite)
42
21
  #library(flashClust)
43
22
  library(dendextend)
44
23
  library(reshape)
45
- library(ggplot2)
24
+ #library(ggplot2) # Uncomment this line to plot heatmap in R
46
25
 
47
26
  # Distance matrix
48
27
  args <- commandArgs(trailingOnly = T)
@@ -50,43 +29,27 @@ if (length(args) != 1) stop("Usage: Rscript test.R in.json > results")
50
29
  infile <- args[1]
51
30
  input <- fromJSON(infile)
52
31
 
53
- if (length(input$valueIsTransformed) == 0 || input$valueIsTransformed == FALSE) {
54
- normalized_matrix <- t(scale(t(input$matrix))) # Applying z-score normalization
55
- } else { # No normalization
56
- normalized_matrix <- input$matrix
57
- }
32
+ #if (length(input$valueIsTransformed) == 0 || input$valueIsTransformed == FALSE) {
33
+ # normalized_matrix <- t(scale(t(input$matrix))) # Applying z-score normalization
34
+ #} else { # No normalization
35
+ # normalized_matrix <- input$matrix
36
+ #}
58
37
 
59
- rownames(normalized_matrix) <- input$row_names
60
- colnames(normalized_matrix) <- input$col_names
61
- normalized_matrix <- na.omit(normalized_matrix) # Removes rows with NA values
38
+ #rownames(normalized_matrix) <- input$row_names
39
+ #colnames(normalized_matrix) <- input$col_names
40
+ #normalized_matrix <- na.omit(normalized_matrix) # Removes rows with NA values
62
41
 
63
42
  #print ("normalized_matrix")
64
43
  #print (dim(normalized_matrix))
65
44
 
66
45
  # For columns (i.e samples)
67
- RowDist <- dist(normalized_matrix, method = "euclidean") # Transposing the matrix
46
+ RowDist <- dist(input$matrix, method = "euclidean") # Transposing the matrix
68
47
 
69
48
 
70
49
  # Hierarchical clustering
71
50
  RowDend <- hclust(RowDist, method = tolower(input$cluster_method))
72
51
  #RowDend <- flashClust(RowDist, method = tolower(input$cluster_method))
73
- #print (RowDend$order)
74
- #print ("Merge")
75
- #print (RowDend$merge)
76
- #print ("Height")
77
- #print (RowDend$height)
78
- #print ("Labels")
79
- #print (RowDend$labels)
80
- #print ("Calls")
81
- #print (RowDend$call)
82
- #print ("Attributes")
83
- #attributes(RowDend)
84
- #print ("methods")
85
- #methods(class=class(RowDend))
86
52
  RowDendro <- as.dendrogram(RowDend)
87
- #print ("Attributes as.dendrogram")
88
- #attributes(RowDendro)
89
- #plot(RowDendro)
90
53
 
91
54
  row_node_coordinates <- get_nodes_xy(
92
55
  RowDendro,
@@ -96,18 +59,8 @@ row_node_coordinates <- get_nodes_xy(
96
59
  row_node_df <- as.data.frame(row_node_coordinates)
97
60
  colnames(row_node_df) <- c("x","y")
98
61
 
99
- #row_node_transform <- apply(row_node_coordinates, 1, function(row){
100
- # lapply(c(1,2), function(col_index){
101
- # if (col_index == 1) {
102
- # list(x=row[col_index])
103
- # } else if (col_index == 2) {
104
- # list(y=row[col_index])
105
- # }
106
- # })
107
- #})
108
-
109
62
  # For columns (i.e samples)
110
- ColumnDist <- dist(t(normalized_matrix), method = "euclidean") # Transposing the matrix
63
+ ColumnDist <- dist(t(input$matrix), method = "euclidean") # Transposing the matrix
111
64
 
112
65
  # Hierarchical clustering
113
66
 
@@ -127,9 +80,9 @@ colnames(col_node_df) <- c("x","y")
127
80
 
128
81
  # Sorting the matrix
129
82
 
130
- SortedMatrix <- normalized_matrix[RowDend$order, ColumnDend$order]
131
- SortedRowNames <- rownames(normalized_matrix)[RowDend$order]
132
- SortedColumnNames <- colnames(normalized_matrix)[ColumnDend$order]
83
+ #SortedMatrix <- normalized_matrix[RowDend$order, ColumnDend$order]
84
+ SortedRowNames <- input$row_names[RowDend$order]
85
+ SortedColumnNames <- input$col_names[ColumnDend$order]
133
86
 
134
87
  #m <- matrix(SortedMatrix,length(SortedRowNames),length(SortedColumnNames))
135
88
  #colnames(m) <- SortedColumnNames
@@ -139,9 +92,7 @@ output_df <- list()
139
92
  output_df$method <- input$cluster_method
140
93
  output_df$RowNodeJson <- row_node_df
141
94
  output_df$ColNodeJson <- col_node_df
142
- #output_df$RowDendOrder <- {lapply(1:length(RowDend$order), function(y){
143
- # list(i=RowDend$order[y])
144
- #})}
95
+
145
96
  row_dend_order_df <- as.data.frame(RowDend$order)
146
97
  colnames(row_dend_order_df) <- c("ind")
147
98
  output_df$RowDendOrder <- row_dend_order_df
@@ -150,10 +101,6 @@ col_dend_order_df <- as.data.frame(ColumnDend$order)
150
101
  colnames(col_dend_order_df) <- c("ind")
151
102
  output_df$ColumnDendOrder <- col_dend_order_df
152
103
 
153
- #output_df$SortedRowNames <- {lapply(1:length(SortedRowNames), function(y){
154
- # list(gene=SortedRowNames[y])
155
- #})}
156
-
157
104
  sorted_row_names_df <- as.data.frame(SortedRowNames)
158
105
  colnames(sorted_row_names_df) <- c("gene")
159
106
  output_df$SortedRowNames <- sorted_row_names_df
@@ -161,56 +108,18 @@ output_df$SortedRowNames <- sorted_row_names_df
161
108
  sorted_col_names_df <- as.data.frame(SortedColumnNames)
162
109
  colnames(sorted_col_names_df) <- c("sample")
163
110
  output_df$SortedColumnNames <- sorted_col_names_df
164
-
165
- output_df$OutputMatrix <- {lapply(1:length(normalized_matrix), function(y){
166
- list(elem=normalized_matrix[y])
167
- })}
168
-
169
- # Converting to data frame does not work for the raw counts since the key of the json needs to be unique.
170
-
171
- #output_matrix_df <- as.data.frame(normalized_matrix)
172
- #colnames(output_matrix_df) <- c(rep("elem",dim(normalized_matrix)[2]))
173
- #print (output_matrix_df)
174
- #output_df$OutputMatrix <- output_matrix_df
175
-
176
111
  toJSON(output_df)
177
112
 
178
- #cat("rowindexes",RowDend$order,"\n",sep="\t") # Prints out row indices
179
- #cat("colindexes",ColumnDend$order,"\n",sep="\t") # Prints out column indicies
180
- #cat("rownames",SortedRowNames,"\n",sep="\t") # Prints out row names
181
- #cat("colnames",SortedColumnNames,"\n",sep="\t") # Prints out column names
182
- #cat ("OutputMatrix",normalized_matrix,"\n",sep="\t") # This outputs the 2D array in 1D column-wise. This is later converted to 2D array in nodejs.
183
-
184
113
 
185
- #df <- melt(m)
186
- #colnames(df) <- c("Genes", "Samples", "value")
114
+ # Visualization of heatmap, uncomment code below to get ggplot2 image of heatmap
187
115
 
188
- #ggplot(df, aes(x = Genes, y = Samples, fill = value)) + geom_tile() + scale_fill_gradient(low="blue", high="red") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
116
+ #SortedMatrix <- input$matrix[RowDend$order, ColumnDend$order]
117
+ #SortedRowNames <- rownames(input$matrix)[RowDend$order]
118
+ #SortedColumnNames <- colnames(input$matrix)[ColumnDend$order]
119
+ #
120
+ #matrix_melt <- melt(SortedMatrix)
121
+ #ggplot(data = matrix_melt, aes(x = X1, y = X2, fill = value)) +
122
+ # geom_tile() + scale_fill_gradient(low="blue", high="red")
189
123
 
190
124
  #ggsave("heatmap.png")
191
125
 
192
-
193
- ##print (SortedMatrix)
194
- #df <- as.data.frame(SortedMatrix)
195
- #rownames(df) <- SortedRowNames
196
- #colnames(df) <- SortedColumnNames
197
- #print ("DataFrame")
198
- #print (df)
199
- ##plt <- ggplot(as.data.frame(SortedMatrix), aes(SortedColumnNames,SortedRowNames)) + geom_tile() + theme_minimal()
200
- #plt <- ggplot(df,aes(x=colname,y=rownames(df))) + geom_tile() + theme_minimal()
201
- #
202
- ## setting gradient color as red and white
203
- #plt <- plt + scale_fill_gradient(low="blue", high="red")
204
- #
205
- ## setting the title and subtitles using
206
- ## title and subtitle
207
- #plt <- plt + labs(title = "Heatmap")
208
- #plt <- plt + labs(subtitle = "A simple heatmap using geom_tile()")
209
- #
210
- ## setting x and y labels using labs
211
- #plt <- plt + labs(x ="Samples", y ="Genes")
212
- #
213
- ## plotting the Heatmap
214
- #plt
215
- #png("heatmap.png")
216
- #dev.off()