Skip to content

Commit

Permalink
Merge pull request #428 from OHDSI/codelist-intersect
Browse files Browse the repository at this point in the history
Add intersect codes to `cohort_codelist` attribute
  • Loading branch information
edward-burn authored Jan 20, 2025
2 parents 2a00285 + cc46088 commit 7d2ff8c
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 119 deletions.
12 changes: 11 additions & 1 deletion R/requireCohortIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,21 @@ requireCohortIntersect <- function(cohort,
reason <- glue::glue("{reason}, censoring at {censorDate}")
}

# codelist
targetCodelist <- attr(cdm[[targetCohortTable]], "cohort_codelist") |>
dplyr::filter(.data$cohort_definition_id %in% .env$targetCohortId) |>
dplyr::collect()
newCodelist <- getIntersectionCodelist(
cohort, cohortId, targetCodelist
)

# add additional columns
x <- cohort |>
dplyr::inner_join(subsetCohort, by = c(cols)) |>
dplyr::compute(name = name, temporary = FALSE) |>
omopgenerics::newCohortTable(.softValidation = TRUE) |>
omopgenerics::newCohortTable(
.softValidation = TRUE, cohortCodelistRef = newCodelist
) |>
omopgenerics::recordCohortAttrition(reason = reason, cohortId = cohortId)

omopgenerics::dropTable(cdm = cdm, name = subsetName)
Expand Down
26 changes: 25 additions & 1 deletion R/requireConceptIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,18 @@ requireConceptIntersect <- function(cohort,
reason <- glue::glue("{reason}, censoring at {censorDate}")
}

#codelist
newCodelist <- getIntersectionCodelist(
cohort, cohortId, conceptSetToCohortCodelist(conceptSet)
)

# cohort
cohort <- cohort |>
dplyr::inner_join(subsetCohort, by = c(cols)) |>
dplyr::compute(name = name, temporary = FALSE) |>
omopgenerics::newCohortTable(.softValidation = TRUE) |>
omopgenerics::newCohortTable(
.softValidation = TRUE, cohortCodelistRef = newCodelist
) |>
omopgenerics::recordCohortAttrition(reason = reason, cohortId = cohortId)

omopgenerics::dropTable(cdm = cdm, name = subsetName)
Expand All @@ -151,3 +159,19 @@ requireConceptIntersect <- function(cohort,

return(cohort)
}

getIntersectionCodelist <- function(cohort, cohortId, codelist) {
criteria <- "inclusion criteria"
intersectCodelist <- lapply(
as.list(cohortId),
function(x, tab = codelist) {
tab |> dplyr::mutate(cohort_definition_id = .env$x)
}) |>
dplyr::bind_rows() |>
dplyr::mutate(type = .env$criteria)
newCodelist <- attr(cohort, "cohort_codelist") |>
dplyr::collect() |>
dplyr::union(intersectCodelist) |>
dplyr::arrange(.data$cohort_definition_id)
return(newCodelist)
}
Loading

0 comments on commit 7d2ff8c

Please sign in to comment.