読者です 読者をやめる 読者になる 読者になる

biocGraph パッケージを使用して Rgraphviz で描いた図にリンクをはる方法

R
library(RCurl) 
library(XML) 
library(geneplotter) 
library(Rgraphviz) 
library(biocGraph) 
library(org.Hs.eg.db) 
gene_article <- function(geneID){ 
ID2symbol <- as.list(org.Hs.egSYMBOL) 
gene_name <- unlist(ID2symbol[geneID]) 
url <- paste("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=gene&retmode=xml&id=",geneID,sep="") 
x <- getURL(url) 
pubmed <- xmlRoot(xmlTreeParse(x)) 
pmid <- (xmlElementsByTagName(pubmed,"PubMedId",recursive = T)) 
pmid <- unique(sapply(pmid,xmlValue))             
gene_list<-list(NULL) 
for(i in 1:length(pmid)){
url<-paste("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&term=",pmid[i],"[PMID]+AND+human[ORGN]",sep="")
data<-getURL(url)
doc<-xmlRoot(xmlTreeParse(data))
id<-xmlSApply(doc[["IdList"]], xmlValue)
gene_list[[i]]<-unlist(ID2symbol[id]) 
} 
a<-sapply(gene_list,length) 
ok<-a>1&a<5 
genes<-gene_list[ok] 
pmid<-pmid[ok] 
names(genes)<-pmid
nd<-c(unique(unlist(genes)),pmid)
ed<-lapply(nd,function(z)list(edges=NULL))
names(ed)<-nd
for(i in 1:length(genes)){
p<-pmid[i] 
g<-genes[[i]]
ed[[p]]<-list(edges=match(g,nd))
} 
graph<-new("graphNEL",nodes=nd,edgeL=ed,edgemode="directed")
nt <- rep(c(1,2),c(length(unique(unlist(genes))),length(pmid)))
fill <- c("yellow","red")[nt]
names(fill) <- nd
png_name <- paste(gene_name,".png",sep="")
html_name <- paste(gene_name,".html",sep="")
png(png_name,width=800,height=600)
graph <- layoutGraph(graph,layoutType="neato")
nodeRenderInfo(graph) <- list(fill=fill)
graph.par(list(nodes=list(fontsize=14)))
graph <- renderGraph(graph)
dev.off() 
con <- openHtmlPage(html_name,gene_name)
gnodes <- nodes(graph)[nt==1]
gnodes_id <- unlist(as.list(org.Hs.egSYMBOL2EG[gnodes]))
pnodes <- nodes(graph)[nt==2]
gpnodes <- c(gnodes,pnodes)
links <- character(length(gpnodes))
tooltips <- gpnodes
gene_links <- paste("http://www.ncbi.nlm.nih.gov/gene/",gnodes_id,sep="")
pubmed_links <- paste("http://www.ncbi.nlm.nih.gov/pubmed/",pnodes,sep="")
links <- c(gene_links,pubmed_links)
names(links) <- gpnodes
names(tooltips) <- gpnodes
tags <- list(HREF=links,TITLE=tooltips)
imageMap(graph,con=con,tags=tags,imgname=png_name)
closeHtmlPage(con)
}
gene_article("5652") 

結果はこちら