stefpeschel / NetCoMi

Network construction, analysis, and comparison for microbial compositional data
GNU General Public License v3.0
143 stars 24 forks source link

How to look at sub-clusters? #105

Closed bridgeto closed 5 months ago

bridgeto commented 8 months ago

Hi there,

This is a great package and am really enjoying using it. I was curious if I could plot one specific node and its connections? I want to focus in on one particular sub-cluster but I'm not sure how to do this.

Thanks, Bridget

stefpeschel commented 8 months ago

Hi,

I think these are two questions: How to plot only nodes that are directly connected to a certain node? How to plot nodes belonging to a certain cluster? Since I'm not sure what you're interested in, I'll give you an example for both :)

library(NetCoMi)
library(phyloseq)

# Load data sets from American Gut Project (from SpiecEasi package)
data("amgut2.filt.phy")

# Agglomerate to genus level
amgut_genus <- tax_glom(amgut2.filt.phy, taxrank = "Rank6")

# Rename taxonomic table and make Rank6 (genus) unique
amgut_genus_renamed <- renameTaxa(amgut_genus, 
                                  pat = "<name>", 
                                  substPat = "<name>_<subst_name>(<subst_R>)",
                                  numDupli = "Rank6")

# Network construction
amgut_net <- netConstruct(amgut_genus_renamed, 
                          taxRank = "Rank6",
                          measure = "pearson",
                          #filtTax = "highestVar",
                          #filtTaxPar = list(highestVar = 50),
                          zeroMethod = "pseudoZO",
                          normMethod = "clr",
                          sparsMethod = "threshold", 
                          thresh = 0.3)

# Network analysis
amgut_props <- netAnalyze(amgut_net)

### Network plots ###
# Plot the whole network
plot(amgut_props, nodeColor = "cluster")

# Plot only nodes belonging to cluster 1
clust1 <- amgut_props$clustering$clust1
cl1 <- names(clust1[clust1 == 1])

plot(amgut_props, nodeFilter = "names", nodeFilterPar = cl1)

# Plot only nodes that are connected to a certain taxon (here Salmonella)
sel <- amgut_net$edgelist1[amgut_net$edgelist1$v1 == "Salmonella", "v2"]
sel <- c("Salmonella", sel)

# Alternatively, you can extract them from the adjacency matrix
adja_salmon <- amgut_net$adjaMat1["Salmonella", ]
sel <- names(adja_salmon[adja_salmon != 0])

plot(amgut_props, nodeFilter = "names", nodeFilterPar = sel)