Skip to content
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

cf_partner summary enh #16

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions R/partners.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ connection_table2queryids <- function(x) {
#'
#' @param rval Choose what the function will return. \code{sparse} and
#' \code{matrix} return sparse and dense (standard) matrices, respectively.
#' @param aggregate.query Whether to aggregate all query neurons of the same
#' type (the default) or when \code{aggregate.query=FALSE} only to aggregate
#' the partner neurons.
#' @param normalise Whether to normalise the reported weights as a fraction of
#' the total for each query cell type (or individual query neuron when
#' \code{aggregate.query=TRUE}).
#' @inheritParams cf_partners
#' @return a data.frame or (sparse) matrix based on \code{rval}. The column
#' \code{n} refers to the number of \emph{partner} neurons.
#' @export
#' @details This function currently groups by dataset, and pre and postsynaptic
#' type. It does not currently group by side. The forms returning matrices rely
#' on \code{coconat::\link{partner_summary2adjacency_matrix}}.
#' type. It does not currently group by side. The forms returning matrices
#' rely on \code{coconat::\link{partner_summary2adjacency_matrix}}.
#'
#' @examples
#' \dontrun{
Expand All @@ -156,8 +162,10 @@ connection_table2queryids <- function(x) {
#' names_from = c(type.post,dataset), values_from = weight, values_fill = 0)
#' }
#' @importFrom glue glue
#' @importFrom dplyr .data select mutate left_join group_by n_distinct summarise arrange desc
#' @importFrom dplyr .data select mutate left_join group_by n_distinct summarise
#' arrange desc
cf_partner_summary <- function(ids, threshold=1L, partners=c("inputs", "outputs"),
aggregate.query=TRUE, normalise=FALSE,
rval=c("data.frame", "sparse", "matrix")) {
# ids=expand_ids(ids)
partners=match.arg(partners)
Expand All @@ -172,16 +180,32 @@ cf_partner_summary <- function(ids, threshold=1L, partners=c("inputs", "outputs"
names(join_spec)=paste0(qfix, "_key")
suffix=paste0(".",c(pfix, qfix))

gv <- if(aggregate.query) c("dataset", "type.pre", "type.post") else {
pp <- if(qfix=='post')
pp %>% mutate(query=post_key)
else
pp %>% mutate(query=pre_key)
c("dataset", "query", "type.pre", "type.post")
}

pp2 <- pp %>%
select(-dataset) %>%
left_join(qmeta, by = join_spec, suffix=suffix) %>%
group_by(dataset, type.pre, type.post) %>%
group_by(across(all_of(gv))) %>%
summarise(weight=sum(weight),
npre=n_distinct(pre_key),
npost=n_distinct(post_key),
.groups='drop') %>%
arrange(desc(weight)) %>%
mutate(query=paste0(abbreviate_datasets(dataset),":", .data[[glue("type.{qfix}")]]))
arrange(desc(weight))
if(aggregate.query) {
# make a query type
pp2 <- pp2 %>%
mutate(query=paste0(abbreviate_datasets(dataset),":", .data[[glue("type.{qfix}")]]))
}
if(normalise) {
pp2 <- pp2 %>% group_by(query) %>% mutate(weight=weight/sum(weight)) %>% ungroup()
}

if(rval=='data.frame')
return(pp2)
pp2 %>%
Expand Down
14 changes: 12 additions & 2 deletions man/cf_partner_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading