diff --git a/DESCRIPTION b/DESCRIPTION index 77fc197..ddb9e5e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: categoryCompare2 -Version: 0.100.8 +Version: 0.100.11 Title: Meta-Analysis of High-Throughput Experiments Using Feature Annotations Author: Robert M. Flight @@ -25,5 +25,5 @@ SystemRequirements: Cytoscape (>= 3.0) (if used for visualization of biocViews: Annotation, GO, MultipleComparison, Pathways, GeneExpression VignetteBuilder: knitr Encoding: UTF-8 -RoxygenNote: 7.2.1 +RoxygenNote: 7.2.3 Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index e2db2a2..9a11954 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(combined_statistics) export(csv_annotation_table) export(enriched_result) export(executable_path) +export(extract_enrich_stats) export(extract_statistics) export(filter_annotation_graph) export(generate_annotation_graph) @@ -58,15 +59,26 @@ exportClasses(combined_enrichment) exportClasses(hypergeom_features) exportClasses(node_assign) exportClasses(statistical_results) +exportMethods(Extract) +exportMethods(a) exportMethods(annotation_combinations) exportMethods(combine_annotations) exportMethods(combine_enrichments) +exportMethods(enrich) +exportMethods(enrichment) +exportMethods(extract) exportMethods(extract_statistics) +exportMethods(from) exportMethods(generate_annotation_graph) exportMethods(generate_table) exportMethods(get_significant_annotations) +exportMethods(object.) exportMethods(remove_edges) exportMethods(show) +exportMethods(single) +exportMethods(statistical) +exportMethods(stats) +exportMethods(table) import(methods) importFrom(base64enc,dataURI) importFrom(colorspace,desaturate) diff --git a/R/combine_enrichments.R b/R/combine_enrichments.R index 9915f11..a4acf80 100644 --- a/R/combine_enrichments.R +++ b/R/combine_enrichments.R @@ -584,3 +584,20 @@ setMethod("extract_statistics", signature = list(in_results = "combined_enrichme #' #' @param combined_enrichment a \code{\link{combined_enrichment}} object #' @exportMethod + + +#' extract enrich stats +#' +#' Extract statistical table from a single enrichment object. +#' +#' @param enrichment_result the enrichment result object +#' +#' @export +#' @return data.frame +extract_enrich_stats = function(enrichment_result) +{ + stats = as.data.frame(enrichment_result@statistics@statistic_data) + stats$ID = enrichment_results@statistics@annotation_id + stats$description = enrichment_result@annotation@description[stats$ID] + return(stats) +} diff --git a/R/gocats.R b/R/gocats.R new file mode 100644 index 0000000..4a22fd4 --- /dev/null +++ b/R/gocats.R @@ -0,0 +1,81 @@ +#' gocats to annnotations +#' +#' Transforms a gocats ancestors JSON list to a GO annotation object. +#' +#' @param ancestors_file the ancestors.json file from gocats (required) +#' @param namespace_file the namespace.json file from gocats (optional) +#' @param annotation_type what annotations are we making? (gocatsGO by default) +#' @param feature_type what type of features are we using (assume Uniprot) +#' @param feature_translation a data.frame used to convert the feature IDs +#' +#' @return annotation object +#' @export +gocats_to_annotation = function(ancestors_file = "ancestors.json", + namespace_file = "namespace.json", + annotation_type = "gocatsGO", + feature_type = "Uniprot", + feature_translation = NULL) +{ + stopifnot(file.exists(ancestors_file)) + + ancestors = jsonlite::fromJSON(ancestors_file) + + if (!is.null(feature_translation)) { + if (!inherits(feature_translation, "data.frame")) { + stop("feature_translation must be a data.frame!") + } + if (!all(names(feature_translation) %in% c("from", "to"))) { + stop("feature_translation must contain the columns 'from' and 'to'!") + } else { + match_names = intersect(feature_translation$from, names(ancestors)) + + ancestors = ancestors[match_names] + feature_translation = feature_translation[feature_translation$from %in% match_names, ] + translations = feature_translation$to + names(translations) = feature_translation$from + translations = translations[match_names] + names(ancestors) = translations + } + + } + + go_2_gene = Biobase::reverseSplit(ancestors) + go_2_gene = purrr::map(go_2_gene, unique) + + if (!is.null(namespace_file)) { + if (!file.exists(namespace_file)) { + message(paste0(namespace_file, " does not exist. GO namespace will not be updated.")) + namespaces_short = character(0) + } else { + namespaces = jsonlite::fromJSON(namespace_file) |> unlist() + namespaces_short = gsub("biological_process", "BP", namespaces) + namespaces_short = gsub("molecular_function", "MF", namespaces_short) + namespaces_short = gsub("cellular_component", "CC", namespaces_short) + } + } else { + namespaces_short = character(0) + } + + + if (requireNamespace("GO.db", quietly = TRUE)) { + descriptions = suppressMessages(AnnotationDbi::select(GO.db::GO.db, keys = names(go_2_gene), columns = "TERM", keytype = "GOID")$TERM) + names(descriptions) = names(go_2_gene) + } else { + message("GO.db is not installed, no descriptions will be added to the GO terms.") + descriptions = character(0) + } + + if ((length(namespaces_short) > 0) && (length(descriptions) > 0)) { + namespaces_short = namespaces_short[names(go_2_gene)] + descriptions = descriptions[names(go_2_gene)] + descriptions = paste0(namespaces_short, ":", descriptions) + names(descriptions) = names(go_2_gene) + } + + out_annotation = annotation(annotation_features = go_2_gene, + annotation_type = annotation_type, + description = descriptions, + feature_type = feature_type) + return(out_annotation) + +} diff --git a/R/managing_annotations.R b/R/managing_annotations.R index 8cb31a1..1f68eb9 100644 --- a/R/managing_annotations.R +++ b/R/managing_annotations.R @@ -251,59 +251,3 @@ json_annotation_reversal <- function(json_file, out_file = "annotations.json", out_json <- annotation_2_json(out_annotation, out_file) out_json } - - -#' annotation from GOcats -#' -#' Given a JSON file of features to annotations from GOcats, reverse to turn it into -#' annotations to features, and optionally add some meta-information about them. -#' -#' @param json_file the json file to use -#' @param feature_type the type of features -#' @param annotation_type the type of annotations -#' -#' @importFrom jsonlite fromJSON -#' @export -#' @return an annotation object -#' -gocats_to_annotation <- function(json_file, - feature_type = NULL, - annotation_type = NULL){ - stopifnot(file.exists(json_file)) - - in_annotation <- jsonlite::fromJSON(json_file, simplifyVector = FALSE, flatten = TRUE) - if (length(in_annotation) == 1) { - in_annotation <- in_annotation[[1]] - } - - if (!is.null(in_annotation$Annotations)) { - gene_annotations <- in_annotation$Annotations - } else { - gene_annotations <- in_annotation # we assume that if there is no Annotation - # specific entry, then it is probably just the - # gene annotations, and grab them all. - } - - if (!is.null(in_annotation$Description)) { - annotation_description <- in_annotation$Description - if (is.list(annotation_description)) { - annotation_description <- unlist(annotation_description, use.names = TRUE) - } else { - warning("Description must be a named list! Removing Descriptions!") - annotation_description <- character(0) - } - } else { - annotation_description <- character(0) - } - - rev_annotation <- Biobase::reverseSplit(gene_annotations) - rev_annotation <- purrr::map(rev_annotation, unique) - - out_annotation <- annotation(annotation_features = rev_annotation, - description = annotation_description, - links = character(0), - annotation_type = annotation_type, - feature_type = feature_type) - - out_annotation -} diff --git a/inst/extdata/test_data/ancestors.json.gz b/inst/extdata/test_data/ancestors.json.gz new file mode 100644 index 0000000..5b455d9 Binary files /dev/null and b/inst/extdata/test_data/ancestors.json.gz differ diff --git a/inst/extdata/test_data/namespace.json.gz b/inst/extdata/test_data/namespace.json.gz new file mode 100644 index 0000000..817de3f Binary files /dev/null and b/inst/extdata/test_data/namespace.json.gz differ diff --git a/man/extract_enrich_stats.Rd b/man/extract_enrich_stats.Rd new file mode 100644 index 0000000..67ff8d2 --- /dev/null +++ b/man/extract_enrich_stats.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/combine_enrichments.R +\name{extract_enrich_stats} +\alias{extract_enrich_stats} +\title{add data to graph} +\usage{ +extract_enrich_stats(enrichment_result) +} +\arguments{ +\item{enrichment_result}{the enrichment result object} + +\item{combined_enrichment}{a \code{\link{combined_enrichment}} object} +} +\value{ +data.frame +} +\description{ +given a \code{\link{combined_enrichment}} object, add the data about the significant +and present annotations, their descriptions, links (if present), and all the statistics, +and add that data to the annotation graph object. +} diff --git a/man/gocats_to_annotation.Rd b/man/gocats_to_annotation.Rd index 130ef9d..cb08991 100644 --- a/man/gocats_to_annotation.Rd +++ b/man/gocats_to_annotation.Rd @@ -1,22 +1,31 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/managing_annotations.R +% Please edit documentation in R/gocats.R \name{gocats_to_annotation} \alias{gocats_to_annotation} -\title{annotation from GOcats} +\title{gocats to annnotations} \usage{ -gocats_to_annotation(json_file, feature_type = NULL, annotation_type = NULL) +gocats_to_annotation( + ancestors_file = "ancestors.json", + namespace_file = "namespace.json", + annotation_type = "gocatsGO", + feature_type = "Uniprot", + feature_translation = NULL +) } \arguments{ -\item{json_file}{the json file to use} +\item{ancestors_file}{the ancestors.json file from gocats (required)} -\item{feature_type}{the type of features} +\item{namespace_file}{the namespace.json file from gocats (optional)} -\item{annotation_type}{the type of annotations} +\item{annotation_type}{what annotations are we making? (gocatsGO by default)} + +\item{feature_type}{what type of features are we using (assume Uniprot)} + +\item{feature_translation}{a data.frame used to convert the feature IDs} } \value{ -an annotation object +annotation object } \description{ -Given a JSON file of features to annotations from GOcats, reverse to turn it into -annotations to features, and optionally add some meta-information about them. +Transforms a gocats ancestors JSON list to a GO annotation object. } diff --git a/tests/testthat/test-gocats.R b/tests/testthat/test-gocats.R new file mode 100644 index 0000000..974356c --- /dev/null +++ b/tests/testthat/test-gocats.R @@ -0,0 +1,38 @@ +test_that("gocats annotation importing works", { + ancestors_file = system.file("extdata", "test_data", "ancestors.json.gz", package = "categoryCompare2") + namespace_file = system.file("extdata", "test_data", "namespace.json.gz", package = "categoryCompare2") + + ensembl_keys = AnnotationDbi::keys(org.Hs.eg.db::org.Hs.eg.db, keytype = "ENSEMBL") + ensembl_uniprot = suppressMessages(AnnotationDbi::select(org.Hs.eg.db::org.Hs.eg.db, keys = ensembl_keys, + keytype = "ENSEMBL", columns = c("ENSEMBL", "UNIPROT"))) + names(ensembl_uniprot) = c("to", "from") + ensembl_uniprot = ensembl_uniprot[!(is.na(ensembl_uniprot$to)) & !is.na(ensembl_uniprot$from), ] + bad_translation = ensembl_uniprot + names(bad_translation) = c("other", "one") + list_translation = as.list(ensembl_uniprot) + + expect_error(gocats_to_annotation("random_file")) + expect_warning(gocats_to_annotation(ancestors_file, "random_file"), "does not exist") + expect_error(gocats_to_annotation(ancestors_file, feature_translation = list_translation), "must be a data.frame") + expect_error(gocats_to_annotation(ancestors_file, feature_translation = bad_translation), "must contain the columns") + + without_namespace = gocats_to_annotation(ancestors_file, namespace_file = NULL) + has_bp = all(grepl("^BP|^CC|^MF", without_namespace@description)) + expect_true(!has_bp) + with_namespace = gocats_to_annotation(ancestors_file, namespace_file) + has_namespace = all(grepl("^BP|^CC|^MF", with_namespace@description)) + expect_true(has_namespace) + + expect_equal(without_namespace@annotation_features[[1]][1], "O60313") + expect_equal(without_namespace@annotation_type, "gocatsGO") + expect_equal(without_namespace@feature_type, "Uniprot") + expect_equal(length(without_namespace@annotation_features), 22415) + + with_translation = gocats_to_annotation(ancestors_file, namespace_file, + feature_type = "ENSEMBL", + annotation_type = "whatever", + feature_translation = ensembl_uniprot) + expect_equal(with_translation@annotation_features[[1]][1], "ENSG00000143799") + expect_equal(with_translation@annotation_type, "whatever") + expect_equal(with_translation@feature_type, "ENSEMBL") +})