Skip to content

Commit

Permalink
Merge pull request #316 from OHDSI/gap_intersect
Browse files Browse the repository at this point in the history
apply gap in intersect
  • Loading branch information
edward-burn authored Sep 9, 2024
2 parents 1a3eb7d + 4d8135d commit 49793f7
Show file tree
Hide file tree
Showing 2 changed files with 435 additions and 325 deletions.
37 changes: 24 additions & 13 deletions R/intersectCohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ intersectCohorts <- function(cohort,

# get intersections between cohorts
tblName <- omopgenerics::uniqueTableName(prefix = uniquePrefix)
lowerWindow <- ifelse(gap != 0, -gap, gap)
cohortOut <- cohort %>%
dplyr::filter(.data$cohort_definition_id %in% .env$cohortId) %>%
dplyr::select(-"cohort_definition_id") %>%
splitOverlap(by = "subject_id", name = tblName, tmp = paste0(tblName)) |>
PatientProfiles::addCohortIntersectFlag(
targetCohortTable = omopgenerics::tableName(cohort),
targetCohortId = cohortId,
window = c(0, 0),
window = c(lowerWindow, gap),
nameStyle = "{cohort_name}",
name = tblName
)
Expand Down Expand Up @@ -131,11 +132,11 @@ intersectCohorts <- function(cohort,
dplyr::inner_join(cdm[[setName]], by = cohortNames) %>%
dplyr::select("cohort_definition_id", "subject_id",
"cohort_start_date", "cohort_end_date") %>%
dplyr::compute(name = name, temporary = FALSE)
dplyr::compute(name = tblName, temporary = FALSE)
if (cohortOut |> dplyr::tally() |> dplyr::pull("n") > 0) {
cohortOut <- cohortOut %>%
dplyr::compute(name = name, temporary = FALSE) |>
joinOverlap(name = name, gap = gap)
dplyr::compute(name = tblName, temporary = FALSE) |>
joinOverlap(name = tblName, gap = gap)
}

# attributes
Expand Down Expand Up @@ -191,19 +192,29 @@ intersectCohorts <- function(cohort,
)))

# intersect cohort
cdm[[name]] <- omopgenerics::newCohortTable(
table = cohortOut,
cohortSetRef = cohSet,
cohortAttritionRef = intersectAttrition,
cohortCodelistRef = intersectCodelist,
.softValidation = FALSE
)

if (keepOriginalCohorts) {
cdm <- bind(cdm[[name]], originalCohorts, name = name)
cohortOut <- omopgenerics::newCohortTable(
table = cohortOut,
cohortSetRef = cohSet,
cohortAttritionRef = intersectAttrition,
cohortCodelistRef = intersectCodelist,
.softValidation = FALSE
)
cdm <- bind(originalCohorts, cohortOut, name = name)
} else {
cohortOut <- cohortOut %>%
dplyr::compute(name = name, temporary = FALSE)
cdm[[name]] <- omopgenerics::newCohortTable(
table = cohortOut,
cohortSetRef = cohSet,
cohortAttritionRef = intersectAttrition,
cohortCodelistRef = intersectCodelist,
.softValidation = FALSE
)
}

CDMConnector::dropTable(cdm, name = dplyr::starts_with(uniquePrefix))
CDMConnector::dropTable(cdm, name = tblName)

return(cdm[[name]])
}
Expand Down
Loading

0 comments on commit 49793f7

Please sign in to comment.