-
Notifications
You must be signed in to change notification settings - Fork 2
/
heatmap_module.R
executable file
·131 lines (99 loc) · 4.57 KB
/
heatmap_module.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# UI function for the waterfall plot module
heatmapUI <- function(id, label = "Unsupervised clustering"){
library(DT)
library(shinyjs)
library(shinyWidgets)
ns <- NS(id) # Setting a unique namespace for this module
fluidPage(
sidebarLayout(
position = "left",
sidebarPanel(
helpText("Upload an Excel spreadsheet or CSV file with a single column of gene symbols.
Do not include a column title or header.
Do not inclue any rows/columns other than 1 single column of gene symbols.
The file MUST be an Excel or CSV."), # need a place to paste a gene list
helpText("Placeholder. Might want to describe difference btwn TPM and CPM"), # need a place to paste a gene list
br(),
fileInput(ns("gene_list"),
label = "Upload gene list here (.csv only)",
multiple = F,
accept = c(".csv"),
buttonLabel = "Upload"),
checkboxGroupInput(ns("sample_types"),
label = "Select ?",
choices = c("AML", "NBM", "CD34+ PB", "MPN", "DS-AML", "TMD"),
selected = c("AML", "NBM")),
radioButtons(ns("data_type"),
label = "Which form of expression data?",
choices = list("TPM" = "tpm",
"TMM-normalized CPM" = "cpm")),
checkboxInput(ns("log_transform"),
label = "Log2-transform?",
value = F),
),
mainPanel(position = "right",
tabsetPanel(
tabPanel("Figures",
br(),
fluidRow(
column(10, offset = 0, align = "left",
plotOutput(ns("plot"), width = "100%")
)
)
)
)
)
)
)
}
heatmap <- function(input, output, session, clinData, expData, gene, dataset) {
library(tidyverse)
library(DT)
library(shinyWidgets)
library(ComplexHeatmap)
#################################################################
#------------------------- FUNCTIONS ---------------------------#
#################################################################
readGeneList <- reactive({
validate(need(input$gene_list, "Please upload a list of genes to get started."))
# Checking to make sure the extension is correct
file <- input$gene_list
ext <- tools::file_ext(file$datapath)
req(file)
validate(
need(ext == "csv", "Please upload a .csv file.")
)
read.csv(file$datapath, header = F, blank.lines.skip = T, strip.white = T)[,1]
})
hmMatrix <- reactive({
gene_list <- readGeneList()
gene_list <- unique(gene_list)
# There's a bunch of cols of all NA at the far right of the dataframe, need a way to exclude these (must be miRNA-seq only samples)
mat <- expData()[intersect(gene_list, rownames(expData())),]
mat <- mat[,colSums(is.na(mat)) == 0] # Removing any cols with an NA
if (input$log_transform == TRUE) {
mat <- log2(mat + 1)
}
mat <- t(scale(t(mat), scale = T, center = T)) # Creating mean-centered, scaled z-scores
return(mat)
})
plotHeatmap <- reactive({
mat <- hmMatrix()
print("Length of gene list is...")
print(nrow(mat))
# Only displays row labels (aka gene names) if there are a small number of them
show_geneLabs <- ifelse(ncol(mat) < 100, TRUE, FALSE)
show_patIDs <- ifelse(nrow(mat) < 50, TRUE, FALSE)
Heatmap(mat,
show_column_names = show_geneLabs,
show_row_names = show_patIDs,
clustering_method_columns = "ward.D2",
clustering_method_rows = "ward.D2")
})
#################################################################
#-------------------- FINAL MODULE OUTPUTS ---------------------#
#################################################################
output$plot <- renderPlot({
plotHeatmap()
})
}