-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
327 lines (286 loc) · 13.2 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
library(shiny)
library(tidyverse)
library(DT)
library(shinyWidgets)
options(dplyr.summarise.inform = FALSE)
options(digits=2)
# Get ASE data into shiny app
aseData <- readRDS("/projects/glchang_prj/finalPOGdata/allASEdata_anonymize.RDS") %>%
dplyr::mutate(methylation = case_when(
methyl_state < 0 ~ "allele2Methyl",
TRUE ~ "allele1Methyl"
)) %>%
dplyr::mutate(expression = case_when(
allele1IsMajor ~ "alelle1Expression",
TRUE ~ "alelle2Expression"
)) %>%
dplyr::mutate(aseResult = case_when(
majorAlleleFrequency < 0.65 ~ "BAE",
majorAlleleFrequency >= 0.65 ~ "ASE"
)) %>%
dplyr::mutate(majorAlleleFrequency = format(round(majorAlleleFrequency, 3), nsmall = 3)) %>%
dplyr::mutate(padj = format(round(padj, 3), nsmall = 3))
pogSample <- c("All", unique(aseData$sample))
defaultColumn <- c("gene", "majorAlleleFrequency",
"padj", "aseResults", "sample")
column <- setdiff(colnames(aseData), c(defaultColumn, "methylation", "expression", "aseResult"))
geneOfInterest <- readRDS("src/geneOfInterest.RDS")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
div(
style={'padding: 15px'},
img(src = "impala_logo.svg", align = "right", width = 150, height = 150),
titlePanel("ASE data for Long POG cohort"),
h5("This shiny app is used to explore Allele Specific Expression (ASE) data
from the Long POG cohort. Using Oxford Nanopore long reads for genome phasing
and Illumina RNA-seq data, the ", a("IMPALA pipeline", href="https://github.com/bcgsc/IMPALA/tree/master"),
" was used to call ASE data in 174 cancer samples. Genes are annotated with
allelic CNV data, allelic methylation and nonsense mutation to explain the mechanism of ASE"),
hr(),
br(),
fluidRow(
sidebarPanel(
width = 3,
# Show number of ASE genes
h4(textOutput("aseNum")),
hr(),
# Select Column
pickerInput(
"columnSelect",
"Select columns to show",
choices = column,
multiple = TRUE
),
# Filter for sample
selectInput(
"sample",
label = "POG sample",
choices = pogSample
),
# Filter for gene
textInput(
"geneText",
label = "Search for gene",
value = ""
),
# Filter for major allele frequency
sliderInput("mafSlider",
"Major Allele Frequency",
min = 0.5,
max = 1.0,
value = c(0.5, 1.0)),
# Filter for allele
radioButtons("allele1majorRadio",
"Filter major expressing haplotype",
choices = c("Both",
"Haplotype 1",
"Haplotype 2"),
selected = "Both"),
# Filter for gene type
pickerInput("GeneOfInterest",
"Special types of genes",
choices = list("Chromosome X genes" = "chrX",
"Imprinting genes" = "imprinting",
"Olfactory genes" = "olfactory",
"HLA genes" = "HLA",
"Tumor Suppressors" = "TumorSuppressor",
"Oncogenes" = "Oncogene"),
multiple = T),
# Filter for CNV
checkboxGroupInput("cnvCheckList",
"Allelic CNV states",
choices = list("balance" = "balance",
"imbalance" = "imbalance",
"LOH" = "LOH",
"No CNV data" = "NA"),
selected = c("balance",
"imbalance",
"LOH",
"NA")),
# Filter for Methylation
checkboxGroupInput("allelicMethyl",
"Allelic Methylation",
choices = list("Haplotype 1",
"Haplotype 2",
"No allelic methylation"),
selected = c("Haplotype 1",
"Haplotype 2",
"No allelic methylation")),
# Filter for nonsense mutation
checkboxGroupInput("nonsenseChecklist",
"Nonsense mutation",
choices = list("Haplotype 1" = 1,
"Haplotype 2" = 2,
"No nonsense mutation" = "NA"),
selected = c(1,2,"NA")),
# Filter for nonsense mutation
checkboxGroupInput("somaticChecklist",
"Somatic Mutation",
choices = list("SNV",
"Indel")),
# Filter for significance
checkboxGroupInput("significance",
"Filter for significant genes",
choices = list("Only Significant")),
hr(),
downloadButton('downloadData', 'Download current table as csv')
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table",
h4("Long POG data"),
dataTableOutput("impala")),
tabPanel("Figures",
h4("Summary Figures"),
br(),
fluidRow(
column(6, plotOutput("density")),
column(6, plotOutput("general"))
),
fluidRow(
column(6, plotOutput("cnvBar")),
column(6, plotOutput("methyl"))
))
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
methylFilter <- function(table, methyl){
result <- tibble()
if ("Haplotype 1" %in% methyl){
result <- table %>%
dplyr::filter(methyl_state >= 0) %>%
bind_rows(result)
}
if ("Haplotype 2" %in% methyl){
result <- table %>%
dplyr::filter(methyl_state < 0) %>%
bind_rows(result)
}
if ("No allelic methylation" %in% methyl){
result <- table %>%
dplyr::filter(is.na(methyl_state)) %>%
bind_rows(result)
}
return(result)
}
table <- reactive({
cnvCheckList <- replace(input$cnvCheckList, input$cnvCheckList=="NA", NA)
tbl <- aseData %>%
dplyr::filter(cnv_state %in% cnvCheckList) %>%
dplyr::filter(grepl(input$geneText,gene) ) %>%
dplyr::filter(majorAlleleFrequency >= input$mafSlider[1]) %>%
dplyr::filter(majorAlleleFrequency <= input$mafSlider[2]) %>%
dplyr::filter(stop_variant_allele %in% suppressWarnings(
as.numeric(input$nonsenseChecklist)))
# Filter for Significance
if (!is.null(input$significance)) {
tbl <- tbl %>%
dplyr::filter(padj <= 0.05)
}
# Filter for sample
if (input$sample != "All") {
tbl <- tbl %>%
dplyr::filter(sample == input$sample)
}
# Filter for major expressing allele
if (input$allele1majorRadio == "Haplotype 1") {
tbl <- tbl %>%
dplyr::filter(allele1IsMajor)
} else if (input$allele1majorRadio == "Haplotype 2") {
tbl <- tbl %>%
dplyr::filter(!allele1IsMajor)
}
if (length(input$GeneOfInterest) != 0 ){
specialGenes <- c()
for (type in input$GeneOfInterest){
specialGenes <- c(specialGenes, geneOfInterest[[type]])
}
tbl <- tbl %>%
dplyr::filter(gene %in% specialGenes)
}
# Filter based on allelic methylation
tbl <- methylFilter(tbl, input$allelicMethyl)
# Filter somatic mutation
if ("SNV" %in% input$somaticChecklist & "Indel" %in% input$somaticChecklist){
tbl <- tbl %>%
dplyr::filter(somaticSNV | somaticIndel)
} else if ("SNV" %in% input$somaticChecklist) {
tbl <- tbl %>%
dplyr::filter(somaticSNV)
} else if ("Indel" %in% input$somaticChecklist) {
tbl <- tbl %>%
dplyr::filter(somaticIndel)
}
tbl
})
output$impala <- DT::renderDataTable({
table() %>%
dplyr::select(c(defaultColumn, input$columnSelect))
})
output$aseNum <- renderText(paste0(table() %>%
dplyr::filter(aseResults == "ASE") %>%
nrow() %>%
format(nsmall=1, big.mark=","),
" ASE genes found"))
output$cnvBar <- renderPlot(table() %>%
dplyr::filter(padj < 0.05) %>%
dplyr::filter(!is.na(cnv_state)) %>%
ggplot(aes(aseResults)) +
geom_bar() +
facet_grid(~cnv_state) +
ggtitle("Number of ASE and BAE gene in each CNV state")
)
output$general <- renderPlot(table() %>%
group_by(sample, aseResult) %>%
summarize(n=n()) %>%
dplyr::mutate(prop = n/sum(n)) %>%
ggplot(aes(aseResult, prop)) +
geom_boxplot() +
ggtitle("Proportion of ASE and BAE gene in each sample",
subtitle = "Major Allele Frequency above 0.65 = ASE") +
xlab("aseResults")
)
output$density <- renderPlot(table() %>%
dplyr::mutate(majorAlleleFrequency = as.numeric(majorAlleleFrequency)) %>%
ggplot(aes(majorAlleleFrequency)) +
geom_density() +
geom_vline(xintercept = 0.65) +
ggtitle("Density of Major Allele Frequency")
)
output$methyl <- renderPlot(table() %>%
dplyr::filter(!is.na(methyl_state)) %>%
dplyr::select(methylation, expression) %>%
group_by(methylation, expression) %>%
summarize(n=n()) %>%
ggplot(mapping = aes(x = methylation, y = expression)) +
geom_tile(aes(fill = n), colour = "white") +
geom_text(aes(label = sprintf("%1.0f", n)), vjust = 1, colour = "white") +
ggtitle("Allelic Methylation and Expression Contigency Table") +
theme_bw() +
theme(legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
)
output$downloadData <- downloadHandler(
filename = function() {
paste('IMPALA-data-', Sys.Date(), '.csv', sep='')
},
content = function(file) {
write.csv(table(), file)
},
contentType = 'text/csv'
)
}
# Run the application
shinyApp(ui = ui, server = server)