R/move_final_package.R

Defines functions move_final_granges_package move_final_organismdbi_package move_final_orgdb_package move_final_txdb_package move_final_bsgenome_package make_move_retlist

#' Define the final locations for built packages and databases.
#'
#' I am hoping to make the organization of the final outputs more
#' consistent.  This function is intended to put those locations in
#' place.
#'
#' @param pkglist Result from the package builder,
#'  bsgenome/orgdb/txdb/granges/orgdbi.
#' @param build_dir toplevel dirname of the intermediate/final files.
#' @param type Type of package containing the final outputs.
make_move_retlist <- function(pkglist, build_dir = build, type = "orgdb") {
  pkgname <- basename(as.character(pkglist))
  version_string <- format(Sys.time(), "%Y.%m")

  final_tar_dir <- file.path(build_dir, "tar", type)
  final_tar_filename <- glue("{pkgname}_{version_string}.tar.gz")
  final_tar_file <- file.path(final_tar_dir, final_tar_filename)
  current_tar_filename <- file.path(build_dir, final_tar_filename)
  if (!file.exists(final_tar_dir)) {
    dir.create(final_tar_dir, recursive = TRUE)
  }
  final_data_dir <- file.path(build_dir, type, pkgname)
  starting_orgdb <- file.path(build_dir, pkgname)
  retlist <- list(
    "pkgname" = pkgname,
    "starting_tar" = current_tar_filename,
    "starting_orgdb" = starting_orgdb,
    "version_string" = version_string,
    "data_path" = final_data_dir,
    "package_path" = final_tar_file)
  return(retlist)
}

#' FIXME: Not completed yet.  This should move the intermediates and
#' final bsgenomes.
#'
#' When completed, this will make certain that all the BSgenome pieces
#' will end up in the correct locations.
#'
#' @param pkglist Package name of the bsgenome.
#' @param build_dir dirname of the build prefix.
move_final_bsgenome_package <- function(pkglist, build_dir = "build") {
  retlist <- make_move_retlist(pkglist, build_dir = build_dir, type = "bsgenome")
  ## These are not needed because I do this at the download step now.
  current_path <- basename(final_path)
  fasta_file <- file.path(build_dir, glue("{pkgname}.fasta"))
  fasta_dir <- file.path(build_dir, "fasta")
  if (!file.exists(fasta_dir)) {
    dir.create(fasta_dir, recursive = TRUE)
  }
  fasta_moved <- file.rename(from = fasta_file, file.path(fasta_dir, basename(fasta_file)))
  return(retlist)
}

#' FIXME: Complete this.  It should move the final TxDB data.
#'
#' @param pkglist Package name.
#' @param build_dir dirname of the build tree.
move_final_txdb_package <- function(pkglist, build_dir = "build") {
  retlist <- make_move_retlist(pkglist, build_dir = build_dir, type = "txdb")
  ## These are not needed because I do this at the download step now.
  gff_file <- file.path(build_dir, glue("{pkgname}.gff"))
  data_destination <- gff_file
  gff_dir <- file.path(build_dir, "gff")
  if (!file.exists(gff_dir)) {
    dir.create(gff_dir, recursive = TRUE)
  }
  data_destination <- file.path(gff_dir, basename(gff_file))
  gff_moved <- file.rename(from = gff_file, data_destination)
  tar_moved <- file.rename()
  retlist[["data_path"]] <- data_destination
  return(retlist)
}

#' Move a package file to its final location for collation by
#' AnnotationHubData.
#'
#' I want to have one location for the tarballs generated by
#' devtools::build() and another for the sqlite files which would be
#' used by AnnotationHub.  It can be a little tricky to get these
#' correct because of the various ways in which the built packages
#' might fail or need to be restarted.
#'
#' @param pkglist Information list from a make_pkg function.
#' @param type Which type of package is this?
#' @param build_dir base working directory.
move_final_orgdb_package <- function(pkglist, build_dir = "build") {
  retlist <- make_move_retlist(pkglist, build_dir = build_dir, type = "orgdb")
  tar_moved <- FALSE
  if (file.exists(retlist[["package_path"]])) {
    message("It appears the tarball was already moved to: ", retlist[["package_path"]])
    tar_moved <- "already"
    if (file.exists(retlist[["starting_tar"]])) {
      message("The starting tarball also exists, deleting it.")
      file.remove(retlist[["starting_tar"]])
    }
  } else {
    if (file.exists(retlist[["starting_tar"]])) {
      tar_moved <- try(file.rename(from = retlist[["starting_tar"]],
                                 to = retlist[["package_path"]]))
    } else {
      warning("The starting file does not exist: ", retlist[["starting_tar"]])
      tar_moved <- "missing"
    }
  }

  orgdb_moved <- FALSE
  if (file.exists(retlist[["data_path"]])) {
    message("It appears the orgdb was already moved to: ", retlist[["data_path"]])
    orgdb_moved <- "already"
    if (file.exists(retlist[["starting_orgdb"]])) {
      message("The starting package directory also exists, deleting it.")
      file.remove(retlist[["pkgname"]], recursive = TRUE)
    }
  } else {
    if (file.exists(retlist[["starting_orgdb"]])) {
      orgdb_moved <- file.rename(from = retlist[["starting_orgdb"]],
                                 to = retlist[["data_path"]])
    } else {
      warning("The starting file does not exist: ", retlist[["pkgname"]])
      orgdb_moved <- "missing"
    }
  }

  retlist[["tar_moved"]] <- tar_moved
  retlist[["orgdb_moved"]] <- orgdb_moved
  return(retlist)
}

#' FIXME: Check that this is correct!
#'
#' This should move the organismDBI data into its final locations.
#'
#' @param pkglist Package name.
#' @param build_dir dirname of the build tree.
move_final_organismdbi_package <- function(pkglist, build_dir = build_dir) {
  retlist <- make_move_retlist(pkglist, build_dir = build_dir, type = "organismdbi")
  current_path <- basename(final_path)
  fasta_file <- file.path(build_dir, glue("{pkgname}.fasta"))
  fasta_dir <- file.path(build_dir, "fasta")
  if (!file.exists(fasta_dir)) {
    dir.create(fasta_dir, recursive = TRUE)
  }
  fasta_moved <- try(
    file.rename(from = fasta_file, file.path(fasta_dir, basename(fasta_file))), silent = TRUE)
  data_destination <- fasta_file
  return(retlist)
}

#' FIXME: Make sure this is correct. It should move granges
#' information to a final location.
#'
#' @param pkglist Package name.
#' @param build_dir dirname of the build tree.
move_final_granges_package <- function(pkglist, build_dir = "build") {
  pkgname <- basename(as.character(pkglist))
  version_string <- format(Sys.time(), "%Y.%m")

  final_tar_dir <- file.path(build_dir, "tar", "orgdb")
  final_tar_filename <- glue("{pkgname}_{version_string}.tar.gz")
  final_tar_file <- file.path(final_tar_dir, final_tar_filename)
  current_tar_filename <- file.path(build_dir, final_tar_filename)
  if (!file.exists(final_tar_dir)) {
    dir.create(final_tar_dir, recursive = TRUE)
  }

  final_data_dir <- file.path(build_dir, "orgdb", pkgname)
  retlist <- list(
    "data_path" = final_data_dir,
    "package_path" = final_tar_file)

  if (file.exists(final_tar_filename)) {
    message("It appears this file was already moved.")
  }
  return(retlist)
}
khughitt/EuPathDB documentation built on Nov. 4, 2023, 4:19 a.m.