library(shiny)
|
|
library(rhandsontable)
|
|
library(openCyto)
|
|
library(flowCore)
|
|
library(flowWorkspace)
|
|
library(CytoML)
|
|
library(ggcyto)
|
|
library(reshape2)
|
|
library(Matrix)
|
|
library(CitFuns)
|
|
library(BDCIT)
|
|
library(tidyverse)
|
|
|
|
print(getwd())
|
|
source("../sqlFunctions.R", encoding = "UTF-8")
|
|
|
|
DF<-data.frame("NHC"="","Samples"="")
|
|
samples<-data.frame("UMID"="","UM"="")
|
|
CLINICS<-data.frame("UMID"="","UM"="")
|
|
cnag<-data.frame("UMID"="","UM"="")
|
|
rna<-data.frame("UMID"="","UM"="")
|
|
|
|
# Cargar dependencias
|
|
sqlInitialize(ruta="../ruta_database.R")
|
|
|
|
|
|
# UI ----
|
|
|
|
ui <- fluidPage(
|
|
|
|
# Application title
|
|
#titlePanel("BDAccess"),
|
|
|
|
#sidebarLayout(
|
|
#Navbar
|
|
navbarPage("BDAccess",
|
|
|
|
## Update ----
|
|
tabPanel("Update",
|
|
sidebarPanel(
|
|
selectInput("dbtype", "", selected="UM", choices=c("UM", "OV","CC")),
|
|
checkboxInput(inputId = "backup", label="Backup", value=F),
|
|
actionButton("butdbtype", "Carrega BD"),
|
|
hr(),
|
|
fileInput(inputId = "file_query", label = "Plantilla", multiple = F),
|
|
actionButton("impNHC", "Importa NHC"),
|
|
actionButton("goButton", "Genera PATID"),
|
|
hr(),
|
|
actionButton("filltemplate", "Plantilla"),
|
|
hr(),
|
|
actionButton("synctemplate", "Sincronizar")
|
|
),
|
|
|
|
mainPanel(
|
|
tabsetPanel(
|
|
tabPanel("NHC", rHandsontableOutput("hot")),
|
|
tabPanel("Samples",rHandsontableOutput("samples")),
|
|
tabPanel("CLINICS",rHandsontableOutput("CLINICS")),
|
|
tabPanel("CNAG",rHandsontableOutput("cnag")),
|
|
tabPanel("RNADNA",rHandsontableOutput("rna")),
|
|
)
|
|
)
|
|
),
|
|
|
|
## Visor ----
|
|
tabPanel("Visor",
|
|
sidebarPanel(
|
|
radioButtons("nhc", label = h3("Código"),
|
|
choices = list("NHC" = 1, "UMID/OVID" = 2, "UM/OV"=3),
|
|
selected = 2),
|
|
textInput("id", label = "ID", value = ""),
|
|
actionButton("nitrosync", "Actualiza Nitrógeno")
|
|
),
|
|
mainPanel(
|
|
htmlOutput("report"),
|
|
h3("Nitrogen"),
|
|
tableOutput("nitrogen"),
|
|
plotOutput("visorplot", height = "1000px")
|
|
)
|
|
),
|
|
|
|
## Citometría ----
|
|
tabPanel("Citometría",
|
|
sidebarPanel(
|
|
selectInput("phenotype", "Tipo de análisis", selected="Pop", choices=c("Pop", "IC")),
|
|
),
|
|
mainPanel(
|
|
tabsetPanel(
|
|
tabPanel("Entrada",
|
|
actionButton("goButtonDir","Selecciona directorio fenotipo"),
|
|
textOutput("session"),
|
|
hr(),
|
|
actionButton("fcsconvert", "Convertir a fcs"),
|
|
hr(),
|
|
actionButton("pngexport", "Exportar informes"),
|
|
actionButton("popexport", "Actualizar BBDD")
|
|
),
|
|
tabPanel("Visor",
|
|
|
|
)
|
|
)
|
|
)
|
|
),
|
|
|
|
## scRNAseq ----
|
|
tabPanel("scRNAseq",
|
|
sidebarPanel(
|
|
textInput("sqlquery", label = "sqlquery", value = ""),
|
|
uiOutput("PATID"),
|
|
checkboxInput("sct_sel", "Mostrar filtrados"),
|
|
checkboxInput("cd45_chk", "Purificación CD45"),
|
|
textInput("genes", label="genes", value = "")
|
|
),
|
|
mainPanel(
|
|
tabsetPanel(
|
|
tabPanel("Table", tableOutput("sc_table")),
|
|
tabPanel("Plots",
|
|
plotOutput("sc_plot", height = "1000px"),
|
|
plotOutput("sc_expr"), height = "600px")
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
|
|
# Define server logic required to draw a histogram
|
|
# Server ----
|
|
server <- function(input, output) {
|
|
|
|
## Update ----
|
|
|
|
values <- reactiveValues()
|
|
values[["DF"]]<-DF
|
|
values[["samples"]]<-samples
|
|
values[["CLINICS"]]<-CLINICS
|
|
values[["cnag"]]<-cnag
|
|
values[["rna"]]<-rna
|
|
values[["Excel"]]<-""
|
|
|
|
observeEvent(input$butdbtype, {
|
|
print(UMfile)
|
|
print(OVfile)
|
|
print(CCfile)
|
|
if (input$dbtype == "UM"){
|
|
dta<<-odbcConnectAccess2007(access.file = UMfile,
|
|
pwd = .rs.askForPassword("Enter password:"))
|
|
}
|
|
if (input$dbtype == "OV"){
|
|
dta<<-odbcConnectAccess2007(access.file = OVfile,
|
|
pwd = .rs.askForPassword("Enter password:"))
|
|
}
|
|
if (input$dbtype == "CC"){
|
|
dta<<-odbcConnectAccess2007(access.file = CCfile,
|
|
pwd = .rs.askForPassword("Enter password:"))
|
|
}
|
|
print(dta)
|
|
if (input$backup == T){
|
|
if (! input$dbtype %in% c("UM","OV")){
|
|
sqlBackUp(bu.dir="CC_BU")
|
|
}else{
|
|
sqlBackUp()
|
|
}
|
|
|
|
}
|
|
})
|
|
|
|
# observe({
|
|
# if (!is.null(input$file_access)){
|
|
# # Inicializar conexión
|
|
# dta<<-odbcConnectAccess2007(access.file = file,
|
|
# pwd = .rs.askForPassword("Enter password:"))
|
|
# print(dta)
|
|
# sqlBackUp()
|
|
# }
|
|
# })
|
|
|
|
observe({
|
|
if (!is.null(input$file_query)){
|
|
## Importamos los NHC de las muestras nuevas
|
|
values[["Excel"]]<-read.xlsx(input$file_query$datapath, sheet = "NHC") %>% pull(NHC) %>% as.character()
|
|
print(values[["Excel"]])
|
|
}
|
|
})
|
|
observeEvent(input$impNHC, {
|
|
values[["DF"]]<-merge(values[["DF"]], data.frame("NHC"=values[["Excel"]]), all.y=T)
|
|
})
|
|
|
|
## Handsontable
|
|
observe({
|
|
if (!is.null(input$hot)) {
|
|
DF = hot_to_r(input$hot)
|
|
} else {
|
|
if (is.null(values[["DF"]])){
|
|
DF <- DF
|
|
}
|
|
else{
|
|
DF <- values[["DF"]]
|
|
}
|
|
}
|
|
values[["DF"]] <- DF
|
|
})
|
|
|
|
output$hot <- renderRHandsontable({
|
|
if (!is.null(DF)){
|
|
rhandsontable(values[["DF"]], stretchH = "all", readOnly = F, useTypes = T)
|
|
}
|
|
})
|
|
|
|
observe({
|
|
if (!is.null(input$samples)) {
|
|
samples = hot_to_r(input$samples)
|
|
} else {
|
|
if (is.null(values[["samples"]])){
|
|
samples <- samples
|
|
}
|
|
else{
|
|
samples <- values[["samples"]]
|
|
}
|
|
}
|
|
values[["samples"]] <- samples
|
|
})
|
|
|
|
output$samples <- renderRHandsontable({
|
|
if (!is.null(samples)){
|
|
rhandsontable(values[["samples"]], stretchH = "all", readOnly = F, useTypes = T)
|
|
}
|
|
})
|
|
|
|
observe({
|
|
if (!is.null(input$CLINICS)) {
|
|
CLINICS = hot_to_r(input$CLINICS)
|
|
} else {
|
|
if (is.null(values[["CLINICS"]])){
|
|
CLINICS <- CLINICS
|
|
}
|
|
else{
|
|
CLINICS <- values[["CLINICS"]]
|
|
}
|
|
}
|
|
values[["CLINICS"]] <- CLINICS
|
|
})
|
|
|
|
output$CLINICS <- renderRHandsontable({
|
|
if (!is.null(CLINICS)){
|
|
rhandsontable(values[["CLINICS"]], stretchH = "all", readOnly = F, useTypes = T) %>%
|
|
hot_col(values[["CLINICS"]] %>% select(lubridate::is.Date) %>% colnames,
|
|
dateFormat = "DD/MM/YYYY", type = "date")
|
|
}
|
|
})
|
|
|
|
observe({
|
|
if (!is.null(input$cnag)) {
|
|
cnag = hot_to_r(input$cnag)
|
|
} else {
|
|
if (is.null(values[["cnag"]])){
|
|
cnag <- cnag
|
|
}
|
|
else{
|
|
cnag <- values[["cnag"]]
|
|
}
|
|
}
|
|
values[["cnag"]] <- cnag
|
|
})
|
|
|
|
output$cnag <- renderRHandsontable({
|
|
if (!is.null(cnag)){
|
|
rhandsontable(values[["cnag"]], stretchH = "all", readOnly = F, useTypes = T)
|
|
}
|
|
})
|
|
|
|
observe({
|
|
if (!is.null(input$rna)) {
|
|
rna = hot_to_r(input$rna)
|
|
} else {
|
|
if (is.null(values[["rna"]])){
|
|
rna <- rna
|
|
}
|
|
else{
|
|
rna <- values[["rna"]]
|
|
}
|
|
}
|
|
values[["rna"]] <- rna
|
|
})
|
|
|
|
output$rna <- renderRHandsontable({
|
|
if (!is.null(rna)){
|
|
rhandsontable(values[["rna"]], stretchH = "all", readOnly = F, useTypes = T)
|
|
}
|
|
})
|
|
|
|
observeEvent(input$goButton, {
|
|
|
|
if (input$dbtype %in% c("UM","OV")){
|
|
sqlGenOVID(dta, nhcs = values[["DF"]]$NHC, sinc=T)
|
|
}
|
|
if (input$dbtype %in% c("CC")){
|
|
sqlGenOVID(dta, nhcs = values[["DF"]]$NHC, sinc=T, dbtype = input$dbtype)
|
|
}
|
|
|
|
if (input$dbtype == "UM"){
|
|
values[["DF"]]<-merge(values[["DF"]], sqlFetch(dta, "UMID"))
|
|
}
|
|
if (input$dbtype == "OV"){
|
|
values[["DF"]]<-merge(values[["DF"]], sqlFetch(dta, "OVID"))
|
|
}
|
|
if (input$dbtype == "CC"){
|
|
values[["DF"]]<-merge(values[["DF"]], sqlFetch(dta, "PATID"))
|
|
}
|
|
print(values[["DF"]])
|
|
})
|
|
|
|
observeEvent(input$filltemplate,{
|
|
today=T
|
|
if (input$dbtype == "UM"){
|
|
upd.umid<-sqlFetch(dta, "UMID") %>% filter(NHC %in% values[["DF"]]$NHC)
|
|
## Generar código para las nuevas muestras
|
|
samples<-sqlFetch(dta, "MUESTRAS")
|
|
if(sum(grepl(paste0("UM",Sys.time() %>% format("%y")), samples$CODIGO)) > 0){
|
|
next.samp<-gsub(paste0("UM",Sys.time() %>% format("%y")),"", samples$CODIGO) %>% as.numeric %>% max(na.rm=T)+1
|
|
}else{
|
|
next.samp<-1
|
|
}
|
|
last.samp<-next.samp+(length(values[["DF"]]$NHC)-1)
|
|
new.samp<-sprintf("UM%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp)
|
|
new.samp.df<-data.frame("NHC"=values[["DF"]]$NHC, "CODIGO"=new.samp) %>% merge(sqlFetch(dta,"UMID"), all.x=T) %>% arrange(CODIGO)
|
|
samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(CODIGO)
|
|
if (today==TRUE){
|
|
samples.exp$FECHA_RECEPCION<-format(Sys.Date(), "%d/%m/%y")
|
|
}
|
|
|
|
nhc.table<-values[["DF"]]
|
|
|
|
if (any(sapply(nhc.table$Samples, function(x) "cnag" %in% strsplit(x,",")[[1]]) == T)){
|
|
nhcs.cnag<-nhc.table[sapply(nhc.table$Samples, function(x) "cnag" %in% strsplit(x,",")[[1]]),"NHC"]
|
|
umid.cnag<-sqlFetch(dta, "UMID") %>% filter(NHC %in% nhcs.cnag) %>% pull(UMID)
|
|
sample.cnag<-samples.exp %>% filter(UMID %in% umid.cnag) %>% pull(CODIGO)
|
|
cnag.exp<-merge(data.frame("UMID"=umid.cnag, "CODIGO"=sample.cnag), sqlFetch(dta, "CNAG") %>% slice(0), all=T)
|
|
if (today==TRUE){
|
|
cnag.exp$FECHA_ENVIO<-format(Sys.Date(), "%d/%m/%y")
|
|
}
|
|
}else{
|
|
cnag.exp<-sqlFetch(dta, "CNAG") %>% slice(0) %>%
|
|
mutate(across(lubridate::is.POSIXct, as.character))
|
|
}
|
|
if (any(sapply(nhc.table$Samples, function(x) "rna" %in% strsplit(x,",")[[1]]) == T)){
|
|
nhcs.rna<-nhc.table[sapply(nhc.table$Samples, function(x) "rna" %in% strsplit(x,",")[[1]]),"NHC"]
|
|
umid.rna<-sqlFetch(dta, "UMID") %>% filter(NHC %in% nhcs.rna) %>% pull(UMID)
|
|
sample.rna<-samples.exp %>% filter(UMID %in% umid.rna) %>% pull(CODIGO)
|
|
rna.exp<-merge(data.frame("UMID"=umid.rna, "CODIGO"=sample.rna), sqlFetch(dta, "RNADNA") %>% slice(0)%>%
|
|
mutate(across(lubridate::is.POSIXct, as.character)), all=T)
|
|
}else{
|
|
rna.exp<-sqlFetch(dta, "RNADNA") %>% slice(0) %>%
|
|
mutate(across(lubridate::is.POSIXct, as.character))
|
|
}
|
|
|
|
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
|
|
upd.clinics<-sqlFetch(dta, "CLINICOS")
|
|
umid.new<-sqlFetch(dta, "UMID") %>% filter(NHC %in% values[["DF"]]$NHC)
|
|
upd.clinics<-merge(umid.new,upd.clinics, all.x=T, by="UMID")
|
|
upd.clinics$NHC<-as.character(upd.clinics$NHC)
|
|
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])}
|
|
|
|
values[["samples"]]<-samples.exp
|
|
values[["CLINICS"]]<-upd.clinics
|
|
values[["cnag"]]<-cnag.exp
|
|
values[["rna"]]<-rna.exp
|
|
}
|
|
if (input$dbtype == "OV"){
|
|
upd.umid<-sqlFetch(dta, "OVID") %>% filter(NHC %in% values[["DF"]]$NHC)
|
|
## Generar código para las nuevas muestras
|
|
samples<-sqlFetch(dta, "SAMPLES")
|
|
if(sum(grepl(paste0("OV",Sys.time() %>% format("%y")), samples$samples)) > 0){
|
|
next.samp<-gsub(paste0("OV",Sys.time() %>% format("%y")),"", samples$samples) %>% as.numeric %>% max(na.rm=T)+1
|
|
}else{
|
|
next.samp<-1
|
|
}
|
|
last.samp<-next.samp+(length(values[["DF"]]$NHC)-1)
|
|
new.samp<-sprintf("OV%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp)
|
|
new.samp.df<-data.frame("NHC"=values[["DF"]]$NHC, "samples"=new.samp) %>% merge(sqlFetch(dta,"OVID"), all.x=T) %>% arrange(samples)
|
|
samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples)
|
|
if (today==TRUE){
|
|
samples.exp$IQ_date<-format(Sys.Date(), "%d/%m/%y")
|
|
}
|
|
|
|
nhc.table<-values[["DF"]]
|
|
|
|
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
|
|
upd.clinics<-sqlFetch(dta, "CLINICS")
|
|
umid.new<-sqlFetch(dta, "OVID") %>% filter(NHC %in% values[["DF"]]$NHC)
|
|
upd.clinics<-merge(umid.new,upd.clinics, all.x=T, by="OVID")
|
|
upd.clinics$NHC<-as.character(upd.clinics$NHC)
|
|
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])}
|
|
|
|
values[["samples"]]<-samples.exp %>% mutate(across(lubridate::is.POSIXct, as.character))
|
|
values[["CLINICS"]]<-upd.clinics %>% mutate(across(lubridate::is.POSIXct, as.character))
|
|
}
|
|
|
|
if (input$dbtype %in% c("CC")){
|
|
upd.umid<-sqlFetch(dta, "PATID") %>% filter(NHC %in% values[["DF"]]$NHC)
|
|
## Generar código para las nuevas muestras
|
|
samples<-sqlFetch(dta, "MUESTRAS")
|
|
if(sum(grepl(paste0(input$dbtype,Sys.time() %>% format("%y")), samples$CODIGO)) > 0){
|
|
next.samp<-gsub(paste0(input$dbtype,Sys.time() %>% format("%y")),"", samples$CODIGO) %>% as.numeric %>% max(na.rm=T)+1
|
|
}else{
|
|
next.samp<-1
|
|
}
|
|
last.samp<-next.samp+(length(values[["DF"]]$NHC)-1)
|
|
new.samp<-sprintf("%s%s%02d",input$dbtype,Sys.time() %>% format("%y"),next.samp:last.samp)
|
|
new.samp.df<-data.frame("NHC"=values[["DF"]]$NHC, "CODIGO"=new.samp) %>% merge(sqlFetch(dta,"PATID"), all.x=T) %>% arrange(CODIGO)
|
|
samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(CODIGO)
|
|
if (today==TRUE){
|
|
samples.exp$FECHA_RECEPCION<-format(Sys.Date(), "%d/%m/%y")
|
|
}
|
|
|
|
nhc.table<-values[["DF"]]
|
|
|
|
if (any(sapply(nhc.table$Samples, function(x) "cnag" %in% strsplit(x,",")[[1]]) == T)){
|
|
nhcs.cnag<-nhc.table[sapply(nhc.table$Samples, function(x) "cnag" %in% strsplit(x,",")[[1]]),"NHC"]
|
|
umid.cnag<-sqlFetch(dta, "PATID") %>% filter(NHC %in% nhcs.cnag) %>% pull(PATID)
|
|
sample.cnag<-samples.exp %>% filter(PATID %in% umid.cnag) %>% pull(CODIGO)
|
|
cnag.exp<-merge(data.frame("PATID"=umid.cnag, "CODIGO"=sample.cnag), sqlFetch(dta, "CNAG") %>% slice(0), all=T)
|
|
if (today==TRUE){
|
|
cnag.exp$FECHA_ENVIO<-format(Sys.Date(), "%d/%m/%y")
|
|
}
|
|
}else{
|
|
cnag.exp<-sqlFetch(dta, "CNAG") %>% slice(0) %>%
|
|
mutate(across(lubridate::is.POSIXct, as.character))
|
|
}
|
|
if (any(sapply(nhc.table$Samples, function(x) "rna" %in% strsplit(x,",")[[1]]) == T)){
|
|
nhcs.rna<-nhc.table[sapply(nhc.table$Samples, function(x) "rna" %in% strsplit(x,",")[[1]]),"NHC"]
|
|
umid.rna<-sqlFetch(dta, "PATID") %>% filter(NHC %in% nhcs.rna) %>% pull(PATID)
|
|
sample.rna<-samples.exp %>% filter(PATID %in% umid.rna) %>% pull(CODIGO)
|
|
rna.exp<-merge(data.frame("PATID"=umid.rna, "CODIGO"=sample.rna), sqlFetch(dta, "RNADNA") %>% slice(0)%>%
|
|
mutate(across(lubridate::is.POSIXct, as.character)), all=T)
|
|
}else{
|
|
rna.exp<-sqlFetch(dta, "RNADNA") %>% slice(0) %>%
|
|
mutate(across(lubridate::is.POSIXct, as.character))
|
|
}
|
|
|
|
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
|
|
upd.clinics<-sqlFetch(dta, "CLINICOS")
|
|
umid.new<-sqlFetch(dta, "PATID") %>% filter(NHC %in% values[["DF"]]$NHC)
|
|
upd.clinics<-merge(umid.new,upd.clinics, all.x=T, by="PATID")
|
|
upd.clinics$NHC<-as.character(upd.clinics$NHC)
|
|
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])}
|
|
|
|
values[["samples"]]<-samples.exp
|
|
values[["CLINICS"]]<-upd.clinics
|
|
values[["cnag"]]<-cnag.exp
|
|
values[["rna"]]<-rna.exp
|
|
}
|
|
|
|
})
|
|
|
|
observeEvent(input$synctemplate,{
|
|
if (input$dbtype == "UM"){
|
|
# conn=dta, filetemp="queryOV.xlsx", sinc.samples=F, sinc.clinics=F, dbtype=NULL
|
|
|
|
## Nuevas entradas en MUESTRAS
|
|
nsamples<-values[["samples"]] %>% nrow
|
|
upd.samples<-values[["samples"]]
|
|
if (nrow(upd.samples) > 0){rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character}
|
|
|
|
if (nrow(upd.samples) > 0){
|
|
upd.samples$FECHA_RECEPCION<-lubridate::parse_date_time(upd.samples$FECHA_RECEPCION, c("d/m/Y","d/m/y")) %>% as.Date()
|
|
upd.samples$TIPO<-as.character(upd.samples$TIPO)
|
|
upd.samples$OBS<-as.character(upd.samples$OBS)
|
|
### !! Atención, esto cambia la base de datos:
|
|
sqlSave(dta, upd.samples, tablename="MUESTRAS", append = T, varTypes = c("FECHA_RECEPCION"="Date"), rownames = F)
|
|
print("Tabla MUESTRAS sincronizada.")
|
|
}
|
|
|
|
## Entradas modificadas en CLINICOS
|
|
upd.clinics<-values[["CLINICS"]]
|
|
umid.mod<-upd.clinics$UMID[upd.clinics$UMID %in% (sqlFetch(dta, "CLINICOS") %>% pull(UMID))]
|
|
rnames<-sqlFetch(dta, "CLINICOS") %>% filter(UMID %in% umid.mod) %>% rownames
|
|
clinics.mod<-upd.clinics %>% filter(UMID %in% umid.mod) %>% select(-NHC)
|
|
rownames(clinics.mod)<-rnames
|
|
|
|
### !! Atención, esto cambia la base de datos:
|
|
fechas<-colnames(clinics.mod)[grepl("date|MET_DX|DoB", colnames(clinics.mod), ignore.case = T)]
|
|
for (i in fechas){
|
|
clinics.mod[,i]<-lubridate::parse_date_time(clinics.mod[,i], c("d/m/Y","d/m/y")) %>% as.Date()
|
|
}
|
|
sqlUpdate(dta, clinics.mod,"CLINICOS", index="UMID")
|
|
print("Tabla CLINICOS modificada.")
|
|
|
|
## Nuevas entradas en CLINICOS
|
|
nsamples.clin<-sqlFetch(dta, "CLINICOS") %>% nrow
|
|
umid.new<-upd.clinics$UMID[!upd.clinics$UMID %in% (sqlFetch(dta, "CLINICOS") %>% pull(UMID))]
|
|
clinics.new<-upd.clinics %>% filter(UMID %in% umid.new) %>% select(-NHC)
|
|
if (length(umid.new) > 0){rownames(clinics.new)<-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %>% as.character}
|
|
|
|
### !! Atención, esto cambia la base de datos:
|
|
fechas<-colnames(clinics.new)[grepl("date|MET_DX|DoB", colnames(clinics.new), ignore.case = T)]
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
for (i in fechas){
|
|
clinics.new[,i]<-lubridate::parse_date_time(clinics.new[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
sqlSave(dta, clinics.new, tablename="CLINICOS", append = T, varTypes = varTypes, rownames = F)
|
|
print("Tabla CLINICOS sincronizada.")
|
|
|
|
## Nuevas entradas en CNAG
|
|
if (nrow(values[["cnag"]]) > 0){
|
|
cnag.sync<-values[["cnag"]]
|
|
fechas<-colnames(cnag.sync)[sqlFetch(dta, "CNAG") %>% sapply(lubridate::is.POSIXct)]
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
print(fechas)
|
|
for (i in fechas){
|
|
cnag.sync[,i]<-lubridate::parse_date_time(cnag.sync[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
sqlSave(dta, cnag.sync, tablename="CNAG", append = T, varTypes = varTypes, rownames = F)
|
|
}
|
|
|
|
## Nuevas entradas en RNADNA
|
|
if (nrow(values[["rna"]]) > 0){
|
|
rna.sync<-values[["rna"]]
|
|
fechas<-colnames(rna.sync)[sqlFetch(dta, "RNADNA") %>% sapply(lubridate::is.POSIXct)]
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
for (i in fechas){
|
|
rna.sync[,i]<-lubridate::parse_date_time(rna.sync[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
sqlSave(dta, rna.sync, tablename="RNADNA", append = T, varTypes = varTypes, rownames = F)
|
|
}
|
|
}
|
|
if (input$dbtype == "OV"){
|
|
## Nuevas entradas en SAMPLES
|
|
nsamples<-values[["samples"]] %>% nrow
|
|
upd.samples<-values[["samples"]]
|
|
if (nrow(upd.samples) > 0){rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character}
|
|
|
|
if (nrow(upd.samples) > 0){
|
|
fechas<-colnames(upd.samples)[sqlFetch(dta, "SAMPLES") %>% sapply(lubridate::is.POSIXct)]
|
|
for (i in fechas){
|
|
upd.samples[,i]<-lubridate::parse_date_time(upd.samples[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
# upd.samples$TIPO<-as.character(upd.samples$TIPO)
|
|
# upd.samples$OBS<-as.character(upd.samples$OBS)
|
|
### !! Atención, esto cambia la base de datos:
|
|
sqlSave(dta, upd.samples, tablename="SAMPLES", append = T, varTypes = varTypes, rownames = F)
|
|
print("Tabla SAMPLES sincronizada.")
|
|
}
|
|
|
|
## Entradas modificadas en CLINICOS
|
|
upd.clinics<-values[["CLINICS"]]
|
|
umid.mod<-upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
|
|
rnames<-sqlFetch(dta, "CLINICS") %>% filter(OVID %in% umid.mod) %>% rownames
|
|
clinics.mod<-upd.clinics %>% filter(OVID %in% umid.mod) %>% select(-NHC)
|
|
rownames(clinics.mod)<-rnames
|
|
|
|
### !! Atención, esto cambia la base de datos:
|
|
print(clinics.mod)
|
|
fechas<-colnames(clinics.mod)[sqlFetch(dta, "CLINICS") %>% sapply(lubridate::is.POSIXct)]
|
|
for (i in fechas){
|
|
clinics.mod[,i]<-lubridate::parse_date_time(clinics.mod[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
print(clinics.mod)
|
|
sqlUpdate(dta, clinics.mod,"CLINICS", index="OVID")
|
|
print("Tabla CLINICS modificada.")
|
|
|
|
## Nuevas entradas en CLINICOS
|
|
nsamples.clin<-sqlFetch(dta, "CLINICS") %>% nrow
|
|
umid.new<-upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
|
|
clinics.new<-upd.clinics %>% filter(OVID %in% umid.new) %>% select(-NHC)
|
|
if (length(umid.new) > 0){rownames(clinics.new)<-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %>% as.character}
|
|
|
|
### !! Atención, esto cambia la base de datos:
|
|
fechas<-colnames(clinics.new)[sqlFetch(dta, "CLINICS") %>% sapply(lubridate::is.POSIXct)]
|
|
for (i in fechas){
|
|
clinics.new[,i]<-lubridate::parse_date_time(clinics.new[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
print(varTypes)
|
|
print(clinics.new)
|
|
sqlSave(dta, clinics.new, tablename="CLINICS", append = T, varTypes = varTypes, rownames = F)
|
|
print("Tabla CLINICS sincronizada.")
|
|
}
|
|
if (input$dbtype %in% c("CC")){
|
|
## Nuevas entradas en MUESTRAS
|
|
nsamples<-values[["samples"]] %>% nrow
|
|
upd.samples<-values[["samples"]]
|
|
if (nrow(upd.samples) > 0){rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character}
|
|
|
|
if (nrow(upd.samples) > 0){
|
|
upd.samples$FECHA_RECEPCION<-lubridate::parse_date_time(upd.samples$FECHA_RECEPCION, c("d/m/Y","d/m/y")) %>% as.Date()
|
|
upd.samples$TIPO<-as.character(upd.samples$TIPO)
|
|
upd.samples$OBS<-as.character(upd.samples$OBS)
|
|
### !! Atención, esto cambia la base de datos:
|
|
sqlSave(dta, upd.samples, tablename="MUESTRAS", append = T, varTypes = c("FECHA_RECEPCION"="Date"), rownames = F)
|
|
print("Tabla MUESTRAS sincronizada.")
|
|
}
|
|
|
|
## Entradas modificadas en CLINICOS
|
|
upd.clinics<-values[["CLINICS"]]
|
|
PATID.mod<-upd.clinics$PATID[upd.clinics$PATID %in% (sqlFetch(dta, "CLINICOS") %>% pull(PATID))]
|
|
rnames<-sqlFetch(dta, "CLINICOS") %>% filter(PATID %in% PATID.mod) %>% rownames
|
|
clinics.mod<-upd.clinics %>% filter(PATID %in% PATID.mod) %>% select(-NHC)
|
|
rownames(clinics.mod)<-rnames
|
|
|
|
### !! Atención, esto cambia la base de datos:
|
|
fechas<-colnames(clinics.mod)[grepl("date|MET_DX|DoB", colnames(clinics.mod), ignore.case = T)]
|
|
for (i in fechas){
|
|
clinics.mod[,i]<-lubridate::parse_date_time(clinics.mod[,i], c("d/m/Y","d/m/y")) %>% as.Date()
|
|
}
|
|
sqlUpdate(dta, clinics.mod,"CLINICOS", index="PATID")
|
|
print("Tabla CLINICOS modificada.")
|
|
|
|
## Nuevas entradas en CLINICOS
|
|
nsamples.clin<-sqlFetch(dta, "CLINICOS") %>% nrow
|
|
PATID.new<-upd.clinics$PATID[!upd.clinics$PATID %in% (sqlFetch(dta, "CLINICOS") %>% pull(PATID))]
|
|
clinics.new<-upd.clinics %>% filter(PATID %in% PATID.new) %>% select(-NHC)
|
|
if (length(PATID.new) > 0){rownames(clinics.new)<-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %>% as.character}
|
|
|
|
### !! Atención, esto cambia la base de datos:
|
|
fechas<-colnames(clinics.new)[grepl("date|MET_DX|DoB", colnames(clinics.new), ignore.case = T)]
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
for (i in fechas){
|
|
clinics.new[,i]<-lubridate::parse_date_time(clinics.new[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
sqlSave(dta, clinics.new, tablename="CLINICOS", append = T, varTypes = varTypes, rownames = F)
|
|
print("Tabla CLINICOS sincronizada.")
|
|
|
|
## Nuevas entradas en CNAG
|
|
if (nrow(values[["cnag"]]) > 0){
|
|
cnag.sync<-values[["cnag"]]
|
|
fechas<-colnames(cnag.sync)[sqlFetch(dta, "CNAG") %>% sapply(lubridate::is.POSIXct)]
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
print(fechas)
|
|
for (i in fechas){
|
|
cnag.sync[,i]<-lubridate::parse_date_time(cnag.sync[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
sqlSave(dta, cnag.sync, tablename="CNAG", append = T, varTypes = varTypes, rownames = F)
|
|
}
|
|
|
|
## Nuevas entradas en RNADNA
|
|
if (nrow(values[["rna"]]) > 0){
|
|
rna.sync<-values[["rna"]]
|
|
fechas<-colnames(rna.sync)[sqlFetch(dta, "RNADNA") %>% sapply(lubridate::is.POSIXct)]
|
|
varTypes<-rep("Date",length(fechas))
|
|
names(varTypes)<-fechas
|
|
for (i in fechas){
|
|
rna.sync[,i]<-lubridate::parse_date_time(rna.sync[,i], c("d/m/Y","d/m/y","Y-m-d")) %>% as.Date()
|
|
}
|
|
sqlSave(dta, rna.sync, tablename="RNADNA", append = T, varTypes = varTypes, rownames = F)
|
|
}
|
|
}
|
|
})
|
|
|
|
|
|
## Visor ----
|
|
|
|
output$report<-renderUI({
|
|
samples<-sqlFetch(dta, "samples")
|
|
if (input$nhc == 1){samples_sel<-samples %>% filter(OVID == input$id)}
|
|
if (input$nhc == 2){samples_sel<-samples %>% filter(OVID == input$id)}
|
|
if (input$nhc == 3){samples_sel<-samples %>% filter(samples == input$id)}
|
|
|
|
print(input$id)
|
|
print(samples_sel)
|
|
|
|
if (input$nhc == 3){
|
|
HTML(paste0(
|
|
"
|
|
<style>
|
|
table {
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
border-collapse: collapse;
|
|
width: 100%;
|
|
}
|
|
|
|
table td, table th {
|
|
border: 1px solid #ddd;
|
|
padding: 8px;
|
|
}
|
|
|
|
table tr:nth-child(even){background-color: #f2f2f2;}
|
|
|
|
table tr:hover {background-color: #ddd;}
|
|
|
|
table th {
|
|
padding-top: 12px;
|
|
padding-bottom: 12px;
|
|
text-align: left;
|
|
background-color: #04AA6D;
|
|
color: white;
|
|
}
|
|
</style>
|
|
",
|
|
"<h3>Muestra: ",input$id,"</h3>",
|
|
"<b>OVID</b>: ",samples_sel$OVID,"<br>",
|
|
"<b>Fecha</b>: ",samples_sel$IQ_date,"<br>",
|
|
"<b>Tipo de tejido: </b>",samples_sel$Tissue,"<br><br>",
|
|
"<b>AP: </b>",samples_sel$AP,"<br><br>",
|
|
"<b>Comments:</b>","<br> ",samples_sel$Coments,"<br><br>",
|
|
|
|
"<table style='width:auto;'>
|
|
<tg>
|
|
<th><b>Hist</b></th>
|
|
<th><b>RNA</b></th>
|
|
<th><b>Slide</b></th>
|
|
<th><b>Frag</b></th>
|
|
<th><b>Disg</b></th>
|
|
</tr>
|
|
<tr>
|
|
<td>",samples_sel$Hist_dic,"</td>
|
|
<td>",samples_sel$RNA_dic,"</td>
|
|
<td>",samples_sel$Slide_dic,"</td>
|
|
<td>",samples_sel$Frag_vial,"</td>
|
|
<td>",samples_sel$Disg_vial,"</td>
|
|
"
|
|
))
|
|
}
|
|
#**AP**: `r samples_sel$AP`
|
|
|
|
#**Macro**:
|
|
# `r samples_sel$Macro`
|
|
|
|
#**Despcripción**:
|
|
# `r samples_sel$Description`
|
|
|
|
#**Procesado**:
|
|
# `r samples_sel$Process`
|
|
})
|
|
|
|
output$nitrogen<-renderTable({
|
|
nitro<-sqlFetch(dta, "NITROGEN")
|
|
if (input$nhc == 3){nitro<-nitro %>% filter(CODIGO == input$id)}
|
|
else{nitro<-as.data.frame(matrix(ncol=0, nrow=0))}
|
|
nitro
|
|
})
|
|
|
|
observeEvent(input$nitrosync, {
|
|
|
|
if (input$dbtype == "UM"){
|
|
## Copia de backup
|
|
|
|
if (!dir.exists(NitroRoute)){
|
|
dir.create(NitroRoute)
|
|
print(paste0("Back Up directory ", NitroRoute, " created"))
|
|
}
|
|
|
|
write.xlsx(
|
|
sqlFetch(dta, "NITROGEN"),
|
|
file=paste0(NitroRoute, format(Sys.time(), format="%Y%m%d"),"-","UM-Nitrogen.xlsx")
|
|
)
|
|
|
|
table<-read.xlsx(paste0(gsub("/BU_NITRO/", "", NitroRoute),"/Nitrogen_ICO.xlsx"))
|
|
|
|
table.um<-table %>% filter( grepl("UM|MU", Nombre) | grepl("Met|Prim|hep|Enn",Nombre, ignore.case = T) |
|
|
Nombre == "TILs f. 21/09/20" | Nombre == "TILS DISG f. 03/07/20")
|
|
|
|
table.um<-add_column(table.um,.before=1, "CODIGO"=str_extract(table.um$Nombre, "UM[0-9]{4}"))
|
|
|
|
table.samp<-sqlQuery(dta, "SELECT U.NHC,M.* FROM MUESTRAS M INNER JOIN UMID U ON M.UMID=U.UMID")
|
|
table.samp$FECHA_RECEPCION<-table.samp$FECHA_RECEPCION+24*60*60
|
|
|
|
|
|
table.um<-add_column(table.um, .after=1,
|
|
"FECHA"=str_extract(table.um$Nombre, "[0-9]{2}[\\./][0-9]{2}[\\./][0-9]{2}") %>% gsub("\\.","/",.))
|
|
table.um$FECHA<-as.POSIXct(table.um$FECHA, format="%d/%m/%y")
|
|
|
|
table.um<-merge(table.um, table.samp %>% select(CODIGO,FECHA_RECEPCION), by.x="FECHA", by.y="FECHA_RECEPCION", all.x=T)
|
|
table.um<-table.um %>% mutate(CODIGO=case_when(is.na(CODIGO.x)~CODIGO.y, TRUE~CODIGO.x), .before=1) %>% select(-CODIGO.x,-CODIGO.y)
|
|
|
|
table.um[table.um$Nombre == "MU Met f. 10/02/20","CODIGO"]<-"UM0009"
|
|
table.um[table.um$Nombre == "TILs Diss Met","CODIGO"]<-"UM0020"
|
|
table.um[table.um$Nombre == "UM Met","CODIGO"]<-"UM0020"
|
|
table.um[table.um$Nombre == "UM MET NHC 11489341","CODIGO"]<-"UM0044"
|
|
table.um[table.um$Nombre == "UM Primary 20278822","CODIGO"]<-"UM0029"
|
|
table.um[table.um$Nombre == "Hip Nod Hep NHC11489341","CODIGO"]<-"UM0044"
|
|
|
|
## Actualización
|
|
|
|
sqlDrop(dta, "NITROGEN")
|
|
sqlSave(dta, table.um %>% select(-FECHA) %>% filter(!is.na(CODIGO)), tablename="NITROGEN", rownames=F)
|
|
}
|
|
if (input$dbtype == "OV"){
|
|
## Copia de backup
|
|
|
|
if (!dir.exists(NitroRoute)){
|
|
dir.create(NitroRoute)
|
|
print(paste0("Back Up directory ", NitroRoute, " created"))
|
|
}
|
|
|
|
write.xlsx(
|
|
sqlFetch(dta, "NITROGEN"),
|
|
file=paste0(NitroRoute, format(Sys.time(), format="%Y%m%d"),"-","OV-Nitrogen.xlsx")
|
|
)
|
|
|
|
## Lectura del excel
|
|
table<-read.xlsx(paste0(gsub("/BU_NITRO/", "", NitroRoute),"/Nitrogen_ICO.xlsx"))
|
|
|
|
table.ov<-table %>% filter(grepl("OV",Nombre))
|
|
table.ov<-filter(table.ov, !grepl("OVA",Nombre))
|
|
|
|
table.ov<-add_column(table.ov,.before=1, "CODIGO"=str_extract(table.ov$Nombre, "OV[0-9]{4}"))
|
|
table.ov[table.ov$Nombre == "Ascitis OV","CODIGO"]<-"OV2105"
|
|
|
|
table.ov<-table.ov %>% add_column(SampType=NA, .after=1)
|
|
table.ov[grepl("TIL",table.ov$Nombre, ignore.case = T),"SampType"]<-"TILs"
|
|
table.ov[grepl("frag",table.ov$Nombre, ignore.case = T)|grepl("frag",table.ov$Observaciones, ignore.case = T),"SampType"]<-"FRAG"
|
|
table.ov[grepl("slide",table.ov$Nombre, ignore.case = T)|grepl("slide",table.ov$Observaciones, ignore.case = T),"SampType"]<-"SLIDE"
|
|
table.ov[(is.na(table.ov$SampType) | !(table.ov$SampType == "TILs")) &
|
|
(grepl("disg|diss",table.ov$Nombre, ignore.case = T)|
|
|
grepl("disg|diss",table.ov$Observaciones, ignore.case = T)),"SampType"]<-"DISG"
|
|
table.ov[table.ov$Nombre == "Ascitis OV", "SampType"]<-"ASC"
|
|
table.ov[20:25,"SampType"]<-"DISG"
|
|
table.ov[63:68,"SampType"]<-"DISG"
|
|
|
|
## Actualización
|
|
|
|
sqlDrop(dta, "NITROGEN")
|
|
sqlSave(dta, table.ov, tablename="NITROGEN", rownames=F)
|
|
}
|
|
|
|
})
|
|
|
|
output$visorplot<-renderPlot({
|
|
if (input$nhc == 3){
|
|
|
|
data<-sqlFetch(dta, "IC") %>% filter(samples == input$id)
|
|
data1<-data %>% gather(phen, value, -samples, -Population)
|
|
|
|
data1$phen<-gsub("p","+",data1$phen)
|
|
data1$phen<-gsub("n","-",data1$phen)
|
|
data1$phen<-gsub("_"," ",data1$phen)
|
|
|
|
data1$phen<-gsub("n","-",data1$phen, fixed = T)
|
|
data1$phen<-gsub("p","+",data1$phen, fixed = T)
|
|
data1$phen<-gsub("_"," ",data1$phen)
|
|
data1[data1$value < 0.5, "phen"]<-"Other"
|
|
data1$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data1$phen)
|
|
data1$phen<-gsub("+ $", "", data1$phen)
|
|
data1$phen[data1$phen == ""]<-"All Negative"
|
|
|
|
# data1<-data1 %>% filter(value > 0.5)
|
|
|
|
data1["phen1"]<-"PD1"
|
|
data1[!grepl("PD1+", data1$phen),"phen1"]<-NA
|
|
|
|
data1["phen2"]<-"TIM3"
|
|
data1[!grepl("TIM3+", data1$phen),"phen2"]<-NA
|
|
|
|
data1["phen3"]<-"CTLA4"
|
|
data1[!grepl("CTLA4+", data1$phen),"phen3"]<-NA
|
|
|
|
data1["phen4"]<-"TIGIT"
|
|
data1[!grepl("TIGIT+", data1$phen),"phen4"]<-NA
|
|
|
|
data1["phen5"]<-"LAG3"
|
|
data1[!grepl("LAG3+", data1$phen),"phen5"]<-NA
|
|
|
|
data1<-data1 %>% arrange(desc(value))
|
|
data2<-data1 %>% filter(!phen %in% c("All Negative","Other"))
|
|
data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen)))
|
|
|
|
data_cd8<-data1 %>% filter(Population == "CD8")
|
|
data_cd4<-data1 %>% filter(Population == "CD4")
|
|
|
|
data_cd8$ymax<-cumsum(data_cd8$value)
|
|
data_cd8$ymin<-c(0, head(data_cd8$ymax, n=-1))
|
|
|
|
data_cd4$ymax<-cumsum(data_cd4$value)
|
|
data_cd4$ymin<-c(0, head(data_cd4$ymax, n=-1))
|
|
|
|
data1<-rbind(data_cd8, data_cd4)
|
|
|
|
|
|
color<-c(c("CTLA4+ LAG3+ PD1+ TIGIT+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E","TIGIT+"="#FA7055"),
|
|
c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F","PD1+ TIGIT+"="#D259AA", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "CTLA4+ TIGIT+"="#9C929A", "LAG3+ TIM3+"="#9ED848", "TIGIT+ TIM3+"="#ACB353", "LAG3+ TIGIT+"="#EC964A"),
|
|
c("CTLA4+ PD1+ TIGIT+"="#B86B6A","CTLA4+ PD1+ TIGIT+ TIM3+"="#B81515","LAG3+ PD1+ TIGIT+"="#007D8A", "PD1+ TIGIT+ TIM3+"="#D64545", "LAG3+ PD1+ TIGIT+ TIM3+"="#0f5860", "LAG3+ TIGIT+ TIM3+"="#50cad3"))
|
|
basic.color<-color[c("PD1+","TIGIT+","TIM3+","CTLA4+","LAG3+")]
|
|
names(basic.color)<-c("PD1","TIGIT","TIM3","CTLA4","LAG3")
|
|
# Make the plot
|
|
g1<-ggplot(data1)+
|
|
facet_wrap(.~Population)+
|
|
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+
|
|
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
|
|
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen4, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
|
|
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen2, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
|
|
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen3, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
|
|
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=7.4, xmin=7, fill=factor(phen5, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
|
|
scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","TIGIT","TIM3","CTLA4","LAG3"), name="IC")+
|
|
coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
|
|
xlim(c(0, 8)) +# Try to remove that to see how to make a pie chart
|
|
theme_classic()+
|
|
theme(strip.background = element_blank(),
|
|
strip.text = element_text(size=12, face="bold"),
|
|
axis.line = element_blank(),
|
|
axis.ticks = element_blank(),
|
|
plot.margin = margin(-200,0,0,0),
|
|
axis.text = element_blank())
|
|
|
|
# df.color<-data.frame("phen"=names(color), "color"=color)
|
|
# df.color<-rbind(
|
|
# df.color %>% filter(!phen %in% c("All Negative","Other")) %>% arrange(phen),
|
|
# df.color %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen))
|
|
# )
|
|
#
|
|
# g2<-ggplot(df.color, aes(phen, 1))+
|
|
# geom_tile(fill=df.color$color)+
|
|
# scale_x_discrete(limits=df.color$phen)+
|
|
# ggtitle("Phenotype Combination")+
|
|
# theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5),
|
|
# axis.text.y = element_blank(),
|
|
# axis.ticks = element_blank(),
|
|
# axis.title = element_blank(),
|
|
# panel.background = element_blank())+
|
|
# coord_equal()
|
|
|
|
# g_IC<-ggpubr::ggarrange(g1,g2,ncol=1)
|
|
g_IC<-g1
|
|
|
|
pops<-sqlFetch(dta, "POPULATIONS")
|
|
|
|
g_pop<-pops %>% dplyr::filter(samples == input$id) %>% gather(pop,value,-samples) %>%
|
|
mutate(pop=factor(pop, levels=c("CD45pos_Alive","T_cells","CD8","CD4","DN","NK", "B_cells",
|
|
"CD45neg_LDneg","EpCAMneg_HLAIneg","EpCAMneg_HLAIpos","EpCAMpos_HLAIpos"))) %>%
|
|
ggplot(aes(pop, value))+
|
|
geom_bar(stat="identity", color="black", fill="grey70")+
|
|
labs(title = input$id, y="% parent", x="")+
|
|
theme_bw()+
|
|
theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5))
|
|
# tl<-sqlFetch(dta, "IC") %>% filter(samples == input$id)
|
|
#
|
|
# mtl<-melt(tl, variable.name = "Receptors")
|
|
# mtl$Receptors<-as.character(mtl$Receptors) #Para poder depurar bien el texto, lo pasamos a tipo character
|
|
# mtl$Receptors<-gsub("n","-",mtl$Receptors, fixed = T)
|
|
# mtl$Receptors<-gsub("p","+",mtl$Receptors, fixed = T)
|
|
# mtl$Receptors<-gsub("_"," ",mtl$Receptors)
|
|
# mtl[mtl$value < 1, "Receptors"]<-"Other"
|
|
# mtl$Receptors<-gsub("[A-Z]*-*[0-9T]- *", "", mtl$Receptors)
|
|
# mtl$Receptors<-gsub("+ $", "", mtl$Receptors)
|
|
# mtl$Receptors[mtl$Receptors == ""]<-"All Negative"
|
|
#
|
|
# mtl$Receptors<-factor(mtl$Receptors)
|
|
# mtl$Population<-factor(mtl$Population, levels = c("CD8", "CD4"))
|
|
#
|
|
# # colorCount<-length(unique(mtl$Receptors))
|
|
# # getPalette = colorRampPalette(RColorBrewer::brewer.pal(12, "Set3"))
|
|
#
|
|
# color<-c(c("CTLA4+ LAG3+ PD1+ TIGIT+ TIM3+"="black","All Negative"="white","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E","TIGIT+"="#FA7055"),
|
|
# c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F","PD1+ TIGIT+"="#D259AA", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "CTLA4+ TIGIT+"="#9C929A", "LAG3+ TIM3+"="#9ED848", "TIGIT+ TIM3+"="#ACB353", "LAG3+ TIGIT+"="#EC964A"),
|
|
# c("CTLA4+ PD1+ TIGIT+"="#B86B6A","CTLA4+ PD1+ TIGIT+ TIM3+"="#B81515","LAG3+ PD1+ TIGIT+"="#007D8A", "PD1+ TIGIT+ TIM3+"="#D64545", "LAG3+ PD1+ TIGIT+ TIM3+"="#0f5860", "LAG3+ TIGIT+ TIM3+"="#50cad3"))
|
|
#
|
|
# g_IC<-ggplot(mtl, aes(samples, value, fill=Receptors))+
|
|
# geom_bar(stat="summary", fun="sum",color="black")+
|
|
# labs(x="Patient", y="% CD8+", fill="")+
|
|
# facet_grid(.~Population)+
|
|
# scale_fill_manual(values = color[levels(mtl$Receptors)[levels(mtl$Receptors) %in% unique(mtl$Receptors)]])+
|
|
# theme_bw()+
|
|
# theme(axis.text.x=element_text(angle=45, hjust=1))
|
|
ggpubr::ggarrange(g_pop, g_IC, heights = c(0.4, 0.6), ncol = 1)
|
|
}
|
|
})
|
|
|
|
|
|
## Citometría ----
|
|
|
|
observe({
|
|
if(input$goButtonDir > 0){
|
|
cito_dir<<-choose.dir() %>% gsub("\\","/",. ,fixed=T) %>% paste0("/")
|
|
|
|
output$session <- renderText(
|
|
cito_dir
|
|
)
|
|
}
|
|
})
|
|
|
|
observeEvent(input$fcsconvert,{
|
|
route<-cito_dir
|
|
|
|
files<-list.files(route, ".LMD")
|
|
for (lmd in files){
|
|
fcs<-read.FCS(paste0(route,lmd), dataset = 2)
|
|
# fcs@parameters$desc<-c("FS-A","SS-A", paste("FL",1:10,"-A", sep = ""), "TIME")
|
|
# fcs@parameters$desc<-c("FS-H","FS-A","FS-W","SS-H","SS-A","TIME", paste("FL",1:10,"-A", sep = ""))
|
|
keyword(fcs)['$FIL']<-paste0(gsub(".LMD","",lmd), ".fcs")
|
|
write.FCS(fcs, paste0(route, gsub(".LMD","",lmd), ".fcs"))
|
|
}
|
|
})
|
|
|
|
observeEvent(input$pngexport,{
|
|
if (input$phenotype == "Pop"){
|
|
route<-cito_dir
|
|
|
|
ws<-open_flowjo_xml(paste0(route,"Populations.wsp"))
|
|
gs<-flowjo_to_gatingset(ws, name="All Samples")
|
|
|
|
sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Pop ")[[1]][2]) %>%
|
|
gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
|
|
|
|
for (samp in sampleNames(gs)){
|
|
print(samp)
|
|
p<-autoplot(gs[[samp]], bins=64)
|
|
ggsave(paste0(route, samp,".pop.png"),p,width = 10, height = 10)
|
|
}
|
|
}
|
|
|
|
if (input$phenotype == "IC"){
|
|
route<-cito_dir
|
|
|
|
ws<-open_flowjo_xml(paste0(route,"IC.wsp"))
|
|
gs<-flowjo_to_gatingset(ws, name="All Samples")
|
|
|
|
sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "ICs ")[[1]][2]) %>%
|
|
gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
|
|
|
|
names<-sampleNames(gs) %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique()
|
|
|
|
nodes<-gs_get_pop_paths(gs)
|
|
nodes_parent<-nodes[!grepl("CTLA4|LAG3|PD1|TIGIT|TIM3|root$", nodes)]
|
|
nodes_cd4<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIGIT$|TIM3$", nodes) & grepl("/CD4/",nodes)]
|
|
nodes_cd8<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIGIT$|TIM3$", nodes) & grepl("/CD8/",nodes)]
|
|
|
|
for (id in names){
|
|
print(id)
|
|
iso<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("iso",sampleNames(gs))]
|
|
ab<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("ab",sampleNames(gs))]
|
|
|
|
g1<-ggcyto_arrange(autoplot(gs[[ab]], nodes_parent, bins=128), nrow=1)
|
|
g2<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd8, bins=64), nrow=1)
|
|
g3<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd8, bins=64), nrow=1)
|
|
g4<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd4, bins=64), nrow=1)
|
|
g5<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd4, bins=64), nrow=1)
|
|
g_all<-gridExtra::gtable_rbind(g1,g2,g3,g4,g5)
|
|
ggsave(paste0(route,id,".IC.png"), g_all, width = 10, height = 10)
|
|
}
|
|
}
|
|
})
|
|
|
|
observeEvent(input$popexport,{
|
|
if (input$phenotype == "Pop"){
|
|
route<-cito_dir
|
|
|
|
ws<-open_flowjo_xml(paste0(route,"Populations.wsp"))
|
|
gs<-flowjo_to_gatingset(ws, name="All Samples")
|
|
|
|
sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Pop ")[[1]][2]) %>%
|
|
gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
|
|
|
|
nodes<-sapply(strsplit(gs_get_pop_paths(gs), "/"), tail, 1)
|
|
nodes<-nodes[grepl("_",nodes)]
|
|
pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
|
|
|
|
pop[,"pop"]<-gsub("_","",pop$pop)
|
|
pop$pop<-gsub(" ","_",pop$pop)
|
|
pop$pop<-gsub("+","pos",pop$pop, fixed=T)
|
|
pop$pop<-gsub("-","neg",pop$pop, fixed=T)
|
|
pop<-rename(pop, "samples"="sample")
|
|
pop$percent<-round(pop$percent, digits=2)
|
|
pop_sp<-pop %>% spread(pop, percent)
|
|
|
|
pop_sql<-sqlFetch(dta, "POPULATIONS") %>% slice(0)
|
|
pop_sp<-pop_sp %>% merge(pop_sql, all=T) %>% select(colnames(pop_sql))
|
|
|
|
vartypes<-rep("Number", pop_sp %>% select(-samples) %>% colnames %>% length)
|
|
names(vartypes)<-pop_sp %>% select(-samples) %>% colnames
|
|
|
|
sqlSave(dta, pop_sp, tablename="POPULATIONS", append = T, varTypes = vartypes, rownames = F)
|
|
print("Tabla POPULATIONS sincronizada.")
|
|
}
|
|
if (input$phenotype == "IC"){
|
|
route<-cito_dir
|
|
|
|
ws<-open_flowjo_xml(paste0(route,"IC.wsp"))
|
|
gs<-flowjo_to_gatingset(ws, name="All Samples")
|
|
|
|
sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "ICs ")[[1]][2]) %>%
|
|
gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
|
|
|
|
nodes<-gs_get_pop_paths(gs)
|
|
# nodes<-gsub("â\u0081»", "-", nodes)
|
|
# nodes<-gsub("â\u0081º", "+", nodes)
|
|
nodes<-nodes[grepl("CTLA4", nodes)]
|
|
nodes<-nodes[!grepl("CD4$|CD8$|CTLA4$|TIM3$|PD1$|LAG3$|TIGIT$|/CTLA4â\u0081»$|/TIM3â\u0081»$|/PD1â\u0081»$|/LAG3â\u0081»$|/TIGITâ\u0081»$", nodes)]
|
|
|
|
pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
|
|
pop$percent<-round(pop$percent, digits=2)
|
|
|
|
pop$pop<-gsub("â\u0081»", "n", pop$pop)
|
|
pop$pop<-gsub("â\u0081º", "p", pop$pop)
|
|
pop$pop<-gsub(" ", "_", pop$pop)
|
|
|
|
pop["Type"]<-"ab"
|
|
pop[grepl("iso|ISO|Iso",pop$sample),"Type"]<-"iso"
|
|
pop$sample<-gsub("iso|ISO|Iso|ab|AB|Ab| ","",pop$sample)
|
|
|
|
pop_sp<-pop %>% spread(Type, percent)
|
|
pop_sp["Net"]<-pop_sp$ab
|
|
pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"Net"]<-pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"ab"]-pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"iso"]
|
|
pop_sp$Net[pop_sp$Net < 0]<-0
|
|
pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/","",.)
|
|
pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
|
|
|
|
pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net)
|
|
pop_sp$`CTLA4n_LAG3n_PD1n_TIGITn_TIM3n`<- pop_sp %>% select(-`CTLA4n_LAG3n_PD1n_TIGITn_TIM3n`) %>% group_by(sample,Population) %>%
|
|
gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n)
|
|
pop_sp <- rename(pop_sp, "samples"="sample")
|
|
|
|
vartypes<-rep("Number", pop_sp %>% select(-samples, -Population) %>% colnames %>% length)
|
|
names(vartypes)<-pop_sp %>% select(-samples, -Population) %>% colnames
|
|
|
|
sqlSave(dta, pop_sp, tablename="IC", append = T, varTypes = vartypes, rownames = F)
|
|
print("Tabla IC sincronizada.")
|
|
}
|
|
})
|
|
|
|
|
|
## scRNAseq ----
|
|
|
|
output$PATID = renderUI({
|
|
observeEvent(input$goButton, {})
|
|
sc_cod<-sqlFetch(dta, "CNAG") %>% pull(CODIGO)
|
|
selectizeInput("sc_cod", "CÓDIGO", sc_cod, multiple = T)
|
|
})
|
|
|
|
output$sc_table<-renderTable({
|
|
if (input$sct_sel){
|
|
if (input$sqlquery != ""){
|
|
print(input$sqlquery)
|
|
sqlQuery(dta, input$sqlquery)
|
|
}else{
|
|
if (!is.null(input$sc_cod)){
|
|
sqlFetch(dta, "CNAG") %>% filter(CODIGO %in% input$sc_cod)
|
|
}else{
|
|
sqlFetch(dta, "CNAG")
|
|
}
|
|
}
|
|
}else{
|
|
sqlFetch(dta, "CNAG")
|
|
}
|
|
|
|
})
|
|
|
|
output$sc_plot <-renderPlot({
|
|
meta<<-readRDS(paste0(scRNAseqRoute,"metadata_full_object.rds"))
|
|
|
|
if (input$sqlquery != ""){
|
|
sc_codigos<-sqlQuery(dta, input$sqlquery) %>% pull(CNAG_NAME)
|
|
}else{
|
|
if (!is.null(input$sc_cod)){
|
|
sc_codigos<-sqlFetch(dta, "CNAG") %>% filter(CODIGO %in% input$sc_cod) %>% pull(CNAG_NAME)
|
|
}else{
|
|
sc_codigos<-sqlFetch(dta, "CNAG") %>% pull(CNAG_NAME)
|
|
}
|
|
}
|
|
sc_codigos<-gsub(" _","_", sc_codigos )
|
|
sc_codigos<-gsub("_ ","_", sc_codigos )
|
|
sc_codigos<-gsub(" ","_", sc_codigos )
|
|
|
|
meta<-meta %>% mutate(sample2=gsub("_CD45", "", sample)) %>% filter(sample2 %in% sc_codigos)
|
|
if (isFALSE(input$cd45_chk)){ meta<-meta %>% filter(!grepl("_CD45", sample))}
|
|
|
|
g1<-ggplot(meta, aes(coord_x, coord_y, color=predicted.id))+
|
|
geom_point(size=0.2)+
|
|
guides(colour = guide_legend(override.aes = list(size=2)))+
|
|
theme_bw()+
|
|
theme(aspect.ratio = 1)
|
|
|
|
meta_perc<-meta %>% group_by(sample, predicted.id) %>% summarise(N=n()) %>% mutate(N=perc(N)) %>%
|
|
spread(predicted.id, N) %>% gather("predicted.id","N",-sample) %>% mutate(N=case_when(is.na(N)~0,TRUE~N))
|
|
g2<-ggheatmap(meta_perc, "sample","predicted.id", "N", color = "grey50")
|
|
ggpubr::ggarrange(g1,g2, ncol=1, heights = c(0.3, 0.7))
|
|
})
|
|
|
|
output$sc_expr <-renderPlot({
|
|
if (input$genes != ""){
|
|
expr<-readRDS(paste0(scRNAseqRoute,"expression_full_object.rds"))
|
|
genes<-strsplit(input$genes, ",")[[1]]
|
|
|
|
if (length(genes) > 1){
|
|
df.expr<-as.data.frame(as.matrix(expr[genes,]))
|
|
df.expr["Gene"]<-rownames(df.expr)
|
|
mdf.expr<-melt(df.expr)
|
|
}else{
|
|
df.expr<-as.data.frame(t(as.matrix(expr[genes[1],])))
|
|
df.expr["Gene"]<-genes[1]
|
|
mdf.expr<-melt(df.expr)
|
|
}
|
|
|
|
alldata<-merge(meta, mdf.expr, by.x="barcode", by.y = "variable")
|
|
|
|
}
|
|
|
|
# order <- clustsort(alldata %>% spread(Gene, value) %>% select(predicted.id, all_of(genes)) %>%
|
|
# group_by(predicted.id) %>% summarise(across(all_of(genes), mean)) %>% as.data.frame)
|
|
#
|
|
# g1<-ggplot(alldata, aes(predicted.id, value, fill=predicted.id))+
|
|
# geom_violin(scale = "width")+
|
|
# geom_jitter(width=0.2, size=0.1, alpha=0.3)+
|
|
# scale_x_discrete(limits=order$x)+
|
|
# guides(fill=F)+
|
|
# facet_wrap(.~Gene)+
|
|
# theme_bw()+
|
|
# theme(axis.text.x = element_text(angle=90, hjust=1, vjust = 0.5))
|
|
g2<-ggheatmap(alldata, x="predicted.id",y="Gene",value="value", color="grey")+coord_equal()
|
|
# ggpubr::ggarrange(g1,g2, ncol=1)
|
|
g2
|
|
})
|
|
}
|
|
|
|
# Run the application
|
|
shinyApp(ui = ui, server = server)
|