R/HydratePackage.R

Defines functions loadSpecifications hydrate

Documented in hydrate loadSpecifications

# Copyright 2023 Observational Health Data Sciences and Informatics
#
# This file is part of Hydra
#
# 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.

#' Hydrate a package skeleton according to study specifications
#'
#' @param specifications     A string containing JSON specifications for hydrating
#'                           the study. Specifications will be generated by ATLAS.
#' @param outputFolder       The folder where the hydrated package will be stored.
#' @param skeletonFileName   (Mostly for debugging purposes:) Specify the location
#'                           of a package skeleton to hydrate. If not specified,
#'                           the appropriate skeleton will be selected from those
#'                           inside the Hydra package.
#' @param packageName        The name of the R package to create. If provided, will
#'                           override the \code{packageName} variable in the
#'                           specifications JSON.
#'
#' @export
hydrate <- function(specifications, outputFolder, skeletonFileName = NULL, packageName = NULL) {
  if (!is.character(specifications)) {
    stop("Specifications should be character (a JSON string). You can load JSON files using loadSpecifications()")
  }
  if (file.exists(outputFolder)) {
    warning("Output folder ", outputFolder, " already exists. Any contents will be overwritten")
  } else {
    dir.create(outputFolder, recursive = TRUE)
  }
  if (!is.null(packageName)) {
    if (grepl("packageName", specifications)) {
      specifications <- gsub("\"packageName\":.*?,", sprintf("\"packageName\": \"%s\",", packageName), specifications)
    } else {
      specifications <- gsub("\"skeletonType\"", sprintf("\"packageName\": \"%s\",\"skeletonType\"", packageName), specifications)
    }
  }

  hydra <- rJava::new(
    Class = rJava::J("org.ohdsi.hydra.Hydra"),
    as.character(specifications)[1]
  )
  hydra$setPackageFolder(system.file(package = "Hydra"))



  if (!is.null(skeletonFileName)) {
    hydra$setExternalSkeletonFileName(as.character(skeletonFileName))
  }
  tempFile <- tempfile()
  hydra$hydrate(tempFile)
  utils::unzip(zipfile = tempFile, exdir = outputFolder, overwrite = TRUE)
  unlink(tempFile)
}

#' Load specifications from a JSON file
#'
#' @param fileName   Path to the JSON file
#'
#' @return A string representation of the JSON.
#'
#' @export
loadSpecifications <- function(fileName) {
  return(readChar(fileName, file.info(fileName)$size))
}
OHDSI/Hydra documentation built on March 7, 2023, 3:48 a.m.