![](/img/trans.png)
[英]change dictionary values to dictionaries from list of dictionaries in python
[英]Return values from a Python Entrez dictionary of dictionaries
我想从Entrez Gene页面中抓取Interactions表。
Interactions表是从Web服务器填充的,当我尝试在R中使用XML包时,可以获取Entrez基因页,但是Interactions表主体为空(Web服务器未填充)。
处理R中的Web服务器问题可能是可以解决的(我很想看看如何解决),但是Biopython似乎是一条更简单的路径。
我整理了以下内容,这给出了我想要的示例基因:
# Pull the Entrez gene page for MAP1B using Biopython
from Bio import Entrez
Entrez.email = "jamayfie@vasci.umass.edu"
handle = Entrez.efetch(db="gene", id="4131", retmode="xml")
record = Entrez.read(handle)
handle.close()
PPI_Entrez = []
PPI_Sym = []
# Find the Dictionary that contains the Interaction table
for x in range(1, len(record[0]["Entrezgene_comments"])):
if ('Gene-commentary_heading', 'Interactions') in record[0]["Entrezgene_comments"][x].items():
for y in range(0, len(record[0]["Entrezgene_comments"][x]['Gene-commentary_comment'])):
EntrezID = record[0]["Entrezgene_comments"][x]['Gene-commentary_comment'][y]['Gene-commentary_comment'][1]['Gene-commentary_source'][0]['Other-source_src']['Dbtag']['Dbtag_tag']['Object-id']['Object-id_id']
PPI_Entrez.append(EntrezID)
Sym = record[0]["Entrezgene_comments"][x]['Gene-commentary_comment'][y]['Gene-commentary_comment'][1]['Gene-commentary_source'][0]['Other-source_anchor']
PPI_Sym.append(Sym)
# Return the desired values: I want the Entrez ID and Gene symbol for each interacting protein
PPI_Entrez # Returns the EntrezID
PPI_Sym # Returns the gene symbol
这段代码有效,给了我我想要的东西。 但是我认为它很丑陋,并且担心如果Entrez基因页的格式稍有变化,它将破坏代码。 特别是,与指定完整路径相比,必须有一种更好的方法来提取所需信息,就像我所做的那样:
record[0]["Entrezgene_comments"][x]['Gene-commentary_comment'][y]['Gene-commentary_comment'][1]['Gene-commentary_source'][0]['Other-source_anchor']
但是我不知道如何在不指定要降级的每个级别的情况下搜索字典词典。 当我尝试使用诸如find()之类的函数时,它们将在下一级进行操作,但并非一直到最底层。
是否有通配符,相当于Python的“ //”或我可以用来在不命名完整路径的情况下获取['Object-id_id']的函数? 对于更干净的代码的其他建议也将受到赞赏。
我不确定Python中的xpath,但是如果代码有效,则不必担心删除完整路径或Entrez Gene XML是否会更改。 自从您第一次尝试R以来,您可以使用下面对Entrez Direct的系统调用或类似rentrez的软件包来获取XML。
doc <- xmlParse( system("efetch -db=gene -id=4131 -format xml", intern=TRUE) )
接下来,在http://www.ncbi.nlm.nih.gov/gene/4131#interactions获取与表中的行对应的节点
x <- getNodeSet(doc, "//Gene-commentary_heading[.='Interactions']/../Gene-commentary_comment/Gene-commentary" )
length(x)
[1] 64
x[1]
x[50]
首先尝试简单的东西
xmlToDataFrame(x[1:4])
Gene-commentary_type Gene-commentary_text Gene-commentary_refs Gene-commentary_source Gene-commentary_comment
1 18 Affinity Capture-MS 24457600 BioGRID110304BioGRID 255BioGRID110304255GeneID8726EEDBioGRID114265
2 18 Reconstituted Complex 20195357 BioGRID110304BioGRID 255BioGRID110304255GeneID2353FOSBioGRID108636
3 18 Reconstituted Complex 20195357 BioGRID110304BioGRID 255BioGRID110304255GeneID1936EEF1DBioGRID108256
4 18 Affinity Capture-MS 2345592220562859 BioGRID110304BioGRID 255BioGRID110304255GeneID6789STK4BioGRID112665
Gene-commentary_create-date Gene-commentary_update-date
1 2014461120 201410513330
2 201312810490 201410513330
3 201312810490 201410513330
4 20137710360 201410513330
某些标签(例如文本,引用,来源和日期)应易于解析
sapply(x, function(x) paste( xpathSApply(x, ".//PubMedId", xmlValue), collapse=", "))
我不确定注释或表中列出的产品,交互剂和其他基因如何存储在XML中,但是我不确定每个节点有一个或三个符号和三个ID。
sapply(x, function(x) paste( xpathSApply(x, ".//Gene-commentary_comment//Other-source_anchor", xmlValue), collapse=" + "))
sapply(x, function(x) paste( xpathSApply(x, ".//Gene-commentary_comment//Object-id_id", xmlValue), collapse=" + "))
最后,由于我认为Entrez Gene只是复制IntAct和BioGrid,因此您也可以尝试这些站点。 Biogrid的休息服务非常简单,但是您必须注册一个密钥。
url <- "http://webservice.thebiogrid.org/interactions?geneList=MAP1B&taxId=9606&includeHeader=TRUE&accesskey=[ your ACCESSKEY ]"
biogrid <- read.delim(url)
dim(biogrid)
[1] 58 24
head(biogrid[, c(8:9,12)])
Official.Symbol.Interactor.A Official.Symbol.Interactor.B Experimental.System
1 ANP32A MAP1B Two-hybrid
2 MAP1B ANP32A Two-hybrid
3 RASSF1 MAP1B Affinity Capture-Western
4 RASSF1 MAP1B Two-hybrid
5 ANP32A MAP1B Affinity Capture-Western
6 GAN MAP1B Affinity Capture-Western
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.