-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
379 lines (329 loc) · 12 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# A curator app to anntate and update figure number, title and caption
library(shiny)
library(shinyjs)
library(filesstrings)
library(tidyr)
library(dplyr)
library(magrittr)
## TRAINING
training <- TRUE
## Filename variables
todo.df <- "pfocr_figures_curating.rds"
done.df <- "pfocr_curated.rds"
if (training){
todo.df <- "pfocr_figures_training.rds"
done.df <- "pfocr_curated_training.rds"
}
## Read in PFOCR database
pfocr.df <- readRDS(todo.df)
pfocr.df.ori <- pfocr.df # stash complete df for preamble calc
## OPTIONALLY DEFINE A SUBSET ##
### filter by assigned range
# pfocr.df <- pfocr.df[1:100,] ## ASSIGNED RANGE
### filter by figids
# target.figids <- #read in file
# pfocr.df <- pfocr.df %>% dplyr::filter(figid %in% target.figids)
### filter by titles <10 and >150
# pfocr.df <- pfocr.df %>%
# dplyr::filter(nchar(figtitle) > 200)
### filter by labels
# pfocr.df <- pfocr.df %>%
# dplyr::filter(!is.na(plant)) #latin, plant
### filter by captions containing (A) and (B)
# pfocr.df <- pfocr.df %>% filter(grepl("^\\(A\\).*\\(B\\)",figtitle))
## END OPTIONAL SUBSETTING ##
fig.list <- unlist(unname(as.list(pfocr.df[,1])))
# set headers for output files
headers <- c("figid","pmcid","filename","number", "figtitle", "papertitle", "caption", "organism")
if(!file.exists(done.df)){
df <- data.frame(matrix(ncol=8,nrow=0))
names(df)<-headers
saveRDS(df, done.df)
}
getFigListTodo <- function(){
data <- readRDS(done.df)
fig.list.done <<-data[,1, drop=TRUE]
todo<-base::setdiff(fig.list, fig.list.done)
done<-base::intersect(fig.list, fig.list.done)
return(list(done,todo))
}
saveInput <- function(df){
df.old <- readRDS(done.df)
df <- df[,names(df.old)]
df.new <- rbind(df.old,df)
saveRDS(df.new, done.df)
}
undoSave <- function(){
data <- readRDS(done.df)
data <- head(data, -1)
saveRDS(data, done.df)
}
ungreekText <- function(input.text){
greek.text <- input.text
greek.text <- gsub("α-", "Alpha-", greek.text)
greek.text <- gsub("β-", "Beta-", greek.text)
greek.text <- gsub("γ-", "Gamma-", greek.text)
greek.text <- gsub("Ω-", "Omega-", greek.text)
greek.text <- gsub("ω-", "omega-", greek.text)
greek.text <- gsub("(-)?α", "A", greek.text)
greek.text <- gsub("(-)?β", "B", greek.text)
greek.text <- gsub("(-)?γ", "G", greek.text)
greek.text <- gsub("(-)?δ", "D", greek.text)
greek.text <- gsub("(-)?ε", "E", greek.text) #latin
greek.text <- gsub("(-)?ϵ", "E", greek.text )#greek
greek.text <- gsub("(-)?κ", "K", greek.text)
return(greek.text)
}
# SHINY UI
ui <- fluidPage(
titlePanel("PFOCR Curator"),
sidebarLayout(
sidebarPanel(
fluidPage(
useShinyjs(),
# Figure information
textOutput("fig.count"),
h5("Current figure"),
textOutput("fig.name"),
uiOutput("url"),
# textOutput("reftext"),
p(),
textInput("fig.num", "Figure number","NA"),
textInput("fig.org", "Organism","Homo sapiens"),
textAreaInput("fig.title", "Figure title", "NA", width = "100%",rows = 3, resize = "vertical" ),
textAreaInput("fig.caption", "Figure caption", "NA", width = "100%", rows = 6, resize = "vertical" ),
hr(),
# Buttons
actionButton("preamble", label = "Remove Preamble"),
actionButton("word", label = "Remove Word"),
actionButton("greek", label = "Un-Greek"),
actionButton("cap", label = "Capitalize"),
br(),
actionButton("papertitle", label = "Replace: Paper Title"),
actionButton("xxxpathway", label = "Replace: ... pathway"),
hr(),
# Buttons
tags$script(HTML("$(function(){
$(document).keyup(function(e) {
if (e.which == 39) {
$('#save').click()
}
});
})")),
actionButton("save", label = "Save"), #right arrow
tags$script(HTML("$(function(){
$(document).keyup(function(e) {
if (e.which == 38) {
$('#reload').click()
}
});
})")),
actionButton("reload", label = "Reload"), #up arrow
actionButton("reject", label = "Reject"),
tags$script(HTML("$(function(){
$(document).keyup(function(e) {
if (e.which == 40) {
$('#other').click()
}
});
})")),
actionButton("other", label = "Non-human"), #down arrow
tags$script(HTML("$(function(){
$(document).keyup(function(e) {
if (e.which == 37) {
$('#undo').click()
}
});
})")),
actionButton("undo", label = "Undo"), #left arrow
),
width = 6
),
mainPanel(
htmlOutput("figure"),
# imageOutput("figure"),
width = 6
)
)
)
# SHINY SERVER
server <- function(input, output, session) {
## FUNCTION: retrieve next figure
nextFigure <- function(){
# Display remaining count and select next figure to process
flt <- getFigListTodo()
fig.list.todo <- flt[[2]]
fig.done <- length(flt[[1]])
fig.cnt <- length(flt[[2]])
output$fig.count <- renderText({paste(fig.done,"curated /",fig.cnt,"remaining")})
if (fig.cnt == 0){
shinyjs::disable("save")
df<-data.frame(filename="No more files!")
output$fig.name <- renderText({as.character(df$filename)})
updateTextInput(session, "fig.org", value="")
updateTextInput(session, "fig.num", value="")
updateTextInput(session, "fig.title", value="")
updateTextInput(session, "fig.caption", value="")
display.url <- a("", href="")
output$url <- renderUI({display.url})
return(df)
}
# Get next fig info
df <- pfocr.df %>%
dplyr::filter(figid==fig.list.todo[1]) %>%
droplevels()
# output$reftext <- renderText({as.character(df$reftext)})
figname <- df$filename
pmcid <- df$pmcid
output$fig.name <- renderText({as.character(df$filename)})
## retrieve image from local
# output$figure <- renderImage({
# list(src = df$figid,
# alt = "No image available",
# width="600px")
# }, deleteFile = FALSE)
## retrieve image from online
linkout <- paste0("https://www.ncbi.nlm.nih.gov/",df$figlink)
figid.split <- strsplit(df$figid, "__")[[1]]
src <- paste0("https://www.ncbi.nlm.nih.gov/pmc/articles/",figid.split[1],"/bin/",figid.split[2])
output$figure <- renderText({
c('<a href="',linkout,'" target="_blank"><img src="',src,'", width="600px"></a>')})
updateTextInput(session, "fig.org", value="Homo sapiens")
updateTextInput(session, "fig.num", value=df$number)
updateTextInput(session, "fig.title", value=df$figtitle)
updateTextInput(session, "fig.caption", value=df$caption)
pmc.url <- paste0("https://www.ncbi.nlm.nih.gov/pmc/articles/",pmcid)
display.url <- a(df$papertitle, href=pmc.url)
output$url <- renderUI({display.url})
# Check for preamble
## update top 20 preambles so far
df.cur <- readRDS(done.df)
df.ori <- as.data.frame(pfocr.df.ori %>%
filter(figid %in% df.cur$figid))
df.diff <- merge(df.ori, df.cur, by="figid")
df.diff <- droplevels(df.diff)
sub_v <- Vectorize(sub, c("pattern", "x"))
df.diff <- df.diff %>%
mutate(diff = unname(sub_v(tolower(gsub("[\\[\\]]","",figtitle.y, perl=T)), "XXXXXX", tolower(gsub("[\\[\\]]","",figtitle.x, perl=T))))) %>%
tidyr::separate(diff, c("diff.pre","diff.suf"),"XXXXXX", remove = F, fill="right") %>%
mutate(diff.pre = ifelse(diff.pre == diff|diff.pre == "", NA, diff.pre))
pre.20 <- names(sort(table(df.diff$diff.pre),decreasing = T)[1:40])
if(!is.null(pre.20))
pre.20 <- pre.20[order(nchar(pre.20), pre.20, decreasing = T)]
## update title
cur.title <- as.character(df$figtitle)
new.title.list <- sapply(pre.20, function(x){
sub(paste0("^",x),"", cur.title, ignore.case = T)
})
new.title.list <- new.title.list[order(nchar(new.title.list), new.title.list, decreasing = F)]
new.title <- unname(new.title.list[1])
## Check for "of the"
pattern <- "^(.*\\s(of|by|between)\\s((the|which)\\s)?)"
if (grepl(pattern, cur.title)){
new.title2 <- gsub(pattern, "", cur.title)
if (nchar(new.title2) < nchar(new.title)) { #keep shortest
new.title <- new.title2
}
}
## Did we find anything?
if(is.na(cur.title)|cur.title == ""){
cur.title <- df$papertitle
updateTextInput(session, "fig.title", value=df$papertitle)
shinyjs::disable("preamble")
} else {
if (!nchar(new.title) < nchar(cur.title)){
df$new.title <- cur.title
shinyjs::disable("preamble")
} else {
## capitalize first characters
if(is.character(new.title)){
substr(new.title, 1, 1) <- toupper(substr(new.title, 1, 1))
new.title.cnt <- sapply(strsplit(new.title, " "), length)
if(new.title.cnt > 3){ #if more than 3 words remain, then apply it now!
cur.title<-new.title
updateTextInput(session, "fig.title", value=new.title)
shinyjs::disable("preamble")
} else { #store it as a button option
df$new.title <- new.title
shinyjs::enable("preamble")
}
}
}
}
## Remove last period and un-Greek, every time!
update.title <- ungreekText(cur.title)
update.title <- sub("\\.$", "", update.title)
updateTextInput(session, "fig.title", value=update.title)
# Check for "XXX pathway"
pattern <- "^.*?\\s*?([A-Za-z0-9_/-]+\\s([Ss]ignaling\\s)*pathway).*$"
if (grepl(pattern, cur.title)){
df$alt.title <- gsub(pattern, "\\1", cur.title)
substr(df$alt.title, 1, 1) <- toupper(substr(df$alt.title, 1, 1))
shinyjs::enable("xxxpathway")
} else {
df$alt.title <- df$figtitle
shinyjs::disable("xxxpathway")
}
return(df)
}
fig <- nextFigure()
## DEFINE SHARED VARS
rv <- reactiveValues(fig.df=fig)
## FUNCTION: override rv with input values
getInputValues <- function(df) {
df$fig.df$number <- input$fig.num
df$fig.df$organism <- input$fig.org
df$fig.df$figtitle <- input$fig.title
df$fig.df$caption <- input$fig.caption
return(df)
}
## BUTTON FUNCTIONALITY
observeEvent(input$save, {
rv <- getInputValues(rv)
saveInput(rv$fig.df)
rv$fig.df <- nextFigure()
})
observeEvent(input$reload, {
rv$fig.df <- nextFigure()
})
observeEvent(input$reject, {
updateTextInput(session, "fig.org", value="REJECT")
updateTextInput(session, "fig.num", value="REJECT")
updateTextInput(session, "fig.title", value="REJECT")
updateTextInput(session, "fig.caption", value="REJECT")
})
observeEvent(input$other, {
updateTextInput(session, "fig.org", value="XXX")
})
observeEvent(input$undo, {
undoSave()
rv$fig.df <- nextFigure()
})
observeEvent(input$preamble, {
updateTextInput(session, "fig.title", value=rv$fig.df$new.title)
})
observeEvent(input$papertitle, {
paper.title <- rv$fig.df$papertitle
paper.title <- sub("\\.$", "", paper.title)
updateTextInput(session, "fig.title", value=paper.title)
})
observeEvent(input$xxxpathway, {
updateTextInput(session, "fig.title", value=rv$fig.df$alt.title)
})
observeEvent(input$word, {
new.title <- input$fig.title
new.title <- gsub("^\\w+\\s","",new.title)
substr(new.title, 1, 1) <- toupper(substr(new.title, 1, 1))
updateTextInput(session, "fig.title", value=new.title)
})
observeEvent(input$cap, {
new.title <- input$fig.title
substr(new.title, 1, 1) <- toupper(substr(new.title, 1, 1))
updateTextInput(session, "fig.title", value=new.title)
})
observeEvent(input$greek, {
ungreek.title <- ungreekText(input$fig.title)
updateTextInput(session, "fig.title", value=ungreek.title)
})
}
shinyApp(ui = ui, server = server)