microeco icon indicating copy to clipboard operation
microeco copied to clipboard

Convert net_construct or net_analyse object to trans_network or cal_network class

Open chrissy005 opened this issue 1 year ago • 16 comments

Dear @ChiLiubio,

I have already constructed my network plot using NetCoMi with measure as 'spieceasi' and I was wondering if it is possible to import the net_construct or net_analyse object from NetCoMi into trans_network or cal_network class.

The reason is I am interested in constructing the correlation heatmap as well as using the cal_sum_links to 'quantify' the interaction amongst the different taxa as seen in your package

I could directly create a new network plot as per your tutorial but that would result in a new network and would not be compatible to my existing network.

chrissy005 avatar Oct 09 '23 01:10 chrissy005

Hi @chrissy005

Good idea! There is only one tricky step. The reason is netConstruct function does not return an network object with igraph class. This is necessary in trans_network class. So you should create one based on the netConstruct analysis results by using a function like graph_from_adjacency_matrix in igraph package. Here is an example of all the operations.

# network example from igraph
adjm <- matrix(sample(0:1, 100, replace = TRUE, prob = c(0.9, 0.1)), ncol = 10)
g1 <- graph_from_adjacency_matrix(adjm)
# must assign names
V(g1)$name <- as.character(as_ids(V(g1)))
# please install microeco v1.1.0
library(microeco)
testnet <- trans_network$new()
testnet$cal_network(network_method = NULL)
# tax_table is necessary for the summary when you use cal_sum_links function. Please manually assign it
testnet$tax_table <- your_microtable$tax_table
# g1 to your trans_network object
testnet$res_network <- g1
testnet$cal_module()
testnet$get_node_table()
testnet$cal_sum_links(taxa_level = "Phylum")

Please feel free to tell me if something is wrong.

Best, Chi

ChiLiubio avatar Oct 10 '23 04:10 ChiLiubio

Dear @ChiLiubio ,

Thank you very much for your help on the matter.

chrissy005 avatar Oct 12 '23 02:10 chrissy005

Dear @ChiLiubio ,

I followed as you suggested but I run into an error at the last step while calculating links: "Error in data.frame(t(sapply(1:ecount(network), function(x) ends(network, : arguments imply differing number of rows: 9428, 0"

chrissy005 avatar Oct 12 '23 12:10 chrissy005

Could you attach your data and script so that I can find out the reason? To save your data, please use save function as the tutorial shows (https://chiliubio.github.io/microeco_tutorial/notes.html#save-function).

ChiLiubio avatar Oct 13 '23 01:10 ChiLiubio

Dear @ChiLiubio ,

Sorry for the delay. I also encountered a few other problems with other functions so I have consolidated then and listing it here.

1) Unable to get edge_links and cal_sum_links for network object constructed using Netcomi and converted to igraph.

The codes I have used are as follows:

Compute layout: graph3 <- igraph::graph_from_adjacency_matrix(ASV_net_spieceasi_mb$adjaMat1, weighted = TRUE) must assign names: V(graph3)$name <- as.character(as_ids(V(graph3)))

testnet <- trans_network$new(dataset = microeco.dataset, cor_method = NULL, taxa_level = "OTU")

testnet$cal_network(network_method = NULL)

testnet$tax_table <- microeco.dataset$tax_table

g1 to your trans_network object testnet$res_network <- graph3

testnet$cal_module(method = "cluster_fast_greedy", module_name_prefix = "M")

testnet$cal_network_attr() testnet$res_network_attr

testnet$get_node_table(node_roles = TRUE) testnet$res_node_table

####no edges were returned. I suspect this could be a problem. #testnet$get_edge_table() #testnet$res_edge_table

testnet$get_adjacency_matrix() testnet$res_adjacency_matrix

plot the node classification in terms of the within-module connectivity and among-module connectivity. testnet$plot_taxa_roles(use_type = 1, add_label = TRUE)

plot node roles with phylum information testnet$plot_taxa_roles(use_type = 2)

testnet$cal_eigen() testnet$res_eigen

