Skip to content

Commit

Permalink
add edge_node_distance class
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiLiubio committed Oct 5, 2022
1 parent 0d5b7d9 commit b5a5398
Show file tree
Hide file tree
Showing 23 changed files with 440 additions and 62 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: meconetcomp
Type: Package
Title: Compare Microbial Networks of 'trans_network' Class of 'microeco' Package
Version: 0.1.1
Version: 0.2.0
Authors@R: c(person("Chi", "Liu", role = c("aut", "cre"), email = "liuchi0426@126.com"),
person("Minjie", "Yao", role = "ctb", email = "yaomj@fafu.edu.cn"),
person("Xiangzhen", "Li", role = "ctb", email = "lixz@cib.ac.cn")
Expand All @@ -12,13 +12,13 @@ Description: Compare microbial co-occurrence networks created from 'trans_networ
This package is the extension of 'trans_network' class of 'microeco' package and especially useful when different networks are constructed and analyzed simultaneously.
URL: https://github.com/ChiLiubio/meconetcomp
Depends: R (>= 3.5.0)
Imports: microeco, magrittr, dplyr, igraph, reshape2
Suggests: rgexf, ape, file2meco
Imports: R6, microeco (>= 0.12.0), magrittr, dplyr, igraph, reshape2, ggpubr
Suggests: rgexf, ape, file2meco, agricolae
License: GPL-3
LazyData: true
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2022-09-04
Packaged: 2022-10-04
Repository: CRAN
Date/Publication: 2022-09-07
RoxygenNote: 7.1.2
Date/Publication: 2022-10-04
RoxygenNote: 7.2.1
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
export(cal_module)
export(cal_network_attr)
export(edge_comp)
export(edge_node_distance)
export(edge_tax_comp)
export(get_edge_table)
export(get_node_table)
export(node_comp)
export(subnet_property)
export(subset_network)
import(microeco)
importFrom(R6,R6Class)
importFrom(dplyr,full_join)
importFrom(igraph,is_directed)
importFrom(magrittr,"%<>%")
Expand Down
8 changes: 4 additions & 4 deletions R/cal_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#' @description
#' Calculating modularity of networks and assign the modules to nodes for each network.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @param undirected_method default "cluster_fast_greedy"; the modularity algorithm for undirected network;
#' see cal_module function of \code{\link{trans_network}} class for more algorithms.
#' see \code{cal_module} function of \code{\link{trans_network}} class for more algorithms.
#' @param directed_method default 'cluster_optimal'; the modularity algorithm for directed network.
#' @param ... other parameters (except for method) passed to cal_module function of \code{\link{trans_network}} class.
#' @return list, with module attribute in nodes of each network
#' @param ... other parameters (except for method) passed to \code{cal_module} function of \code{\link{trans_network}} class.
#' @return \code{list}, with module attribute in nodes of each network
#' @examples
#' data(soil_amp_network)
#' soil_amp_network <- cal_module(soil_amp_network)
Expand Down
4 changes: 2 additions & 2 deletions R/cal_network_attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' @description
#' Calculate the topological properties of all the networks and merge the results into one table.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @return data.frame
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @return \code{data.frame}
#' @examples
#' data(soil_amp_network)
#' test <- cal_network_attr(soil_amp_network)
Expand Down
8 changes: 4 additions & 4 deletions R/edge_comp.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' Generate a microtable object with paired nodes distributions of edges across networks
#' Generate a \code{microtable} object with paired nodes distributions of edges across networks
#'
#' @description
#' Generate a microtable object with paired nodes distributions of edges across networks. Useful for the edge comparisons across different networks.
#' The return otu_table in microtable object has the binary numbers in which 1 represents the presence of the edge in the corresponding network.
#' Generate a \code{microtable} object with paired nodes distributions of edges across networks. Useful for the edge comparisons across different networks.
#' The return \code{otu_table} in \code{microtable} object has the binary numbers in which 1 represents the presence of the edge in the corresponding network.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @return microtable object
#' @return \code{microtable} object
#' @examples
#' data(soil_amp_network)
#' test <- edge_comp(soil_amp_network)
Expand Down
174 changes: 174 additions & 0 deletions R/edge_node_distance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#' @title Perform the distance distribution of paired nodes in edges across networks.
#'
#' @description
#' This class is a wrapper for a series of analysis on the distance distribution
#' of paired nodes in edges across networks and the differential test.
#'
#' @export
edge_node_distance <- R6::R6Class(classname = "edge_node_distance",
public = list(
#' @param network_list a list with multiple networks; all the networks should be \code{trans_network} object
#' created from \code{\link{trans_network}} class of microeco package.
#' @param dis_matrix default NULL; the distance matrix of nodes, used for the value extraction;
#' must be a symmetrical matrix with both colnames and rownames.
#' @param label default "+"; "+" or "-" or \code{c("+", "-")}; the edge label used for the selection of edges.
#' @param with_module default FALSE; whether show the module classification of nodes in the result.
#' @param module_thres default 2; the threshold of the nodes number of modules remained when \code{with_module = TRUE}.
#' @return \code{data_table} in the object
#' @examples
#' \donttest{
#' data(soil_amp_network)
#' data(soil_amp)
#' # select a small dataset to speed up the calculation
#' node_names <- unique(unlist(lapply(soil_amp_network, function(x){colnames(x$data_abund)})))
#' filter_soil_amp <- microeco::clone(soil_amp)
#' filter_soil_amp$otu_table <- filter_soil_amp$otu_table[node_names, ]
#' filter_soil_amp$tidy_dataset()
#' # obtain phylogenetic distance matrix
#' phylogenetic_distance <- as.matrix(cophenetic(filter_soil_amp$phylo_tree))
#' # choose the positive labels
#' t1 <- edge_node_distance$new(network_list = soil_amp_network, dis_matrix = phylogenetic_distance, label = "+")
#' }
initialize = function(network_list, dis_matrix = NULL, label = "+", with_module = FALSE, module_thres = 2){
check_input(network_list)
if(is.null(dis_matrix)){
stop("Please provide dis_matrix parameter!")
}
res_table <- data.frame()
for(i in names(network_list)){
tmp <- private$get_matrix_value(
network = network_list[[i]],
label = label,
dis_matrix = dis_matrix,
group_name = i,
with_module = with_module,
module_thres = module_thres
)
res_table %<>% rbind(., tmp)
}
res_table$Group %<>% factor(., levels = names(network_list))
res_table %<>% .[!is.na(.$Value), ]
self$data_table <- res_table
self$label <- label
},
#' @description
#' Differential test across networks.
#'
#' @param method default "KW"; see the following available options:
#' \describe{
#' \item{\strong{'anova'}}{Duncan's multiple range test for anova}
#' \item{\strong{'KW'}}{KW: Kruskal-Wallis Rank Sum Test for all groups (>= 2)}
#' \item{\strong{'KW_dunn'}}{Dunn's Kruskal-Wallis Multiple Comparisons, see \code{dunnTest} function in \code{FSA} package}
#' \item{\strong{'wilcox'}}{Wilcoxon Rank Sum and Signed Rank Tests for all paired groups }
#' \item{\strong{'t.test'}}{Student's t-Test for all paired groups}
#' }
#' @param ... parameters passed to \code{cal_diff} function of \code{trans_alpha} class of \code{microeco} package.
#' @return \code{res_diff} in object. See the Return of \code{cal_diff} function in \code{trans_alpha} class of \code{microeco} package.
#' @examples
#' \donttest{
#' t1$cal_diff(method = "wilcox")
#' }
cal_diff = function(method = c("anova", "KW", "KW_dunn", "wilcox", "t.test")[1], ...){
res <- self$data_table
res$Measure <- "Value"
# two cases: only one type of label and two types of labels
if(length(unique(res$label)) == 1){
suppressMessages(tmp2 <- trans_alpha$new(dataset = NULL))
tmp2$data_alpha <- res
tmp2$group <- "Group"
tmp2$cal_diff(method = method, measure = "Value", ...)
}else{
res$raw_Group <- res$Group
res$Label <- paste0(res$Group, " - ", res$label)
suppressMessages(tmp2 <- trans_alpha$new(dataset = NULL))
tmp2$data_alpha <- res
tmp2$group <- "Label"
if(method != "anova"){
message("For multiple labels, only anova can be used!")
}
tmp2$cal_diff(method = "anova", measure = "Value", ...)
split_raw <- strsplit(rownames(tmp2$res_diff), split = " - ")
tmp2$res_diff$by_group <- lapply(split_raw, function(x){x[1]}) %>% unlist
tmp2$res_diff$Group <- lapply(split_raw, function(x){x[2]}) %>% unlist
res$by_group <- res$raw_Group
res$Label <- res$label
res$Label %<>% factor(., levels = self$label)
tmp2$data_alpha <- res
tmp2$by_group <- "by_group"
}
self$res_diff <- tmp2$res_diff
self$cal_diff_method <- method
self$tmp_diff <- tmp2
message('The result is stored in object$res_diff ...')
},
#' @description
#' Plot the distance.
#'
#' @param ... parameters pass to \code{plot_alpha} function of \code{trans_alpha} class of \code{microeco} package.
#' @return \code{ggplot}.
#' @examples
#' \donttest{
#' t1$plot(boxplot_add = "none", add_sig = TRUE)
#' }
plot = function(
...
){
self$tmp_diff$plot_alpha(measure = "Value", ...)
}
),
private = list(
get_matrix_value = function(network, label, dis_matrix, group_name, with_module, module_thres){
if(!with_module){
if(is.null(network$res_edge_table)){
network$get_edge_table()
}
tmp <- network$res_edge_table %>% .[.$label %in% label, ]
if(nrow(tmp) == 0){
res <- NA
}else{
select_value <- private$select_value_matrix(input_table = tmp, input_matrix = dis_matrix)
res <- data.frame(Group = group_name, Value = select_value, label = tmp$label)
}
}else{
if(!is.numeric(module_thres)){
stop("module_thres must be numeric!")
}
if(! "module" %in% colnames(network$res_node_table)){
stop("please first use cal_module function to calculate modularity!")
}
if(is.null(network$res_node_table)){
network$get_node_table(node_roles = FALSE)
}
# check module nodes number
use_modules <- table(network$res_node_table$module) %>% .[. >= module_thres] %>% names
res <- NULL
for(k in use_modules){
module_nodes <- network$res_node_table %>% .[.$module == k, ] %>% rownames
t1 <- clone(network)
t1$res_network <- t1$subset_network(node = module_nodes, rm_single = TRUE)
suppressMessages(t1$get_edge_table())
tmp <- t1$res_edge_table %>% .[.$label %in% label, ]
if(nrow(tmp) == 0){
next
}else{
select_value <- private$select_value_matrix(input_table = tmp, input_matrix = dis_matrix)
res <- rbind(res, data.frame(Group = group_name, Value = select_value, module = k, label = tmp$label))
}
}
}
res
},
select_value_matrix = function(input_table, input_matrix){
select_value <- lapply(seq_len(nrow(input_table)), function(x){
if(all(c(input_table[x, 1], input_table[x, 2]) %in% colnames(input_matrix))){
input_matrix[input_table[x, 1], input_table[x, 2]]
}else{
NA
}
}) %>% unlist
select_value
}
),
lock_objects = FALSE,
lock_class = FALSE
)
9 changes: 5 additions & 4 deletions R/edge_tax_comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#' @description
#' Taxonomic sum of linked nodes in edges across networks.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @param taxrank default "Phylum"; Which taxonomic level is used for the sum of nodes in edges.
#' @param label default "+"; "+" or "-" or c("+", "-"); the edge label used for the selection of edges for the sum.
#' @param rel default TRUE; TRUE represents using ratio, the denominator is the number of selected edges; FALSE represents the absolute number of the sum of edges.
#' @return data.frame
#' @param label default "+"; "+" or "-" or \code{c("+", "-")}; the edge label used for the selection of edges for the sum.
#' @param rel default \code{TRUE}; \code{TRUE} represents using ratio, the denominator is the number of selected edges;
#' \code{FALSE} represents the absolute number of the sum of edges.
#' @return \code{data.frame}
#' @examples
#' data(soil_amp_network)
#' test <- edge_tax_comp(soil_amp_network)
Expand Down
4 changes: 2 additions & 2 deletions R/get_edge_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' @description
#' Get edge property table for each network in the list with multiple networks.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @return list, with res_edge_table in each network
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @return \code{list}, with \code{res_edge_table} in each network
#' @examples
#' data(soil_amp_network)
#' soil_amp_network <- get_edge_table(soil_amp_network)
Expand Down
4 changes: 2 additions & 2 deletions R/get_node_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#' @description
#' Get node property table for each network in the list with multiple networks.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @param ... parameter passed to get_node_table function of \code{\link{trans_network}} class.
#' @return list, with res_node_table in each network
#' @return \code{list}, with \code{res_node_table} in each network
#' @examples
#' \donttest{
#' data(soil_amp_network)
Expand Down
1 change: 1 addition & 0 deletions R/import.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' @import microeco
#' @importFrom R6 R6Class
#' @importFrom magrittr %<>%
#' @importFrom magrittr %>%
#' @importFrom dplyr full_join
Expand Down
8 changes: 4 additions & 4 deletions R/node_comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#' @description
#' Generate a microtable object with node distributions across networks. Useful for the node information comparisons across different networks.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @param property default "name"; a colname of res_node_table in each network;
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @param property default "name"; a colname of \code{res_node_table} in each network;
#' the default "name" represents using node presence/absence information in the otu_table of final output, in which
#' 1 represents presence of the node in the corresponding network;
#' For other options (such as degree), the results in the output otu_table are the actual values of res_node_table.
#' @return microtable object
#' For other options (such as degree), the results in the output otu_table are the actual values of \code{res_node_table}.
#' @return \code{microtable} object
#' @examples
#' \donttest{
#' data(soil_amp_network)
Expand Down
4 changes: 2 additions & 2 deletions R/subnet_property.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' @description
#' Extracting sub-network according to the presence of features in each sample across networks and calculate the sub-network properties.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @return data.frame
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of \code{microeco} package.
#' @return \code{data.frame}
#' @examples
#' \donttest{
#' data(soil_amp_network)
Expand Down
8 changes: 4 additions & 4 deletions R/subset_network.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#' @description
#' Extracting a network according to the edge intersection of networks.
#'
#' @param network_list a list with multiple networks; all the networks should be trans_network object created from \code{\link{trans_network}} class of microeco package.
#' @param venn default NULL; a microtable object which must be converted by trans_comm function of trans_venn class.
#' @param name default NULL; integer or character; must be a number or one of colnames of the otu_table in venn.
#' @return a trans_network object, with only the extracted edges in the network
#' @param network_list a list with multiple networks; all the networks should be \code{trans_network} object created from \code{\link{trans_network}} class of microeco package.
#' @param venn default NULL; a \code{microtable} object which must be converted by \code{trans_comm} function of \code{trans_venn} class.
#' @param name default NULL; integer or character; must be a number or one of colnames of the \code{otu_table} in the input \code{venn} parameter.
#' @return \code{trans_network} object, with only the extracted edges in the network
#' @examples
#' \donttest{
#' data(soil_amp_network)
Expand Down
8 changes: 4 additions & 4 deletions man/cal_module.Rd

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

4 changes: 2 additions & 2 deletions man/cal_network_attr.Rd

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

Loading

0 comments on commit b5a5398

Please sign in to comment.