#This scrpit describe how to build a global network from KEGG database files using the KEML2igraph function from package NetPathMiner library(igraph) library(NetPathMiner) library(org.Hs.eg.db) #load all the kegg human pathways into one igraph object path.db<-"Path to KEGG files" #modify this path according to your own path files<-list.files(path.db) setwd(path.db) graph<-KGML2igraph(filename=files,parse.as = "signaling",expand.complexes = TRUE) #convert id to gene symbol g.ft<-formatGraph(graph) #remove the vertices which could not be converted to entrez gene id #remove the vertices which have no links to others g.fm<-filterGraph(g.ft) #remove the loops g.dl<- simplify(g.fm, remove.multiple = F, remove.loops = T) save(g.dl,file="keggAllPath.RData") #Functions needed formatGraph<-function(tmp.graph){ #convert id to symbol v<-V(tmp.graph) v.entrez<-as.vector(names(v)) v.entrez<-gsub("hsa:","",v.entrez) v.sym<-mget(v.entrez,org.Hs.egSYMBOL,ifnotfound = NA) tmp.num<-sum(ifelse(is.na(v.sym),1,0)) if(tmp.num==0){ print("All entrez ids were converted to symbols.") }else{ print(paste(tmp.num," entrez ids were not converted to symbols.",sep="")) v.sym[is.na(v.sym)]<-"NotGene" } graph.sym <- set.vertex.attribute(tmp.graph, "label", value=v.entrez) graph.sym <- set.vertex.attribute(tmp.graph, "name", value=v.sym) graph.sim<-simplify(graph.sym, remove.multiple = T, remove.loops = T) return(graph.sim) } filterGraph<-function(graph,deg=0,unmapped="NotGene"){ #remove vertices which failed in id conversion, and remove isolated genes tmp.deg <- degree(graph, mode="all") n<-length(tmp.deg[names(tmp.deg) == unmapped]) graph.rm<-graph if(n>0){ for(i in 1:n){ graph.rm<-delete.vertices(graph.rm,"NotGene") } } deg.in<-colSums(as.matrix(graph.rm[])) deg.out<-rowSums(as.matrix(graph.rm[])) deg.new<-deg.in+deg.out v.rm<-names(deg.new[deg.new==deg]) graph.rm.2<-delete_vertices(graph.rm,v.rm) return(graph.rm.2) }