R/rest-geneOntology.R

Defines functions geneOntology

Documented in geneOntology

#' Gene Ontology terms
#'
#' @note Updated 2023-09-25.
#' @export
#'
#' @inheritParams params
#' @inheritParams AcidRoxygen::params
#'
#' @return `List`.
#'
#' @examples
#' genes <- c("WBGene00000912", "WBGene00004804")
#' x <- geneOntology(genes)
#' print(x)
geneOntology <- function(genes) {
    assert(.allAreGenes(genes))
    list <- lapply(genes, function(gene) {
        query <- paste(
            "widget",
            "gene",
            gene,
            "gene_ontology",
            sep = "/"
        )
        rest <- .rest(query)[["fields"]][["gene_ontology"]][["data"]]
        if (is.null(rest)) {
            return(NULL)
        }
        goTerms <- lapply(
            X = rest,
            FUN = function(process) {
                x <- lapply(
                    X = seq_along(process),
                    FUN = function(x) {
                        id <- process[[x]][["term_description"]][["id"]]
                        label <- process[[x]][["term_description"]][["label"]]
                        paste(id, label, sep = "~")
                    }
                )
                x <- unlist(x, recursive = FALSE, use.names = FALSE)
                x <- sort(unique(x))
                x
            }
        )
        goTerms <- CharacterList(goTerms)
        names(goTerms) <- camelCase(names(goTerms), strict = TRUE)
        goTerms <- goTerms[sort(names(goTerms))]
        goTerms
    })
    names(list) <- genes
    list <- List(list)
    list
}
acidgenomics/r-wormbase documentation built on Oct. 3, 2023, 1:22 p.m.