-
Notifications
You must be signed in to change notification settings - Fork 3
New cf_add_meta function #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
cb77c34
441649b
2e450f1
ffeb026
9622d91
41ded20
ffc4252
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -334,6 +334,113 @@ cf_meta <- function(ids, bind.rows=TRUE, integer64=FALSE, keep.all=FALSE, | |||||||
| res | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| #' Add metadata to a dataframe containing neuron keys | ||||||||
| #' | ||||||||
| #' @description Enriches a dataframe with metadata for neurons identified by | ||||||||
| #' key columns. This is useful for adding metadata to partner data or other | ||||||||
| #' results that contain neuron keys. | ||||||||
| #' | ||||||||
| #' @param x A data.frame containing one or more columns with keys | ||||||||
| #' @param keycol Character vector of column names containing keys. | ||||||||
| #' Default \code{"key"}. When multiple columns specified, metadata is joined | ||||||||
| #' for each with corresponding suffixes. | ||||||||
| #' @param suffix Character vector of suffixes for added columns. Must match | ||||||||
| #' length of keycol. Default generates \code{""} for single keycol, or | ||||||||
| #' \code{".1"}, \code{".2"}, etc. for multiple. Use \code{""} for no suffix. | ||||||||
| #' @param cols Character vector of column names to add from metadata. | ||||||||
| #' Default \code{NULL} adds all columns. Warns if any specified columns | ||||||||
| #' are not found in metadata. The \code{key} column is always kept for joining. | ||||||||
| #' @param ... Additional arguments passed to \code{\link{cf_meta}} | ||||||||
| #' | ||||||||
| #' @return data.frame with additional metadata columns | ||||||||
| #' @export | ||||||||
| #' | ||||||||
| #' @examples | ||||||||
| #' \dontrun{ | ||||||||
| #' # Add metadata to partner column only | ||||||||
| #' partners <- cf_partners(cf_ids(hemibrain='DA2_lPN'), threshold=10, details=FALSE) | ||||||||
| #' partners_meta <- cf_add_meta(partners, keycol="post_key") | ||||||||
| #' | ||||||||
| #' # Add metadata to both pre and post | ||||||||
| #' partners_both <- cf_add_meta(partners, | ||||||||
| #' keycol = c("pre_key", "post_key"), | ||||||||
| #' suffix = c(".pre", ".post")) | ||||||||
| #' | ||||||||
| #' # Add only type and side columns | ||||||||
| #' partners_minimal <- cf_add_meta(partners, keycol="post_key", | ||||||||
| #' cols = c("type", "side")) | ||||||||
| #' } | ||||||||
| cf_add_meta <- function(x, keycol = "key", suffix = NULL, cols = NULL, ...) { | ||||||||
| # Validate inputs | ||||||||
| if (!is.data.frame(x) || nrow(x) == 0) { | ||||||||
| return(x) | ||||||||
| } | ||||||||
|
|
||||||||
| missing_cols <- setdiff(keycol, names(x)) | ||||||||
| if (length(missing_cols)) | ||||||||
| stop("keycol column(s) not found in x: ", paste(missing_cols, collapse = ", "), | ||||||||
| "\nHint: use keys(x) to add a '", paste(missing_cols, collapse = "/"), | ||||||||
| "' column, or set keycol= to identify the correct key column.") | ||||||||
|
|
||||||||
| # Set default suffixes | ||||||||
| if (is.null(suffix)) { | ||||||||
| suffix <- if (length(keycol) == 1) "" else paste0(".", seq_along(keycol)) | ||||||||
| } | ||||||||
| if (length(suffix) != length(keycol)) | ||||||||
| stop("Length of suffix must match length of keycol") | ||||||||
|
|
||||||||
| # Extract unique keys and fetch metadata once | ||||||||
| all_keys <- unique(unlist(x[keycol], use.names = FALSE)) | ||||||||
| all_keys <- all_keys[!is.na(all_keys) & nzchar(all_keys)] | ||||||||
|
||||||||
| all_keys <- all_keys[!is.na(all_keys) & nzchar(all_keys)] | |
| all_keys_chr <- as.character(all_keys) | |
| all_keys <- all_keys_chr[!is.na(all_keys_chr) & nzchar(all_keys_chr)] |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,14 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param partners Whether to return inputs or outputs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param MoreArgs Additional arguments in the form of a hierarchical list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' (expert use; see details and examples). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @param details Which neurons to enrich with metadata. Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' \itemize{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' \item \code{"partner"} (default): Add metadata for partner neurons only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' \item \code{"query"}: Add metadata for query neurons only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' \item \code{"both"}: Add metadata for both with \code{.pre}/\code{.post} suffixes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' \item \code{"neither"}: No metadata, return minimal columns for speed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' Metadata can also be added later via \code{\link{cf_add_meta}}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @inheritParams cf_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' @return A data.frame or a named list (when \code{bind.rows=FALSE}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,9 +46,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cf_partners <- function(ids, threshold=1L, partners=c("inputs", "outputs"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bind.rows=TRUE, MoreArgs=list(), keep.all=FALSE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| details=c("partner", "query", "both", "neither"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_superclass=getOption("coconatfly.use_superclass", FALSE), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| harmonise_class=getOption("coconatfly.harmonise_class", FALSE)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| partners=match.arg(partners) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| details=match.arg(details) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| threshold <- checkmate::assert_integerish( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| threshold, lower=0L,len = 1, null.ok = F, all.missing = F) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -51,7 +61,7 @@ cf_partners <- function(ids, threshold=1L, partners=c("inputs", "outputs"), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(is.data.frame(ids)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ss=split(ids$id, ids$dataset) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res=cf_partners(ss, threshold = threshold, partners = partners, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bind.rows = bind.rows, MoreArgs=MoreArgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bind.rows = bind.rows, MoreArgs=MoreArgs, details=details, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use_superclass=use_superclass, harmonise_class=harmonise_class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return(res) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -90,14 +100,7 @@ cf_partners <- function(ids, threshold=1L, partners=c("inputs", "outputs"), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| do.call(PFUN, commonArgs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Enrich with partner metadata if partnerfun returned minimal data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres <- add_partner_metadata(tres, dataset = n, partners = partners) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres=coconat:::standardise_partner_summary(tres) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(isTRUE(harmonise_class) && "class" %in% colnames(tres)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$class=harmonise_top_class_values(tres$class, n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if("side" %in% colnames(tres)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$side=normalise_side(tres$side) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(nrow(tres)>0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$dataset=n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$tissue=dataset_tissue(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -108,8 +111,37 @@ cf_partners <- function(ids, threshold=1L, partners=c("inputs", "outputs"), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$sex=character() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| warning("no ", partners, " found for `", n, "` dataset.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Add keys before metadata enrichment | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$pre_key=keys(tres, idcol="pre_id") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres$post_key=keys(tres, idcol='post_id') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Enrich with metadata based on details option | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Always use cf_add_meta rather than relying on partnerfun metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (details != "neither" && nrow(tres) > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Keep only core connectivity columns, drop all partnerfun metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # cf_add_meta will re-add metadata from cf_meta for consistency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| core_cols <- c("pre_id", "post_id", "weight", "dataset", "tissue", "sex", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "pre_key", "post_key") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tres <- tres[, intersect(names(tres), core_cols), drop = FALSE] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+118
to
+126
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Enrich with metadata based on details option | |
| # Always use cf_add_meta rather than relying on partnerfun metadata | |
| if (details != "neither" && nrow(tres) > 0) { | |
| # Keep only core connectivity columns, drop all partnerfun metadata | |
| # cf_add_meta will re-add metadata from cf_meta for consistency | |
| core_cols <- c("pre_id", "post_id", "weight", "dataset", "tissue", "sex", | |
| "pre_key", "post_key") | |
| tres <- tres[, intersect(names(tres), core_cols), drop = FALSE] | |
| # Always drop partnerfun-specific metadata, keep only core connectivity columns | |
| # cf_add_meta will re-add standardized metadata from cf_meta as needed | |
| core_cols <- c("pre_id", "post_id", "weight", "dataset", "tissue", "sex", | |
| "pre_key", "post_key") | |
| tres <- tres[, intersect(names(tres), core_cols), drop = FALSE] | |
| # Enrich with metadata based on details option | |
| # Always use cf_add_meta rather than relying on partnerfun metadata | |
| if (details != "neither" && nrow(tres) > 0) { |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subsetting to core_cols drops all partnerfun-provided columns beyond the core connectivity fields (including potentially non-metadata columns). This also makes keep.all ineffective for preserving dataset-specific extra columns across datasets. If the goal is to ignore partnerfun metadata only, consider dropping just the known metadata columns (or only those that overlap cf_meta outputs) while preserving any additional connectivity metrics.
| # Always use cf_add_meta rather than relying on partnerfun metadata | |
| if (details != "neither" && nrow(tres) > 0) { | |
| # Keep only core connectivity columns, drop all partnerfun metadata | |
| # cf_add_meta will re-add metadata from cf_meta for consistency | |
| core_cols <- c("pre_id", "post_id", "weight", "dataset", "tissue", "sex", | |
| "pre_key", "post_key") | |
| tres <- tres[, intersect(names(tres), core_cols), drop = FALSE] | |
| # Always use cf_add_meta for standardized metadata; preserve any | |
| # additional partnerfun-provided columns (e.g. extra connectivity metrics) | |
| if (details != "neither" && nrow(tres) > 0) { |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example uses details=FALSE, but cf_partners() now defines details as a character option (partner/query/both/neither). As written, this example will error; update it to a valid value (e.g. details='neither') or remove the argument if the default is intended.