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

how to add the cohort into the cdm #98

Closed
tiozab opened this issue Jul 18, 2024 · 5 comments
Closed

how to add the cohort into the cdm #98

tiozab opened this issue Jul 18, 2024 · 5 comments

Comments

@tiozab
Copy link

tiozab commented Jul 18, 2024

using the example from the article creating a cohort

metforminConceptSet <- cs(descendants(1503297), name = "metformin")
t1dConceptSet <- cs(descendants(195771), name = "T1D")

metforminCohort <- cohort(
entry = entry(
# metformin drug query as index event
drugExposure(metforminConceptSet, firstOccurrence()),
observationWindow = continuousObservation(priorDays = -365, postDays = 365L),
primaryCriteriaLimit = "All"
),
attrition = attrition(
'noT1d' = withAll(
exactly(
x = 0,
# query for t1d occurrence to exclude patients
query = conditionOccurrence(t1dConceptSet),
aperture = duringInterval(
startWindow = eventStarts(a = -Inf, b = 0, index = "startDate")
)
)
)
),
exit = exit(
endStrategy = observationExit(),
censor = censoringEvents(
#exit based on observence of t1d condition
# query for t1d occurrence for censor
conditionOccurrence(t1dConceptSet)
)
)
)

however, how to add it to the cdm?

This does not work:
cdm <- generateCohortSet(cdm, metforminCohort, "metforminCohort", overwrite = TRUE)

Error in generateCohortSet():
! cohortSet must be a dataframe or a named list of Capr cohort definitions
Run rlang::last_trace() to see where the error occurred.

@mdlavallee92
Copy link
Collaborator

Maybe you need to put list() around your metforminCohort. This has to do with the interface to the other package running generateCohortSet, I am guessing that is a DARWIN package. I am not familiar with this tool. Perhaps @ablack3 can add clarity?

@tiozab
Copy link
Author

tiozab commented Jul 18, 2024

anxiety_cohort <- list(cohort(entry = entry( drugExposure(anxiety), primaryCriteriaLimit = "All"),exit = exit(fixedExit("startDate", 0L))))
class(anxiety_cohort)
[1] "list"
cdm <- generateCohortSet(cdm, anxiety_cohort, "anxiety_cohort", overwrite = TRUE)
Error in generateCohortSet(cdm, anxiety_cohort, "anxiety_cohort", overwrite = TRUE) :
Assertion on 'cohortSet' failed: Must have names.

@ablack3
Copy link
Collaborator

ablack3 commented Jul 19, 2024

I had to remove the functionality to pass capr cohort directly into generateCohortSet in the most recent version of CDMConnector because CRAN was complaining that Capr wasn't on CRAN. I'm not sure why it was a problem this time but it seems much easier to keep a package on CRAN if all the packages in the Description file are also on CRAN.

For now this functionality is no longer working but you can work around it by writing the cohort to a json file and then reading it back in. Here is a reprex. This might be better anyway because you have the json file as a record of the cohort definition which you can save in Atlas.

library(CDMConnector)
library(Capr)
#> 
#> Attaching package: 'Capr'
#> The following object is masked from 'package:CDMConnector':
#> 
#>     attrition
con <- DBI::dbConnect(duckdb::duckdb(), eunomia_dir())
cdm <- cdm_from_con(con, "main", "main")

# create a capr cohort

vaccine_concept_set <- cs(1127433L, name = "vaccine") %>% 
  getConceptSetDetails(con, vocabularyDatabaseSchema = "main")


vaccine_concept_set
#> ── <Capr Concept Set> vaccine ──────────────────────────────────────────────────
#> # A tibble: 1 × 9
#>   conceptId conceptCode conceptName        domainId vocabularyId standardConcept
#>       <int> <chr>       <chr>              <chr>    <chr>        <chr>          
#> 1   1127433 313782      Acetaminophen 325… Drug     RxNorm       S              
#> # ℹ 3 more variables: includeDescendants <lgl>, isExcluded <lgl>,
#> #   includeMapped <lgl>
# create the cohort and save it to a folder as a json file
vaccineCohort <- cohort(entry = entry(drugExposure(vaccine_concept_set),
                                      primaryCriteriaLimit = "All"),
                        exit = exit(endStrategy = fixedExit("startDate", 0L)))

folder_with_cohort_definitions <- tempfile()
dir.create(folder_with_cohort_definitions)

Capr::writeCohort(vaccineCohort, file.path(folder_with_cohort_definitions, "vaccineCohort.json"))

