Scripts relacionados con el acceso y análisis en bases de datos Access.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.5 KiB

#############
## Script para añadir nuevas muestras a la base de datos de Ovario
#############
library(tidyverse)
library(RODBC)
library(openxlsx)
## Llamar funciones sql
source("sqlFunctions.R")
## Conexión a la base de datos
source("ruta_database.R", encoding = "UTF-8")
dta<-odbcConnectAccess2007(access.file = file,
pwd = .rs.askForPassword("Enter password:"))
## Importamos los NHC de las muestras nuevas
nhc.test<-read.xlsx("queryOV.xlsx", sheet = "NHC") %>% pull(NHC)
## Mostramos si ya hay muestras del mismo paciente
sqlQuery(dta, "SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test) %>%
group_by(NHC,OVID) %>% summarise(Samples=length(samples), Names=paste0(samples, collapse = ";")) %>% merge(data.frame(NHC=nhc.test),all=T)
## Versión más extensa
sqlQuery(dta, "SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test)
## Generar OVIDs para nuevos NHC
ovid<-sqlFetch(dta,"OVID")
new.nhc<-nhc.test[!nhc.test %in% ovid$NHC]
next.num<-gsub("OVID","",ovid$OVID) %>% as.numeric %>% max(na.rm=T)+1
last.num<-next.num+(length(new.nhc)-1)
upd.ovid<-rbind(ovid,data.frame("NHC"=new.nhc, "OVID"=sprintf("OVID%04d",next.num:last.num)))
rownames(upd.ovid)<-as.character(1:nrow(upd.ovid))
upd.ovid<-filter(upd.ovid, NHC %in% new.nhc) %>% mutate(NHC=as.character(NHC))
### !! Atención, esto cambia la base de datos:
sqlSave(dta, upd.ovid, tablename="OVID", append = T)
## 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(nhc.test)-1)
new.samp<-sprintf("OV%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp)
new.samp.df<-merge(sqlFetch(dta,"OVID") %>% merge(data.frame("NHC"=nhc.test)), data.frame("NHC"=nhc.test, "samples"=new.samp))
samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples)
# samples.exp[is.na(samples.exp)]<-""
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
upd.clinics<-sqlFetch(dta, "CLINICS")
ovid.new<-sqlFetch(dta, "OVID") %>% filter(NHC %in% nhc.test)
upd.clinics<-merge(ovid.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])}
## Exportar tablas a la plantilla de entrada para su rellenado
wb <- loadWorkbook("queryOV.xlsx")
writeData(wb, "NHC", upd.ovid)
writeData(wb,"samples",samples.exp)
writeData(wb,"CLINICS",upd.clinics)
saveWorkbook(wb,"queryOV.xlsx",overwrite = TRUE)
## Añadir código de muestra nueva a la base de datos
nsamples<-sqlFetch(dta, "SAMPLES") %>% nrow
upd.samples<-read.xlsx("QueryOV.xlsx", sheet = "samples", detectDates = T)
rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character
### !! Atención, esto cambia la base de datos:
sqlSave(dta, upd.samples, tablename="SAMPLES", append = T)
## Añadir datos clínicos modificados a la base de datos
upd.clinics<-read.xlsx("QueryOV.xlsx", sheet = "CLINICS",detectDates = T)
ovid.mod<-upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
rnames<-sqlFetch(dta, "CLINICS") %>% filter(OVID %in% ovid.mod) %>% rownames
clinics.mod<-upd.clinics %>% filter(OVID %in% ovid.mod) %>% select(-NHC)
rownames(clinics.mod)<-rnames
### !! Atención, esto cambia la base de datos:
fechas<-colnames(clinics.mod)[grepl("DO|date", colnames(clinics.mod))]
for (i in fechas){
clinics.mod[,i]<-as.Date(clinics.mod[,i])
}
sqlUpdate(dta, clinics.mod,"CLINICS")
## Añadir datos clínicos nuevos a la base de datos
nsamples.clin<-sqlFetch(dta, "CLINICS") %>% nrow
ovid.new<-upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
clinics.new<-upd.clinics %>% filter(OVID %in% ovid.new) %>% select(-NHC)
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("DO|date", colnames(clinics.new))]
varTypes<-rep("Date",length(fechas))
names(varTypes)<-fechas
for (i in fechas){
clinics.new[,i]<-as.Date(clinics.new[,i])
}
sqlSave(dta, clinics.new, tablename="CLINICS", append = T, varTypes = varTypes)