diff --git a/DESCRIPTION b/DESCRIPTION index e3860381277040f83d38f8eb9c4d0b2d42f59576..3cf4ce8549784ff40a6f98244498bdebdf3d89de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rsAnalysis Title: High-Level Tools for rsfMRI analysis -Version: 0.4.1 +Version: 0.4.2 Authors@R: person(given = "Christian", family = "Hohenfeld", diff --git a/NAMESPACE b/NAMESPACE index 644fcb93e8c8aa845a7f0904044a9d8202a06c37..e87f8b36676a0fec7c9bc457fd4ba6365e5e1b29 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(remove_loops) export(rs_pipeline) export(single_component_threshold) export(split_graph_list) +export(to_subgraph) importFrom(magrittr,"%>%") importFrom(rlang,":=") importFrom(rlang,.data) diff --git a/R/to_subgraph.R b/R/to_subgraph.R new file mode 100644 index 0000000000000000000000000000000000000000..80715af6a910e7a9652e0095244e615e714064f9 --- /dev/null +++ b/R/to_subgraph.R @@ -0,0 +1,23 @@ +#' Take a list of graphs and return subgraphs. +#' +#' `to_subgraph` makes the assumption that all graphs share a common set of +#' nodes like it is common with graph analysis of the brain. +#' +#' @param graph_list A list of tbl_graphs +#' @param to_keep The names of the nodes to keep. +#' +#' @return A list of the graphs in `graph_list` reduced to the respective +#' subgraphs. +#' @export +to_subgraph <- function(graph_list, to_keep) { + all_nodes <- graph_list[[1]] %>% + tidygraph::activate("nodes") %>% + tibble::as_tibble() %>% + tibble::deframe() + + to_remove_net <- all_nodes[!all_nodes %in% to_keep] + graph_list <- lapply(graph_list, + function(x) igraph::delete.vertices(x, to_remove_net)) + graph_list <- lapply(graph_list, tidygraph::as_tbl_graph) + graph_list +} diff --git a/man/to_subgraph.Rd b/man/to_subgraph.Rd new file mode 100644 index 0000000000000000000000000000000000000000..0c71cc4bfce6e315a888c6953b83db6879515c07 --- /dev/null +++ b/man/to_subgraph.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/to_subgraph.R +\name{to_subgraph} +\alias{to_subgraph} +\title{Take a list of graphs and return subgraphs.} +\usage{ +to_subgraph(graph_list, to_keep) +} +\arguments{ +\item{graph_list}{A list of tbl_graphs} + +\item{to_keep}{The names of the nodes to keep.} +} +\value{ +A list of the graphs in `graph_list` reduced to the respective +subgraphs. +} +\description{ +`to_subgraph` makes the assumption that all graphs share a common set of +nodes like it is common with graph analysis of the brain. +} diff --git a/tests/testthat/test-to_subgraph.R b/tests/testthat/test-to_subgraph.R new file mode 100644 index 0000000000000000000000000000000000000000..daecd32eb98a9b6391e01178c03e67ebddede8f6 --- /dev/null +++ b/tests/testthat/test-to_subgraph.R @@ -0,0 +1,9 @@ +describe("to_subgraph", { + data("graph_list") + it("creates a graph only containing the specified nodes", { + to_keep <- c("Precentral_L", "Precentral_R") + sub_list <- to_subgraph(graph_list, to_keep) + sub_order <- sapply(sub_list, igraph::gorder, USE.NAMES = FALSE) + expect_true(all(sub_order == length(to_keep))) + }) +})