R/NanoMethResults.R

Defines functions NanoMethResult

Documented in NanoMethResult

#' Nanopore Methylation Result
#'
#' A NanoMethResult object stores data used for NanoMethViz visualisation. It
#' contains stores a path to the methylation data, sample information and
#' optional exon information. The object is constructed using the
#' NanoMethResult() constructor function described in "Usage".
#'
#' @slot methy the path to the methylation tabix file.
#' @slot samples the data.frame of sample annotation containg at least columns
#'   sample and group.
#' @slot exons the data.frame of exon information containing at least columns
#'   gene_id, chr, strand, start, end, transcript_id and symbol.
#'
#' @return a NanoMethResult object to be used with plotting functions
#' @export
setClass(
    "NanoMethResult",
    representation(
        methy = "character",
        samples = "data.frame",
        exons = "data.frame"
    )
)

#' @describeIn NanoMethResult-class Constructor
#'
#' @param methy the path to the methylation tabix file.
#' @param samples the data.frame of sample annotation containg at least columns
#'   sample and group.
#' @param exons (optional) the data.frame of exon information containing at least columns
#'   gene_id, chr, strand, start, end, transcript_id and symbol.
#'
#' @export
NanoMethResult <- function(methy, samples, exons = NULL) {
    if (is.null(exons)) {
        exons <- tibble::tibble(
            gene_id = character(),
            chr = character(),
            strand = character(),
            start = integer(),
            end = integer(),
            transcript_id = character(),
            symbol = character()
        )
    }

    assertthat::is.readable(methy)
    assert_has_columns(samples, c("sample", "group"))

    # Check in first 1000 entries that samples are in sample annotation
    head_values <- read.table(
        gzfile(methy),
        col.names = methy_col_names(),
        nrows = 1000
    )
    if (length(intersect(head_values$sample, samples$sample)) == 0) {
        stop("in first 1000 entries, no sample names matched samples from annotation")
    }
    if (!all(head_values$sample %in% samples$sample)) {
        warning(glue::glue(
            "in first 1000 entires, the following samples were not in annotation: {missing_cols}",
            missing_cols = paste(setdiff(head_values$sample, samples$sample), collapse = ", ")
        ))
    }

    assertthat::assert_that(any(head_values$sample %in% samples$sample))

    methods::new(
        "NanoMethResult",
        methy = methy,
        samples = tibble::as_tibble(samples),
        exons = tibble::as_tibble(exons)
    )
}


#' Get methylation data
#' @keywords internal
#'
#' @param object the object.
#'
#' @return the path to the methylation data.
#'
#' @examples
#' showMethods("methy")
#'
#' @export
setGeneric("methy", valueClass = "character", function(object) {
    standardGeneric("methy")
})

#' @describeIn NanoMethResult-class methylation data path getter.
#'
#' @param object the NanoMethResult object.
#'
#' @return the path to the methylation data.
#'
#' @examples
#' x <- load_example_nanomethresult()
#' methy(x)
#'
#' @export
setMethod("methy", signature("NanoMethResult"), function(object) {
    object@methy
})

#' Get sample annotation
#'
#' @param object the object.
#'
#' @return the sample annotation.
#'
#' @examples
#' showMethods("samples")
#'
#' @export
#'
#' @keywords internal
setGeneric("samples", valueClass = "data.frame", function(object) {
    standardGeneric("samples")
})

#' @describeIn NanoMethResult-class sample annotation getter.
#'
#' @param object the NanoMethResult object.
#'
#' @return the sample annotation.
#'
#' @export
setMethod("samples", signature("NanoMethResult"), function(object) {
    object@samples
})

#' Get exon annotation
#' @keywords internal
#'
#' @param object the object.
#'
#' @return the exon annotation.
#'
#' @examples
#' showMethods("exons")
#'
#' @export
setGeneric("exons", valueClass = "data.frame", function(object) {
    standardGeneric("exons")
})

#' @describeIn NanoMethResult-class exon annotation getter.
#'
#' @param object the NanoMethResult object.
#'
#' @return the exon annotation.
#'
#' @export
setMethod("exons", signature("NanoMethResult"), function(object) {
    object@exons
})

Try the NanoMethViz package in your browser

Any scripts or data that you put into this service are public.

NanoMethViz documentation built on Nov. 8, 2020, 4:51 p.m.