R/utils.R

Defines functions path_to_oc_executable key_modules local_oc_variant_tbl local_oc_sqlite_con import_local_crx local_oc_job_resources local_oc_jobids get_oc_job_status connect_local_annotator list_local_annotators local_oc_path

Documented in connect_local_annotator get_oc_job_status import_local_crx key_modules list_local_annotators local_oc_jobids local_oc_job_resources local_oc_path local_oc_sqlite_con local_oc_variant_tbl path_to_oc_executable

 

#' use reticulate to determine where locally installed OpenCRAVAT modules reside
#' @export
local_oc_path = function() {
 proc = basilisk::basiliskStart(ocbenv) # avoid package-specific import
 on.exit(basilisk::basiliskStop(proc))
 basilisk::basiliskRun(proc, function() {
 cra = reticulate::import("cravat", convert=TRUE)
 adm = cra$cravat_admin$au
 adm$get_modules_dir()
 })
}

#' use reticulate/basilisk to list locally installed OpenCRAVAT annotators
#' @export
list_local_annotators = function() 
   dir(paste0(local_oc_path(), "/annotators"))

#' connect to local annotator SQLite database
#' @import RSQLite
#' @param name character(1) OpenCRAVAT annotator name
#' @export
connect_local_annotator = function(name="clinvar") {
  avail = list_local_annotators()
  if (!(name %in% avail)) stop(paste(name, "not in list of local annotators"))
  pa = local_oc_path()
  dpath = paste0(pa, "/annotators/", name, "/data/", name, ".sqlite")
  RSQLite::dbConnect(RSQLite::SQLite(), dpath)
}

#' get job status
#' @param baseurl character(1) server url
#' @param job_id character(1) id
#' @export
get_oc_job_status = function(baseurl = "http://0.0.0.0:8080/", job_id) {
  httr::content(httr::GET(paste0(baseurl, "submit/jobs/", job_id, "/status")))$status
}

#' list local job id folder names
#' @param path character(1) defaults to `local_oc_path()`
#' @export
local_oc_jobids = function(path=local_oc_path()) dir(paste0(path, "/cravat/jobs/default"))

#' list local job folder content
#' @param path character(1) defaults to `local_oc_path()`
#' @param job_id character(1) a job id
#' @param \dots passed to `dir()`
#' @export
local_oc_job_resources = function(path=local_oc_path(), job_id, ...) dir(paste0(path, "/cravat/jobs/default/", job_id), ...)

#' import the 'crx' file with variant annotation
#' @importFrom utils browseURL read.delim
#' @param path character(1) defaults to `local_oc_path()`
#' @param job_id character(1) required job id
#' @return a data.frame
#' @export
import_local_crx = function(path=local_oc_path(), job_id) {
 stopifnot(job_id %in% local_oc_jobids())
 res = local_oc_job_resources(path=path, job_id=job_id, full=TRUE)
 crxf = grep("crx$", res, value=TRUE)
 lines = readLines(crxf)
 comminds = grep("^#", lines)
 ncomm = length(comminds)-1
 read.delim(textConnection(lines[-seq_len(ncomm)]), h=TRUE, 
   row.names=NULL, sep="\t", check.names=FALSE)
}

#' get connection to SQLite resource generated by a job
#' @param path character(1) defaults to `local_oc_path()`
#' @param job_id character(1) required job id
#' @export
local_oc_sqlite_con = function(path=local_oc_path(), job_id) {
  allres = local_oc_job_resources(path=path, job_id=job_id, full=TRUE)
  sqlite_path = grep("sqlite$", allres, value=TRUE)
  RSQLite::dbConnect(RSQLite::SQLite(), sqlite_path, flags=SQLITE_RO)
}

#' get variant tbl generated by a job
#' @importFrom magrittr %>%
#' @importFrom dplyr tbl
#' @param path character(1) defaults to `local_oc_path()`
#' @param job_id character(1) required job id
#' @export
local_oc_variant_tbl = function(path=local_oc_path(), job_id) {
  local_oc_sqlite_con(path=path, job_id=job_id) %>% tbl("variant")
}

#' enumerate modules that are installed by GUI when an empty installation is encountered
#' @export
key_modules = function() c("cravat-converter", 
  "excelreporter", 
  "hg38", 
  "hg38wgs", 
  "oldcravat-converter", 
  "tagsampler", 
  "textreporter", 
  "vcf-converter", 
  "vcfinfo", 
  "wgcircossummary", 
  "wgcodingvsnoncodingsummary", 
  "wggosummary", 
  "wgsosamplesummary", 
  "wgsosummary", "clinvar", "genehancer", "wgncrna", "mupit", "mutpanning",
  "wghg19", "wgbase", "wglollipop", "wgnote", "varmeta",
  "wgndex", "wgvcfinfo")
 
#' install in the ocbenv
#' @param modname character(1) 'name' as defined in OpenCRAVAT installation protocol
#' @export
install_oc_module = function (modname) 
{
    proc = basilisk::basiliskStart(ocbenv)
    on.exit(basilisk::basiliskStop(proc))
    basilisk::basiliskRun(proc, function(modname) {
        adm = reticulate::import("cravat", convert = TRUE)$admin_util
        adm$install_module(modname)
    }, modname = modname)
}

#' path to the primary executable
#' @export
path_to_oc_executable = function() {
  paste(sub("ocbenv.*", "ocbenv", local_oc_path()), "/bin/oc", sep="")
}
vjcitn/oc2bioc documentation built on Sept. 7, 2024, 10:23 a.m.