在 GitHub 上編輯此頁面

使用 Entrez 模組檢索並註解 Entrez 基因 ID。

問題

如果您處理大量的基因 ID(例如微陣列分析產生的基因 ID),註解它們對於確定其潛在的生物學意義非常重要。然而,許多註解系統僅基於網路,或者無法與 Python 配合使用。

解決方案

感謝 Entrez 模組,可以使用下面提供的 “retrieve_ids” 函數輕鬆註解批次的 Entrez 基因 ID。

此範例假設您有一個 Entrez 基因 ID 的列表。注意:即使它們是數字,也應該將它們儲存為字串,而不是整數。

import sys

from Bio import Entrez

# *Always* tell NCBI who you are
Entrez.email = "your email here"


def retrieve_annotation(id_list):

    """Annotates Entrez Gene IDs using Bio.Entrez, in particular epost (to
    submit the data to NCBI) and esummary to retrieve the information.
    Returns a list of dictionaries with the annotations."""

    request = Entrez.epost("gene", id=",".join(id_list))
    try:
        result = Entrez.read(request)
    except RuntimeError as e:
        # FIXME: How generate NAs instead of causing an error with invalid IDs?
        print "An error occurred while retrieving the annotations."
        print "The error returned was %s" % e
        sys.exit(-1)

    webEnv = result["WebEnv"]
    queryKey = result["QueryKey"]
    data = Entrez.esummary(db="gene", webenv=webEnv, query_key=queryKey)
    annotations = Entrez.read(data)

    print "Retrieved %d annotations for %d genes" % (len(annotations), len(id_list))

    return annotations

正如函數的 docstring 所述,該函數返回一個字典列表,每個基因一個字典,您可以以任何您想要的方式使用它。以下範例印出檢索到的註解的 ID、基因符號和基因名稱。

def print_data(annotation):
    for gene_data in annotation:
        gene_id = gene_data["Id"]
        gene_symbol = gene_data["NomenclatureSymbol"]
        gene_name = gene_data["Description"]
        print "ID: %s - Gene Symbol: %s - Gene Name: %s" % (
            gene_id,
            gene_symbol,
            gene_name,
        )

更多資訊

Tao Liu 使用將 Entrez ID 和 RefSeq ID 之間轉換的完整範例擴展了此程式碼。