|
| 1 | +#' FHIR-to-OMOP Concept Resolver |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' R6 class providing access to the FHIR-to-OMOP Concept Resolver endpoints. |
| 5 | +#' Translates FHIR coded values (system URI + code) into OMOP standard |
| 6 | +#' concepts, CDM target tables, and optional Phoebe recommendations. |
| 7 | +#' |
| 8 | +#' @details |
| 9 | +#' Access via the `fhir` active binding on an `OMOPHubClient`: |
| 10 | +#' ```r |
| 11 | +#' client <- OMOPHubClient$new(api_key = "oh_xxx") |
| 12 | +#' result <- client$fhir$resolve( |
| 13 | +#' system = "http://snomed.info/sct", |
| 14 | +#' code = "44054006", |
| 15 | +#' resource_type = "Condition" |
| 16 | +#' ) |
| 17 | +#' result$data$resolution$target_table |
| 18 | +#' # "condition_occurrence" |
| 19 | +#' ``` |
| 20 | +#' |
| 21 | +#' @keywords internal |
| 22 | +FhirResource <- R6::R6Class( |
| 23 | + "FhirResource", |
| 24 | + public = list( |
| 25 | + #' @description |
| 26 | + #' Create a new FhirResource. |
| 27 | + #' @param base_req Base httr2 request object. |
| 28 | + initialize = function(base_req) { |
| 29 | + private$.base_req <- base_req |
| 30 | + }, |
| 31 | + |
| 32 | + #' @description |
| 33 | + #' Resolve a single FHIR Coding to an OMOP standard concept. |
| 34 | + #' |
| 35 | + #' Provide at least one of (`system` + `code`), (`vocabulary_id` + `code`), |
| 36 | + #' or `display`. |
| 37 | + #' |
| 38 | + #' @param system FHIR code system URI (e.g. `"http://snomed.info/sct"`). |
| 39 | + #' @param code Code value from the FHIR Coding. |
| 40 | + #' @param display Human-readable text (semantic search fallback). |
| 41 | + #' @param vocabulary_id Direct OMOP vocabulary_id, bypasses URI resolution. |
| 42 | + #' @param resource_type FHIR resource type (e.g. `"Condition"`, `"Observation"`). |
| 43 | + #' @param include_recommendations Logical. Include Phoebe recommendations. Default `FALSE`. |
| 44 | + #' @param recommendations_limit Integer. Max recommendations (1-20). Default `5L`. |
| 45 | + #' @param include_quality Logical. Include mapping quality signal. Default `FALSE`. |
| 46 | + #' |
| 47 | + #' @returns A list with `input` and `resolution` containing source/standard |
| 48 | + #' concepts, target CDM table, and optional enrichments. |
| 49 | + resolve = function(system = NULL, |
| 50 | + code = NULL, |
| 51 | + display = NULL, |
| 52 | + vocabulary_id = NULL, |
| 53 | + resource_type = NULL, |
| 54 | + include_recommendations = FALSE, |
| 55 | + recommendations_limit = 5L, |
| 56 | + include_quality = FALSE) { |
| 57 | + body <- compact_list( |
| 58 | + system = system, |
| 59 | + code = code, |
| 60 | + display = display, |
| 61 | + vocabulary_id = vocabulary_id, |
| 62 | + resource_type = resource_type |
| 63 | + ) |
| 64 | + if (isTRUE(include_recommendations)) { |
| 65 | + body$include_recommendations <- TRUE |
| 66 | + body$recommendations_limit <- as.integer(recommendations_limit) |
| 67 | + } |
| 68 | + if (isTRUE(include_quality)) { |
| 69 | + body$include_quality <- TRUE |
| 70 | + } |
| 71 | + |
| 72 | + perform_post(private$.base_req, "fhir/resolve", body = body) |
| 73 | + }, |
| 74 | + |
| 75 | + #' @description |
| 76 | + #' Batch-resolve up to 100 FHIR Codings. |
| 77 | + #' |
| 78 | + #' Failed items are reported inline without failing the batch. |
| 79 | + #' |
| 80 | + #' @param codings A list of coding lists, each with optional elements |
| 81 | + #' `system`, `code`, `display`, `vocabulary_id`. |
| 82 | + #' @param resource_type FHIR resource type applied to all codings. |
| 83 | + #' @param include_recommendations Logical. Default `FALSE`. |
| 84 | + #' @param recommendations_limit Integer. Default `5L`. |
| 85 | + #' @param include_quality Logical. Default `FALSE`. |
| 86 | + #' |
| 87 | + #' @returns A list with `results` (per-item) and `summary` |
| 88 | + #' (total/resolved/failed). |
| 89 | + resolve_batch = function(codings, |
| 90 | + resource_type = NULL, |
| 91 | + include_recommendations = FALSE, |
| 92 | + recommendations_limit = 5L, |
| 93 | + include_quality = FALSE) { |
| 94 | + stopifnot(is.list(codings), length(codings) >= 1, length(codings) <= 100) |
| 95 | + |
| 96 | + body <- list(codings = codings) |
| 97 | + if (!is.null(resource_type)) body$resource_type <- resource_type |
| 98 | + if (isTRUE(include_recommendations)) { |
| 99 | + body$include_recommendations <- TRUE |
| 100 | + body$recommendations_limit <- as.integer(recommendations_limit) |
| 101 | + } |
| 102 | + if (isTRUE(include_quality)) body$include_quality <- TRUE |
| 103 | + |
| 104 | + perform_post(private$.base_req, "fhir/resolve/batch", body = body) |
| 105 | + }, |
| 106 | + |
| 107 | + #' @description |
| 108 | + #' Resolve a FHIR CodeableConcept with vocabulary preference. |
| 109 | + #' |
| 110 | + #' Picks the best match per OHDSI preference order |
| 111 | + #' (SNOMED > RxNorm > LOINC > CVX > ICD-10). Falls back to `text` |
| 112 | + #' via semantic search if no coding resolves. |
| 113 | + #' |
| 114 | + #' @param coding A list of coding lists, each with `system`, `code`, |
| 115 | + #' and optional `display`. |
| 116 | + #' @param text Optional CodeableConcept.text for semantic fallback. |
| 117 | + #' @param resource_type FHIR resource type. |
| 118 | + #' @param include_recommendations Logical. Default `FALSE`. |
| 119 | + #' @param recommendations_limit Integer. Default `5L`. |
| 120 | + #' @param include_quality Logical. Default `FALSE`. |
| 121 | + #' |
| 122 | + #' @returns A list with `best_match`, `alternatives`, and `unresolved`. |
| 123 | + resolve_codeable_concept = function(coding, |
| 124 | + text = NULL, |
| 125 | + resource_type = NULL, |
| 126 | + include_recommendations = FALSE, |
| 127 | + recommendations_limit = 5L, |
| 128 | + include_quality = FALSE) { |
| 129 | + stopifnot(is.list(coding), length(coding) >= 1, length(coding) <= 20) |
| 130 | + |
| 131 | + body <- list(coding = coding) |
| 132 | + if (!is.null(text)) body$text <- text |
| 133 | + if (!is.null(resource_type)) body$resource_type <- resource_type |
| 134 | + if (isTRUE(include_recommendations)) { |
| 135 | + body$include_recommendations <- TRUE |
| 136 | + body$recommendations_limit <- as.integer(recommendations_limit) |
| 137 | + } |
| 138 | + if (isTRUE(include_quality)) body$include_quality <- TRUE |
| 139 | + |
| 140 | + perform_post(private$.base_req, "fhir/resolve/codeable-concept", body = body) |
| 141 | + } |
| 142 | + ), |
| 143 | + private = list( |
| 144 | + .base_req = NULL |
| 145 | + ) |
| 146 | +) |
| 147 | + |
| 148 | + |
| 149 | +#' Helper to remove NULL entries from a named list. |
| 150 | +#' @param ... Named arguments. |
| 151 | +#' @returns A list with NULL entries removed. |
| 152 | +#' @keywords internal |
| 153 | +compact_list <- function(...) { |
| 154 | + args <- list(...) |
| 155 | + args[!vapply(args, is.null, logical(1))] |
| 156 | +} |
0 commit comments