list.files(folder_with_cohort_definitions)
#> [1] "vaccineCohort.json"
# read the cohorts in the folder you created
cohort_set <- read_cohort_set(folder_with_cohort_definitions)
                     
cohort_set
#> # A tibble: 1 × 5
#>   cohort_definition_id cohort_name   cohort       json   cohort_name_snakecase
#>                  <int> <chr>         <list>       <list> <chr>                
#> 1                    1 vaccinecohort <named list> <chr>  vaccinecohort
cdm <- generateCohortSet(cdm, cohort_set, "cohort", overwrite = TRUE)
#> ℹ Generating 1 cohort
#> ℹ Generating cohort (1/1) - vaccinecohort
#> ✔ Generating cohort (1/1) - vaccinecohort [77ms]
#> 
cdm$cohort
#> # Source:   table<cohort> [?? x 4]
#> # Database: DuckDB v1.0.0 [root@Darwin 23.0.0:R 4.3.1//private/var/folders/xx/01v98b6546ldnm1rg1_bvk000000gn/T/RtmpZxCAka/filec63712f234f.duckdb]
#>    cohort_definition_id subject_id cohort_start_date cohort_end_date
#>                   <int>      <dbl> <date>            <date>         
#>  1                    1        246 1970-03-05        1970-03-05     
#>  2                    1        526 1973-07-15        1973-07-15     
#>  3                    1       1377 2008-12-02        2008-12-02     
#>  4                    1       1391 1954-01-25        1954-01-25     
#>  5                    1       1435 1967-12-16        1967-12-16     
#>  6                    1       1446 2003-10-17        2003-10-17     
#>  7                    1       1574 1966-07-25        1966-07-25     
#>  8                    1       1702 1967-09-20        1967-09-20     
#>  9                    1       1748 1949-04-23        1949-04-23     
#> 10                    1       2037 1970-07-23        1970-07-23     
#> # ℹ more rows
DBI::dbDisconnect(con, shutdown = T)

sessionInfo()
#> R version 4.3.1 (2023-06-16)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Sonoma 14.0
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: Europe/Amsterdam
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] Capr_2.0.8         CDMConnector_1.3.2
#> 
#> loaded via a namespace (and not attached):
#>  [1] styler_1.10.3           utf8_1.2.4              generics_0.1.3         
#>  [4] tidyr_1.3.1             stringi_1.8.4           hms_1.1.3              
#>  [7] digest_0.6.35           magrittr_2.0.3          evaluate_0.24.0        
#> [10] timechange_0.3.0        fastmap_1.2.0           blob_1.2.4             
#> [13] R.oo_1.26.0             DatabaseConnector_6.3.2 R.cache_0.16.0         
#> [16] jsonlite_1.8.8          R.utils_2.12.3          backports_1.5.0        
#> [19] DBI_1.2.3               purrr_1.0.2             fansi_1.0.6            
#> [22] duckdb_1.0.0            cli_3.6.3               rlang_1.1.4            
#> [25] dbplyr_2.4.0            R.methodsS3_1.8.2       bit64_4.0.5            
#> [28] reprex_2.1.0            withr_3.0.0             yaml_2.3.8             
#> [31] tools_4.3.1             tzdb_0.4.0              checkmate_2.3.1        
#> [34] dplyr_1.1.4             SqlRender_1.18.1        vctrs_0.6.5            
#> [37] R6_2.5.1                lifecycle_1.0.4         lubridate_1.9.3        
#> [40] snakecase_0.11.1        stringr_1.5.1           fs_1.6.4               
#> [43] bit_4.0.5               pkgconfig_2.0.3         rJava_1.0-11           
#> [46] pillar_1.9.0            glue_1.7.0              xfun_0.45              
#> [49] tibble_3.2.1            tidyselect_1.2.1        rstudioapi_0.16.0      
#> [52] knitr_1.47              htmltools_0.5.8.1       rmarkdown_2.27         
#> [55] readr_2.1.5             compiler_4.3.1          CirceR_1.3.3           
#> [58] omopgenerics_0.2.1

Created on 2024-07-19 with reprex v2.1.0

If #50 gets resolved then I can add the Capr tests back into CDMConnector.

@mdlavallee92
Copy link
Collaborator

Now that CirceR is on CRAN it is a much easier lift to get Capr on CRAN. This is in scope as all HADES is suppose to make it to CRAN now. Will tag you when I get to that!

@tiozab
Copy link
Author

tiozab commented Aug 19, 2024

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants