This funtion allows to extract a graph object and create a table with nodes, based on node degree. Table also include taxonomic information as well as network attributes such as node degree (nd) or size (frequence of observation). Output table is ordered from highest to lowest node degree.

vtable.ON.nodeDegree <- function(graph_object = net.grph, selected_taxonomy = sel.tax){
  aa <- data.frame(node = V(net.grph)$name, nd = degree(net.grph))
  bb <- aa[order(aa$nd, decreasing = T), ]
  test <- sel.tax[rownames(sel.tax) %in% rownames(bb),]
  kkk <- test[rownames(bb),,drop=FALSE]
  ddd <- cbind(bb,kkk)
  return(ddd)
}

Example

library(devtools)
install_github("ravinpoudel/myFunctions")
library(myFunctions)
library(igraph)
library(magrittr)
library(tidyverse)
library(Hmisc) 
library(Matrix)
library(knitr)
# import count data with read.mothur.shared function
otu_data <- read.mothur.shared(Sys.glob("*.shared"))
otu_data [1:4, 1:3]
##           Otu00001 Otu00002 Otu00003
## SRh.2p.A1        1        0      510
## SRh.2p.A2      692      201        3
## SRh.2p.A3      208       14        0
## SRh.2p.A4        4      151        0
# upload taxanomy file using read.mothur.taxonomy function
tax <- read.mothur.taxonomy(Sys.glob("*.taxonomy"))
head(tax)
##           Count  Kingdom           Phylum              Class
## Otu00001 130638 k__Fungi    p__Ascomycota   c__Pezizomycetes
## Otu00002 119471 k__Fungi    p__Ascomycota   c__Pezizomycetes
## Otu00003 108083 k__Fungi p__Basidiomycota  c__Agaricomycetes
## Otu00004  82435 k__Fungi    p__Ascomycota c__Dothideomycetes
## Otu00005  50921 k__Fungi    p__Ascomycota   c__Pezizomycetes
## Otu00006  49323 k__Fungi    p__Ascomycota c__Dothideomycetes
##                      Order                                Family
## Otu00001      o__Pezizales                     f__Pyronemataceae
## Otu00002      o__Pezizales                     f__Pyronemataceae
## Otu00003 o__Cantharellales                  f__Ceratobasidiaceae
## Otu00004   o__Pleosporales                      f__Pleosporaceae
## Otu00005      o__Pezizales                     f__Pyronemataceae
## Otu00006   o__Pleosporales f__Pleosporales_family_Incertae_sedis
##                                   Genus                    Species
## Otu00001 f__Pyronemataceae_unclassified                       <NA>
## Otu00002 f__Pyronemataceae_unclassified                       <NA>
## Otu00003               g__Thanatephorus s__Thanatephorus_cucumeris
## Otu00004                  g__Alternaria        s__Alternaria_porri
## Otu00005 g__unclassified_Pyronemataceae       s__Pyronemataceae_sp
## Otu00006                       g__Phoma      s__Phoma_sp_UASWS0872
# if everything went smoothly then rownames of these two files should get you a true output
all.equal(colnames(otu_data), rownames(tax))
## [1] TRUE
# Keep the OTUs with more than 100 counts
otu.table.filter <- otu_data[ , colSums(otu_data) > 100]

# Check for the decrease in the number of OTUs
dim(otu.table.filter)
## [1] 152 423
# Calculate pairwise correlation between OTUs
otu.cor <- rcorr(as.matrix(otu.table.filter), type="spearman")

# Get p-value matrix
otu.pval <- forceSymmetric(otu.cor$P) # Self-correlation as NA

# Select only the taxa for the filtered OTUs by using rownames of otu.pval
sel.tax <-tax[rownames(otu.pval),,drop=FALSE]

# Sanity check 
all.equal(rownames(sel.tax), rownames(otu.pval))
## [1] TRUE
# Filter the association based on p-values and level of correlations
p.yes <- otu.pval < 0.001

# Select the r values for p.yes
r.val <- otu.cor$r # select all the correlation values 
p.yes.r <- r.val * p.yes # only select correlation values based on p-value criterion 

# Select OTUs by level of correlation
p.yes.r <- abs(p.yes.r) > 0.7 # output is logical vector
p.yes.rr <- p.yes.r * r.val # use logical vector for subscripting.


# Create an adjacency matrix
adjm <- as.matrix(p.yes.rr)

# Add taxonomic information associated with adjacency matrix
colnames(adjm) <- as.vector(sel.tax$Family)
rownames(adjm) <- as.vector(sel.tax$Family)

# Create an adjacency matrix in igraph format                               
net.grph <- graph.adjacency(adjm,mode="undirected", weighted=TRUE,diag=FALSE)


# Calculate edge weight == level of correlation
edgew <- E(net.grph)$weight

# Identify isolated nodes
bad.vs <- V(net.grph)[degree(net.grph) == 0] 

# Remove isolated nodes
net.grph <- delete.vertices(net.grph, bad.vs)


# Plot the graph object
plot(net.grph,
     vertex.size=4,
     vertex.frame.color="black",
     edge.curved=F,
     edge.width=1.5,
     layout=layout.fruchterman.reingold,
     edge.color=ifelse(edgew<0,"red","blue"),
     vertex.label=NA,
     vertex.label.color="black",
     vertex.label.family="Times New Roman",
     vertex.label.font=2)

