R/utilities.R

Defines functions filterResources_EH createHubAccessors hubAccessorFactory

Documented in createHubAccessors

### =========================================================================
### Helpers for package-specific resource discovery
### -------------------------------------------------------------------------

## export resources as accessor functions

.hubAccessorFactory <- function(ehid) {
     force(ehid)
     function(metadata=FALSE) {
         eh <- ExperimentHub()
         if (metadata) {
             eh[ehid]
         } else
             eh[[ehid]]
     }
}

createHubAccessors <- function(pkgname, titles) {
    ## map titles to ExperimentHub identifiers
    eh <- query(ExperimentHub(), "alpineData")
    ehids <- sapply(titles, function(title) {
        ehid <- names(query(eh, title))
        if (length(ehid) == 0L) {
            stop(sQuote(title), " not found in ExperimentHub")
        } else if (length(ehid) != 1L) {
            stop(sQuote(title), 
                 " matches more than 1 ExperimentHub resource")
        }
        ehid
    })

    ## create and export accessor functions in package namespace
    ns <- asNamespace(pkgname)
    for (i in seq_along(titles)) {
        assign(titles[[i]], .hubAccessorFactory(ehids[[i]]), envir=ns)
        namespaceExport(ns, titles[[i]])
    }
}

## resource discovery

.filterResources_EH <- function(package, filterBy=character()) {
    if (!is.character(filterBy))
        stop("'filterBy' must be a character vector")
    suppressMessages({eh <- ExperimentHub()})
    if (!package %in% unique(package(eh)))
        stop(paste0("'", package, "' resources were not found in ExperimentHub"))

    sub <- query(eh, package)
    if (length(filterBy))
        query(sub, filterBy)
    else
        sub
}

setMethod("listResources", "ExperimentHub", 
    function(hub, package, filterBy=character()) {
        metadata <- .filterResources_EH(package, filterBy)
        mcols(metadata)$title
})

setMethod("loadResources", "ExperimentHub",
    function(hub, package, filterBy=character()) {
        metadata <- .filterResources_EH(package, filterBy)
        eh <- ExperimentHub()
        lapply(names(metadata), function(i) eh[[i]]) 
})
Bioconductor-mirror/ExperimentHub documentation built on May 29, 2017, 4:15 a.m.