Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db summary #769

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(exportToAres)
export(exportToJson)
export(exportVisitDetailToJson)
export(exportVisitToJson)
export(generateDbSummary)
export(getAnalysisDetails)
export(getSeasonalityScore)
export(getTemporalData)
Expand Down
124 changes: 124 additions & 0 deletions R/generateDbSummary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# @file generateDbSummary
#
# Copyright 2021 Observational Health Data Sciences and Informatics
#
# This file is part of Achilles
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#' @title
#' generateDbSummary
#'
#' @description
#' \code{generateDbSummary} can be run after the Achilles analyses are complete
#' to create a high-level database summary.
#'
#' @details
#' Used to generate a high-level database summary consisting of earliest date available, latest
#' date available, median age at first observation, total persons, etc. This function
#' creates a summary table meant for a manuscript detailing the network of databases
#' used in an analysis
#'
#' @param connectionDetails An R object of type \code{connectionDetails} created using the
#' function \code{createConnectionDetails} in the
#' \code{DatabaseConnector} package.
#' @param cdmDatabaseSchema Fully qualified name of database schema that contains OMOP CDM
#' schema. On SQL Server, this should specifiy both the database and the
#' schema, so for example, on SQL Server, 'cdm_instance.dbo'.
#' @param resultsDatabaseSchema Fully qualified name of database schema that we can write final
#' results to. Default is cdmDatabaseSchema. On SQL Server, this should
#' specifiy both the database and the schema, so for example, on SQL
#' Server, 'cdm_results.dbo'.
#' @param country The country of origin of the database
#' @param provenance The provenance of the data (EHR, claims, registry, etc)
#'
#' @return
#' none
#'
#' @examples
#' \dontrun{
#' connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = "sql server",
#' server = "yourserver")
#' dbSummary <- generateDbSummary(connectionDetails,
#' cdmDatabaseSchema = "cdm_schema",
#' resultsDatabaseSchema = "results_schema",
#' country = "Country of Origin",
#' provenance = "Provenance of data")
#' }
#' @export

generateDbSummary <- function (connectionDetails,
cdmDatabaseSchema,
resultsDatabaseSchema,
country,
provenance){

conn <- DatabaseConnector::connect(connectionDetails)

sql <-
SqlRender::loadRenderTranslateSql(
sqlFilename = "summary/generateDbSummary.sql",
packageName = "Achilles",
dbms = connectionDetails$dbms,
warnOnMissingParameters = FALSE,
cdm_database_schema = cdmDatabaseSchema,
results_database_schema = resultsDatabaseSchema,
country = country,
provenance = provenance
)

dbSummary <- DatabaseConnector::querySql(conn, sql)

sql <-
SqlRender::loadRenderTranslateSql(
sqlFilename = "summary/dbSourceVocabs.sql",
packageName = "Achilles",
dbms = connectionDetails$dbms,
warnOnMissingParameters = FALSE,
cdm_database_schema = cdmDatabaseSchema,
results_database_schema = resultsDatabaseSchema,
country = country,
provenance = provenance
)

dbSourceVocabs <- DatabaseConnector::querySql(conn, sql)

sql <-
SqlRender::loadRenderTranslateSql(
sqlFilename = "summary/dbVisitDist.sql",
packageName = "Achilles",
dbms = connectionDetails$dbms,
warnOnMissingParameters = FALSE,
cdm_database_schema = cdmDatabaseSchema,
results_database_schema = resultsDatabaseSchema,
country = country,
provenance = provenance
)

dbVisitDist <- DatabaseConnector::querySql(conn, sql)

DatabaseConnector::dbDisconnect(conn)

# extract columns and pivot
dbInfo <- dbSummary[1,c(1,2,3,4)]
row.names(dbSummary) <- dbSummary$ATTRIBUTE_NAME
df <- dbSummary[,c('ATTRIBUTE_VALUE')]
df_t <- t(df)
colnames(df_t) <- rownames(dbSummary)
dbSummaryFinal <- cbind(dbInfo, df_t)

colnames(dbSummaryFinal)[1:4] <- c("Data Source Name", "Data Source Abbreviation", "Source Country", "Data Provenance")

return(list(summary=dbSummaryFinal, visitDist=dbVisitDist, sourceVocabs = dbSourceVocabs))
}
23 changes: 21 additions & 2 deletions extras/GenerateDatabaseSummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ options(connectionObserver = NULL)

demoCountry <- "United States"
demoProvenance <- "Synthetic"
connectionDetails <- Eunomia::getEunomiaConnectionDetails()

dbSummary <- generateDbSummary(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema,demoCountry,demoProvenance)
cdmDatabaseSchema <- "main"
resultsDatabaseSchema <- "main"

cdmVersion <- "5.3"

Achilles::achilles(
cdmVersion = cdmVersion,
connectionDetails = connectionDetails,
cdmDatabaseSchema = cdmDatabaseSchema,
resultsDatabaseSchema = cdmDatabaseSchema,
smallCellCount = 0,
createTable = TRUE,
createIndices = FALSE,
sqlOnly = FALSE
)

dbSummary <- Achilles::generateDbSummary(connectionDetails, cdmDatabaseSchema, resultsDatabaseSchema,demoCountry,demoProvenance)

tableOutput <- dbSummary$summary
tableOutput$"Source Vocabularies" <- paste(dbSummary$sourceVocabs$VOCABULARY_ID, collapse="<br>")
tableOutput$"Visits" <- paste(dbSummary$visitDist$CONCEPT_NAME, collapse="<br>")


# this will open results in the RStudio Viewer which can then be exported to image or html.
kable(tableOutput,escape=F) %>% kableExtra::kable_styling()
kbl(tableOutput,escape=F) %>% kableExtra::kable_styling()

56 changes: 56 additions & 0 deletions man/generateDbSummary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading