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'] に到達するために使用できる関数はありますか? よりクリーンなコードの提案も歓迎します。