R/gson.R

Defines functions normalize_gson_data_frame gson

Documented in gson

#' construct a 'GSON' object
#'
#' @param gsid2gene A data frame with first column of gene set IDs and second column of genes
#' @param gsid2name A data frame with first column of gene set IDs and second column of gene set names
#' @param gene2name A data frame with first column of genes and second column of gene symbols
#' @param schema_version GSON file schema version
#' @param species Which species of the genes belongs to
#' @param gsname Name of the gene set (e.g., GO, KEGG, etc.)
#' @param version version of the gene set
#' @param accessed_date date to obtain the gene set data
#' @param keytype keytype of genes
#' @param gsidtype identifier domain of the gene set IDs, e.g., GO, KEGG, REAC
#' @param urlpattern URL pattern
#' @param info extra information
#'
#' @return A 'GSON' instance
#' @importFrom methods new
#' @export
#'
#' @examples
#' wpfile <- system.file('extdata', "wikipathways-20220310-gmt-Homo_sapiens.gmt", package='gson')
#' x <- read.gmt.wp(wpfile)
#' gsid2gene <- data.frame(gsid=x$wpid, gene=x$gene)
#' gsid2name <- unique(data.frame(gsid=x$wpid, name=x$name))
#' species <- unique(x$species)
#' version <- unique(x$version)
#' gson(gsid2gene=gsid2gene, gsid2name=gsid2name, species=species, version=version)
gson <- function(gsid2gene, gsid2name = NULL, gene2name = NULL,
                 schema_version = "1.0", species = NULL, gsname = NULL, version = NULL,
                 accessed_date = NULL, keytype = NULL, gsidtype = NULL,
                 urlpattern = NULL, info = NULL) {

    gsid2gene <- normalize_gson_data_frame(gsid2gene, c("gsid", "gene"), "gsid2gene")
    gsid2name <- normalize_gson_data_frame(gsid2name, c("gsid", "name"), "gsid2name")
    gene2name <- normalize_gson_data_frame(gene2name, c("gene", "name"), "gene2name")

    new("GSON",
        gsid2gene = gsid2gene,
        gsid2name = gsid2name,
        gene2name = gene2name,
        schema_version = schema_version,
        species = species,
        gsname = gsname,
        version = version,
        accessed_date = accessed_date,
        keytype = keytype,
        gsidtype = gsidtype,
        urlpattern = urlpattern,
        info = info)
}

normalize_gson_data_frame <- function(x, cols, arg) {
    if (is.null(x)) {
        return(NULL)
    }
    if (!is.data.frame(x)) {
        stop(arg, " should be a data.frame", call. = FALSE)
    }
    if (ncol(x) < length(cols)) {
        stop(arg, " should have at least ", length(cols), " columns", call. = FALSE)
    }
    x <- x[, seq_along(cols), drop = FALSE]
    colnames(x) <- cols
    ## row labels survive subsetting and named vectors -- gson_GO() rows
    ## gsid2name by GO term -- but JSON has none, so they are dropped here
    rownames(x) <- NULL
    ## producers routinely pass factors -- `utils::stack()` returns its `ind`
    ## column as a factor -- and a factor keeps the levels it is no longer
    ## using, so IDs are always held as character here; doubles go through
    ## format(), because as.character() would print 1e6 as "1e+06"
    for (i in seq_along(cols)) {
        value <- x[[i]]
        if (is.double(value)) {
            ## format() renders NA as the string "NA", as.character() prints
            ## 1e6 as "1e+06", so the digits come from format() over non-NA
            nas <- is.na(value)
            value <- format(value, scientific = FALSE, trim = TRUE)
            value[nas] <- NA_character_
        } else {
            value <- as.character(value)
        }
        x[[i]] <- value
    }
    x
}

Try the gson package in your browser

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

gson documentation built on Oct. 9, 2026, 5:09 p.m.