diff --git a/NAMESPACE b/NAMESPACE index 89e1fec..903501e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(get_monarch_models) export(get_mondo_maps) export(get_ols_options) export(get_ontology) +export(get_ontology_descendants) export(get_ontology_dict) export(get_ontology_levels) export(get_pli) diff --git a/R/0docs.R b/R/0docs.R index 9c490dd..974c8a7 100644 --- a/R/0docs.R +++ b/R/0docs.R @@ -1,3 +1,23 @@ +#### main_ #### +#' @title Main functions +#' +#' @description +#' Documentation for common arguments. +#' @param ont An ontology of class \link[simona]{ontology_DAG}. +#' @param terms A vector of ontology term IDs. +#' @param g \link[tidygraph]{tbl_graph} object. +#' @param force_new Create a new file instead of using any cached files. +#' @param save_dir Directory to save a file to. +#' @param save_path File name to save to. +#' @param obj R object. +#' @family main_ +#' @returns R object. +#' +#' @name main_ +NULL + + + #### utils_ #### #' @title Utility functions #' @@ -16,6 +36,7 @@ NULL #' Functions to query specific subset of data via dedicated APIs. #' @param ids IDs to query. #' @param batch_size Number of IDs to query at once. +#' @inheritParams main_ #' #' @family query_ #' @returns Queried data. @@ -28,10 +49,7 @@ NULL #' #' @description #' Functions to plot data. -#' @param ont An ontology of class \link[simona]{ontology_DAG}. #' @param types Types of graph to produce. Can be one or more. - -#' @param g \link[tidygraph]{tbl_graph} object. #' @param layout_func Layout function for the graph. #' @param node_color_var Variable in the vertex metadata to color nodes by. #' @param edge_color_var Variable in the edge metadata to color edges by. @@ -67,6 +85,8 @@ NULL #' @param size_var Column to scale node size by. #' @param colour_var Column to color nodes by. #' @param ... Additional arguments passed to plot-specific functions. +#' @inheritParams main_ +#' @inheritParams filter_ #' @import simona #' @family plot_ #' @returns A named list containing the plot and the data. @@ -77,11 +97,6 @@ NULL #### get_ #### #' @title Get functions #' -#' @param force_new Create a new file instead of using any cached files. -#' @param save_dir Directory to save a file to. -#' @param filters A named list, where each element in the list is the name of -#' a column in the data, and the vector within each element represents the -#' values to include in the final data. #' @param maps A list of paired to/from types to filter Monarch association #' files by. For example, \code{list(c("gene","disease"))} will return any #' files that contains gene-disease associations. @@ -102,6 +117,7 @@ NULL #' @param run_map_genes Map genes to standardised HGNC symbols using #' \link[orthogene]{map_genes}. #' @param from The designated from column in from-to mapping or relations. +#' @inheritParams main_ #' @inheritParams add_ #' @inheritParams to_ #' @inheritParams map_ @@ -133,6 +149,7 @@ NULL #' @param add_definitions logical, if TRUE, add mondo definition column. #' @param gr A \link[GenomicRanges]{GRanges} object. #' @param build Genome build to use when mapping genomic coordinates. +#' @inheritParams main_ #' @inheritParams to_ #' @inheritParams filter_ #' @inheritParams data.table::merge.data.table @@ -150,12 +167,13 @@ NULL #' #' @description #' Functions to filter objects -#' @param terms Term IDs to include. Can alternatively be an integer, -#' which will be used to randomly sample N terms from the data. #' @param remove_terms Character vector of term IDs to exclude. #' @param use_simona Use \link[simona]{dag_filter} to filter terms. #' @param keep_chr Which chromosomes to keep. #' @param grlist Named list of \link[GenomicRanges]{GRanges} objects. +#' @param filters A named list, where each element in the list is the name of +#' a column in the data, and the vector within each element represents the +#' values to include in the final data. #' @param node_filters A named list of filters to apply to the node data. #' Names should be name of the metadata column, and values should be a vector of #' valid options. For example, \code{list("type" = c("gene","variant"))} will @@ -167,6 +185,7 @@ NULL #' @param remove_descendants Terms whose descendants should be removed #' (including themselves). #' Set to \code{NULL} (default) to skip this filtering step. +#' @inheritParams main_ #' @inheritParams plot_ #' @inheritParams get_ #' @import simona @@ -186,6 +205,7 @@ NULL #' @param as_graph Return the object as a \link[tidygraph]{tbl_graph}. #' @param as_sparse Return the object as a \link[Matrix]{sparseMatrix}. #' @param as_granges Return the object as a \link[GenomicRanges]{GRanges}. +#' @inheritParams main_ #' @inheritParams plot_ #' @inheritParams filter_ #' @import tidygraph @@ -200,6 +220,9 @@ NULL #' #' @description #' Functions to add extra metadata to an ontology or data.table object. +#' @param ont An ontology of class \link[simona]{ontology_DAG}. +#' @param terms A vector of ontology term IDs. +#' @param g \link[tidygraph]{tbl_graph} object. #' @param force_new Add the data again even if the associated column already #' exists. #' @param lvl Depth of the ancestor terms to add. @@ -207,6 +230,7 @@ NULL #' @param add_ancestors Add ancestors for each term. #' @param add_n_edges Add the number of edges (connections) for each term. #' @param add_ontology_levels Add the ontology level for each term. +#' @inheritParams main_ #' @inheritParams plot_ #' @inheritParams simona::dag_ancestors #' @import simona @@ -220,9 +244,7 @@ NULL #' #' @description #' Functions to cache objects in order to speed up processes the second time. -#' @param obj R object. -#' @param path File name to save to. -#' @param save_dir Path to cache directory. +#' @inheritParams main_ #' @inheritParams base::unlink #' @family cache_ #' @returns Null. @@ -234,6 +256,7 @@ NULL #' #' @description #' Functions to merge data resources. +#' @inheritParams main_ #' @inheritParams get_ #' @inheritParams map_ #' @inheritParams to_ diff --git a/R/add_ancestors.R b/R/add_ancestors.R index a68b7e8..16faa66 100644 --- a/R/add_ancestors.R +++ b/R/add_ancestors.R @@ -1,5 +1,4 @@ -#' @describeIn add_ add_ -#' Add ancestor +#' Add ancestors #' #' For each term, get its ancestor at a given level #' and add the ID and name of the ancestor to the ontology metadata. @@ -10,6 +9,10 @@ #' to itself as its own ancestor. #' When a term has multiple ancestors at a given level, #' the first ancestor is arbitrarily chosen. +#' @param prefix A prefix for the ancestor column names. +#' @param fill_na If \code{TRUE}, fill ancestor columns with +#' NA values using the term ID and name. +#' @inheritParams add_ #' @export #' @inheritParams simona::dag_offspring #' @examples diff --git a/R/add_db.R b/R/add_db.R index 6f93289..84b5bef 100644 --- a/R/add_db.R +++ b/R/add_db.R @@ -1,6 +1,8 @@ -#' @describeIn add_ add_ +#' Add database +#' #' Extract a database identifier from an ID column and add it #' as a separate column. +#' @inheritParams add_ #' @param dat data.table or tbl_graph. #' @param input_col Column containing IDs. #' @param output_col Column to add. @@ -27,4 +29,4 @@ add_db <- function(dat, } else { stop("dat must be a tbl_graph or data.table") } -} \ No newline at end of file +} diff --git a/R/add_hoverboxes.R b/R/add_hoverboxes.R index d5db22e..18d7be1 100644 --- a/R/add_hoverboxes.R +++ b/R/add_hoverboxes.R @@ -1,4 +1,3 @@ -#' @describeIn add_ add_ #' Add hoverboxes #' #' A hoverbox is a box of text that shows up when the cursor @@ -6,6 +5,7 @@ #' These can be useful when making interactive network plots #' of the HPO phenotypes because we can include a hoverbox that gives #' information and data associated with each phenotype. +#' @inheritParams add_ #' @param columns Character vector of column names to include in the hoverbox. #' @param hoverbox_column Name of the new hoverbox column to add. #' @param decorators Left and right decorators to add to the hoverbox titles. diff --git a/R/add_ontology_metadata.R b/R/add_ontology_metadata.R index b6e1e09..49bc214 100644 --- a/R/add_ontology_metadata.R +++ b/R/add_ontology_metadata.R @@ -1,6 +1,7 @@ -#' @describeIn add_ add_ +#' Add ontology metadata #' #' Add per-term metadata to ontology. +#' @inheritParams add_ #' @export #' @import simona #' @importFrom Matrix colSums diff --git a/R/cache_dir.R b/R/cache_dir.R index 3e56ae0..44c43bc 100644 --- a/R/cache_dir.R +++ b/R/cache_dir.R @@ -1,4 +1,4 @@ -#' @describeIn utils_ utils_ +#' @describeIn cache_ cache_ #' Cache directory #' #' Provides the path to the package-wide cache directory. diff --git a/R/cache_save.R b/R/cache_save.R index 30e4c02..a9fce25 100644 --- a/R/cache_save.R +++ b/R/cache_save.R @@ -4,10 +4,10 @@ #' @examples #' cache_save(mtcars, tempfile()) cache_save <- function(obj, - path){ - if(!is.null(path)){ - dir.create(dirname(path), showWarnings = FALSE, recursive = TRUE) - messager("Caching file -->",path) - saveRDS(obj, path) + save_path){ + if(!is.null(save_path)){ + dir.create(dirname(save_path), showWarnings = FALSE, recursive = TRUE) + messager("Caching file -->",save_path) + saveRDS(obj, save_path) } } \ No newline at end of file diff --git a/R/get_data.R b/R/get_data.R index fcf06da..e99779e 100644 --- a/R/get_data.R +++ b/R/get_data.R @@ -1,8 +1,8 @@ #' @describeIn utils_ utils_ #' Get remote data #' -#' Download remotely stored data via \link[piggyback]{pb_download}. -#' @param save_dir Directory to save data to. +#' Download remotely stored data via \link[piggyback]{pb_download}. +#' @inheritParams get_ #' @param add_version Add the release version #' to the returned object's \link[base]{attributes} #' @returns Path to downloaded file or the object itself (when ".rds" format). diff --git a/R/get_monarch_kg.R b/R/get_monarch_kg.R index a22f8d4..ddd5c23 100644 --- a/R/get_monarch_kg.R +++ b/R/get_monarch_kg.R @@ -25,13 +25,13 @@ get_monarch_kg <- function(as_graph=TRUE, ...){ files <- get_monarch_files(subdir = "monarch-kg/latest/", queries = "\\.tsv\\.gz") - path <- file.path( + save_path <- file.save_path( save_dir, paste0(gsub("\\.tsv\\.gz","",basename(files$url[1])),".rds")) - if(file.exists(path) & + if(file.exists(save_path) & isFALSE(force_new)){ - messager("Importing",path) - g <- readRDS(path) + messager("Importing",save_path) + g <- readRDS(save_path) return(g) } else { d <- data.table::fread(files$url[1], @@ -39,7 +39,7 @@ get_monarch_kg <- function(as_graph=TRUE, ...) if(isFALSE(as_graph)) return(d) g <- dt_to_kg(d) - cache_save(g,path) + cache_save(g,save_path) } return(g) } diff --git a/R/get_mondo_maps.R b/R/get_mondo_maps.R index b105eed..bcfeee9 100644 --- a/R/get_mondo_maps.R +++ b/R/get_mondo_maps.R @@ -31,24 +31,24 @@ get_mondo_maps <- function(map_types=c("default", top_by=c("subject","object"), save_dir=cache_dir() ){ - path <- subject_label <- object_label <- disease_label <- map_type <- to <- - db <- NULL; requireNamespace("downloadR") + save_path <- subject_label <- object_label <- map_type <- label <- file <- + NULL; if(length(map_types)==1 && map_types=="default"){ - path <- downloadR::downloader( + save_path <- downloadR::downloader( input_url = paste0( "https://github.com/monarch-initiative/mondo/raw/master/", "src/ontology/mappings/mondo.sssom.tsv"), output_dir = save_dir, download_method = "download.file") - map <- data.table::fread(path, + map <- data.table::fread(save_path, skip = "subject", tmpdir = save_dir) data.table::setnames(map, gsub("_id$","",names(map))) - map[,file:=basename(path)] + map[,file:=basename(save_path)] add_db(dat=map, input_col="subject", output_col="subject_db") @@ -63,10 +63,10 @@ get_mondo_maps <- function(map_types=c("default", map <- lapply(stats::setNames(files$link_raw, basename(files$link_raw)), function(x){ - path <- downloadR::downloader(input_url = x, + save_path <- downloadR::downloader(input_url = x, output_dir = save_dir, download_method = "download.file") - data.table::fread(path, + data.table::fread(save_path, skip="subject_id", tmpdir = save_dir) }) |> data.table::rbindlist(fill = TRUE, idcol = "file") diff --git a/R/get_mondo_maps_files.R b/R/get_mondo_maps_files.R index fdbdb3d..be673b4 100644 --- a/R/get_mondo_maps_files.R +++ b/R/get_mondo_maps_files.R @@ -3,7 +3,7 @@ get_mondo_maps_files <- function(map_types, map_to, save_dir){ requireNamespace("echogithub") - path <- map_type <- to <- NULL; + save_path <- map_type <- to <- NULL; files_path <- file.path(save_dir,"mondo_maps.csv.gz") if(!file.exists(files_path)){ @@ -15,13 +15,13 @@ get_mondo_maps_files <- function(map_types, files <- data.table::fread(files_path) } #### Filter map types #### - files[,map_type:=data.table::tstrsplit(basename(path),"_",keep=2)] + files[,map_type:=data.table::tstrsplit(basename(save_path),"_",keep=2)] files[is.na(map_type),map_type:="default"] if(!is.null(map_types)){ files <- files[map_type %in% map_types] } #### Filter mapping to #### - files[,to:=data.table::tstrsplit(basename(path),"_|[.]",keep=3)] + files[,to:=data.table::tstrsplit(basename(save_path),"_|[.]",keep=3)] if(!is.null(map_to)) { if("hpo" %in% map_to) { map_to <- c(map_to[map_to!="hpo"], diff --git a/R/get_ontology_descendants.R b/R/get_ontology_descendants.R new file mode 100644 index 0000000..6af7ec4 --- /dev/null +++ b/R/get_ontology_descendants.R @@ -0,0 +1,30 @@ +#' Get ontology descendants +#' +#' Get the descendants of a set of ontology terms. +#' @inheritParams get_ +#' @inheritParams simona::dag_offspring +#' @returns A named list of descendants, where the names are the input terms. +#' @inheritDotParams simona::dag_offspring +#' @export +#' @examples +#' ont <- get_ontology("hp", terms=10) +#' d <- get_ontology_descendants(ont, c("HP:0000001","HP:0000002")) +get_ontology_descendants <- function(ont, + terms, + include_self = TRUE, + ...){ + lapply(terms, function(x){ + message(x) + xt <- map_ontology_terms(ont = ont, + terms = x, + to = 'id') + if(all(is.na(xt))) { + messager("WARNING: The term",x,"was not found in the ontology.") + return(NULL) + } + simona::dag_offspring(ont, + include_self = include_self, + term=xt, + ...) + }) +} diff --git a/R/get_ontology_dict.R b/R/get_ontology_dict.R index 3c089af..a612021 100644 --- a/R/get_ontology_dict.R +++ b/R/get_ontology_dict.R @@ -1,13 +1,17 @@ #' @describeIn get_ get_ #' +#' @param as_datatable Return as a data.table instead of a named vector. #' @export #' @examples #' ont <- get_ontology("hp", terms=10) #' dict <- get_ontology_dict(ont) get_ontology_dict <- function(ont, from="id", - to="name", - include_self=FALSE){ + to=c("name","label","term"), + include_self=FALSE, + as_datatable=FALSE){ + + to <- to[to %in% colnames(ont@elementMetadata)][1] ## Check from col exists if(!from %in% colnames(ont@elementMetadata)){ stopper("Column",from,"not found in ontology metadata.") @@ -16,13 +20,26 @@ get_ontology_dict <- function(ont, if(!to %in% colnames(ont@elementMetadata)){ stopper("Column",to,"not found in ontology metadata.") } - dict <- stats::setNames(ont@elementMetadata[[to]], - ont@elementMetadata[[from]]) - if(isTRUE(include_self)){ - dict <- c(dict, - stats::setNames(ont@elementMetadata[[to]], - ont@elementMetadata[[to]]) - ) - } + + if(isTRUE(as_datatable)){ + #### As data.table #### + dict <- data.table::data.table(ont@elementMetadata)[,from:=get(from)][,to:=get(to)][,c("from","to")] + if(isTRUE(include_self)){ + dict <- rbind(dict, + data.table::data.table(ont@elementMetadata)[,from:=get(to)][,to:=get(to)][,c("from","to")]) + } + dict <- unique(dict) + data.table::setkeyv(dict, c("from")) + } else { + #### As named vector #### + dict <- stats::setNames(ont@elementMetadata[[to]], + ont@elementMetadata[[from]]) + if(isTRUE(include_self)){ + dict <- c(dict, + stats::setNames(ont@elementMetadata[[to]], + ont@elementMetadata[[to]]) + ) + } + } return(dict) } diff --git a/R/get_ontology_github.R b/R/get_ontology_github.R index 309de74..92d1405 100644 --- a/R/get_ontology_github.R +++ b/R/get_ontology_github.R @@ -15,8 +15,8 @@ get_ontology_github <- function(name, force_new=FALSE, ...){ - path <- file.path(save_dir,file) - if(!file.exists(path) || isTRUE(force_new)){ + save_path <- file.path(save_dir,file) + if(!file.exists(save_path) || isTRUE(force_new)){ #### From Releases #### requireNamespace("piggyback") if(is.null(tag)){ @@ -30,10 +30,10 @@ get_ontology_github <- function(name, messager("Preparing ontology_index object from:",URL) ont <- simona::import_ontology(file=URL, ...) - saveRDS(ont,path) + saveRDS(ont,save_path) } else { - messager("Importing cached file:",path) - ont <- readRDS(path) + messager("Importing cached file:",save_path) + ont <- readRDS(save_path) } return(ont) } diff --git a/R/get_ontology_url.R b/R/get_ontology_url.R index cecd756..af306df 100644 --- a/R/get_ontology_url.R +++ b/R/get_ontology_url.R @@ -3,13 +3,13 @@ get_ontology_url <- function(URL, import_func=simona::import_ontology, force_new=FALSE, ...){ - path <- file.path(save_dir,basename(URL)) - if(file.exists(path) && !isTRUE(force_new)){ - messager("Importing cached file:",path) - ont <- readRDS(path) + save_path <- file.path(save_dir,basename(URL)) + if(file.exists(save_path) && !isTRUE(force_new)){ + messager("Importing cached file:",save_path) + ont <- readRDS(save_path) return(ont) } ont <- import_func(URL, ...) - saveRDS(ont, path) + saveRDS(ont, save_path) return(ont) } \ No newline at end of file diff --git a/R/get_prevalence_orphanet.R b/R/get_prevalence_orphanet.R index 3911815..20ce2cf 100644 --- a/R/get_prevalence_orphanet.R +++ b/R/get_prevalence_orphanet.R @@ -3,9 +3,9 @@ get_prevalence_orphanet <- function(agg_by=c("mondo_id","id","Name"), prevalence_numerator <- prevalence <- prevalence_mean <- Prevalence.ValMoy <- prevalence_denominator <- OrphaCode <- disease_id <- NULL; ## Open in Excel and convert to CSV first ## - path <- system.file("extdata", "orphanet_epidemiology.csv.gz", + save_path <- system.file("extdata", "orphanet_epidemiology.csv.gz", package = "KGExplorer") - d <- data.table::fread(path) + d <- data.table::fread(save_path) #### Fix column names #### names(d) <- trimws(whitespace = "[.]", gsub("[.]+",".", diff --git a/R/map_ontology_terms.R b/R/map_ontology_terms.R index 17122ba..ae686cd 100644 --- a/R/map_ontology_terms.R +++ b/R/map_ontology_terms.R @@ -10,46 +10,65 @@ #' sometimes in a different order. #' @param invert Invert the keys/values of the dictionary, #' such that the key becomes the values (and vice versa). -#' @returns Character vector +#' @param ignore_case Ignore case when mapping terms. +#' @param verbose Print messages. +#' @returns Character vector. #' #' @export #' @examples #' ont <- get_ontology("hp") -#' terms <- c("Focal motor seizure","HP:0000002","HP:0000003") +#' terms <- c("Focal motor seizure", +#' "Focal MotoR SEIzure", +#' "HP:0000002","HP:0000003") #' term_names <- map_ontology_terms(ont=ont, terms=terms) #' term_ids <- map_ontology_terms(ont=ont, terms=terms, to="id") map_ontology_terms <- function(ont, terms = NULL, to=c("name","id"), keep_order = TRUE, - invert = FALSE){ + invert = FALSE, + ignore_case=TRUE, + verbose=1){ + from <- NULL; to <- match.arg(to) if(!is.null(terms)) terms <- as.character(terms) terms_og <- terms terms <- unique(terms) #### to IDs ### if(to=="id"){ - messager("Translating all terms to HPO IDs.") - dict <- get_ontology_dict(ont, - from="name", - to=to, - include_self=TRUE) + messager("Translating ontology terms to ids.",v=verbose) + map <- get_ontology_dict(ont, + from="name", + to=to, + as_datatable=TRUE, + include_self=TRUE) } else { #### to names ### - messager("Translating all terms to names.") - dict <- get_ontology_dict(ont, - from="id", - to=to, - include_self=TRUE) - } - out <- dict[terms] + messager("Translating ontology terms to names.",v=verbose) + map <- get_ontology_dict(ont, + from="id", + to=to, + as_datatable=TRUE, + include_self=TRUE) + } + if(isTRUE(ignore_case)){ + map <- rbind( + map, + data.table::copy(map)[,from:=tolower(from)] + ) |> unique() + } + data.table::setkey(map,"from") + out <- stats::setNames( + map[tolower(terms)]$to, + terms + ) #### Return #### if(isFALSE(keep_order)){ messager("+ Returning a dictionary of terms", - "(different order as input).") + "(different order as input).",v=verbose>1) } else { messager("+ Returning a vector of terms", - "(same order as input).") + "(same order as input).",v=verbose>1) out <- out[terms_og] } #### Invert #### diff --git a/R/map_upheno.R b/R/map_upheno.R index 3f6c4c0..1cc26fc 100644 --- a/R/map_upheno.R +++ b/R/map_upheno.R @@ -24,8 +24,6 @@ #' \itemize{ #' \item{"monarch"}{Use Monarch's gene-to-gene mappings.} #' } -#' @param force_new Force new data to be downloaded and processed. -#' @param subset_db1 Subset of ontologies to include in the plot. #' @param save_dir Directory to save cached data. #' @param show_plot Show the plot. #' @param fill_scores Fill missing scores in the "equivalence_score" and @@ -43,7 +41,8 @@ #' } map_upheno <- function(pheno_map_method=c("upheno","monarch"), gene_map_method=c("monarch"), - subset_db1=c("HP"), + filters=list(db1="HP", + gene_taxon_label1="Homo sapiens"), terms=NULL, fill_scores=NULL, show_plot=TRUE, @@ -57,7 +56,7 @@ map_upheno <- function(pheno_map_method=c("upheno","monarch"), terms=terms) #### Plots #### plots <- plot_upheno(pheno_map_genes_match, - subset_db1=subset_db1) + filters=filters) #### Return #### return( list(data=pheno_map_genes_match, diff --git a/R/map_upheno_data.R b/R/map_upheno_data.R index 5b33a4b..e5b7313 100644 --- a/R/map_upheno_data.R +++ b/R/map_upheno_data.R @@ -31,12 +31,12 @@ map_upheno_data <- function(pheno_map_method=c("upheno","monarch"), save_dir=cache_dir(), force_new=FALSE){ #### Check for cached data #### - path <- file.path(save_dir,"pheno_map_genes_match.rds") - if(file.exists(path) && + save_path <- file.path(save_dir,"pheno_map_genes_match.rds") + if(file.exists(save_path) && isFALSE(force_new)){ ## Read from cache - messager("Importing cached data:",path) - pheno_map_genes_match <- readRDS(path) + messager("Importing cached data:",save_path) + pheno_map_genes_match <- readRDS(save_path) } else { ## Create pheno_map_genes_match <- lapply( @@ -50,10 +50,10 @@ map_upheno_data <- function(pheno_map_method=c("upheno","monarch"), terms=terms) }) |> data.table::rbindlist(fill=TRUE, idcol = "pheno_map_method") ## Save - messager("Caching processed file -->",path) + messager("Caching processed file -->",save_path) attr(pheno_map_genes_match,"version") <- format(Sys.Date(), "%Y-%m-%d") dir.create(save_dir, showWarnings = FALSE, recursive = TRUE) - saveRDS(pheno_map_genes_match,path) + saveRDS(pheno_map_genes_match,save_path) } ## Return return(pheno_map_genes_match) diff --git a/R/plot_graph_visnetwork.R b/R/plot_graph_visnetwork.R index 3749820..c917940 100644 --- a/R/plot_graph_visnetwork.R +++ b/R/plot_graph_visnetwork.R @@ -130,7 +130,7 @@ plot_graph_visnetwork <- function(g, } #### Save network #### plot_save(plt=visnet, - path=save_path) + save_path=save_path) return(list( data=g, plot=visnet diff --git a/R/plot_ontology_heatmap.R b/R/plot_ontology_heatmap.R index 30f78cc..864af52 100644 --- a/R/plot_ontology_heatmap.R +++ b/R/plot_ontology_heatmap.R @@ -113,7 +113,7 @@ plot_ontology_heatmap <- function(ont, #### Save plot #### if(!is.null(save_path)){ plot_save(plt = hm, - path = save_path, + save_path = save_path, height = height, width = width) } diff --git a/R/plot_save.R b/R/plot_save.R index a2fc40d..7c3d078 100644 --- a/R/plot_save.R +++ b/R/plot_save.R @@ -4,45 +4,45 @@ #' #' @export plot_save <- function(plt, - path, + save_path=save_path, width=NULL, height=width){ - if(is.null(path)) return(NULL) + if(is.null(save_path)) return(NULL) requireNamespace("grDevices") - messager("Saving plot -->",path) - dir.create(dirname(path), recursive = TRUE, showWarnings = FALSE) + messager("Saving plot -->",save_path) + dir.create(dirname(save_path), recursive = TRUE, showWarnings = FALSE) if(methods::is(plt,"gg")||methods::is(plt,"patchwork")){ if(is.null(width)) width <- NA if(is.null(height)) height <- NA - ggplot2::ggsave(filename = path, + ggplot2::ggsave(filename = save_path, plot = plt, width = width, height = height) - } else if(endsWith(path,".pdf")){ + } else if(endsWith(save_path,".pdf")){ if(is.null(width)) width <- 7 if(is.null(height)) height <- 7 { - grDevices::pdf(file = path, + grDevices::pdf(file = save_path, width = width, height = height) methods::show(plt) grDevices::dev.off() } - } else if(endsWith(path,".png")){ + } else if(endsWith(save_path,".png")){ if(is.null(width)) width <- 480 if(is.null(height)) height <- 480 { - grDevices::png(file = path, + grDevices::png(file = save_path, width = width, height = height) methods::show(plt) grDevices::dev.off() } - } else if(endsWith(path,".html")){ + } else if(endsWith(save_path,".html")){ visNetwork::visSave(plt, - file = path, + file = save_path, selfcontained = TRUE, background = "transparent") } diff --git a/R/plot_upheno.R b/R/plot_upheno.R index f7f8ec1..a7bc4c5 100644 --- a/R/plot_upheno.R +++ b/R/plot_upheno.R @@ -2,10 +2,11 @@ #' #' Generate multiple kinds of plots summarising mappings of phenotypes and genes #' between the HPO and various non-human ontologies within uPheno. +#' @inheritParams plot_ +#' @inheritParams filter_ #' @param types A character vector of plot types to generate. #' @param pheno_map_genes_match uPheno cross-species mapping data generated by #' \link[KGExplorer]{map_upheno_data}. -#' @param subset_db1 A character vector of reference ontologies to subset #' \code{pheno_map_genes_match} by. #' @returns A named list of \pkg{ggplot2} objects. #' diff --git a/R/prune_ancestors.R b/R/prune_ancestors.R index 25d4e5a..6ff999f 100644 --- a/R/prune_ancestors.R +++ b/R/prune_ancestors.R @@ -3,6 +3,8 @@ #' Prune redundant ancestral terms from a \link{data.table}. #' @export #' @param dat A \link{data.table} with a column of ontology terms. +#' @param id_col The name of the column containing ontology term IDs. +#' @inheritParams filter_ #' @examples #' dat <- data.table::data.table(hpo_id=c("HP:0000001","HP:0000002","HP:0000003"), #' name=c("term1","term2","term3")) @@ -29,6 +31,6 @@ prune_ancestors <- function(dat, return(x) } }) |> unlist() - messager(length(pruned_ids),"/",length(ids),"terms were pruned.") + messager(length(pruned_ids),"/",length(ids),"terms were kept after pruning.") return(dat[get(id_col) %in% pruned_ids]) } diff --git a/man/add_.Rd b/man/add_.Rd index 3a81f6e..8a45132 100644 --- a/man/add_.Rd +++ b/man/add_.Rd @@ -1,95 +1,20 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/0docs.R, R/add_ancestors.R, R/add_db.R, -% R/add_hoverboxes.R, R/add_ontology_metadata.R +% Please edit documentation in R/0docs.R \name{add_} \alias{add_} -\alias{add_ancestors} -\alias{add_db} -\alias{add_hoverboxes} -\alias{add_ontology_metadata} \title{Add functions} -\usage{ -add_ancestors( - ont, - lvl = 2, - include_self = TRUE, - prefix = "ancestor", - fill_na = TRUE, - force_new = FALSE -) - -add_db( - dat, - input_col, - output_col = paste0(input_col, "_db"), - split = ":", - keep = 1 -) - -add_hoverboxes( - g, - columns = get_graph_colnames(g), - hoverbox_column = c("hover", "title", "label"), - width = 60, - digits = 3, - decorators = c("", ""), - as_html = TRUE, - force_new = FALSE -) - -add_ontology_metadata( - ont, - add_ancestors = 2, - add_n_edges = TRUE, - add_ontology_levels = TRUE -) -} \arguments{ \item{ont}{An ontology of class \link[simona]{ontology_DAG}.} -\item{lvl}{Depth of the ancestor terms to add. -Will get the closest ancestor to this level if none have this exact level.} +\item{terms}{A vector of ontology term IDs.} -\item{include_self}{For \code{dag_offspring()} and \code{dag_ancestors()}, this controls whether to also include the query term itself.} +\item{g}{\link[tidygraph]{tbl_graph} object.} \item{force_new}{Add the data again even if the associated column already exists.} -\item{dat}{data.table or tbl_graph.} - -\item{input_col}{Column containing IDs.} - -\item{output_col}{Column to add.} - -\item{split}{ - character vector (or object which can be coerced to such) - containing \link[base]{regular expression}(s) (unless \code{fixed = TRUE}) - to use for splitting. If empty matches occur, in particular if - \code{split} has length 0, \code{x} is split into single characters. - If \code{split} has length greater than 1, it is re-cycled along - \code{x}. - } - -\item{keep}{Specify indices corresponding to just those list elements to retain in the transposed result. Default is to return all.} - -\item{g}{ggnetwork object -(or an igraph/tbl_graph to be converted to ggnetwork format).} - -\item{columns}{Character vector of column names to include in the hoverbox.} - -\item{hoverbox_column}{Name of the new hoverbox column to add.} - -\item{width}{Positive integer giving target line width (in number of -characters). A width less than or equal to 1 will put each word on its -own line.} - -\item{digits}{integer indicating the number of decimal places - (\code{round}) or significant digits (\code{signif}) to be used. - For \code{round}, negative values are allowed (see \sQuote{Details}).} - -\item{decorators}{Left and right decorators to add to the hoverbox titles.} - -\item{as_html}{Provide the hoverbox in HTML format.} +\item{lvl}{Depth of the ancestor terms to add. +Will get the closest ancestor to this level if none have this exact level.} \item{add_ancestors}{Add ancestors for each term.} @@ -99,52 +24,8 @@ own line.} } \value{ Added data. - -tidygraph with additional metadata column named "hover". } \description{ Functions to add extra metadata to an ontology or data.table object. } -\section{Functions}{ -\itemize{ -\item \code{add_ancestors()}: add_ -Add ancestor - -For each term, get its ancestor at a given level -and add the ID and name of the ancestor to the ontology metadata. -By default, lower numbers are higher in the ontology - (e.g. level 0 includes the most broad ontology term). -When a terms does not have an ancestor at a given level - (e.g. \code{lvl=3} but the term itself is at level 1) the term is assigned - to itself as its own ancestor. -When a term has multiple ancestors at a given level, -the first ancestor is arbitrarily chosen. - -\item \code{add_db()}: add_ -Extract a database identifier from an ID column and add it -as a separate column. - -\item \code{add_hoverboxes()}: add_ -Add hoverboxes - -A hoverbox is a box of text that shows up when the cursor - hovers over something. -These can be useful when making interactive network plots -of the HPO phenotypes because we can include a hoverbox that gives -information and data associated with each phenotype. - -\item \code{add_ontology_metadata()}: add_ - -Add per-term metadata to ontology. - -}} -\examples{ -ont <- get_ontology("hp") -ont2 <- add_ancestors(ont, lvl=4) -ont <- get_ontology("hp") -g <- ontology_to(ont, to="tbl_graph") -g2 <- add_hoverboxes(g) -ont <- get_ontology("hp", terms=10) -ont2 <- add_ontology_metadata(ont) -} \concept{add_} diff --git a/man/add_ancestors.Rd b/man/add_ancestors.Rd new file mode 100644 index 0000000..1f44b8c --- /dev/null +++ b/man/add_ancestors.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_ancestors.R +\name{add_ancestors} +\alias{add_ancestors} +\title{Add ancestors} +\usage{ +add_ancestors( + ont, + lvl = 2, + include_self = TRUE, + prefix = "ancestor", + fill_na = TRUE, + force_new = FALSE +) +} +\arguments{ +\item{ont}{An ontology of class \link[simona]{ontology_DAG}.} + +\item{lvl}{Depth of the ancestor terms to add. +Will get the closest ancestor to this level if none have this exact level.} + +\item{include_self}{For \code{dag_offspring()} and \code{dag_ancestors()}, this controls whether to also include the query term itself.} + +\item{prefix}{A prefix for the ancestor column names.} + +\item{fill_na}{If \code{TRUE}, fill ancestor columns with +NA values using the term ID and name.} + +\item{force_new}{Add the data again even if the associated column already +exists.} +} +\description{ +For each term, get its ancestor at a given level +and add the ID and name of the ancestor to the ontology metadata. +By default, lower numbers are higher in the ontology + (e.g. level 0 includes the most broad ontology term). +When a terms does not have an ancestor at a given level + (e.g. \code{lvl=3} but the term itself is at level 1) the term is assigned + to itself as its own ancestor. +When a term has multiple ancestors at a given level, +the first ancestor is arbitrarily chosen. +} +\examples{ +ont <- get_ontology("hp") +ont2 <- add_ancestors(ont, lvl=4) +} diff --git a/man/add_db.Rd b/man/add_db.Rd new file mode 100644 index 0000000..6d65b53 --- /dev/null +++ b/man/add_db.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_db.R +\name{add_db} +\alias{add_db} +\title{Add database} +\usage{ +add_db( + dat, + input_col, + output_col = paste0(input_col, "_db"), + split = ":", + keep = 1 +) +} +\arguments{ +\item{dat}{data.table or tbl_graph.} + +\item{input_col}{Column containing IDs.} + +\item{output_col}{Column to add.} + +\item{split}{ + character vector (or object which can be coerced to such) + containing \link[base]{regular expression}(s) (unless \code{fixed = TRUE}) + to use for splitting. If empty matches occur, in particular if + \code{split} has length 0, \code{x} is split into single characters. + If \code{split} has length greater than 1, it is re-cycled along + \code{x}. + } + +\item{keep}{Specify indices corresponding to just those list elements to retain in the transposed result. Default is to return all.} +} +\description{ +Extract a database identifier from an ID column and add it +as a separate column. +} diff --git a/man/add_hoverboxes.Rd b/man/add_hoverboxes.Rd new file mode 100644 index 0000000..115a3e8 --- /dev/null +++ b/man/add_hoverboxes.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_hoverboxes.R +\name{add_hoverboxes} +\alias{add_hoverboxes} +\title{Add hoverboxes} +\usage{ +add_hoverboxes( + g, + columns = get_graph_colnames(g), + hoverbox_column = c("hover", "title", "label"), + width = 60, + digits = 3, + decorators = c("", ""), + as_html = TRUE, + force_new = FALSE +) +} +\arguments{ +\item{g}{\link[tidygraph]{tbl_graph} object.} + +\item{columns}{Character vector of column names to include in the hoverbox.} + +\item{hoverbox_column}{Name of the new hoverbox column to add.} + +\item{width}{Positive integer giving target line width (in number of +characters). A width less than or equal to 1 will put each word on its +own line.} + +\item{digits}{integer indicating the number of decimal places + (\code{round}) or significant digits (\code{signif}) to be used. + For \code{round}, negative values are allowed (see \sQuote{Details}).} + +\item{decorators}{Left and right decorators to add to the hoverbox titles.} + +\item{as_html}{Provide the hoverbox in HTML format.} + +\item{force_new}{Add the data again even if the associated column already +exists.} +} +\value{ +tidygraph with additional metadata column named "hover". +} +\description{ +A hoverbox is a box of text that shows up when the cursor + hovers over something. +These can be useful when making interactive network plots +of the HPO phenotypes because we can include a hoverbox that gives +information and data associated with each phenotype. +} +\examples{ +ont <- get_ontology("hp") +g <- ontology_to(ont, to="tbl_graph") +g2 <- add_hoverboxes(g) +} diff --git a/man/add_ontology_metadata.Rd b/man/add_ontology_metadata.Rd new file mode 100644 index 0000000..fa0a1bc --- /dev/null +++ b/man/add_ontology_metadata.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add_ontology_metadata.R +\name{add_ontology_metadata} +\alias{add_ontology_metadata} +\title{Add ontology metadata} +\usage{ +add_ontology_metadata( + ont, + add_ancestors = 2, + add_n_edges = TRUE, + add_ontology_levels = TRUE +) +} +\arguments{ +\item{ont}{An ontology of class \link[simona]{ontology_DAG}.} + +\item{add_ancestors}{Add ancestors for each term.} + +\item{add_n_edges}{Add the number of edges (connections) for each term.} + +\item{add_ontology_levels}{Add the ontology level for each term.} +} +\description{ +Add per-term metadata to ontology. +} +\examples{ +ont <- get_ontology("hp", terms=10) +ont2 <- add_ontology_metadata(ont) +} diff --git a/man/cache_.Rd b/man/cache_.Rd index e084e6c..2b5efbc 100644 --- a/man/cache_.Rd +++ b/man/cache_.Rd @@ -1,21 +1,24 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/0docs.R, R/cache_clear.R, R/cache_files.R, -% R/cache_save.R +% Please edit documentation in R/0docs.R, R/cache_clear.R, R/cache_dir.R, +% R/cache_files.R, R/cache_save.R \name{cache_} \alias{cache_} \alias{cache_clear} +\alias{cache_dir} \alias{cache_files} \alias{cache_save} \title{Cache functions} \usage{ cache_clear(save_dir = cache_dir(), recursive = TRUE, ...) +cache_dir(package = "KGExplorer") + cache_files(save_dir = cache_dir(), recursive = TRUE, ...) -cache_save(obj, path) +cache_save(obj, save_path) } \arguments{ -\item{save_dir}{Path to cache directory.} +\item{save_dir}{Directory to save a file to.} \item{recursive}{logical. Should directories be deleted recursively?} @@ -30,13 +33,17 @@ cache_save(obj, path) tilde (see \code{\link[base]{path.expand}}) be expanded?} }} +\item{package}{a character string giving the name of an \R package} + \item{obj}{R object.} -\item{path}{File name to save to.} +\item{save_path}{File name to save to.} } \value{ Null. +Cache directory path. + Null. } \description{ @@ -49,6 +56,11 @@ Clear cache Remove all data cached by the R package. +\item \code{cache_dir()}: cache_ +Cache directory + +Provides the path to the package-wide cache directory. + \item \code{cache_files()}: cache_ List cache @@ -62,6 +74,7 @@ Save cache. \dontrun{ cache_clear() } +save_dir <- cache_dir \dontrun{ cache_files() } diff --git a/man/filter_.Rd b/man/filter_.Rd index c833fda..3d898b8 100644 --- a/man/filter_.Rd +++ b/man/filter_.Rd @@ -52,8 +52,7 @@ filter_ontology( a column in the data, and the vector within each element represents the values to include in the final data.} -\item{g}{ggnetwork object -(or an igraph/tbl_graph to be converted to ggnetwork format).} +\item{g}{\link[tidygraph]{tbl_graph} object.} \item{node_filters}{A named list of filters to apply to the node data. Names should be name of the metadata column, and values should be a vector of @@ -64,8 +63,7 @@ return any rows where the "type" column contains either "gene" or "variant".} \item{ont}{An ontology of class \link[simona]{ontology_DAG}.} -\item{terms}{Term IDs to include. Can alternatively be an integer, -which will be used to randomly sample N terms from the data.} +\item{terms}{A vector of ontology term IDs.} \item{remove_terms}{Character vector of term IDs to exclude.} diff --git a/man/get_.Rd b/man/get_.Rd index cdb701f..9537697 100644 --- a/man/get_.Rd +++ b/man/get_.Rd @@ -148,7 +148,13 @@ get_ontology( ... ) -get_ontology_dict(ont, from = "id", to = "name", include_self = FALSE) +get_ontology_dict( + ont, + from = "id", + to = c("name", "label", "term"), + include_self = FALSE, + as_datatable = FALSE +) get_ontology_levels( ont, @@ -281,8 +287,7 @@ return any rows where the "type" column contains either "gene" or "variant".} \item{\code{tag}}{tag for the GitHub release to which this data should be attached.} }} -\item{g}{ggnetwork object -(or an igraph/tbl_graph to be converted to ggnetwork format).} +\item{g}{\link[tidygraph]{tbl_graph} object.} \item{what}{What should get activated? Possible values are \code{nodes} or \code{edges}.} @@ -311,7 +316,7 @@ together. Otherwise, returns a named list of separated a column in the data, and the vector within each element represents the values to include in the final data.} -\item{input_col}{Column containing IDs.} +\item{input_col}{Column name of input IDs.} \item{map_orthologs}{Add gene-level data.} @@ -337,8 +342,7 @@ Can be a character vector of one or more column names.} \item{"depth"} \link[simona]{dag_depth}. }} -\item{terms}{Term IDs to include. Can alternatively be an integer, -which will be used to randomly sample N terms from the data.} +\item{terms}{A vector of ontology term IDs.} \item{add_metadata}{Add metadata to the resulting ontology object.} @@ -350,6 +354,8 @@ which will be used to randomly sample N terms from the data.} \item{include_self}{For \code{dag_offspring()} and \code{dag_ancestors()}, this controls whether to also include the query term itself.} +\item{as_datatable}{Return as a data.table instead of a named vector.} + \item{remove_terms}{Character vector of term IDs to exclude.} \item{absolute}{Make the levels absolute in the sense that they consider diff --git a/man/get_ontology_descendants.Rd b/man/get_ontology_descendants.Rd new file mode 100644 index 0000000..34c7cb2 --- /dev/null +++ b/man/get_ontology_descendants.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_ontology_descendants.R +\name{get_ontology_descendants} +\alias{get_ontology_descendants} +\title{Get ontology descendants} +\usage{ +get_ontology_descendants(ont, terms, include_self = TRUE, ...) +} +\arguments{ +\item{ont}{An ontology of class \link[simona]{ontology_DAG}.} + +\item{terms}{A vector of ontology term IDs.} + +\item{include_self}{For \code{dag_offspring()} and \code{dag_ancestors()}, this controls whether to also include the query term itself.} + +\item{...}{ + Arguments passed on to \code{\link[simona:dag_terms]{simona::dag_offspring}} + \describe{ + \item{\code{dag}}{An \code{ontology_DAG} object.} + \item{\code{term}}{The value can be a vector of multiple term names. If it is a vector, it returns +union of the upstream/downstream terms of the selected set of terms. For \code{dag_siblings()}, +the value can only be a single term.} + \item{\code{in_labels}}{Whether the terms are represented in their names or as integer indices?} + }} +} +\value{ +A named list of descendants, where the names are the input terms. +} +\description{ +Get the descendants of a set of ontology terms. +} +\examples{ +ont <- get_ontology("hp", terms=10) +d <- get_ontology_descendants(ont, c("HP:0000001","HP:0000002")) +} diff --git a/man/get_ontology_robot.Rd b/man/get_ontology_robot.Rd index 415fee4..474ef48 100644 --- a/man/get_ontology_robot.Rd +++ b/man/get_ontology_robot.Rd @@ -10,11 +10,25 @@ get_ontology_robot(...) \item{...}{ Arguments passed on to \code{\link[=get_data]{get_data}} \describe{ - \item{\code{save_dir}}{Directory to save data to.} \item{\code{add_version}}{Add the release version to the returned object's \link[base]{attributes}} - \item{\code{file}}{name or vector of names of files to be downloaded. If \code{NULL}, -all assets attached to the release will be downloaded.} + \item{\code{file}}{Can be one of the following: +\itemize{ +\item{"ontology"}{Creates an \link[simona]{ontology_DAG} R object by + importing the OBO file directly from the official + \href{https://github.com/obophenotype/upheno}{uPheno GitHub repository}.} +\item{"bestmatches"}{Returns a merged table with the best matches between + human and non-human homologous phenotypes (from multiple species). + Distributed by the official + \href{https://github.com/obophenotype/upheno/tree/master/mappings}{ + uPheno GitHub repository}.} +\item{"upheno_mapping"}{Return a merged table with matches between human +and non-human homologous phenotypes (from multiple species). +Distributed by the + \href{https://data.monarchinitiative.org/upheno2/current/upheno-release/all/index.html}{ + Monarch Initiative server}.} +}} + \item{\code{save_dir}}{Directory to save a file to.} \item{\code{repo}}{Repository name in format "owner/repo". Defaults to \code{guess_repo()}.} \item{\code{tag}}{tag for the GitHub release to which this data should be attached.} \item{\code{overwrite}}{Should any local files of the same name be overwritten? diff --git a/man/main_.Rd b/man/main_.Rd new file mode 100644 index 0000000..321c62d --- /dev/null +++ b/man/main_.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/0docs.R +\name{main_} +\alias{main_} +\title{Main functions} +\arguments{ +\item{ont}{An ontology of class \link[simona]{ontology_DAG}.} + +\item{terms}{A vector of ontology term IDs.} + +\item{g}{\link[tidygraph]{tbl_graph} object.} + +\item{force_new}{Create a new file instead of using any cached files.} + +\item{save_dir}{Directory to save a file to.} + +\item{save_path}{File name to save to.} + +\item{obj}{R object.} +} +\value{ +R object. +} +\description{ +Documentation for common arguments. +} +\concept{main_} diff --git a/man/map_.Rd b/man/map_.Rd index 0dad08f..8136c81 100644 --- a/man/map_.Rd +++ b/man/map_.Rd @@ -56,13 +56,15 @@ map_ontology_terms( terms = NULL, to = c("name", "id"), keep_order = TRUE, - invert = FALSE + invert = FALSE, + ignore_case = TRUE, + verbose = 1 ) map_upheno( pheno_map_method = c("upheno", "monarch"), gene_map_method = c("monarch"), - subset_db1 = c("HP"), + filters = list(db1 = "HP", gene_taxon_label1 = "Homo sapiens"), terms = NULL, fill_scores = NULL, show_plot = TRUE, @@ -148,6 +150,10 @@ sometimes in a different order.} \item{invert}{Invert the keys/values of the dictionary, such that the key becomes the values (and vice versa).} +\item{ignore_case}{Ignore case when mapping terms.} + +\item{verbose}{Print messages.} + \item{pheno_map_method}{Method to use for mapping phenotypes across ontologies. \itemize{ \item{"upheno"}{Use uPheno's phenotype-to-phenotype mappings. @@ -162,7 +168,9 @@ Contains more ontologies but with less coverage of phenotypes. \item{"monarch"}{Use Monarch's gene-to-gene mappings.} }} -\item{subset_db1}{Subset of ontologies to include in the plot.} +\item{filters}{A named list, where each element in the list is the name of +a column in the data, and the vector within each element represents the +values to include in the final data.} \item{fill_scores}{Fill missing scores in the "equivalence_score" and "subclass_score" columns with this value. These columns represent the @@ -170,7 +178,7 @@ quality of mapping between two phenotypes on a scale from 0-1.} \item{show_plot}{Show the plot.} -\item{force_new}{Force new data to be downloaded and processed.} +\item{force_new}{Create a new file instead of using any cached files.} \item{gr}{A \link[GenomicRanges]{GRanges} object.} @@ -193,7 +201,7 @@ Mapped data. Mapped \code{dat} -Character vector +Character vector. A list containing the data and plot. } @@ -242,7 +250,9 @@ dt2 <- map_genes_monarch(dat=dat, gene_col="gene") dat <- example_dat(rm_types="gene") dat2 <- map_mondo(dat = dat, map_to="hpo") ont <- get_ontology("hp") -terms <- c("Focal motor seizure","HP:0000002","HP:0000003") +terms <- c("Focal motor seizure", + "Focal MotoR SEIzure", + "HP:0000002","HP:0000003") term_names <- map_ontology_terms(ont=ont, terms=terms) term_ids <- map_ontology_terms(ont=ont, terms=terms, to="id") \dontrun{ diff --git a/man/map_upheno_data.Rd b/man/map_upheno_data.Rd index cbfaed6..e508d06 100644 --- a/man/map_upheno_data.Rd +++ b/man/map_upheno_data.Rd @@ -41,7 +41,7 @@ quality of mapping between two phenotypes on a scale from 0-1.} \item{save_dir}{Directory to save cached data.} -\item{force_new}{Force new data to be downloaded and processed.} +\item{force_new}{Create a new file instead of using any cached files.} } \value{ A data.table containing the mapped data. diff --git a/man/plot_.Rd b/man/plot_.Rd index d2b89fe..a62a823 100644 --- a/man/plot_.Rd +++ b/man/plot_.Rd @@ -146,7 +146,7 @@ plot_ontology_tidygraph(ont, ...) plot_ontology_visnetwork(ont, ...) -plot_save(plt, path, width = NULL, height = width) +plot_save(plt, save_path = save_path, width = NULL, height = width) plot_upheno_heatmap( plot_dat, @@ -338,8 +338,7 @@ as a self-contained HTML file.} \item{ont}{An ontology of class \link[simona]{ontology_DAG}.} -\item{terms}{A subset of HPO IDs to include in the final dataset and plots -(e.g. c("HP:0001508","HP:0001507")).} +\item{terms}{A vector of ontology term IDs.} \item{types}{Types of graph to produce. Can be one or more.} @@ -359,7 +358,7 @@ metadata annotations.} \item{col}{A vector of colors if the color mapping is discrete or a color mapping function if the matrix is continuous numbers (should be generated by \code{\link[circlize]{colorRamp2}}). If the matrix is continuous, the value can also be a vector of colors so that colors can be interpolated. Pass to \code{\link[ComplexHeatmap]{ColorMapping}}. For more details and examples, please refer to \url{https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html#colors} .} -\item{save_dir}{Directory to save cached data.} +\item{save_dir}{Directory to save a file to.} \item{id_col}{Column containing the unique identifier for each node in a graph (e.g. "name").} diff --git a/man/plot_upheno.Rd b/man/plot_upheno.Rd index 366bc4a..4fad79f 100644 --- a/man/plot_upheno.Rd +++ b/man/plot_upheno.Rd @@ -12,12 +12,14 @@ plot_upheno( } \arguments{ \item{pheno_map_genes_match}{uPheno cross-species mapping data generated by -\link[KGExplorer]{map_upheno_data}.} +\link[KGExplorer]{map_upheno_data}. +\code{pheno_map_genes_match} by.} -\item{types}{A character vector of plot types to generate.} +\item{filters}{A named list, where each element in the list is the name of +a column in the data, and the vector within each element represents the +values to include in the final data.} -\item{subset_db1}{A character vector of reference ontologies to subset -\code{pheno_map_genes_match} by.} +\item{types}{A character vector of plot types to generate.} } \value{ A named list of \pkg{ggplot2} objects. diff --git a/man/prune_ancestors.Rd b/man/prune_ancestors.Rd index 480d919..f7dbb52 100644 --- a/man/prune_ancestors.Rd +++ b/man/prune_ancestors.Rd @@ -8,6 +8,10 @@ prune_ancestors(dat, id_col, ont) } \arguments{ \item{dat}{A \link{data.table} with a column of ontology terms.} + +\item{id_col}{The name of the column containing ontology term IDs.} + +\item{ont}{An ontology of class \link[simona]{ontology_DAG}.} } \description{ Prune redundant ancestral terms from a \link{data.table}. diff --git a/man/to_.Rd b/man/to_.Rd index cf1b33c..37ad493 100644 --- a/man/to_.Rd +++ b/man/to_.Rd @@ -42,8 +42,7 @@ to_graph(g, ...) \arguments{ \item{as_sparse}{Return the object as a \link[Matrix]{sparseMatrix}.} -\item{g}{ggnetwork object -(or an igraph/tbl_graph to be converted to ggnetwork format).} +\item{g}{\link[tidygraph]{tbl_graph} object.} \item{id_col}{Column containing the unique identifier for each node in a graph (e.g. "name").} @@ -58,8 +57,7 @@ in a graph (e.g. "name").} \item{to}{A character string specifying the format to convert to.} -\item{terms}{Term IDs to include. Can alternatively be an integer, -which will be used to randomly sample N terms from the data.} +\item{terms}{A vector of ontology term IDs.} \item{remove_terms}{Character vector of term IDs to exclude.} diff --git a/man/utils_.Rd b/man/utils_.Rd index 86828e8..7e37f86 100644 --- a/man/utils_.Rd +++ b/man/utils_.Rd @@ -1,10 +1,8 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/0docs.R, R/cache_dir.R, R/example_dat.R, -% R/get_data.R, R/messager.R, R/report_filter.R, R/report_missing.R, -% R/stopper.R +% Please edit documentation in R/0docs.R, R/example_dat.R, R/get_data.R, +% R/messager.R, R/report_filter.R, R/report_missing.R, R/stopper.R \name{utils_} \alias{utils_} -\alias{cache_dir} \alias{example_dat} \alias{get_data} \alias{messager} @@ -13,8 +11,6 @@ \alias{stopper} \title{Utility functions} \usage{ -cache_dir(package = "KGExplorer") - example_dat( types = c("omim_id", "orphanet_id", "decipher_id", "hpo_id", "gene"), rm_types = NULL @@ -38,18 +34,30 @@ report_missing(dat, id_col, report_col) stopper(..., v = TRUE) } \arguments{ -\item{package}{a character string giving the name of an \R package} - \item{types}{Types of IDs to include in the data.} -\item{file}{name or vector of names of files to be downloaded. If \code{NULL}, -all assets attached to the release will be downloaded.} +\item{file}{Can be one of the following: +\itemize{ +\item{"ontology"}{Creates an \link[simona]{ontology_DAG} R object by + importing the OBO file directly from the official + \href{https://github.com/obophenotype/upheno}{uPheno GitHub repository}.} +\item{"bestmatches"}{Returns a merged table with the best matches between + human and non-human homologous phenotypes (from multiple species). + Distributed by the official + \href{https://github.com/obophenotype/upheno/tree/master/mappings}{ + uPheno GitHub repository}.} +\item{"upheno_mapping"}{Return a merged table with matches between human +and non-human homologous phenotypes (from multiple species). +Distributed by the + \href{https://data.monarchinitiative.org/upheno2/current/upheno-release/all/index.html}{ + Monarch Initiative server}.} +}} \item{tag}{tag for the GitHub release to which this data should be attached.} \item{repo}{Repository name in format "owner/repo". Defaults to \code{guess_repo()}.} -\item{save_dir}{Directory to save data to.} +\item{save_dir}{Directory to save a file to.} \item{add_version}{Add the release version to the returned object's \link[base]{attributes}} @@ -57,6 +65,8 @@ to the returned object's \link[base]{attributes}} \item{overwrite}{Should any local files of the same name be overwritten? default \code{TRUE}.} +\item{...}{Additional arguments passed to plot-specific functions.} + \item{v}{Whether to print messages or not.} \item{parallel}{Whether to enable message print when wrapped @@ -65,8 +75,6 @@ in parallelised functions.} \value{ Merged data. -Cache directory path. - \link[data.table]{data.table} Path to downloaded file or the object itself (when ".rds" format). @@ -80,11 +88,6 @@ Functions to support other functions. } \section{Functions}{ \itemize{ -\item \code{cache_dir()}: utils_ -Cache directory - -Provides the path to the package-wide cache directory. - \item \code{example_dat()}: utils_ Example data @@ -118,7 +121,6 @@ Allows developers to easily control verbosity of functions, }} \examples{ -save_dir <- cache_dir dat <- example_dat() } \concept{utils_} diff --git a/tests/testthat/test-map_ontology_terms.R b/tests/testthat/test-map_ontology_terms.R new file mode 100644 index 0000000..a8a21be --- /dev/null +++ b/tests/testthat/test-map_ontology_terms.R @@ -0,0 +1,23 @@ +test_that("map_ontology_terms works", { + + ont <- get_ontology("hp") + terms <- c("Focal motor seizure", + "Focal MotoR SEIzure", + "HP:0000002","HP:0000003") + term_names <- map_ontology_terms(ont=ont, terms=terms) + term_ids <- map_ontology_terms(ont=ont, terms=terms, to="id") + + + testthat::expect_true( + length(term_names)==length(terms) + ) + testthat::expect_true( + length(term_ids)==length(terms) + ) + testthat::expect_true( + all(names(term_names)==terms) + ) + testthat::expect_true( + all(names(term_ids)==terms) + ) +})