-
Notifications
You must be signed in to change notification settings - Fork 0
/
runShinyViewer_test.R
286 lines (257 loc) · 13.9 KB
/
runShinyViewer_test.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
## Code for the User Interface through Shiny
library(shiny)
library(crispRdesignR)
library(seqinr)
installed_genomes <- BSgenome::installed.genomes()
installed_genomes_names <- c()
if (length(installed_genomes) == 0) {
installed_genomes[length(installed_genomes)+1] <- "no_genomes_installed"
installed_genomes_names[length(installed_genomes_names)+1] <- "No genomes installed"
names(installed_genomes) <- installed_genomes_names
} else {
for (i in 1:length(installed_genomes)) {
genome_name <- paste(BSgenome::organism(BSgenome::getBSgenome(installed_genomes[i])), " (", metadata(get(installed_genomes[i]))$genome, ")", sep="")
installed_genomes_names[length(installed_genomes_names)+1] <- genome_name
}
names(installed_genomes) <- installed_genomes_names
}
ui <- fluidPage(
navbarPage("crispRdesignR",
tabPanel("sgRNA Viewer",
titlePanel("sgRNA Viewer"),
sidebarLayout(
sidebarPanel(
tags$div(id = "placeholder1"),
selectizeInput("genome_select", "Select Genome",
installed_genomes),
fileInput("sgRNAtable_file", "Table of sgRNA Table (*.csv)",
multiple = FALSE),
fileInput("offtable_file", "Table of Off-target Table (*.csv)",
multiple = FALSE),
tags$div(id = "placeholder5"),
actionButton("run", "Find sgRNA", icon("paper-plane"))
),
mainPanel(
tags$div(id = "placeholder3"),
DT::dataTableOutput("sgRNA_data"),
tags$div(id = "placeholder4"),
DT::dataTableOutput("offtarget_data"),
titlePanel("About"),
column(12, HTML("crispRdesignR designs guide RNA sequences (sgRNA) for Cas9 DNA editing.
To begin, enter a sequence into the sequence box, select a genome to search for
Off-Targets, provide a genome annotation file (.gtf) specific to your genome, and click find sgRNA. <br/><br/> Note about Off-target calling in large genomes: When using a large genome like
Homo sapiens, we reccomend using sequences under 250 base pairs. The time it can take
to search these genomes can be multiple hours if too many sgRNA are generated."))
)
)
)
)
)
server <- function(input, output) {
## Creates a list of reactive values that allows the program to
## update only when the action button is pressed
maindf <- reactiveValues(data = NULL)
downloadmaindf <- reactiveValues(data = NULL)
offtargetdf <- reactiveValues(data = NULL)
downloadofftargetdf <- reactiveValues(data = NULL)
## Creates default values for the arguments in the find sgRNA function
callofftargets <- "yes_off"
annotateofftargets <- "yes_annotate"
givenPAM <- "NGG"
## Creates a variable that assists with adding UI elements
n <- reactiveVal(0)
## Creates a variable for the gene annotation file
gtf_datapath <- reactiveVal(0)
gene_annotation_file <- reactiveVal(0)
## Runs the sgRNA_design function when the action button is pressed
observeEvent(input$run, {
callofftargets <- "yes_off"
annotateofftargets <- "yes_annotate"
# Create a Progress object
designprogress <- shiny::Progress$new()
designprogress$set(message = "Preparing gene annotation file", value = 0, detail = "This may take a while")
# Close the progress when this reactive exits (even if there's an error)
on.exit(designprogress$close())
if (callofftargets == "no_off" | annotateofftargets == "no_annotate") {
annotating <- FALSE
} else {
annotating <- TRUE
}
if (annotating == FALSE) {
gene_annotation_file("placeholder")
}
if (length(input$sgRNAtable_file) == 0) {
showModal(modalDialog(title = "Error","Table files are missing"))
return()
}
if (length(input$offtable_file) == 0) {
showModal(modalDialog(title = "Error","Table files are missing"))
return()
}
## Starts creating the sgRNA table
int_sgRNA_data = read.csv(file=input$sgRNAtable_file$datapath, header=TRUE, row.names=1)
## Adds color to indicate unfavorable GC content
GCinstance <- unlist(int_sgRNA_data[6])*100
GCindex <- which(GCinstance >=80 | GCinstance <=30)
GCinstance_color <- as.character(GCinstance)
for (G in 1:length(GCinstance)){
if (G %in% GCindex){
GCinstance_color[G] <- paste('<span style="color:red">', GCinstance_color[G], '<span>', sep = "")
}
}
## Adds color to indicate unfavorable homopolymers
Homopolymerdetect <- unlist(int_sgRNA_data[7])
Homopolymerdetect_color <- as.character(Homopolymerdetect)
for (H in 1:length(Homopolymerdetect)){
if (Homopolymerdetect[H] == "TRUE"){
Homopolymerdetect_color[H] <- paste('<span style="color:red">', Homopolymerdetect[H], '<span>', sep = "")
}
}
## Adds color to indicate Self-Complementarity
Self_comp_list <- unlist(int_sgRNA_data[8])
Self_comp_index <- which(Self_comp_list > 0)
Self_comp_list_color <- as.character(Self_comp_list)
for (C in 1:length(Self_comp_list)){
if (C %in% Self_comp_index){
Self_comp_list_color[C] <- paste('<span style="color:red">', Self_comp_list_color[C], '<span>', sep = "")
}
}
proc_sgRNA_data <- data.frame(int_sgRNA_data[1:5], GCinstance_color, Homopolymerdetect_color, Self_comp_list_color, int_sgRNA_data[9:15])
colnames(int_sgRNA_data) <- c("sgRNA sequence", "PAM", "Strand", "Start", "End", "GC content",
"Homopolymer", "Self Complementary", "Efficiency Score", "MM0", "MM1", "MM2", "MM3", "MM4", "Note Codes")
colnames(proc_sgRNA_data) <- c("sgRNA sequence", "PAM", "Strand", "Start", "End", "GC %",
"Homopolymer", "Self Complementary", "Efficiency Score", "MM0", "MM1", "MM2", "MM3", "MM4", "Note Codes")
## Adds a title and download button for the sgRNA table to the UI
n(n()+1)
if (n() == 1) {
title = paste("sgRNA Table", sep="")
insertUI(
selector = "#placeholder3",
where = "afterEnd",
ui = tags$div(id = 'sgRNAdftext',
titlePanel(title),
column(12, "Note Codes: GC - Unfavorable GC content (=< 80% or => 30%), HP - Homopolymer detected (4 or more consectutive base pairs),
SC - Region of self complementarity detected, LE - Low efficiency score (< 0.5)"),
downloadButton("Download_sgRNA", "Download sgRNA")
)
)
# }
## Outputs the Table
maindf$sgRNA_data <- proc_sgRNA_data
downloadmaindf$sgRNA_data <- int_sgRNA_data
int_offtarget_data = read.csv(file=input$offtable_file$datapath, header=TRUE, row.names=1)
## Adds code to color mismatches red within the off target sequences
off_offseq <- as.character(unlist(int_offtarget_data[8]))
off_sgRNAseq <- as.character(unlist(int_offtarget_data[1]))
for (x in 1:length(off_offseq)) {
justsgRNA <- off_sgRNAseq[x]
justoff <- off_offseq[x]
splitjustsgRNA <- stringr::str_split(justsgRNA, "", simplify = TRUE)
splitoffsgRNA <- stringr::str_split(justoff, "", simplify = TRUE)
mismatches <- which(splitjustsgRNA != splitoffsgRNA)
splitlistoffsgRNA <- as.list(splitoffsgRNA)
if (length(mismatches) != 0){
for (g in length(mismatches):1) {
splitlistoffsgRNA <- append(splitlistoffsgRNA, '</span>', after = mismatches[g])
splitlistoffsgRNA <- append(splitlistoffsgRNA, '<span style="color:red">', after = mismatches[g]-1)
}
off_offseq[x] <- paste(splitlistoffsgRNA, sep="", collapse = "")
}
}
proc_offtarget_data <- data.frame(int_offtarget_data[1:7], off_offseq, int_offtarget_data[9:12])
####added 5/17/23 SS - Creates link to JBrowse (currently hardcoded) for the Off-targets
library(stringr)
library(kableExtra)
url=paste("http://daphnia-db.crc.nd.edu/jbrowse2/?loc=",int_offtarget_data$Chromosome,'&assembly=Dmagna_LRV0_1_genome&sessionTracks=[{%22type%22:%22FeatureTrack%22,%22trackId%22:%22CrisprTargets%22,%22name%22:%22CrisprTargets%22,%22assemblyNames%22:[%22Dmagna_LRV0_1_genome%22],%22adapter%22:{%22type%22:%22FromConfigAdapter%22,%22features%22:[{%22uniqueId%22:%22primer%22,%22refName%22:%22', int_offtarget_data$Chromosome,"%22,%22start%22:", int_offtarget_data$Start,",%22end%22:", int_offtarget_data$End, "}]}}]&tracks=CrisprTargets,Dmagna_LRV0_1_genome.gff", sep="")
#url=paste("http://daphnia-db.crc.nd.edu/jbrowse2/?loc=",hits$all_offtarget_info.Chromosome,'&assembly=LRV0_1_genome&sessionTracks=[{%22type%22:%22FeatureTrack%22,%22trackId%22:%22CrisprTargets%22,%22name%22:%22CrisprTargets%22,%22assemblyNames%22:[%22LRV0_1_genome%22],%22adapter%22:{%22type%22:%22FromConfigAdapter%22,%22features%22:[{%22uniqueId%22:%22primer%22,%22refName%22:%22', hits$all_offtarget_info.Chromosome,"%22,%22start%22:", hits$all_offtarget_info.Start,",%22end%22:", hits$all_offtarget_info.End, "}]}}]&tracks=CrisprTargets,LRV0_1_genes.sorted.gff", sep="")
url = str_replace_all(url, "chr","scaffold_")
url = text_spec("Jbrowse", link = url)
proc_offtarget_data = cbind(proc_offtarget_data,url)
colnames(int_offtarget_data) <- c("sgRNA sequence", "Chromosome", "Start", "End", "Mismatches", "Strand", "CFD Scores",
"Off-target sequence", "Gene ID", "Gene Name", "Sequence Type", "Exon Number")
#####added link to below
colnames(proc_offtarget_data) <- c("sgRNA sequence", "Chr", "Start", "End", "Mismatches", "Strand", "CFD Scores",
"Off-target sequence", "Gene ID", "Gene Name", "Sequence Type", "Exon Number", "JBlink")
## Adds a title and download button for the off-target table to the UI
if (n() == 1) {
insertUI(
selector = "#placeholder4",
where = "afterEnd",
ui = tags$div(id = 'sgRNAofftext',
titlePanel("Off-target Information"),
column(12, "Note: this program may report sequences in the target region as potential off-target sequences"),
downloadButton("Download_off", "Download Off-Targets")
)
)
}
offtargetdf$data <- proc_offtarget_data
downloadofftargetdf$data <- int_offtarget_data
} else {
showModal(modalDialog(
title = "Error",
"No sgRNA were generated from sequence"
))
}
}
)
output$Download_sgRNA <- downloadHandler(
filename = function(){"sgRNA.csv"},
content = function(file) {
write.csv(downloadmaindf$sgRNA_data, file, row.names = TRUE)
}
)
output$Download_off <- downloadHandler(
filename = function(){"Offtarget.csv"},
content = function(file) {
write.csv(downloadofftargetdf$data, file, row.names = TRUE)
}
)
## Reactively outputs an sgRNA table when the function is complete
output$sgRNA_data <- DT::renderDataTable({maindf$sgRNA_data}, escape = FALSE)
output$offtarget_data <- DT::renderDataTable({offtargetdf$data}, escape = FALSE)
## Add fasta file input to the UI
observeEvent(input$fasta, {
if (input$fasta == TRUE) {
insertUI(
selector = "#placeholder1",
where = "afterEnd",
ui = tags$div(id = 'fastainput',
fileInput("fastafile", "Choose fasta file",
multiple = FALSE)
)
)
} else {
removeUI(
selector = 'div#fastainput',
multiple = FALSE
)
}
})
## Add Additional Options input to the UI
observeEvent(input$options_toggle, {
if (input$options_toggle == TRUE) {
insertUI(
selector = "#placeholder5",
where = "afterEnd",
ui = tags$div(id = 'optionsmenu',
column(12, HTML("Warning: Doench score not accurate for custom PAMS")),
textInput("customPAM", "Custom PAM (Max 6bp)", value = "NGG"),
selectInput("toggle_off_targets", "Call Off-Targets?",
c("Yes" = "yes_off",
"No" = "no_off"),
selected = "yes_off"),
selectInput("toggle_off_annotation", "Annotate Off-Targets?",
c("No" = "no_annotate",
"Yes" = "yes_annotate"),
selected = "yes_annotate")
)
)
} else {
removeUI(
selector = 'div#optionsmenu',
multiple = TRUE
)
}
})
}
shinyApp(ui=ui, server=server)