R/utils.R

#' @title loadGWIPSdata
#' @description Loads data from GWIPS database (in BigWig format) for forward and reverse strands into global GRanges objects. As a result, global objects "gwips_forw" and "gwips_rev" become available for further analyses.
#'
#' @param forward path to the BigWig file containing Riboseq data for forward strand
#' @param reverse path to the BigWig file containing Riboseq data for forward strand
#' @import rtracklayer
#'
#' @return list containing forward and reverse objects
#' @export loadGWIPSdata
loadGWIPSdata <- function(forward, reverse) {
  stopifnot(file.exists(forward))
  stopifnot(file.exists(reverse))
  riboseqlist <- list()
  riboseqlist["forward"] <<- rtracklayer::import(con = forward, format = "bigWig")
  riboseqlist["reverse"] <<- rtracklayer::import(con = reverse, format = "bigWig")
  riboseqlist
}


#' @title verifySeqLevels
#' @description Assumes that global variables gwips_forw and gwips_rev are available. This function checks if the seqLevels in all Genomic Ranges objects are the same. It's a sanity-check function, not to be used directly.
#'
#' @param riboseqlist a list containing two GR objects generated by \code{loadGWIPSdata} function
#' @param x Genomic Ranges object from the genome annotation
#' @import GenomicRanges
#'
#' @return FALSE if seqLevels are different, TRUE if everything is OK
#' @export verifySeqLevels
verifySeqLevels <- function(riboseqlist, gr) {
  if (!(exists(riboseqlist["forward"]) && exists(riboseqlist["reverse"]))) {
    stop("Wrong object, riboseqlist required.")
  }

  if (!((seqlevels(riboseqlist["forward"]) == seqlevels(riboseq["reverse"])) &&
        (seqlevels(riboseqlist["reverse"]) == seqlevels(x)))) {
    print("Please correct seqlevels in your Granges objects.")
    return(FALSE)
  } else {
    return(TRUE)
  }
}
freesci/rGWIPS documentation built on May 31, 2019, 8:07 a.m.