R/subsample.R

Defines functions subsample

Documented in subsample

#' Normalize read counts
#'
#' Normalize read counts to a given read depth. Reads counts are randomly removed from the input to match the specified read depth.
#'
#' @author Aaron Taudt
#' @param binned.data A \code{\link{GRanges-class}} object with meta data column 'reads' that contains the read count.
#' @param sample.reads The number of reads that will be retained.
#' @return A \code{\link{GRanges-class}} object with downsampled read counts.
#' @importFrom stats rbinom
subsample <- function(binned.data, sample.reads) {

    total.reads <- sum(binned.data$counts)
    if (sample.reads >= total.reads) {
        warning("Not resampling reads because sample.reads is bigger than the actual number of reads in the sample.")
        return(binned.data)
    }

    p <- sample.reads / total.reads
    binned.data$counts <- stats::rbinom(binned.data$counts, binned.data$counts, p)

    return(binned.data)
}
    

Try the chromstaR package in your browser

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

chromstaR documentation built on Nov. 8, 2020, 8:29 p.m.