R/WebApiConceptSet.R

Defines functions getConceptSetInfo getConceptSetExpression getConceptSets

Documented in getConceptSetExpression getConceptSetInfo getConceptSets

# @file ConceptSet
#
# Copyright 2019 Observational Health Data Sciences and Informatics
#
# This file is part of ROhdsiWebApi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#' Get a concept set's name from concept Id for a WebAPI
#'
#' @details
#' Get a concept set's name from concept Id for a WebAPI
#'
#' @param baseUrl      The base URL for the WebApi instance, for example:
#'                     "http://server.org:80/WebAPI".
#' @param conceptSetId        The concept set id in Atlas.
#'
#' @return
#' A list object with native and parsed
#'
#' @examples
#' \dontrun{
#' # This will obtain name of the concept set
#'
#' getConceptSetInfo(baseUrl = "http://server.org:80/WebAPI", conceptSetId = 100)
#' }
#'
#'
#' @export
getConceptSetInfo <- function(baseUrl, conceptSetId) {
  .checkBaseUrl(baseUrl)

  url <- sprintf("%s/conceptset/%s", baseUrl, conceptSetId)
  .getApiResponseParse(url)
}


#' Get concept set expression from concept set id for a WebApi
#'
#' @details
#' Get concept set expression from concept set id for a WebApi
#'
#' @param conceptSetId         The concept set id in Atlas.
#' @param baseUrl       The base URL for the WebApi instance, for example:
#'                      "http://server.org:80/WebAPI".
#'
#' @return
#' A list object with native and parsed
#'
#' @examples
#' \dontrun{
#' # This will obtain expression of the concept set
#'
#' getConceptSetExpression(baseUrl = "http://server.org:80/WebAPI", conceptSetId = 100)
#' }
#'
#'
#' @export
getConceptSetExpression <- function(baseUrl, conceptSetId) {
  .checkBaseUrl(baseUrl)

  url <- sprintf("%1s/conceptset/%2s/expression", baseUrl, conceptSetId)
  result <- .getApiResponseParse(url)
  names(result$parsed$items) <-
    stringr::str_replace_all(names(result$parsed$items),pattern = 'concept\\.',replacement = '')
  result$parsed <- result$parsed$items %>% dplyr::mutate(id = conceptSetId)
  result$parsed$items <- NULL

  #meta information about concept set
  conceptSetInfo <- StudyManagement::getConceptSetInfo(baseUrl = baseUrl,
                                                       conceptSetId = conceptSetId
                                                       )$parsed %>%
                    as.data.frame() %>%
                    dplyr::mutate(
                      modifiedDate = .millisecondsToDate(modifiedDate),
                      createdDate = .millisecondsToDate(createdDate)
                    )
  writeLines(paste0('  ', conceptSetInfo$id, ':', conceptSetInfo$name))

  result$parsed <- result$parsed %>% dplyr::left_join(y = conceptSetInfo, by = 'id' )

  list(
    parsed = result$parsed,
    native = result$native
  )
}

#' Get various details for a list of concept set ids in a WebApi
#'
#' @details
#' Get various details for a list of concept set ids in a WebApi
#'
#' @param conceptSetIds       The concept set id in Atlas.
#' @param baseUrl             The base URL for the WebApi instance, for example:
#'                            "http://server.org:80/WebAPI".
#' @param vocabSourceKey      (optional) TO DO By defaul the priority vocabulary for the WebApi is used
#'
#' @return
#' A list object with native and parsed
#'
#' @examples
#' \dontrun{
#' # Get various details for a list of concept set ids in a WebApi
#'
#' getConceptSets(baseUrl = "http://server.org:80/WebAPI", conceptSetIds = c(100,101)
#' }
#'
#'
#' @export
getConceptSets <- function(baseUrl, conceptSetIds, vocabSourceKey = NULL) {
  .checkBaseUrl(baseUrl)
  writeLines('Downloading concept set expressions')

  if (missing(vocabSourceKey) || is.null(vocabSourceKey)) {
    vocabSourceKey <- StudyManagement::getPriorityVocabKey(baseUrl = baseUrl)[['sourceKey']]
  }

  result <- list()
  for (i in (1:length(conceptSetIds))) {
    conceptSetId <- as.integer(conceptSetIds[[i]])
    conceptSetInfo <- StudyManagement::getConceptSetInfo(baseUrl, conceptSetId = conceptSetId)
    conceptSetInfo$parsed$nameFormatted <- .formatName(conceptSetInfo$parsed$name)
    conceptSetExpression <-
      StudyManagement::getConceptSetExpression(baseUrl,
                                               conceptSetId = conceptSetId
                                               )
    conceptSetExpression$parsed <- jsonlite::flatten(conceptSetExpression$parsed)
    names(conceptSetExpression$parsed) <- stringr::str_replace_all(string = names(conceptSetExpression$parsed),
                                                            pattern = 'concept\\.',
                                                            replacement = ""
                                                             )

    result[[paste0('id_', conceptSetId)]] <- list(conceptSetInfo = conceptSetInfo,
                                                  conceptSetExpression = conceptSetExpression
                                                  #, to do: resolved concepts in concept set expression
                                                  )
  }
  result
}
gowthamrao/StudyManagement documentation built on March 9, 2020, 10:48 p.m.