testnet_correlation <- trans_env$new(dataset = microeco.dataset, add_data = metadata.micronet[, 24:31])

testnet_correlation$cal_cor(add_abund_table = testnet$res_eigen)

This also returns a null object.and a network plot

testnet$plot_network(node_color = "module")

This line returns an error "Error in data.frame(t(sapply(1:ecount(network), function(x) ends(network, :

arguments imply differing number of rows: 10306, 0" testnet$cal_sum_links(taxa_level = "Phylum")

microeco.dataset.1.zip ASV_net_spieceasi_mb_1.zip [com.rarefied.min.int.exclude.1.3.rooted.1.zip]

2) I tried constructing a network with the microeco package for comparison. However while I look at the plot using igraph, I do not see the 16 clusters by colors and it also returns a null object.

The codes and data used are as follows

microeco_transnetwork_spieceasi$cal_module(method = "cluster_fast_greedy", module_name_prefix = "M")

calculate network attributes microeco_transnetwork_spieceasi$cal_network_attr() microeco_transnetwork_spieceasi$res_network_attr

get node properties, calculate node roles, i.e. Module hubs, Network hubs, Connectors and Peripherals microeco_transnetwork_spieceasi$get_node_table(node_roles = TRUE) microeco_transnetwork_spieceasi$res_node_table

Get the edge property table, including connected nodes, label and weight. microeco_transnetwork_spieceasi$get_edge_table() microeco_transnetwork_spieceasi$res_edge_table

Get the adjacency matrix from the network graph microeco_transnetwork_spieceasi$get_adjacency_matrix() microeco_transnetwork_spieceasi$res_adjacency_matrix

Now, we show the eigengene analysis of modules. microeco_transnetwork_spieceasi$cal_eigen() microeco_transnetwork_spieceasi$res_eigen

default parameter represents using igraph plot.igraph function microeco_transnetwork_spieceasi$plot_network(method = "igraph", layout = layout_with_kk,node_color = "module")

Nonetheless, here I am able to plot the sum_links microeco_transnetwork_spieceasi$plot_sum_links(plot_pos = TRUE, plot_num = 12, color_values = RColorBrewer::brewer.pal(12, "Paired"))

Could you also advise on how I can save the circos plot created here? I tried it in as follows but it returned a null object.

pdf("test.pdf") microeco_transnetwork_spieceasi$plot_sum_links(plot_pos = TRUE, plot_num = 12, color_values = RColorBrewer::brewer.pal(12, "Paired")) dev.off()

microeco_transnetwork_spieceas.1.zip

chrissy005 avatar Oct 16 '23 08:10 chrissy005

Hi. When I load ASV_net_spieceasi_mb.RData you provided, the session only has dataset, which seems like the example dataset in microeco package, not the ASV_net_spieceasi_mb presented in the step.

load("ASV_net_spieceasi_mb.RData")
# if you use save function, it should be like
save(ASV_net_spieceasi_mb, file = "ASV_net_spieceasi_mb.RData")

The figure from plot_sum_links function is not the traditional plot in R. It is based on the js. So please save it from the Rstudio window directly, not the script.

ChiLiubio avatar Oct 17 '23 05:10 ChiLiubio

Dear @ChiLiubio ,

sorry about providing wrong files.

Attaching the new ones here.

com.rarefied.min.int.exclude.1.3.rooted.1.zip microeco.dataset.1.zip ASV_net_spieceasi_mb_1.zip

Let me know if these are ok.

chrissy005 avatar Oct 17 '23 06:10 chrissy005

Hi. I found that cal_sum_links require the label of edges to partition the links into positive and negative. I will update the package later. To temporarily solve this, we can assign the labels into network: E(graph3)$label <- ifelse(E(graph3)$weight > 0, '+', '-'). The following is all I run.

library(microeco)
library(magrittr)
library(igraph)

load("ASV_net_spieceasi_mb.RData")
load("microeco.dataset.RData")

tmp <- ASV_net_spieceasi_mb$adjaMat1
# 1 of diagonal should be removed
diag(tmp) <- 0

