我想从 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'] 的函数?其他关于更简洁代码的建议也值得赞赏。