stefpeschel / NetCoMi

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

Highlighting significant nodes #34

Closed choon-sim closed 2 years ago

choon-sim commented 2 years ago

Hi, thanks for the useful tool, especially in quantitative comparison of 2 networks.

In my plot, I am trying to somehow highlight the nodes that have a significantly different eigenvector centrality calculated by netCompare. I am not so interested to highlight (eg. bold labels, thicker node circumference) the hubs of each network but rather the nodes that are significantly different between the networks as mentioned above.

I couldn't find out the trick from reading the manual... Thanks for any suggestion.

stefpeschel commented 2 years ago

The issue is already closed but since it could be of interest for others as well, here's a possible solution on how to highlight nodes whose eigenvector centrality is significantly different between the groups:

# Load data set from American Gut Project (from SpiecEasi package)
data("amgut1.filt")

# Generate a random group vector
set.seed(123456)
group <- sample(1:2, nrow(amgut1.filt), replace = TRUE)

# Network construction:
amgut_net <- netConstruct(amgut1.filt, group = group,
                          measure = "pearson",
                          filtTax = "highestVar",
                          filtTaxPar = list(highestVar = 30),
                          zeroMethod = "pseudo", normMethod = "clr")

# Network analysis:
amgut_props <- netAnalyze(amgut_net, clustMethod = "cluster_fast_greedy")

# Network comparison with permutation test
# (with only 100 permutations to decrease runtime):
amgut_comp <- netCompare(amgut_props,
                         adjust = "none",
                         permTest = TRUE,
                         nPerm = 100L,
                         cores = 5L,
                         seed = 123456)

Note: Setting "adjust" to "none" is statistically not correct but with multiple testing correction we wouldn't get any significant differences for such a small number of permutations.

summary(amgut_comp)

# Adjusted p-values of eigenvector centrality
pvals_eigen <- amgut_comp$pvalDiffCentrAdjust$pAdjustDiffEigen

# Generate a vector whose elements equal 2 for significant differences and 1 otherwise
featVecCol <- rep(1, length(pvals_eigen))
names(featVecCol) <- names(pvals_eigen)
featVecCol[pvals_eigen <= 0.05] <- 2

# Nodes with a significantly different eigenvector centrality are colored in red, the others in gray
colVec <- c("gray", "red")

# Generate network plot
plot(amgut_props,
     nodeColor = "feature", 
     featVecCol = featVecCol,
     colorVec = colVec, 
     highlightHubs = FALSE)