From eb9da6eb86b6dda3a752dbf63034375a0d49c06e Mon Sep 17 00:00:00 2001 From: ChiLiubio Date: Fri, 12 Jul 2024 20:26:49 +0800 Subject: [PATCH] fix param usename_rawtaxa_notOTU NA issue in cal_network of trans_network --- R/trans_network.R | 26 ++++++++++++++------------ man/trans_network.Rd | 4 ++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/R/trans_network.R b/R/trans_network.R index 3020ca5..8081d73 100644 --- a/R/trans_network.R +++ b/R/trans_network.R @@ -219,7 +219,7 @@ trans_network <- R6Class(classname = "trans_network", #' the threshold used to limit the number of interactions (stability); same with the t.stab parameter in showInteraction function of beemStatic package. #' @param add_taxa_name default "Phylum"; one or more taxonomic rank name; used to add taxonomic rank name to network node properties. #' @param delete_unlinked_nodes default TRUE; whether delete the nodes without any link. - #' @param usename_rawtaxa_when_taxalevel_notOTU default FALSE; whether use OTU name as representatives of taxa when \code{taxa_level != "OTU"}. + #' @param usename_rawtaxa_notOTU default FALSE; whether use OTU name as representatives of taxa when \code{taxa_level != "OTU"}. #' Default \code{FALSE} means using taxonomic information of \code{taxa_level} instead of OTU name. #' @param ... parameters pass to \code{SpiecEasi::spiec.easi} when \code{network_method = "SpiecEasi"}; #' pass to \code{NetCoMi::netConstruct} when \code{network_method = "gcoda"}; @@ -255,7 +255,7 @@ trans_network <- R6Class(classname = "trans_network", beemStatic_t_stab = 0.8, add_taxa_name = "Phylum", delete_unlinked_nodes = TRUE, - usename_rawtaxa_when_taxalevel_notOTU = FALSE, + usename_rawtaxa_notOTU = FALSE, ... ){ private$check_igraph() @@ -295,8 +295,7 @@ trans_network <- R6Class(classname = "trans_network", message("Start COR optimizing ...") tc1 <- private$rmt(cortable, low_thres = COR_optimization_low_high[1], high_thres = COR_optimization_low_high[2], seq_by = COR_optimization_seq) message("The optimized COR threshold: ", tc1, "...\n") - } - else { + }else{ tc1 <- COR_cut } diag(cortable) <- 0 @@ -310,7 +309,7 @@ trans_network <- R6Class(classname = "trans_network", stop("All the correlation coefficients larger than COR_cut parameter are not significant under current COR_p_thres parameter!") } cor_matrix[cor_matrix != 1] <- 0 - network <- graph.adjacency(cor_matrix, mode = "undirected") + network <- graph.adjacency(cor_matrix, mode = "undirected") # graph_from_adjacency_matrix edges <- t(sapply(1:ecount(network), function(x) ends(network, x))) E(network)$label <- unlist(lapply(seq_len(nrow(edges)), function(x) ifelse(cortable[edges[x, 1], edges[x, 2]] > 0, "+", "-"))) if(COR_weight == T){ @@ -458,14 +457,17 @@ trans_network <- R6Class(classname = "trans_network", } if(taxa_level != "OTU"){ - if(usename_rawtaxa_when_taxalevel_notOTU){ - network <- set_vertex_attr(network, taxa_level, value = V(network)$name %>% - taxa_table[., taxa_level] %>% - gsub("^.__", "", .)) + tmp <- V(network)$name %>% + taxa_table[., taxa_level] %>% + gsub("^.__", "", .) + if(any(is.na(tmp))){ + message("NA is found! For those NA, use original names ...") + tmp[is.na(tmp)] <- V(network)$name[is.na(tmp)] + } + if(usename_rawtaxa_notOTU){ + network <- set_vertex_attr(network, taxa_level, value = tmp) }else{ - network <- set_vertex_attr(network, "name", value = V(network)$name %>% - taxa_table[., taxa_level] %>% - gsub("^.__", "", .)) + network <- set_vertex_attr(network, "name", value = tmp) } } diff --git a/man/trans_network.Rd b/man/trans_network.Rd index c4d4fba..ef443e6 100644 --- a/man/trans_network.Rd +++ b/man/trans_network.Rd @@ -316,7 +316,7 @@ Construct network based on the \code{igraph} package or \code{SpiecEasi} package beemStatic_t_stab = 0.8, add_taxa_name = "Phylum", delete_unlinked_nodes = TRUE, - usename_rawtaxa_when_taxalevel_notOTU = FALSE, + usename_rawtaxa_notOTU = FALSE, ... )}\if{html}{\out{}} } @@ -387,7 +387,7 @@ the threshold used to limit the number of interactions (stability); same with th \item{\code{delete_unlinked_nodes}}{default TRUE; whether delete the nodes without any link.} -\item{\code{usename_rawtaxa_when_taxalevel_notOTU}}{default FALSE; whether use OTU name as representatives of taxa when \code{taxa_level != "OTU"}. +\item{\code{usename_rawtaxa_notOTU}}{default FALSE; whether use OTU name as representatives of taxa when \code{taxa_level != "OTU"}. Default \code{FALSE} means using taxonomic information of \code{taxa_level} instead of OTU name.} \item{\code{...}}{parameters pass to \code{SpiecEasi::spiec.easi} when \code{network_method = "SpiecEasi"};