分享一段从KEGG通路中提取基因互作数据的R代码

从KEGG(Kyoto Encyclopedia of Genes and Genomes)网站上获得差异表达基因所富集通路的XML文件。使用R包XML提取这些XML文件中的relation,entry和group关系。然后利用脚本组合它们从中提取其...

KEGGKyoto Encyclopedia of Genes and Genomes)网站上获得差异表达基因所富集通路的XML文件。使用RXML提取这些XML文件中的relationentrygroup关系。然后利用脚本组合它们从中提取其中基因的互作信息,构建KEGG通路基因互作网络从KEGGKyoto Encyclopedia of Genes and Genomes)网站上获得差异表达基因所富集通路的XML文件。使用RXML提取这些XML文件中的relationentrygroup关系。然后利用脚本组合它们从中提取其中基因的互作信息,构建KEGG通路基因互作网络

kegg.ppi=function(hsas){
library(curl)
library("XML")
library("methods")
all.ppi=rbind()
pal=0

for(hs in hsas){
url <- paste0("http://rest.kegg.jp/get/",hs,"/kgml")
tmp <- tempfile()
curl_download(url, tmp)
result <- xmlParse(file = tmp)
rootnode <- xmlRoot(result)
xmlNet=rbind()
pal=pal+1
print(paste0('parse:',hs,'...',pal,'/',length(hsas)))
for(a in xmlApply(rootnode, xmlToList)){
  ns=names(a$.attrs)
  which(ns=='id')
  e1=a$.attrs['entry1']
  e2=a$.attrs['entry2']
  tp=a$.attrs['type']
  id=a$.attrs['id']
  name=a$.attrs['name']
  xmlNet=rbind(xmlNet,c(id,name,e1,e2,tp)) 
}
genes=xmlNet[which(xmlNet[,5]=='gene'),]
ppi=xmlNet[is.na(xmlNet[,1]),]
ppi=cbind(node1=gsub('hsa:','',genes[match(ppi[,3],genes[,1]),2]),
          node2=gsub('hsa:','',genes[match(ppi[,4],genes[,1]),2]),type=ppi[,5])
ppi=ppi[!is.na(ppi[,1])&!is.na(ppi[,2]),]
all.ppi=rbind(all.ppi,ppi)
}
all.gid=rbind()
for(i in 1:nrow(all.ppi)){
  lk1=unlist(strsplit(all.ppi[i,1],' '))
  lk2=unlist(strsplit(all.ppi[i,2],' '))
  tmp=rbind()
  for(g in lk1){
    tmp=rbind(tmp,cbind(rep(g,length(lk2)),lk2))
  }
  all.gid=rbind(all.gid,cbind(tmp,rep(all.ppi[i,3],nrow(tmp))))  
}
colnames(all.gid)=c('Node1','Node2','type')
all.gid=all.gid[match(unique(paste0(all.gid[,1],'-',all.gid[,2])),paste0(all.gid[,1],'-',all.gid[,2])),]
return(all.gid)
}
调用:all.ppi=kegg.ppi(kegg@result$ID[kegg@result$p.adjust<0.05])
attachments-2019-03-tTtYF2tC5c9338b1e494b.png

  • 发表于 2019-03-21 15:10
  • 阅读 ( 6093 )
  • 分类:编程语言

3 条评论

请先 登录 后评论
不写代码的码农
祝让飞

生物信息工程师

118 篇文章

作家榜 »

  1. 祝让飞 118 文章
  2. 柚子 91 文章
  3. 刘永鑫 64 文章
  4. admin 57 文章
  5. 生信分析流 55 文章
  6. SXR 44 文章
  7. 张海伦 31 文章
  8. 爽儿 25 文章