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

Add utility function to compute node centrality #43

Open
bschilder opened this issue Aug 23, 2024 · 0 comments
Open

Add utility function to compute node centrality #43

bschilder opened this issue Aug 23, 2024 · 0 comments

Comments

@bschilder
Copy link
Contributor

This is useful for creating a node attribute that can scale the size of node when plotting.

#' Add centrality
#'
#' First computes of each node in a graph.
#' Then adds the centrality score as an node attribute.
#' @param fun The centrality function to use.
#'  Default is \link[igraph]{harmonic_centrality}.
#' @param col Name of the new node attribute to store the centrality score.
#' @param ... Additional arguments passed to the centrality function
#'  (\code{sim_fun}).
#' @returns Graph object with centrality added as a new node attribute.
#' @export
#' @examples
#' filename <- system.file("extdata", "eds_marfan_kg.tar.gz", package = "monarchr")
#' g <- file_engine(filename) |>
#'           fetch_nodes(query_ids = "MONDO:0007525") |>
#'           expand(predicates = "biolink:has_phenotype",
#'                  categories = "biolink:PhenotypicFeature")|>
#'           expand(categories = "biolink:Gene")
#' g <- graph_centrality(g)
#' nodes(g)$centrality
graph_centrality <- function(graph,
												 fun=igraph::harmonic_centrality,
												 col="centrality",
												 ...){
	message("Computing node centrality.")
	graph <- graph|>
		activate(nodes)|>
		dplyr::mutate(!!col:=fun(graph, ...))
	return(graph)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant