R/utils.R

Defines functions pgxReferenceRanges pgxHaplotypeRanges pgxHaplotypes pgxGenes

Documented in pgxGenes pgxHaplotypeRanges pgxHaplotypes pgxReferenceRanges

#' Return a vector of available PGx genes
#'
#' Return all available PGx genes for use in the \code{\link{readPGx}} function.
#' @export
#' @return Vector of gene names that can be used in the \code{\link{readPGx}} function.
pgxGenes <- function() { names(pgx:::reference_ranges) }

#' Return a vector of available haplotypes
#'
#' Return all available PGx haplotypes
#' @param suballeles Include suballeles in the returned list? Default (TRUE)
pgxHaplotypes <- function(suballeles = TRUE) {
  stopifnot("suballeles must be TRUE or FALSE" = is.logical(suballeles))
  alleles <- sort(unique(pgx:::haplotypes_gr$`Haplotype Name`))
  if (!suballeles) {
    return(alleles[!grepl("\\.[0-9]+$", alleles)])
  }
  alleles
}

#' Retrieve reference haplotype ranges
#'
#' Return the haplotype GRanges for a given allele or suballele
#' @param haplotype Haplotype (allele) to retrieve ranges from. If NULL (Default)
#' return all haplotype ranges in a single GRanges object. A vector of available
#' alleles can be viewed with \code{pgxHaplotypes}
pgxHaplotypeRanges <- function(haplotype = NULL) {
  haplotypes <- pgx:::haplotypes_gr
  if (!is.null(haplotype)) {
    stopifnot("Haplotype must be one of pgxHaplotypes" = haplotype %in% pgx::pgxHaplotypes())
    stopifnot("Multiple haplotypes not allowed" = length(haplotype) == 1)
    return(haplotypes[haplotypes$`Haplotype Name` == haplotype, ])
  }
  haplotypes
}

#' Retrieve reference PGx ranges
#'
#' This is a convenience function for the extracting unique GRanges used to
#' subset a VCF file in the \code{readPGx} function.
#' @param gene PGx gene to get ranges for. If NULL (Default) return list of GRanges
#' objects for all genes. To view list of available genes use \code{pgxGenes()}
#' @return Either names list of GRanges objects or GRanges object for the given gene
#' @export
pgxReferenceRanges <- function(gene = NULL) {
  if (!is.null(gene)) {
    stopifnot("More than one gene supplied." = length(gene) == 1)
    stopifnot("Gene must be one of pgxGenes()" = gene %in% pgx::pgxGenes())
    return(pgx:::reference_ranges[[gene]])
  }
  pgx:::reference_ranges
}
coriell-research/pgx documentation built on June 4, 2022, 11:08 a.m.