diff --git a/DESCRIPTION b/DESCRIPTION index f58acf2..a3a11b8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ROMOPAPI Title: ROMOPAPI -Version: 2.1.1 +Version: 2.2.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 5bb60c4..1f299cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,18 +22,18 @@ 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=3 +ARG BUILD_CACHE_BUSTER=5 # Install renv and restore packages RUN --mount=type=secret,id=build_github_pat \ cp /usr/local/lib/R/etc/Renviron /tmp/Renviron \ && echo "GITHUB_PAT=$(cat /run/secrets/build_github_pat)" >> /usr/local/lib/R/etc/Renviron \ && Rscript -e 'install.packages("remotes")' \ - && Rscript -e 'remotes::install_github("FINNGEN/ROMOPAPI")' \ + && Rscript -e 'remotes::install_github("FINNGEN/ROMOPAPI@'$ROMOPAPI_BRANCH'")' \ && cp /tmp/Renviron /usr/local/lib/R/etc/Renviron; # Expose the port that the API will run on -EXPOSE 8585 +EXPOSE 8564 # Run the API server -CMD ["Rscript", "-e", "ROMOPAPI::runApiServer(host = '0.0.0.0', port = 8585)"] \ No newline at end of file +CMD ["Rscript", "-e", "ROMOPAPI::runApiServer(host = '0.0.0.0', port = 8564)"] \ No newline at end of file diff --git a/NEWS.md b/NEWS.md index 5c940c6..b5df3e2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# ROMOPAPI +- Added number_of_descendants to concepts with code counts +- Added conceptId 21600744 to testing data + # ROMOPAPI 2.1.1 - Added caching concepts with code counts at startup diff --git a/R/getCodeCounts.R b/R/getCodeCounts.R index 9b478f7..0a8f34d 100644 --- a/R/getCodeCounts.R +++ b/R/getCodeCounts.R @@ -236,7 +236,8 @@ getCodeCounts <- function( dplyr::rename(node_record_counts = record_counts, node_descendant_record_counts = descendant_record_counts) # - Get concept details - conceptsWithCodeCounts <- getConceptsWithCodeCounts_memoise(CDMdbHandler, codeCountsTable = codeCountsTable) + conceptsWithCodeCounts <- getConceptsWithCodeCounts_memoise(CDMdbHandler, codeCountsTable = codeCountsTable) |> + dplyr::select(-number_of_descendants) concepts <- familyTreeWithMappings |> dplyr::distinct(child_concept_id) |> dplyr::rename(concept_id = child_concept_id) |> diff --git a/R/getConceptsWithCodeCounts.R b/R/getConceptsWithCodeCounts.R index dc5fbb9..34b38c3 100644 --- a/R/getConceptsWithCodeCounts.R +++ b/R/getConceptsWithCodeCounts.R @@ -47,7 +47,7 @@ getConceptsWithCodeCounts <- function( SELECT DISTINCT c.concept_id, c.concept_name, c.domain_id, c.vocabulary_id, c.concept_class_id, c.standard_concept, c.concept_code, - cc.record_counts, cc.descendant_record_counts + cc.record_counts, cc.descendant_record_counts, cc.number_of_descendants FROM @vocabularyDatabaseSchema.concept c INNER JOIN @resultsDatabaseSchema.@codeCountsTable cc ON c.concept_id = cc.concept_id;" diff --git a/R/runApiServer.R b/R/runApiServer.R index 74c78bf..93293b6 100644 --- a/R/runApiServer.R +++ b/R/runApiServer.R @@ -8,7 +8,7 @@ #' @param cohortTableHandlerConfig Configuration for connecting to the OMOP CDM database. #' If NULL, uses test Eunomia database #' @param host Host address to run the API server on. Defaults to "127.0.0.1" -#' @param port Port number to run the API server on. Defaults to 8585 +#' @param port Port number to run the API server on. Defaults to 8564 #' @param buildCountsTable Logical indicating whether to build code counts tables. Defaults to FALSE #' @param ... Additional arguments passed to plumber::pr_run() #' @@ -33,7 +33,7 @@ runApiServer <- function( cohortTableHandlerConfig = NULL, host = "127.0.0.1", - port = 8585, + port = 8564, buildCountsTable = FALSE, ...) { # diff --git a/README.md b/README.md index 8612a99..c432df5 100644 --- a/README.md +++ b/README.md @@ -26,19 +26,19 @@ library(ROMOPAPI) runApiServer() ``` -This will start the API server on port 8585. +This will start the API server on port 8564. #### Testing endpoints -The main endpoint for testing is: http://127.0.0.1:8585/report?conceptId= +The main endpoint for testing is: http://127.0.0.1:8564/report?conceptId= -For example, http://127.0.0.1:8585/report?conceptId=317009 +For example, http://127.0.0.1:8564/report?conceptId=317009 This will return an HTML report for the concept id 317009. #### Production endpoints -The main endpoint for production is: http://127.0.0.1:8585/getCodeCounts?conceptId= +The main endpoint for production is: http://127.0.0.1:8564/getCodeCounts?conceptId= This will return the code counts for the concept ids 317009. @@ -49,7 +49,7 @@ Separated in 3 tables: - `stratified_code_counts`: patient counts by conceptId stratified by gender, year, and age decile -See the API documentation for more details: http://127.0.0.1:8585/__docs__/ +See the API documentation for more details: http://127.0.0.1:8564/__docs__/ ### Running the API Server with a custom database @@ -143,7 +143,7 @@ Based on the example output, getCodeCounts returns a list with 3 components: Existing docker image is available at: https://hub.docker.com/repository/docker/javiergrata/romopapi/ ``` -docker run -p 8585:8585 javiergrata/romopapi +docker run -p 8564:8564 javiergrata/romopapi ``` ## Build the docker image @@ -163,7 +163,7 @@ docker build --secret id=build_github_pat,src=GITHUBPAT.txt --build-arg ROMOPAPI ## Run the docker container ``` -docker run -p 8585:8585 romopapi +docker run -p 8564:8564 romopapi ``` diff --git a/inst/plumber/plumber.R b/inst/plumber/plumber.R index 124bd26..2db77da 100644 --- a/inst/plumber/plumber.R +++ b/inst/plumber/plumber.R @@ -58,7 +58,7 @@ function() { function() { concepts <- getConceptsWithCodeCounts_memoise(CDMdbHandler = CDMdbHandler) concepts <- concepts |> - dplyr::select(concept_id, concept_name, vocabulary_id, concept_code) + dplyr::select(concept_id, concept_name, vocabulary_id, concept_code, number_of_descendants) return(concepts) } diff --git a/inst/sql/sql_server/createCodeCountsTable.sql b/inst/sql/sql_server/createCodeCountsTable.sql index 2af48d6..d1d7845 100644 --- a/inst/sql/sql_server/createCodeCountsTable.sql +++ b/inst/sql/sql_server/createCodeCountsTable.sql @@ -3,7 +3,8 @@ DROP TABLE IF EXISTS @resultsDatabaseSchema.@codeCountsTable; CREATE TABLE @resultsDatabaseSchema.@codeCountsTable ( concept_id int, record_counts int, - descendant_record_counts int + descendant_record_counts int, + number_of_descendants int ); INSERT INTO @resultsDatabaseSchema.@codeCountsTable @@ -57,7 +58,8 @@ descendant_counts AS ( SELECT ca.ancestor_concept_id AS concept_id, COALESCE(cc.record_counts, 0) AS record_counts, - SUM(COALESCE(cctosum.record_counts, 0)) AS descendant_record_counts + SUM(COALESCE(cctosum.record_counts, 0)) AS descendant_record_counts, + COUNT(*) AS number_of_descendants FROM temp_concept_ancestor ca INNER JOIN @@ -77,6 +79,7 @@ descendant_counts AS ( SELECT CAST(ccd.concept_id AS BIGINT) AS concept_id, CAST(ccd.record_counts AS BIGINT) AS record_counts, - CAST(ccd.descendant_record_counts AS BIGINT) AS descendant_record_counts + CAST(ccd.descendant_record_counts AS BIGINT) AS descendant_record_counts, + CAST(ccd.number_of_descendants AS BIGINT) AS number_of_descendants FROM descendant_counts ccd; \ No newline at end of file diff --git a/inst/testdata/data/FinnGenR13_countsOnly.sqlite b/inst/testdata/data/FinnGenR13_countsOnly.sqlite index e9dc96d..6dff077 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 fe59290..6761449 100644 --- a/inst/testdata/data/createTestingData.R +++ b/inst/testdata/data/createTestingData.R @@ -9,10 +9,13 @@ conceptIds <- c( 45596282, # ICD10: Asthma 21601855, # ATC level 4: C10AA (Statins) 320136, # Big graph, parent of Asthma snomed concept (Disorders of the respiratory system) - 4024567# biger + 4024567,# biger + 21600744 # bug in plot ) CDMdbHandler <- HadesExtras_createCDMdbHandlerFromList(test_cohortTableHandlerConfig, loadConnectionChecksLevel = "basicChecks") +# uncomment to create code counts tables +createCodeCountsTables(CDMdbHandler) helper_createSqliteDatabaseFromDatabase( CDMdbHandler, conceptIds = conceptIds, diff --git a/man/runApiServer.Rd b/man/runApiServer.Rd index 5bc9734..4e14fb1 100644 --- a/man/runApiServer.Rd +++ b/man/runApiServer.Rd @@ -7,7 +7,7 @@ runApiServer( cohortTableHandlerConfig = NULL, host = "127.0.0.1", - port = 8585, + port = 8564, buildCountsTable = FALSE, ... ) @@ -18,7 +18,7 @@ If NULL, uses test Eunomia database} \item{host}{Host address to run the API server on. Defaults to "127.0.0.1"} -\item{port}{Port number to run the API server on. Defaults to 8585} +\item{port}{Port number to run the API server on. Defaults to 8564} \item{buildCountsTable}{Logical indicating whether to build code counts tables. Defaults to FALSE} diff --git a/tests/testmanual/manualtest-runApiServer.R b/tests/testmanual/manualtest-runApiServer.R index 7c69921..5a91564 100644 --- a/tests/testmanual/manualtest-runApiServer.R +++ b/tests/testmanual/manualtest-runApiServer.R @@ -7,4 +7,4 @@ ROMOPAPI::runApiServer( buildCountsTable = FALSE ) -# http://127.0.0.1:8585/__docs__/ +# http://127.0.0.1:8564/__docs__/ diff --git a/tests/testthat/test-createCodeCountsTable.R b/tests/testthat/test-createCodeCountsTable.R index 17b9b2d..18af03c 100644 --- a/tests/testthat/test-createCodeCountsTable.R +++ b/tests/testthat/test-createCodeCountsTable.R @@ -113,7 +113,8 @@ test_that("createCodeCountsTables works", { expect_equal(c( "concept_id", "record_counts", - "descendant_record_counts" + "descendant_record_counts", + "number_of_descendants" )) # check that descendant_record_counts is greater than or equal to record_counts @@ -129,4 +130,27 @@ test_that("createCodeCountsTables works", { dplyr::count() |> dplyr::pull(n) |> expect_equal(code_counts |> dplyr::count() |> dplyr::pull(n)) + + # check that number_of_descendants is greater than or equal to 1 + code_counts |> + dplyr::filter(number_of_descendants < 1) |> + dplyr::count() |> + dplyr::pull(n) |> + expect_equal(0) + + # check that all with record_counts = descendant_record_counts have number_of_descendants = 1 + code_counts |> + dplyr::filter(record_counts == descendant_record_counts) |> + dplyr::filter(number_of_descendants != 1) |> + dplyr::count() |> + dplyr::pull(n) |> + expect_equal(0) + + # check that all with number_of_descendants > 1 have record_counts > descendant_record_counts + code_counts |> + dplyr::filter(number_of_descendants > 1) |> + dplyr::filter(record_counts > descendant_record_counts) |> + dplyr::count() |> + dplyr::pull(n) |> + expect_equal(0) }) diff --git a/tests/testthat/test-getConceptsWithCodeCounts.R b/tests/testthat/test-getConceptsWithCodeCounts.R index c968959..072ae9e 100644 --- a/tests/testthat/test-getConceptsWithCodeCounts.R +++ b/tests/testthat/test-getConceptsWithCodeCounts.R @@ -18,7 +18,7 @@ test_that("getConceptsWithCodeCounts works", { # Check column names match expected structure expected_columns <- c("concept_id", "concept_name", "domain_id", "vocabulary_id", "concept_class_id", "standard_concept", "concept_code", - "record_counts", "descendant_record_counts") + "record_counts", "descendant_record_counts", "number_of_descendants") result |> colnames() |>