CohortManifest R6 Class
CohortManifest R6 Class
Details
An R6 class that manages a collection of CohortDef objects and maintains metadata in a SQLite database.
The CohortManifest class manages multiple cohort definitions and stores their metadata in a SQLite database located at inputs/cohorts/cohortManifest.sqlite. Each CohortDef is assigned a sequential ID based on its position in the manifest.
This is the detection phase of the ATLAS maintenance workflow. Use this to identify
which ATLAS cohorts have changed, then optionally call updateAtlasCohorts() to
apply updates. Changes are detected by comparing expression JSON hashes.
This method updates ATLAS cohorts that have changed in the remote repository. It:
Fetches current definitions from ATLAS
Updates JSON files on disk
Recomputes and stores hashes
Updates the manifest database
Use checkAtlasChanges() first to identify which cohorts have changed, then call this method
to apply updates.
Requires that executionSettings has been set and includes:
A database connection (via getConnection()
workDatabaseSchema for the target schema
cohortTable with the desired table name
Requires that executionSettings has been set and includes:
A database connection (via getConnection()
workDatabaseSchema for the target schema
cohortTable with the desired table name
tempEmulationSchema if needed for the database platform
Use createAllCohortTables() instead. This alias is kept for
backward compatibility.
Requires that executionSettings has been set and includes:
A database connection (via getConnection()
workDatabaseSchema for the target schema
cohortTable with the desired table name
Requires that executionSettings has been set with a valid database connection,
workDatabaseSchema, and cohortTable.
Execution flow:
Build dependency graph from all CohortDef objects
Validate no circular dependencies (error if found)
Topologically sort cohorts by dependencies (parents before children)
For each cohort in topological order:
circe cohorts: check SQL hash (existing logic)
dependent cohorts: compute dependency hash from parent hashes + rule
Render and execute SQL (circe uses SqlRender parameters, dependent uses metadata JSON)
Record checksums and dependency hashes in database
Report results with cohort_type, depends_on, dependency_status columns
Requires that executionSettings has been set and includes:
A database connection (via getConnection()
cdmDatabaseSchema (where the OMOP CDM data resides)
workDatabaseSchema (where cohort results are written)
cohortTable (destination table name)
tempEmulationSchema if needed for the database platform
Methods
Method new()
Initialize a new CohortManifest
Usage
CohortManifest$new(
dbPath = "inputs/cohorts/cohortManifest.sqlite",
projectRoot = NULL
)Arguments
dbPathCharacter. Path to the SQLite database. Defaults to "inputs/cohorts/cohortManifest.sqlite"
projectRootCharacter or NULL. Study repository root against which stored file paths are resolved. When
NULL(default) the root is discovered once viafindStudyProjectRoot()from the manifest's directory and cached for the life of the object. Supply an explicit path for tests or unusual layouts. Get the manifest as a list of CohortDef objects
Method getManifestHash()
Produces a single SHA256 string over the definition of every active or
stale cohort, used by the study pipeline (via shouldRerunTask()) to
decide whether cohort changes force affected tasks to rerun.
For each cohort, ordered by id, the hash combines:
id,cohort_type,source_typedepends_onanddependency_rule(normalized JSON)the rendered-SQL hash (
CohortDef$getSqlHash()) of the loaded cohort, or the sentinel"<missing>"when the cohort file is absent from disk
The rendered SQL — not the raw file bytes — is what executes against the
CDM, so a cosmetic reformat of a cohort's JSON that renders to identical
SQL does not move the hash, while any change to the executed SQL does.
Cosmetic metadata (label, category, tags) is
deliberately excluded: renaming or retagging a cohort does not change the
analysis.
Method reviewDependentCohorts()
Returns a summary tibble of all active derived cohorts (union, subset, complement, composite, oprior, tprior, censor, custom_derived) with parsed dependency information sourced directly from SQLite. Useful for quickly auditing what each derived cohort depends on and how it was built.
Returns
A tibble with columns:
id- Cohort IDlabel- Cohort labelcohort_type- One of 'union', 'subset', 'complement', 'composite', 'oprior', 'tprior', 'censor', 'custom_derived'parent_cohorts- Human-readable parent list, e.g. "Label A (1), Label B (2)"rule_summary- Compact summary of the dependency rule parametersstatus- 'active', or 'stale' when the cohort awaits regenerationcreated_at- Timestamp of creation
Method tabulateManifest()
Tabulate the cohort manifest
Arguments
filterCharacter. One of "active", "deleted", "stale", or "all". Defaults to "active".
"active"returns every cohort registered in the study — both'active'and'stale'rows.'stale'is a freshness marker (the definition changed since it was last generated in the database), not a lifecycle state: a stale cohort is still a full member of the study and is regenerated by the nextgenerateCohorts()run. Usefilter = "stale"to see just those, or read thestatuscolumn of the returned tibble.tags_formatCharacter. One of "nested", "json", or "wide".
"nested" (default): Parse JSON tags into a nested tibble with tag_name/tag_value columns
"json": Keep tags as raw JSON string
"wide": Expand tags into individual columns (one per unique tag key)
Method viewManifest()
View the cohort manifest in RStudio viewer
Opens an interactive RStudio viewer showing key cohort metadata: id, label, category, tags, status, and file_path. This is a convenience function for exploring manifest contents without console clutter.
Usage
CohortManifest$viewManifest(
filter = c("active", "deleted", "stale", "all"),
tagDelimiter = " | "
)Arguments
filterCharacter. One of "active", "deleted", "stale", or "all". Defaults to "active", which shows every registered cohort — including ones marked
'stale'(pending regeneration). SeetabulateManifest()for the full filter semantics.tagDelimitera character used to seperate tags in the view. Default is |
Method reviewStaleCohorts()
Returns a summary of all cohorts currently marked 'stale' — meaning the
cohort's own definition, or that of a parent it is built on, has changed since it
was last executed. Stale cohorts are still fully registered members of the study
(they appear in tabulateManifest() and the query methods) and are still
valid SQL; they just need to be re-executed. executeCohortGeneration()
will run them automatically regardless of checksum state, and clear the flag.
Use resetCohortManifest(scope = "derived") followed by re-running your build
script if you need to change build parameters rather than just re-execute.
Method reloadFromDb()
Re-reads all active cohort records from SQLite and rebuilds the in-memory
list of CohortDef objects. Useful after external changes to the database
(e.g., after resetCohortManifest(scope = "derived")).
Method getAtlasConnection()
Method setAtlasConnection()
Arguments
atlasConnectionAn ATLAS connection object (from
getAtlasConnection()).
Method addAtlasCohort()
Add a single cohort from ATLAS
Fetches a cohort JSON from ATLAS, saves it to json/, and registers
the cohort in the manifest.
Usage
CohortManifest$addAtlasCohort(
atlasId,
label,
category,
tags = list(),
atlasConnection = NULL,
stopIfExists = TRUE
)Arguments
atlasIdInteger. The ATLAS cohort definition ID.
labelCharacter. Display name for the cohort.
categoryCharacter. Required classification (e.g., 'target', 'outcome').
tagsNamed list. Optional metadata tags.
atlasConnectionAn ATLAS connection object (e.g., from ROhdsiWebApi::createConnectionDetails) with a method
getCohortDefinition(cohortId)that returns a list with anexpressionelement. IfNULL, falls back to the connection stored via$setAtlasConnection().stopIfExistsLogical. If TRUE (default), raises an error when an active cohort with this label is already registered. If FALSE, fetches the current definition from ATLAS and updates the registered cohort in place — same ID and file path, hash refreshed,
category/tags/atlasId replaced, derived dependents marked 'stale'. An unchanged definition leaves the file untouched. Default: TRUE (fail-safe).
Method importAtlasCohorts()
Batch-import cohorts from ATLAS via a cohortsLoad dataframe
Either create a dataframe or read in a csv file with columns atlasId, label, category (required) plus any
additional columns treated as tag key-value pairs for tags. Calls addAtlasCohort() for each row.
By default, the load file is treated as a transient, one-time import
mechanism: rows whose atlasId is already registered in the manifest are
an error, not an update. Set stopIfExists = FALSE to instead update
those rows in place (delegates to addAtlasCohort(stopIfExists = FALSE)
for each), which supports iterating on the load file across repeated
runs. To sync registered cohorts with ATLAS without a load file, use
updateAtlasCohorts().
Arguments
cohortsLoada data frame requiring the columns atlasId, label and category used to bulk add cohorts to the manifest
atlasConnectionAn ATLAS connection object with a
getCohortDefinition(cohortId)method. IfNULL, falls back to the connection stored via$setAtlasConnection().stopIfExistsLogical. If TRUE (default), raises an error when any load row's atlasId is already registered in the manifest. If FALSE, those rows are updated in place instead (same ID/file path, hash refreshed, category/tags replaced) via
addAtlasCohort(stopIfExists = FALSE). Default: TRUE (fail-safe).
Method addCaprCohort()
Add a Capr cohort
Takes a Capr Cohort object, exports it to JSON in json/, and registers
the cohort in the manifest.
Usage
CohortManifest$addCaprCohort(
caprCohort,
label,
category,
tags = list(),
stopIfExists = TRUE
)Arguments
caprCohortA Capr Cohort object (inherits from "Cohort").
labelCharacter. Display name for the cohort.
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
stopIfExistsLogical. If TRUE (default), raises an error when an active cohort with this label is already registered. If FALSE, updates the existing cohort in place via
updateCaprCohort()— the cohort keeps its ID and file path, andcategory/tagsreplace the registered metadata (previous tags are dropped if none are supplied). Default: TRUE (fail-safe).
Method updateCaprCohort()
Update an existing Capr cohort's JSON definition
Takes a revised Capr Cohort object and upserts it over a cohort already
registered via addCaprCohort(): the JSON file recorded in the manifest
is overwritten in place and the manifest hash is refreshed, so the cohort
keeps its ID and file path. Any derived cohorts that depend on it are
marked 'stale'. If the new definition is identical to the registered one,
nothing is changed.
Method addSqlCohort()
Add a custom SQL cohort
Registers an existing SQL file in the manifest. The file must already exist
on disk (typically in sql/).
Usage
CohortManifest$addSqlCohort(
filePath,
label,
category,
tags = list(),
stopIfExists = TRUE
)Arguments
filePathCharacter. Path to the SQL file.
labelCharacter. Display name for the cohort.
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
stopIfExistsLogical. If TRUE (default), raises an error when an active cohort with this label is already registered, or the file path is registered to another cohort. If FALSE, updates the registered cohort in place — same ID, file path registration and content hash refreshed,
category/tagsreplace the registered metadata (previous tags are dropped if none are supplied), and derived dependents are marked 'stale' when the definition changed. Default: TRUE (fail-safe).
Method addDependentCustomCohort()
Add a dependent custom SQL cohort
Registers a SQL file in the manifest as a dependency-aware derived cohort. The source SQL file must preserve the standard Picard cohort write contract using \.code@target_database_schema.@target_cohort_table and \.code@target_cohort_id.
dependentCohortIdList and sqlParameters values are rendered into the
source SQL immediately (via \.codeSqlRender::render()) and the result is
written to \.codeinputs/cohorts/derived/
Arguments
filePathCharacter. Path to the source SQL file/template.
labelCharacter. Display name for the cohort.
categoryCharacter. Required classification.
dependentCohortIdListNamed list. Each name is a SqlRender parameter to render into the SQL file. Each value is, preferably, a manifest entry — a data.frame/tibble with an \.codeid column, as returned by query methods like \.codequeryCohortsByLabel() (a single row bakes in one ID; a multi-row table bakes in a comma-separated vector, for \.codeIN (@param) clauses) — or, for backward compatibility, a raw integer cohort ID (or integer vector). These IDs also become this cohort's \.codedepends_on parents for staleness tracking. When entries carry a \.codelabel column, it's included (purely for QC) in a generated comment header at the top of the derived SQL file. Example: \.codelist(inc_cohort_id = ckdEntry, exc_cohort_id = t2dEntry).
sqlParametersNamed list. Optional additional SqlRender parameters to render into the SQL file (any type — thresholds, dates, strings, etc.), not treated as cohort dependencies. Example: \.codelist(min_days = 30L, index_year = 2020L).
tagsNamed list. Optional metadata tags.
stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered cohort in place (same ID): the dependent cohort IDs/sqlParameters are re-rendered into the generated file, and — only if that actually changes the definition — the cohort is marked 'stale' for regeneration along with its own dependents. Re-running with unchanged inputs leaves the cohort exactly as it was. Default: TRUE (fail-safe).
Method addCirceCohort()
Add a Circe JSON cohort from disk
Registers an existing Circe-compatible JSON file in the manifest.
The file must already exist on disk (typically in json/).
Validates that the JSON is valid Circe format using CirceR.
Usage
CohortManifest$addCirceCohort(filePath, label, category, tags = list())Method buildUnionCohort()
Build a union cohort from existing cohorts
Creates a derived cohort that is the union of specified parent cohorts. Delegates SQL generation to the internal builder function.
Input route policy:
Preferred: provide
cohortEntries(manifest query rows withid)Backward compatible: provide
cohortIdsExactly one route must be provided; passing both or neither is an error.
Usage
CohortManifest$buildUnionCohort(
label,
category,
tags = list(),
cohortIds = NULL,
cohortEntries = NULL,
gapDays = 0L,
eraPadDays = 0L,
minEraDays = 0L,
minCohorts = 1L,
washoutDays = 0L,
firstEraOnly = FALSE,
stopIfExists = TRUE
)Arguments
labelCharacter. Display name for the derived cohort.
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
cohortIdsNumeric vector (minimum 2). Legacy ID route to union cohorts. Supported for backward compatibility and emits a migration warning unless
options(picard.suppressIdRouteWarning = TRUE)is set.cohortEntriesData frame/tibble with an
idcolumn (minimum 2 rows). Preferred route using manifest query results.gapDaysInteger. Bridge eras separated by up to this many days. Default: 0 (only overlapping periods collapse).
eraPadDaysInteger. Expand each source period by this many days on each end before collapsing. Applied to individual periods, not the collapsed result. Default: 0.
minEraDaysInteger. Drop collapsed eras shorter than this many days. Default: 0 (keep all eras).
minCohortsInteger. Only include subjects appearing in at least this many distinct source cohorts. Default: 1 (any subject from any cohort).
washoutDaysInteger. Require a clean period of at least this many days before a new era can open. Subjects must have no source cohort membership for this period. Default: 0.
firstEraOnlyLogical. Return only the first collapsed era per subject. Default: FALSE.
stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
Method buildSubsetCohortTemporal()
Build a subset cohort with temporal criteria
Creates a derived cohort that subsets a base cohort using temporal relationship to a filter cohort.
Input route policy:
Preferred: provide
baseCohortEntry/filterCohortEntryBackward compatible: provide
baseCohortId/filterCohortIdExactly one route per role must be provided; both/neither is an error.
Usage
CohortManifest$buildSubsetCohortTemporal(
label,
category,
tags = list(),
baseCohortId = NULL,
filterCohortId = NULL,
baseCohortEntry = NULL,
filterCohortEntry = NULL,
startWindow,
endWindow = NULL,
endDateType = "base",
subsetLimit = "First",
stopIfExists = TRUE
)Arguments
labelCharacter. Display name.
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
baseCohortIdInteger. Legacy ID route for the base cohort.
filterCohortIdInteger. Legacy ID route for the filter cohort.
baseCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the base cohort.filterCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the filter cohort.startWindowSubsetWindowOperator object. Defines the temporal window for the subset cohort start date relative to the filter cohort event.
endWindowSubsetWindowOperator object (optional, NULL allowed). Defines the temporal window for the subset cohort end date relative to the filter cohort event. If NULL, the filter cohort end date is not used.
endDateTypeCharacter. Whether to use the base cohort end date ('base') or filter cohort end date ('filter') as the cohort end date in the output subset cohort. Default: 'base'.
subsetLimitCharacter. One of 'First', 'Last', or 'All'. Specifies which qualifying filter cohort event(s) to retain per subject. 'First' keeps the earliest event, 'Last' keeps the most recent event, 'All' keeps all qualifying events. Default: 'First'.
stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
Method buildComplementCohort()
Build a complement cohort
Creates a derived cohort containing all subjects from the population cohort who do NOT appear in any (or all) of the exclude cohorts.
Input route policy:
Preferred: provide
populationCohortEntry/excludeCohortEntriesBackward compatible: provide
populationCohortId/excludeCohortIdsExactly one route per role must be provided; both/neither is an error.
Usage
CohortManifest$buildComplementCohort(
label,
category,
tags = list(),
populationCohortId = NULL,
excludeCohortIds = NULL,
populationCohortEntry = NULL,
excludeCohortEntries = NULL,
complementType = "exclude_any",
stopIfExists = TRUE
)Arguments
labelCharacter. Display name.
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
populationCohortIdInteger. Legacy ID route for the population (base) cohort.
excludeCohortIdsInteger vector (min length 1). Legacy ID route for cohorts whose subjects should be excluded from the population.
populationCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the population cohort.excludeCohortEntriesData frame/tibble with an
idcolumn (minimum 1 row). Preferred route using manifest query results for exclusion cohorts.complementTypeCharacter. One of
"exclude_any"(default) or"exclude_all"."exclude_any"removes subjects present in ANY exclude cohort;"exclude_all"removes subjects only if they appear in ALL exclude cohorts.stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
Method buildCompositeCohort()
Build a composite cohort
Creates a derived cohort that requires membership in multiple cohorts (intersection logic).
Input route policy:
Preferred: provide
criteriaCohortEntriesBackward compatible: provide
criteriaCohortIdsExactly one route must be provided; passing both or neither is an error.
Usage
CohortManifest$buildCompositeCohort(
label,
category,
tags = list(),
criteriaCohortIds = NULL,
criteriaCohortEntries = NULL,
eventSelection = "First",
minEventCount = 1L,
stopIfExists = TRUE
)Arguments
labelCharacter. Display name.
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
criteriaCohortIdsInteger vector. Legacy ID route for cohorts to include in the composite (e.g., c(1, 2, 3) for Type 1 diabetes, Type 2 diabetes, and secondary diabetes).
criteriaCohortEntriesData frame/tibble with an
idcolumn (minimum 2 rows). Preferred route using manifest query results.eventSelectionCharacter. One of 'First', 'Last', or 'All'. Specifies which event(s) to retain as the cohort_start_date and cohort_end_date in the output:
'First': Keep the earliest event (earliest index date)
'Last': Keep the most recent event
'All': Keep all qualifying events per subject (may result in multiple rows per subject) Default: 'First'.
minEventCountInteger. Minimum number of distinct cohort events required for a subject to qualify for the composite. Default: 1 (any subject with at least 1 event qualifies).
stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
Method buildDemographicCohort()
Build a demographic subset cohort
Creates a derived cohort that subsets a base cohort by filtering on person-level demographic attributes (age, gender, race, ethnicity).
Input route policy:
Preferred: provide
baseCohortEntryBackward compatible: provide
baseCohortIdExactly one route must be provided; passing both or neither is an error.
Usage
CohortManifest$buildDemographicCohort(
label,
baseCohortId = NULL,
baseCohortEntry = NULL,
category,
minAge = NULL,
maxAge = NULL,
genderConceptIds = NULL,
raceConceptIds = NULL,
ethnicityConceptIds = NULL,
tags = list(),
stopIfExists = TRUE
)Arguments
labelCharacter. Display name (e.g., "CKD - Males 40-75").
baseCohortIdInteger. Legacy ID route for the base cohort.
baseCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results.categoryCharacter. Required classification.
minAgeInteger or NULL. Minimum age at cohort start. Default: NULL (no minimum).
maxAgeInteger or NULL. Maximum age at cohort start. Default: NULL (no maximum).
genderConceptIdsInteger vector or NULL. Gender concept IDs to include. Common values: 8507 = Male, 8532 = Female. Default: NULL (all genders).
raceConceptIdsInteger vector or NULL. Race concept IDs to include. Default: NULL.
ethnicityConceptIdsInteger vector or NULL. Ethnicity concept IDs to include. Default: NULL.
tagsNamed list. Optional metadata tags.
stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
Method buildStratifiedCohorts()
Split a base cohort into stratified sub-cohorts
Splits a single base cohort into N named stratum cohorts plus an automatic
Unclassified cohort containing subjects that match none of the named
strata. Each stratum is registered as a separate manifest entry with
cohort_type = "subset".
Input route policy:
Preferred: provide
baseCohortEntryBackward compatible: provide
baseCohortIdExactly one route must be provided; passing both or neither is an error.
Usage
CohortManifest$buildStratifiedCohorts(
baseCohortId = NULL,
baseCohortEntry = NULL,
strata,
labelPrefix = NULL,
category = "derived",
tags = list()
)Arguments
baseCohortIdInteger. Legacy ID route for the cohort definition ID to split.
baseCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results.strataNamed list. Each element is either a named list of demographic filters (keys:
genderConceptIds,raceConceptIds,ethnicityConceptIds,minAge,maxAge) or a character string SQL WHERE condition referencingbc(cohort table) andp(person table). Names become cohort labels.labelPrefixCharacter or NULL. If provided, prepended to each stratum name with a
" - "separator.categoryCharacter. Category applied to every stratum cohort. Default:
"derived".tagsNamed list. Optional metadata tags applied to every stratum cohort.
Method queryCohortsByIds()
Usage
CohortManifest$queryCohortsByIds(
ids,
tags_format = c("nested", "json", "wide")
)Method queryCohortsByTag()
Arguments
tagStringsCharacter vector. One or more tags in the format "name: value" (e.g., "category: primary"). When multiple tags are supplied, the
matchargument controls whether a cohort must satisfy any or all of them.matchCharacter. "any" (default) returns cohorts matching at least one tag; "all" returns only cohorts matching every tag.
tags_formatCharacter. One of "nested", "json", or "wide".
"nested" (default): Tags as nested tibble with tag_name/tag_value columns
"json": Tags as raw JSON string
"wide": Tags expanded into individual columns
Method queryCohortsByLabel()
Arguments
labelsCharacter vector. One or more labels to search for. A cohort is included when it matches at least one of the supplied labels (OR logic).
matchTypeCharacter. Either "exact" for exact match or "pattern" for pattern matching. Defaults to "exact".
tags_formatCharacter. One of "nested", "json", or "wide".
"nested" (default): Tags as nested tibble with tag_name/tag_value columns
"json": Tags as raw JSON string
"wide": Tags expanded into individual columns
Method queryCohortsByCategory()
Arguments
categoryCharacter vector. One or more category to search for. A cohort is included when it matches at least one of the supplied category (OR logic).
matchTypeCharacter. Either "exact" for exact match or "pattern" for pattern matching. Defaults to "exact".
tags_formatCharacter. One of "nested", "json", or "wide".
"nested" (default): Tags as nested tibble with tag_name/tag_value columns
"json": Tags as raw JSON string
"wide": Tags expanded into individual columns
Method queryCohortsByTagName()
Usage
CohortManifest$queryCohortsByTagName(
tagName,
tags_format = c("nested", "json", "wide")
)Method queryCohortsMissingTag()
Query cohorts missing a specific tag
Usage
CohortManifest$queryCohortsMissingTag(
tagName,
tags_format = c("nested", "json", "wide")
)Method queryCohortsWithTagValues()
Query cohorts by tag value mapping
Usage
CohortManifest$queryCohortsWithTagValues(
tagValueMapping,
tags_format = c("nested", "json", "wide")
)Arguments
tagValueMappingNamed list. Keys are tag names, values are tag values to match. Example:
list(status = "approved", type = "primary")requires both conditions (AND logic).tags_formatCharacter. One of "nested", "json", or "wide".
"nested" (default): Tags as nested tibble with tag_name/tag_value columns
"json": Tags as raw JSON string
"wide": Tags expanded into individual columns
Method getCohortsByTag()
Usage
CohortManifest$getCohortsByTag(tagStrings, match = c("any", "all"))Arguments
tagStringsCharacter vector. One or more tags in the format "name: value" (e.g., "category: primary"). When multiple tags are supplied, the
matchargument controls whether a cohort must satisfy any or all of them.matchCharacter. "any" (default) returns cohorts matching at least one tag; "all" returns only cohorts matching every tag.
Method getCohortsByLabel()
Usage
CohortManifest$getCohortsByLabel(labels, matchType = c("exact", "pattern"))Method updateCohortLabel()
Update a cohort label
Method updateCohortCategory()
Update a cohort category
Method updateCohortTags()
Update cohort tags
Method removeCohortTag()
Remove a specific tag from a cohort
Method modifyCohortTagValue()
Modify the value of an existing tag
Method addCohortTag()
Add a single tag to a cohort (non-destructive)
Method mergeTagsIntoCohort()
Merge multiple tags into a cohort (non-destructive, additive)
Method getTagValue()
Get value of a single tag for a cohort
Method renameTagKey()
Rename a tag key across specified cohorts (or all cohorts)
Method bulkModifyTagValue()
Bulk modify a tag value across cohorts matching an old value
Method checkAtlasCohorts()
Auto-detect changes to ATLAS cohorts in remote repository
Queries the manifest for all active ATLAS cohorts (identified by atlasId in tags),
fetches their current definitions from ATLAS, computes hashes, and compares against
the stored local hash. Provides a read-only summary of which cohorts have changed
in ATLAS since import. No modifications are made.
Method updateAtlasCohorts()
Update ATLAS cohorts with remote definitions
Fetches current definitions from ATLAS for specified cohorts and updates the stored JSON files and manifest entries. This is the modification phase that applies changes detected by checkAtlasChanges().
Method statusReport()
Generate a status report for the manifest
Prints a summary table showing all registered cohorts with their
dependencies and source types. Cohorts marked 'stale' (pending
regeneration) are included — the status column tells them apart.
Useful for auditing the manifest structure.
Method print()
Print a friendly view of the CohortManifest
Displays key metadata about the manifest and its contents. Check cohort tables in the database
Method createCohortTable()
Creates one required cohort table by type using the current execution settings.
Method createAllCohortTables()
Creates the necessary cohort tables in the target database using the execution settings.
First checks if tables already exist before attempting creation.
This is an advanced/manual setup function. Most users should run
executeCohortGeneration() and let it create missing tables automatically.
Method dropCohortTables()
Drops cohort tables from the target database. Can drop all standard cohort tables or specific tables. This is useful for cleaning up or resetting the cohort generation environment.
Method syncManifest()
Scans the json/ and sql/ subdirectories of the cohorts folder, reconciles
them against the SQLite manifest, and updates both the database and the in-memory list:
Active manifest records whose file no longer exists are soft-deleted, along with their derived dependents (which cannot generate without the parent).
Existing files whose SQL hash has changed are updated in the manifest.
Orphaned files on disk not in manifest are automatically deleted.
Only the json/ and sql/ source directories are scanned — derived cohorts
managed via build*() methods are not touched.
Stored file paths are resolved against the study repository root before
any disk comparison, and hashes are computed from file contents only, so a
row is reported as "hash_updated" only when the file's contents
actually changed — never because its stored path used an older convention.
To rewrite legacy stored paths to the current repo-root-relative form, run
normalizeCohortManifestPaths() once; syncManifest() does not
do this.
Method cleanCohortTable()
For every cohort with status = 'deleted' in the SQLite manifest, deletes
the corresponding rows from the DBMS cohort table and checksum table, then marks
the manifest record as status = 'purged' so it is not processed again.
Method executeCohortGeneration()
Generates cohorts in the manifest in the target database using the execution settings. Checks dependency ordering and regenerates dependent cohorts when parents change. Checks the hash of each cohort definition and skips generation if the hash matches what's already stored in the cohort_checksum table. If hashes differ or the cohort is not yet in the checksum table, regenerates and updates the hash.
Arguments
confirmLogical. If TRUE and interactive, asks for confirmation before creating missing tables. If FALSE, missing tables are created without prompting. In non-interactive sessions, missing tables are not created automatically.
Returns
Data frame with execution results including:
cohort_id: ID of the generated cohort
label: Label of the cohort
cohort_type: 'circe', 'subset', 'union', or 'complement'
depends_on: Comma-separated parent cohort IDs (empty for circe cohorts)
execution_time_min: Time taken to generate (0 for skipped)
status: 'Success', 'Skipped - already generated', 'Dependency skipped', or error message
dependency_status: 'Not applicable' for circe, 'Parent changed' or 'Unchanged' for dependent
Method retrieveCohortCounts()
Retrieve cohort counts from the database
Retrieves entry and subject counts for cohorts from the cohort table in the target database. Can retrieve counts for all cohorts or a specific subset. Enriches the results with metadata (label and tags) from the CohortDef objects in the manifest.
Method deleteCohort()
Delete a cohort from manifest and file system
Marks a cohort as deleted in the manifest and removes its file from the file system (json/ or sql/ directory). The SQLite record is preserved with status='deleted' for audit trail purposes.
When a manifest is loaded, only active cohorts are loaded into memory. This enforces strict 1:1 correspondence between active manifest entries and files on disk.
Arguments
idInteger. The cohort ID to delete.
confirmLogical. If FALSE (default), prompts for interactive confirmation. Pass TRUE to skip the prompt (suitable for scripts).
dropFromDBMSLogical. If TRUE, also deletes the cohort from the DBMS cohort table and checksum table. Requires
executionSettingsto be set. Default: FALSE (filesystem/manifest cleanup only).cascadeLogical. If FALSE (default), deletion is refused when the cohort has derived dependents (deleting it would orphan them and break cohort generation). If TRUE, all transitive dependents are deleted too, deepest-first. Default: FALSE (fail-safe).
Method buildOPriorT()
Clean up missing cohorts from manifest
Build a cohort of outcome events with prior target exposure
Creates a derived cohort based on the temporal relationship between an outcome cohort and a target (exposure) cohort. Filters outcome events that have (or lack) a prior target event, optionally within a time window.
Input route policy:
Preferred: provide
outcomeCohortEntry/targetCohortEntryBackward compatible: provide
outcomeCohortId/targetCohortIdExactly one route per role must be provided; both/neither is an error.
Usage
CohortManifest$buildOPriorT(
label,
category,
tags = list(),
outcomeCohortId = NULL,
targetCohortId = NULL,
mode = "prior",
priorTimeWindowDays = NULL,
subsetLimit = "First",
outcomeCohortEntry = NULL,
targetCohortEntry = NULL,
stopIfExists = TRUE
)Arguments
labelCharacter. Display name (e.g., "GI Bleed - Prior NSAID").
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
outcomeCohortIdInteger. Legacy ID route for the outcome cohort definition ID (e.g., GI bleed).
targetCohortIdInteger. Legacy ID route for the target cohort definition ID (e.g., NSAID use).
modeCharacter. One of 'prior' or 'no_prior':
'prior': Retain outcome events where a prior target event exists.
'no_prior': Retain outcome events where no prior target event exists. Default: 'prior'.
priorTimeWindowDaysInteger or NULL. If provided (e.g., 365), only consider target events within this many days before the outcome start. NULL or 0 means all time. Default: NULL.
subsetLimitCharacter. One of 'First', 'Last', or 'All'. Controls which prior target event anchors the match when multiple exist:
'First': Keep the earliest prior target event (default).
'Last': Keep the most recent prior target event.
'All': Keep all prior target events (one output row per pair). Default: 'First'.
outcomeCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the outcome cohort.targetCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the target cohort.stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
keep_traceLogical. If TRUE, marks missing as deleted with timestamp (soft delete). If FALSE, permanently removes from database (hard delete). Defaults to TRUE.
Method buildTPriorO()
Build a cohort of target events with prior outcome occurrence
Creates a derived cohort based on the temporal relationship between a target (exposure) cohort and an outcome cohort. Filters target events that have (or lack) a prior outcome event, optionally within a time window.
This is the reverse direction of buildOPriorT(): instead of
filtering outcome by prior target, filter target by prior outcome.
Input route policy:
Preferred: provide
targetCohortEntry/outcomeCohortEntryBackward compatible: provide
targetCohortId/outcomeCohortIdExactly one route per role must be provided; both/neither is an error.
Usage
CohortManifest$buildTPriorO(
label,
category,
tags = list(),
targetCohortId = NULL,
outcomeCohortId = NULL,
mode = "prior",
priorTimeWindowDays = NULL,
subsetLimit = "First",
targetCohortEntry = NULL,
outcomeCohortEntry = NULL,
stopIfExists = TRUE
)Arguments
labelCharacter. Display name (e.g., "NSAID - Prior GI Bleed").
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
targetCohortIdInteger. Legacy ID route for the target cohort definition ID (e.g., NSAID use).
outcomeCohortIdInteger. Legacy ID route for the outcome cohort definition ID (e.g., GI bleed).
modeCharacter. One of 'prior' or 'no_prior':
'prior': Retain target events where a prior outcome exists.
'no_prior': Retain target events where no prior outcome exists. Default: 'prior'.
priorTimeWindowDaysInteger or NULL. If provided (e.g., 365), only consider outcome events within this many days before the target start. NULL or 0 means all time. Default: NULL.
subsetLimitCharacter. One of 'First', 'Last', or 'All'. Controls which prior outcome event anchors the match when multiple exist:
'First': Keep the earliest prior outcome event (default).
'Last': Keep the most recent prior outcome event.
'All': Keep all prior outcome events (one output row per pair). Default: 'First'.
targetCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the target cohort.outcomeCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the outcome cohort.stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).
Method buildCensorCohort()
Censor a target cohort based on a censoring event
Truncates the cohort_end_date of each target cohort record to the earliest censoring event that occurs between the cohort_start_date and cohort_end_date. If no censoring event occurs, the original cohort_end_date is preserved.
Typical use cases:
Censor a drug exposure cohort at the date of death
Censor a disease cohort at the date of disease exacerbation
Censor a treatment cohort at the date of a procedure (e.g., surgery)
Input route policy:
Preferred: provide
targetCohortEntry/censorCohortEntryBackward compatible: provide
targetCohortId/censorCohortIdExactly one route per role must be provided; both/neither is an error.
Usage
CohortManifest$buildCensorCohort(
label,
category,
tags = list(),
targetCohortId = NULL,
censorCohortId = NULL,
targetCohortEntry = NULL,
censorCohortEntry = NULL,
stopIfExists = TRUE
)Arguments
labelCharacter. Display name (e.g., "NSAID Use - Censored at Death").
categoryCharacter. Required classification.
tagsNamed list. Optional metadata tags.
targetCohortIdInteger. Legacy ID route for the cohort definition ID to censor.
censorCohortIdInteger. Legacy ID route for the cohort definition ID for the censoring event.
targetCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the target cohort.censorCohortEntryData frame/tibble with one row and an
idcolumn. Preferred route using manifest query results for the censor cohort.stopIfExistsLogical. If TRUE (default), raises an error when an active or stale cohort with this label is already registered. If FALSE, updates the registered derived cohort in place (same ID and file path): the SQL is re-rendered, parents and build parameters are replaced, the cohort is marked 'stale' for regeneration (and its own dependents with it) only when the definition actually changed — re-running with unchanged inputs is a no-op. Default: TRUE (fail-safe).