R/miscelaneous.R

read_h5Variant_chunked <- function(x, chunksize = 1000) {
    iter <- 1
    n_max <-  nrow_hdf5_df(x)
    is_open <- FALSE
    iterfun <- function(open_this = TRUE, close_this = TRUE) {
        if(open_this) {
            h5f = H5Fopen(x)
            is_open <<- TRUE
        }
        if(close_this) {
            on.exit(H5Fclose(x))
            is_open <<- FALSE
        }
        maxiter <- iter + chunksize
        if(iter < nmax) {
            rows <- seq(iter, min(maxiter, nmax), 1)
            out <- do.call("rbind", lapply(rows, function(i) {
                as.vector(Rle(lengths = h5read(h5f, paste0("/", i))$length,
                    values =  h5read(h5f, paste0("/", i))$values))
            }))
            rownames(out) <- vapply(rows, function(i) {
                h5read(h5f, paste0("/", i))$name
            }, character(1))
            iter <<- maxiter
            out
        } else {
            NULL
        }
    }
}


gen_ranges_h5Variant <- function(x) {
    rn <- gsub("^(.*)(:)(.*)(_)(.*)(/)(.*)$", "\\1 \\3 \\5 \\7", x)
    rn <- do.call("rbind", strsplit(rn, " "))
    rn <- as.data.frame.matrix(rn, stringsAsFactors = FALSE)
    rn[, 2] <- as.numeric(rn[,2])
    end <- rn[, 2] + nchar(rn[,3]) - 1
    out <- GRanges(seqnames = Rle(rn[, 1]), ranges= IRanges(rn[, 2], end))
    names(out) <- x
    elementMetadata(out) <- data.frame(ref = rn[, 3], alt = rn[, 4])
    out
}



setClass("h5VariantIterator", slots = c(location = "character",
    isOpen = "logical",
    fun = "function"),
    prototype = list(location = "", isOpen = FALSE, fun = function(){}))



setGeneric("is_open", function(object) standardGeneric("is_open"))

setGeneric("is_h5Variant",  function(x) {
    is(x, "h5Variant")
})

setMethod("initialize", "h5VariantIterator",
    function(.Object, x, chunksize,  ...) {
        .Object@fun <- read_h5Variant_chunked(x, chunksize)
        .Object@h4_location <- x@h5_location
        .Object
    })

setMethod("is_open", "h5VariantIterator", function(x) {
    (parent.frame(x))$is_open
})
leandroroser/h5Variant documentation built on May 8, 2019, 3:14 a.m.