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

  1. #############
  2. ## Script para añadir nuevas muestras a la base de datos de Ovario
  3. #############
  4. library(tidyverse)
  5. library(RODBC)
  6. library(openxlsx)
  7. ## Llamar funciones sql
  8. source("sqlFunctions.R")
  9. ## Conexión a la base de datos
  10. source("ruta_database.R", encoding = "UTF-8")
  11. dta<-odbcConnectAccess2007(access.file = file,
  12. pwd = .rs.askForPassword("Enter password:"))
  13. ## Importamos los NHC de las muestras nuevas
  14. nhc.test<-read.xlsx("queryOV.xlsx", sheet = "NHC") %>% pull(NHC)
  15. ## Mostramos si ya hay muestras del mismo paciente
  16. sqlQuery(dta, "SELECT O.NHC,S.*
  17. FROM SAMPLES S
  18. INNER JOIN OVID O
  19. ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test) %>%
  20. group_by(NHC,OVID) %>% summarise(Samples=length(samples), Names=paste0(samples, collapse = ";")) %>% merge(data.frame(NHC=nhc.test),all=T)
  21. ## Versión más extensa
  22. sqlQuery(dta, "SELECT O.NHC,S.*
  23. FROM SAMPLES S
  24. INNER JOIN OVID O
  25. ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test)
  26. ## Generar OVIDs para nuevos NHC
  27. ovid<-sqlFetch(dta,"OVID")
  28. new.nhc<-nhc.test[!nhc.test %in% ovid$NHC]
  29. next.num<-gsub("OVID","",ovid$OVID) %>% as.numeric %>% max(na.rm=T)+1
  30. last.num<-next.num+(length(new.nhc)-1)
  31. upd.ovid<-rbind(ovid,data.frame("NHC"=new.nhc, "OVID"=sprintf("OVID%04d",next.num:last.num)))
  32. rownames(upd.ovid)<-as.character(1:nrow(upd.ovid))
  33. upd.ovid<-filter(upd.ovid, NHC %in% new.nhc) %>% mutate(NHC=as.character(NHC))
  34. ### !! Atención, esto cambia la base de datos:
  35. sqlSave(dta, upd.ovid, tablename="OVID", append = T)
  36. ## Generar código para las nuevas muestras
  37. samples<-sqlFetch(dta, "SAMPLES")
  38. if(sum(grepl(paste0("OV",Sys.time() %>% format("%y")), samples$samples)) > 0){
  39. next.samp<-gsub(paste0("OV",Sys.time() %>% format("%y")),"", samples$samples) %>% as.numeric %>% max(na.rm=T)+1
  40. }else{
  41. next.samp<-1
  42. }
  43. last.samp<-next.samp+(length(nhc.test)-1)
  44. new.samp<-sprintf("OV%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp)
  45. new.samp.df<-merge(sqlFetch(dta,"OVID") %>% merge(data.frame("NHC"=nhc.test)), data.frame("NHC"=nhc.test, "samples"=new.samp))
  46. samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples)
  47. # samples.exp[is.na(samples.exp)]<-""
  48. ## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
  49. upd.clinics<-sqlFetch(dta, "CLINICS")
  50. ovid.new<-sqlFetch(dta, "OVID") %>% filter(NHC %in% nhc.test)
  51. upd.clinics<-merge(ovid.new,upd.clinics, all.x=T, by="OVID")
  52. upd.clinics$NHC<-as.character(upd.clinics$NHC)
  53. for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])}
  54. ## Exportar tablas a la plantilla de entrada para su rellenado
  55. wb <- loadWorkbook("queryOV.xlsx")
  56. writeData(wb, "NHC", upd.ovid)
  57. writeData(wb,"samples",samples.exp)
  58. writeData(wb,"CLINICS",upd.clinics)
  59. saveWorkbook(wb,"queryOV.xlsx",overwrite = TRUE)
  60. ## Añadir código de muestra nueva a la base de datos
  61. nsamples<-sqlFetch(dta, "SAMPLES") %>% nrow
  62. upd.samples<-read.xlsx("QueryOV.xlsx", sheet = "samples", detectDates = T)
  63. rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character
  64. ### !! Atención, esto cambia la base de datos:
  65. sqlSave(dta, upd.samples, tablename="SAMPLES", append = T)
  66. ## Añadir datos clínicos modificados a la base de datos
  67. upd.clinics<-read.xlsx("QueryOV.xlsx", sheet = "CLINICS",detectDates = T)
  68. ovid.mod<-upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
  69. rnames<-sqlFetch(dta, "CLINICS") %>% filter(OVID %in% ovid.mod) %>% rownames
  70. clinics.mod<-upd.clinics %>% filter(OVID %in% ovid.mod) %>% select(-NHC)
  71. rownames(clinics.mod)<-rnames
  72. ### !! Atención, esto cambia la base de datos:
  73. fechas<-colnames(clinics.mod)[grepl("DO|date", colnames(clinics.mod))]
  74. for (i in fechas){
  75. clinics.mod[,i]<-as.Date(clinics.mod[,i])
  76. }
  77. sqlUpdate(dta, clinics.mod,"CLINICS")
  78. ## Añadir datos clínicos nuevos a la base de datos
  79. nsamples.clin<-sqlFetch(dta, "CLINICS") %>% nrow
  80. ovid.new<-upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
  81. clinics.new<-upd.clinics %>% filter(OVID %in% ovid.new) %>% select(-NHC)
  82. rownames(clinics.new)<-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %>% as.character
  83. ### !! Atención, esto cambia la base de datos:
  84. fechas<-colnames(clinics.new)[grepl("DO|date", colnames(clinics.new))]
  85. varTypes<-rep("Date",length(fechas))
  86. names(varTypes)<-fechas
  87. for (i in fechas){
  88. clinics.new[,i]<-as.Date(clinics.new[,i])
  89. }
  90. sqlSave(dta, clinics.new, tablename="CLINICS", append = T, varTypes = varTypes)