7.1 Subnetwork identification

library(XGR)
library(dnet)
RData.location <- "http://galahad.well.ox.ac.uk/Myocarditis"
ig.KEGGmm.list <- xRDataLoader("ig.KEGGmm.list", RData.location=RData.location)

## the gene interaction network (the parent network) merged from 11 KEGG pathways identified above
vec_pathways <- c("Chemokine signaling pathway", "Cytokine-cytokine receptor interaction", "TNF signaling pathway","Viral protein interaction with cytokine and cytokine receptor", "NF-kappa B signaling pathway","Cell adhesion molecules (CAMs)","NOD-like receptor signaling pathway","Osteoclast differentiation","Natural killer cell mediated cytotoxicity","Complement and coagulation cascades","Antigen processing and presentation")
ind <- match(vec_pathways, names(ig.KEGGmm.list))
ig <- ig.KEGGmm.list[ind] %>% xCombineNet(combineBy='union', attrBy="intersect", verbose=TRUE)
ig <- dnet::dNetInduce(g=ig, nodes_query=V(ig)$name, knn=0, remove.loops=FALSE, largest.comp=TRUE) %>% as.undirected()

## logFC matrix
mat_FC <- read_delim('DE_genes.txt.gz', delim='\t') %>% select(mgi_symbol,time,logFC) %>% group_by(mgi_symbol,time) %>% summarise(logFC=median(logFC)) %>% ungroup() %>% pivot_wider(names_from=time, values_from=logFC) %>% column_to_rownames('mgi_symbol')

## mat_FDR matrix
mat_FDR <- read_delim('DE_genes.txt.gz', delim='\t') %>% select(mgi_symbol,time,FDR) %>% group_by(mgi_symbol,time) %>% summarise(FDR=min(FDR)) %>% ungroup() %>% pivot_wider(names_from=time, values_from=FDR) %>% column_to_rownames('mgi_symbol')

## aggregated FDR across timepoints
aFDR <- dnet::dPvalAggregate(mat_FDR, method="fishers")

## identification of gene subnetwork
subg <- xSubneterGenes(aFDR, network.customised=ig, seed.genes=T, subnet.size=50, verbose=T)

Nodes and edges can be output into subnetwork_node_info.txt and subnetwork_edge_info.txt, respectively.

# gene nodes: write into a file 'subnetwork_node_info.txt'
subg %>% igraph::as_data_frame("vertices") %>% as_tibble() %>% select(name,description) %>% inner_join(mat_FC %>% as_tibble(rownames='name'), by='name') %>% inner_join(mat_FDR %>% as_tibble(rownames='name'), by='name') -> df_nodes
colnames(df_nodes) <-  colnames(df_nodes) %>% str_replace_all('.x','.FC') %>% str_replace_all('.y','.FDR')
df_nodes %>% write_delim('subnetwork_node_info.txt', delim='\t')

# interacting edges: write into a file 'subnetwork_edge_info.txt'
subg %>% igraph::as_data_frame("edges") %>% as_tibble() %>% write_delim('subnetwork_edge_info.txt', delim='\t')