diff --git a/DESCRIPTION b/DESCRIPTION index feaa693..c92ed89 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ROMOPAPI Title: ROMOPAPI -Version: 2.0.0 +Version: 2.1.0 Authors@R: person("Javier", "Gracia-Tabuenca", , "javier.graciatabuenca@tuni.fi", role = c("aut", "cre"), comment = c(ORCID = "YOUR-ORCID-ID")) diff --git a/Dockerfile b/Dockerfile index bf76595..e6b2e4e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,7 @@ RUN apt-get update && apt-get install -y openjdk-8-jdk liblzma-dev libbz2-dev li # Install renv and restore packages ARG ROMOPAPI_BRANCH=main -ARG BUILD_CACHE_BUSTER=1 +ARG BUILD_CACHE_BUSTER=2 # Install renv and restore packages RUN --mount=type=secret,id=build_github_pat \ diff --git a/NAMESPACE b/NAMESPACE index 636fce7..2540e8c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,7 +13,7 @@ export(createReport) export(createStratifiedCodeCountsTable) export(create_api) export(fct_setUpLogger) -export(getCDMSource) +export(getAPIInfo) export(getCodeCounts) export(getCodeCounts_memoise) export(getConceptsWithCodeCounts) diff --git a/NEWS.md b/NEWS.md index 18e958f..285f144 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# ROMOPAPI 2.1.0 +- Added getAPIInfo endpoint to API +- Updated db with large tree +- Fixed error in getCodeCounts when conceptId was the root of the tree + # ROMOPAPI 2.0.0 - Updated to use DatabaseConnector v7 diff --git a/R/getCDMSource.R b/R/getAPIInfo.R similarity index 65% rename from R/getCDMSource.R rename to R/getAPIInfo.R index 41c9d8a..0895696 100644 --- a/R/getCDMSource.R +++ b/R/getAPIInfo.R @@ -1,10 +1,9 @@ - -#' Get the CDM source information +#' Get the API information #' #' @description #' Retrieves the CDM source information from the vocabulary schema of an OMOP CDM database. -#' This includes metadata about the database source, version, and other administrative details. -#' +#' Retrieves the API information from the vocabulary schema of an OMOP CDM database. +#' This includes metadata about the API, version, and other administrative details. #' @param CDMdbHandler A CDMdbHandler object that contains database connection details #' #' @return A tibble containing the CDM source information with columns from the cdm_source table @@ -16,9 +15,9 @@ #' #' @export #' -getCDMSource <- function( +getAPIInfo <- function( CDMdbHandler) { - ParallelLogger::logInfo("getCDMSource: Getting CDM source information") + ParallelLogger::logInfo("getAPIInfo: Getting API information") # # VALIDATE # @@ -39,5 +38,12 @@ getCDMSource <- function( vocabularyDatabaseSchema = vocabularyDatabaseSchema ) |> tibble::as_tibble() - return(cdm_source) + info <- list( + cdm_source_name = cdm_source$cdm_source_name, + cdm_source_abbreviation = cdm_source$cdm_source_abbreviation, + vocabulary_version = cdm_source$vocabulary_version, + romop_api_version = as.character(utils::packageVersion("ROMOPAPI")) + ) + + return(info) } diff --git a/R/getCodeCounts.R b/R/getCodeCounts.R index 0687b56..9b478f7 100644 --- a/R/getCodeCounts.R +++ b/R/getCodeCounts.R @@ -106,7 +106,9 @@ getCodeCounts <- function( ) -- take only parents who are someones children, or are the parents of the concept id SELECT * FROM temp_tree tt - WHERE parent_concept_id IN (SELECT DISTINCT child_concept_id FROM temp_tree ) OR child_concept_id IN (@conceptId) + WHERE parent_concept_id IN (SELECT DISTINCT child_concept_id FROM temp_tree ) OR + child_concept_id IN (@conceptId) OR + parent_concept_id IN (@conceptId) -- when concept is top in tree " # Gets tree of descendants and the code counts for each descendant @@ -173,7 +175,9 @@ getCodeCounts <- function( dplyr::distinct(concept_id, maps_to_concept_id) |> dplyr::rename(maps_to_concept_id = concept_id, concept_id = maps_to_concept_id) |> dplyr::mutate(levels = "Maps to") - ) + ) |> + # If concept maps to itself, bcs concept in concept and source concept columns, dont take it + dplyr::filter(concept_id != maps_to_concept_id) familyTreeWithMappings <- dplyr::bind_rows( familyTreeWithInfo, @@ -194,7 +198,9 @@ getCodeCounts <- function( codeCounts |> dplyr::select(-maps_to_concept_id) |> dplyr::group_by(concept_id, calendar_year, gender_concept_id, age_decile) |> dplyr::summarise(record_counts = sum(record_counts), .groups = "drop") - ) + ) |> + # If concept maps to itself, bcs concept in concept and source concept columns, dont take it + dplyr::distinct() familyTreeDescendants <- familyTreeWithMappings |> dplyr::filter(!levels %in% c("Mapped from", "Maps to", "-1", "0")) |> diff --git a/R/helper.R b/R/helper.R index 45257d9..585b296 100644 --- a/R/helper.R +++ b/R/helper.R @@ -237,6 +237,23 @@ helper_createSqliteDatabaseFromDatabase <- function( tempTable = FALSE, ) + # get CDMsource + sql <- "SELECT DISTINCT c.* FROM @vocabularyDatabaseSchema.cdm_source c" + cdmSource <- DatabaseConnector::renderTranslateQuerySql( + connection = sourceConnection, + sql = sql, + vocabularyDatabaseSchema = sourceVocabularyDatabaseSchema + ) |> + tibble::as_tibble() + + targetConnection |> DatabaseConnector::insertTable( + tableName = "cdm_source", + data = cdmSource, + dropTableIfExists = TRUE, + createTable = TRUE, + tempTable = FALSE, + ) + # disconnect targetConnection |> DatabaseConnector::disconnect() sourceConnection |> DatabaseConnector::disconnect() diff --git a/inst/plumber/plumber.R b/inst/plumber/plumber.R index d624f72..124bd26 100644 --- a/inst/plumber/plumber.R +++ b/inst/plumber/plumber.R @@ -45,10 +45,10 @@ function(res, conceptId=0L) { } -#* Get the CDM source information -#* @get /getCDMSource +#* Get the API information +#* @get /getAPIInfo function() { - getCDMSource( + getAPIInfo( CDMdbHandler = CDMdbHandler ) } diff --git a/inst/testdata/data/FinnGenR13_countsOnly.sqlite b/inst/testdata/data/FinnGenR13_countsOnly.sqlite index da24204..e9dc96d 100644 Binary files a/inst/testdata/data/FinnGenR13_countsOnly.sqlite and b/inst/testdata/data/FinnGenR13_countsOnly.sqlite differ diff --git a/inst/testdata/data/createTestingData.R b/inst/testdata/data/createTestingData.R index 6161cc1..fe59290 100644 --- a/inst/testdata/data/createTestingData.R +++ b/inst/testdata/data/createTestingData.R @@ -8,8 +8,8 @@ conceptIds <- c( 317009, # Snomed: Asthma 45596282, # ICD10: Asthma 21601855, # ATC level 4: C10AA (Statins) - #2010001615, # test endpoint - 320136 # Big graph, parent of Asthma snomed concept (Disorders of the respiratory system) + 320136, # Big graph, parent of Asthma snomed concept (Disorders of the respiratory system) + 4024567# biger ) CDMdbHandler <- HadesExtras_createCDMdbHandlerFromList(test_cohortTableHandlerConfig, loadConnectionChecksLevel = "basicChecks") @@ -25,7 +25,7 @@ connection <- DatabaseConnector::connect(DatabaseConnector::createConnectionDet DatabaseConnector::dbListTables(connection) |> sort() |> - expect_equal(c("code_counts", "concept", "concept_ancestor", "stratified_code_counts")) + expect_equal(c("cdm_source", "code_counts", "concept", "concept_ancestor", "stratified_code_counts")) dplyr::tbl(connection, "concept") |> dplyr::count() |> @@ -47,3 +47,8 @@ dplyr::tbl(connection, "stratified_code_counts") |> dplyr::pull(n) |> expect_gt(0) +dplyr::tbl(connection, "cdm_source") |> + dplyr::count() |> + dplyr::pull(n) |> + expect_gt(0) + diff --git a/man/getCDMSource.Rd b/man/getAPIInfo.Rd similarity index 53% rename from man/getCDMSource.Rd rename to man/getAPIInfo.Rd index 62f7bed..b6f6527 100644 --- a/man/getCDMSource.Rd +++ b/man/getAPIInfo.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/getCDMSource.R -\name{getCDMSource} -\alias{getCDMSource} -\title{Get the CDM source information} +% Please edit documentation in R/getAPIInfo.R +\name{getAPIInfo} +\alias{getAPIInfo} +\title{Get the API information} \usage{ -getCDMSource(CDMdbHandler) +getAPIInfo(CDMdbHandler) } \arguments{ \item{CDMdbHandler}{A CDMdbHandler object that contains database connection details} @@ -14,5 +14,6 @@ A tibble containing the CDM source information with columns from the cdm_source } \description{ Retrieves the CDM source information from the vocabulary schema of an OMOP CDM database. -This includes metadata about the database source, version, and other administrative details. +Retrieves the API information from the vocabulary schema of an OMOP CDM database. +This includes metadata about the API, version, and other administrative details. } diff --git a/tests/testmanual/manualtest-timeplotview.R b/tests/testmanual/manualtest-timeplotview.R index f5b2c92..1e3ad14 100644 --- a/tests/testmanual/manualtest-timeplotview.R +++ b/tests/testmanual/manualtest-timeplotview.R @@ -17,7 +17,7 @@ conceptId <- 782748 conceptId <- 21602735 -conceptId <- 2010001615 # test endpoint +conceptId <- 2010001618 # test endpoint results <- getCodeCounts( diff --git a/tests/testthat/test-getAPIInfo.R b/tests/testthat/test-getAPIInfo.R new file mode 100644 index 0000000..198e833 --- /dev/null +++ b/tests/testthat/test-getAPIInfo.R @@ -0,0 +1,20 @@ +# create test for getCDMSource + +test_that("getAPIInfo works", { + # only works in a full CDM databas + + CDMdbHandler <- HadesExtras_createCDMdbHandlerFromList(test_cohortTableHandlerConfig, loadConnectionChecksLevel = "basicChecks") + withr::defer({ + CDMdbHandler <- NULL + gc() + }) + + api_info <- getAPIInfo(CDMdbHandler) + + # Check column names + api_info |> names() |> + expect_equal(c("cdm_source_name", "cdm_source_abbreviation", "vocabulary_version", "romop_api_version")) + api_info$romop_api_version |> + expect_equal(as.character(utils::packageVersion("ROMOPAPI"))) + +}) diff --git a/tests/testthat/test-getCDMSource.R b/tests/testthat/test-getCDMSource.R deleted file mode 100644 index c093855..0000000 --- a/tests/testthat/test-getCDMSource.R +++ /dev/null @@ -1,20 +0,0 @@ -# create test for getCDMSource - -test_that("getCDMSource works", { - # only works in a full CDM database - skip_if(testingDatabase == "OnlyCounts-FinnGen") - - CDMdbHandler <- HadesExtras_createCDMdbHandlerFromList(test_cohortTableHandlerConfig, loadConnectionChecksLevel = "basicChecks") - withr::defer({ - CDMdbHandler <- NULL - gc() - }) - - cdm_source <- getCDMSource(CDMdbHandler) - - # Check column names - cdm_source |> - nrow() |> - expect_equal(1) - -})