graph3 <- igraph::graph_from_adjacency_matrix(tmp, weighted = TRUE)
# must assign names:
V(graph3)$name <- as.character(as_ids(V(graph3)))

E(graph3)$label <- ifelse(E(graph3)$weight > 0, '+', '-')

testnet <- trans_network$new(dataset = microeco.dataset,
cor_method = NULL, taxa_level = "OTU")
testnet$cal_network(network_method = NULL)
testnet$tax_table <- microeco.dataset$tax_table
# g1 to your trans_network object
testnet$res_network <- graph3
testnet$cal_module(method = "cluster_fast_greedy",
module_name_prefix = "M")
testnet$cal_network_attr()
testnet$res_network_attr
testnet$get_node_table(node_roles = TRUE)
# testnet$res_node_table
####no edges were returned. I suspect this could be a problem.
#testnet$get_edge_table()
#testnet$res_edge_table
# testnet$get_adjacency_matrix()
# testnet$res_adjacency_matrix
# plot the node classification in terms of the within-module connectivity and among-module connectivity.
testnet$plot_taxa_roles(use_type = 1, add_label = TRUE)
# plot node roles with phylum information
testnet$plot_taxa_roles(use_type = 2)
testnet$cal_sum_links(taxa_level = "Phylum")

ChiLiubio avatar Oct 17 '23 10:10 ChiLiubio

@ChiLiubio, Thank you for your help. It is all solved now.

However, I am still struggling to adjust the font size on the circos plot while plotting the sum_links.

Also, the only option to save it form the plots panel is as png and not pdf.

chrissy005 avatar Oct 19 '23 16:10 chrissy005

Yes. The reason is it is made by the js, not R, for the interactive vis. So I will consider implement another visualization way. Thanks.

ChiLiubio avatar Oct 20 '23 01:10 ChiLiubio

Thank you so much and will wait for your update to the package.

chrissy005 avatar Oct 20 '23 06:10 chrissy005

@ChiLiubio , I understand that I could actually adjust the font size by passing arguments to the chorddiag function while plotting the sum_links. I also see this in the manual:

"Method plot_sum_links(): Plot the summed linkages among taxa using chorddiag package https://github.com/mattflor/chorddiag. Usage: trans_network$plot_sum_links( plot_pos = TRUE, plot_num = NULL, color_values = RColorBrewer::brewer.pal(8, "Dark2"), ... ) Arguments: plot_pos default TRUE; If TRUE, plot the summed positive linkages; If FALSE, plot the summed negative linkages. plot_num default NULL; number of taxa presented in the plot. color_values default RColorBrewer::brewer.pal(8, "Dark2"); colors palette for taxa. ... parameters pass to chorddiag::chorddiag function."

Could you provide an example on how may I pass additional arguments using '...' to chorddiag?

Also, would it be possible to plot the circos plot without the edge numbers? i.e. I would like to get rid off the numbers on the outside of the circular plot?

chrissy005 avatar Oct 25 '23 16:10 chrissy005

Hi. The parameter passing means you can use the parameters from chorddiag fundtion in the plot_sum_links function like this:

library(chorddiag)
?chorddiag
# if you want to use categorynameFontsize, you can directly use it in the plot_sum_links
your_trans_network$plot_sum_links(categorynameFontsize = 40)

For the edge number issue, please first check whether chorddiag has a param to remove it. Another way to do this is to extract the data coming from the cal_sum_links function and visualize that directly use the chorddiag function.

ChiLiubio avatar Oct 26 '23 00:10 ChiLiubio

@ChiLiubio . Thank you:)

chrissy005 avatar Oct 26 '23 09:10 chrissy005

Hi. New microeco version has been released and solved the adjustment of plot in plot_sum_links function. Another static plot based on circilize package has been implemented and can be easily adjusted and saved into local file.

ChiLiubio avatar Nov 19 '23 13:11 ChiLiubio

Hello @ChiLiubio ,

thank you for attending to that request.

So all I need to know is to install the new version of microeco and save the plot as pdf?

Or are there any other specific codes?

chrissy005 avatar Nov 23 '23 06:11 chrissy005