# Plot the graph object with label.
plot(net.grph,
     vertex.size=4,
     vertex.frame.color="black",
     edge.curved=F,
     edge.width=1.5,
     layout=layout.fruchterman.reingold,
     edge.color=ifelse(edgew<0,"red","blue"),
     vertex.label.color="black",
     vertex.label.family="Times New Roman",
     vertex.label.font=3,
     vertex.label.cex= 0.5)

nodes_info_basedon_nodedegree <- vtable.ON.nodeDegree(net.grph, sel.tax)
kable(nodes_info_basedon_nodedegree)
node nd Count Kingdom Phylum Class Order Family Genus Species
29 f__Wallemiaceae 10 NA NA NA NA NA NA NA NA
54 f__Wallemiaceae 9 NA NA NA NA NA NA NA NA
31 f__Wallemiaceae 8 NA NA NA NA NA NA NA NA
39 f__Wallemiaceae 8 NA NA NA NA NA NA NA NA
41 f__Wallemiaceae 8 NA NA NA NA NA NA NA NA
42 f__Wallemiaceae 8 NA NA NA NA NA NA NA NA
48 f__Wallemiaceae 8 NA NA NA NA NA NA NA NA
40 f__Wallemiaceae 7 NA NA NA NA NA NA NA NA
14 o__Capnodiales_unclassified 3 NA NA NA NA NA NA NA NA
17 f__Wallemiaceae 3 NA NA NA NA NA NA NA NA
18 f__Wallemiaceae 3 NA NA NA NA NA NA NA NA
20 f__Wallemiaceae 3 NA NA NA NA NA NA NA NA
23 f__Pyronemataceae 3 NA NA NA NA NA NA NA NA
30 p__Basidiomycota_unclassified 3 NA NA NA NA NA NA NA NA
33 f__Pyronemataceae 3 NA NA NA NA NA NA NA NA
43 p__Ascomycota_unclassified 3 NA NA NA NA NA NA NA NA
52 f__Pyronemataceae 3 NA NA NA NA NA NA NA NA
1 f__Pyronemataceae 2 NA NA NA NA NA NA NA NA
2 f__Pyronemataceae 2 NA NA NA NA NA NA NA NA
3 f__Pleosporaceae 2 NA NA NA NA NA NA NA NA
4 f__Pleosporales_family_Incertae_sedis 2 NA NA NA NA NA NA NA NA
5 f__Wallemiaceae 2 NA NA NA NA NA NA NA NA
6 f__Tremellales_family_Incertae_sedis 2 NA NA NA NA NA NA NA NA
7 f__Wallemiaceae 2 NA NA NA NA NA NA NA NA
8 f__Pleosporaceae 2 NA NA NA NA NA NA NA NA
9 o__Hypocreales_unclassified 2 NA NA NA NA NA NA NA NA
11 f__Davidiellaceae 2 NA NA NA NA NA NA NA NA
22 f__Wallemiaceae 2 NA NA NA NA NA NA NA NA
24 f__Bionectriaceae 2 NA NA NA NA NA NA NA NA
25 f__Nectriaceae 2 NA NA NA NA NA NA NA NA
26 f__Wallemiaceae 2 NA NA NA NA NA NA NA NA
27 f__Phallaceae 2 NA NA NA NA NA NA NA NA
32 f__Pyronemataceae 2 NA NA NA NA NA NA NA NA
34 f__Pyronemataceae 2 NA NA NA NA NA NA NA NA
37 f__Phallaceae 2 NA NA NA NA NA NA NA NA
38 f__Glomeraceae 2 NA NA NA NA NA NA NA NA
45 f__Glomeraceae 2 NA NA NA NA NA NA NA NA
10 f__Pleosporaceae 1 NA NA NA NA NA NA NA NA
12 f__Tremellales_family_Incertae_sedis 1 NA NA NA NA NA NA NA NA
13 f__Wallemiaceae 1 NA NA NA NA NA NA NA NA
15 c__Eurotiomycetes_unclassified 1 NA NA NA NA NA NA NA NA
16 f__Glomeraceae 1 NA NA NA NA NA NA NA NA
19 f__Hypocreales_family_Incertae_sedis 1 NA NA NA NA NA NA NA NA
21 k__Fungi_unclassified 1 NA NA NA NA NA NA NA NA
28 f__Glomeraceae 1 NA NA NA NA NA NA NA NA
35 f__Phallaceae 1 NA NA NA NA NA NA NA NA
36 f__Pyronemataceae 1 NA NA NA NA NA NA NA NA
44 f__Pyronemataceae 1 NA NA NA NA NA NA NA NA
46 f__Ceratobasidiaceae 1 NA NA NA NA NA NA NA NA
47 f__Ceratobasidiaceae 1 NA NA NA NA NA NA NA NA
49 f__Mortierellaceae 1 NA NA NA NA NA NA NA NA
50 f__Mortierellaceae 1 NA NA NA NA NA NA NA NA
51 f__Agaricaceae 1 NA NA NA NA NA NA NA NA
53 p__Ascomycota_unclassified 1 NA NA NA NA NA NA NA NA