-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
156 lines (123 loc) · 6.34 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
library(Seurat)
library(cytokit)
library(shiny)
library(DT)
library(tidyverse)
load("data/seurat_genes.Rda")
# Define UI for cytoscope
ui <- fluidPage(
# Application title
titlePanel("My dataset"),
# Sidebar with input
sidebarLayout(
sidebarPanel(width = 3,
h3("Data"),
selectInput("sample", "Dataset", multiple = FALSE, selected = "ct_p3",
choices = list(
# ********************
# *** MODIFY THIS ****
# ********************
"Sample collection 1" = c("Sample 1 name" = "s1_id",
"Sample 2 name" = "s2_id"),
"Sample collection 2" = c("Sample 3 name" = "s3_id",
"Sample 4 name" = "s4_id",
"Sample 5 name" = "s5_id")
)),
selectInput("gene", "Genes (max 3)", choices = character(0), multiple = TRUE),
selectInput("dr", "Dimensionality reduction", multiple = FALSE, choices = c("tsne", "pca"), selected = "tsne"),
h3("Feature plot"),
selectInput("label", "Label clusters", multiple = FALSE,
choices = list("Long", "Short", "None")),
selectInput("palette", "Colour palette", choices = list("Grey-red" = "redgrey",
"Blues" = "blues",
"Viridis" = "viridis"), selected = "redgrey"),
selectInput("stat", "Colour cells by", selected = "mean",
choices = list("Mean expression" = "mean",
"Percentile bin of expression" = "percentiles")),
h3("Violin plot"),
numericInput("point_size", "Point size", value = 0.1, min = 0, max = 1, step = 0.1),
selectInput("sort", "Sort clusters by expression", choices = c(TRUE, FALSE), selected = FALSE)
),
# Output plots
mainPanel(tabsetPanel(
tabPanel("Visualize gene expression",
plotOutput("dr_plot", width = "5in", height = "5in"),
plotOutput("feature"),
downloadLink("download_feature", "Download PDF"),
plotOutput("vln"),
downloadLink("download_vln", "Download PDF")
),
tabPanel("Sample info",
tableOutput("ncell_table")),
tabPanel("Cluster markers",
p("Use the navigation tools to search, filter, and order the table of gene markers."),
DT::dataTableOutput("markers"))
)
)
)
)
# Define server logic for cytoscope
server <- function(input, output, session) {
observe({
sample <- input$sample
if (is.null(sample)) sample <- character(0)
updateSelectInput(session, "gene",
choices = genes[[sample]])
})
# Reactive expressions
gene <- reactive({head(input$gene, 3)})
seurat <- reactive({ get(load(paste0("data/seurat/", input$sample, ".seurat_small.Rda")))})
mk <- reactive({read.delim(paste0("data/markers/", input$sample, ".markers.tsv"),
stringsAsFactors = FALSE)})
# tSNE
output$dr_plot <- renderPlot({cytokit::plotDR(seurat(), reduction = input$dr,
colours = seurat()@misc$colours,
title = seurat()@project.name,
point_size = 1.1)})
# Number of cells
output$ncell_table <- renderTable({
n_cells <- as.data.frame(table(seurat()@ident))
colnames(n_cells) <- c("Cluster", "Number of cells")
return(n_cells)
})
# Markers
output$markers <- DT::renderDataTable({
mk() %>%
dplyr::select(cluster, external_gene_name, avg_logFC, p_val_adj, pct.1, pct.2, ensembl_gene_id, description) %>%
DT::datatable(filter = "top",
rownames = FALSE,
selection = "none") %>%
formatStyle("cluster", fontWeight = "bold") %>%
formatStyle("external_gene_name", fontWeight = "bold")
})
# Feature plot
feature_plot <- reactive({cytokit::feature(seurat(), genes = gene(),
palette = input$palette,
reduction = input$dr,
legend = TRUE,
point_size = 1.3,
ncol = length(gene()),
statistic = input$stat,
label = ifelse(input$label == "None", FALSE, TRUE),
label_short = ifelse(input$label == "Short", TRUE, FALSE))})
output$feature <- renderPlot({feature_plot()})
# Feature plot download
output$download_feature <- downloadHandler(filename = "feature.pdf",
content = function(file) {
ggsave(filename = file, plot = feature_plot(), width = 5*length(gene()), height = 4)
})
# Violin plot
vln_plot <- reactive({Seurat::VlnPlot(seurat(), features.plot = gene(),
do.sort = input$sort,
point.size.use = input$point_size,
nCol = length(gene()),
cols.use = seurat()@misc$colours, x.lab.rot = TRUE)})
output$vln <- renderPlot({vln_plot()})
# Violin plot download
output$download_vln <- downloadHandler(filename = "violin.pdf",
content = function(file) {
ggsave(filename = file, plot = vln_plot(), width = 7*length(gene()), height = 5)
})
}
# Run the application
shinyApp(ui = ui, server = server)