## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## -----------------------------------------------------------------------------
exampleStudies <- c("lc500", "lc500s")
exampleRoot <- system.file("example", "st", package = "CohortContrast")

data.frame(
  study = exampleStudies,
  mode = vapply(
    unname(file.path(exampleRoot, exampleStudies)),
    function(path) CohortContrast::checkDataMode(path)$mode,
    character(1)
  ),
  row.names = NULL
)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# library(CohortContrast)
# library(CDMConnector)
# library(DBI)
# library(RPostgres)
# library(dplyr)
# library(tibble)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Initiate the database connection
# #
# #################################################################################
# 
# port <- as.integer(Sys.getenv("DB_PORT"))
# 
# cdmSchema <-
#   Sys.getenv("OHDSI_CDM") # Schema which contains the OHDSI Common Data Model
# cdmVocabSchema <-
#   Sys.getenv("OHDSI_VOCAB") # Schema which contains the OHDSI Common Data Model vocabulary tables.
# cdmResultsSchema <-
#   Sys.getenv("OHDSI_RESULTS") # Schema which contains "cohort" table (is not mandatory)
# writeSchema <-
#   Sys.getenv("OHDSI_WRITE") # Schema for temporary tables, will be deleted
# writePrefix <- "cc_"
# 
# db = DBI::dbConnect(
#   RPostgres::Postgres(),
#   dbname = Sys.getenv("DB_NAME"),
#   host = Sys.getenv("DB_HOST"),
#   user = Sys.getenv("DB_USERNAME"),
#   password = Sys.getenv("DB_PASSWORD"),
#   port  = port
# )
# 
# cdm <- CDMConnector::cdmFromCon(
#   con = db,
#   cdmSchema = cdmSchema,
#   achillesSchema = cdmResultsSchema,
#   writeSchema = c(schema = writeSchema, prefix = writePrefix)
# )

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# configurePython()
# installPythonDeps()
# checkPythonDeps()

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Create target table from OMOP CDM instance (ATLAS's cohort id)
# #
# #################################################################################
# 
# cohortsTableName = 'cohort'
# targetCohortId = 1403
# 
# targetTable <- CohortContrast::cohortFromCohortTable(cdm = cdm, db = db,
#    tableName = cohortsTableName, schemaName = cdmResultsSchema, cohortId = targetCohortId)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Create target table from a JSON
# #
# #################################################################################
# pathToJSON = '/path/to/atlas_json'
# targetTable <- CohortContrast::cohortFromJSON(pathToJSON = pathToJSON, cdm = cdm)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# # Example bundled with CohortContrast:
# exampleJsonDir <- system.file("example/example_json/lung", package = "CohortContrast")
# list.files(exampleJsonDir, pattern = ".json$", full.names = TRUE)
# # [1] ".../example_json/lung/cohort.json"
# 
# targetTable <- CohortContrast::cohortFromJSON(pathToJSON = exampleJsonDir, cdm = cdm)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Create target table from a CSV
# #
# #################################################################################
# pathToCsv = '/path/to/cohort.csv'
# targetTable <- CohortContrast::cohortFromCSV(pathToCsv = pathToCsv, cohortId = 2)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Create target table
# #
# #################################################################################
# # Create a cohort table with a 1-year follow-up window after cohort start
# data <- tibble::tribble(
#   ~cohort_definition_id, ~subject_id, ~cohort_start_date,
#   1, 4804, as.Date('1997-03-23'),
#   1, 4861, as.Date('1982-06-02'),
#   1, 1563, as.Date('1977-06-25'),
#   1, 2830, as.Date('2006-08-11'),
#   1, 1655, as.Date('2004-09-29'),
#   2, 5325, as.Date('1982-06-02'),
#   2, 3743, as.Date('1997-03-23'),
#   2, 2980, as.Date('2004-09-29'),
#   2, 1512, as.Date('2006-08-11'),
#   2, 2168, as.Date('1977-06-25')
# ) |>
#   dplyr::mutate(cohort_end_date = cohort_start_date + 365)
# 
# targetTable <- CohortContrast::cohortFromDataTable(data = data, cohortId = 2)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Create control cohort table based on matches
# #
# #################################################################################
# controlTable = CohortContrast::createControlCohortMatching(cdm = cdm, targetTable = targetTable, ratio = 4)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Create control cohort table based on inverse controls
# #
# #################################################################################
# controlTable = CohortContrast::createControlCohortInverse(cdm = cdm, targetTable = targetTable)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# ################################################################################
# #
# # Resolve conflicts
# #
# #################################################################################
# targetTable = CohortContrast::resolveCohortTableOverlaps(cdm = cdm, cohortTable = targetTable)
# controlTable = CohortContrast::resolveCohortTableOverlaps(cdm = cdm, cohortTable = controlTable)

## ----include = TRUE, eval=FALSE, echo=TRUE------------------------------------
# # Close DB connection when done
# DBI::dbDisconnect(db)

