XLRD / Entrez:搜索Pubmed并提取计数

时间:2016-10-10 23:46:09

标签: python xlrd biopython pubmed

我正在开展一个项目,要求我使用pubmed电子表格中的输入和结果的打印计数来搜索Excel。我一直在使用xlrdentrez来完成这项工作。这是我尝试过的。

  1. 我需要使用作者姓名,他/她的医学院,一系列年份和他/她的导师姓名搜索pubmed,这些都在{{1}中电子表格。我使用Excel将所有信息转换为字符串列表。

    xlrd
  2. 我已设法使用Entrez打印我的特定查询的计数。

    from xlrd import open_workbook
    book = xlrd.open_workbook("HEENT.xlsx").sheet_by_index(0)
    med_name = []
    for row in sheet.col(2):
        med_name.append(row)
    med_school = []
    for row in sheet.col(3):
        med_school.append(row)
    mentor = []
    for row in sheet.col(9):
        mentor.append(row)
    

    问题是我需要将学生姓名(“Jennifer Runch”)替换为学生姓名列表中的下一个学生姓名(“med_name”),下一所学校的医学院,以及当前导师的名字从名单中选择下一位导师的名字。

  3. 我想我应该在向from Bio import Entrez Entrez.email = "your@email.edu" handle = Entrez.egquery(term="Jennifer Runch AND ((2012[Date - Publication] : 2017[Date - Publication])) ") handle_1 = Entrez.egquery(term = "Jennifer Runch AND ((2012[Date - Publication] : 2017[Date - Publication])) AND Leoard P. Byk") handle_2 = Entrez.egquery(term = "Jennifer Runch AND ((2012[Date - Publication] : 2017[Date - Publication])) AND Southern Illinois University School of Medicine") record = Entrez.read(handle) record_1 = Entrez.read(handle_1) record_2 = Entrez.read(handle_2) pubmed_count = [] for row in record["eGQueryResult"]: if row["DbName"] == "pubmed": pubmed_count.append(row["Count"]) for row in record_1["eGQueryResult"]: if row["DbName"] == "pubmed": pubmed_count.append(row["Count"]) for row in record_2["eGQueryResult"]: if row["DbName"] == "pubmed": pubmed_count.append(row["Count"]) print(pubmed_count) >>>['3', '0', '0'] 声明我的电子邮件后写一个for循环,但我不确定如何将两个代码块链接在一起。有没有人知道连接两个代码块的有效方法,或者知道如何以比我尝试过的更有效的方式做到这一点? 谢谢!

1 个答案:

答案 0 :(得分:1)

你已经掌握了大部分代码。它只需要稍微修改一下。

假设您的表格如下:

Jennifer Bunch  |Southern Illinois University School of Medicine|Leonard P. Rybak
Philipp Robinson|Stanford University School of Medicine         |Roger Kornberg

您可以使用以下代码

import xlrd
from Bio import Entrez
sheet = xlrd.open_workbook("HEENT.xlsx").sheet_by_index(0)

med_name = list()
med_school = list()
mentor = list()
search_terms = list()
for row in range(0, sheet.nrows):
    search_terms.append([sheet.cell_value(row, 0), sheet.cell_value(row,1), sheet.cell_value(row, 2)])

pubmed_counts = list()

for search_term in search_terms:
    handle = Entrez.egquery(term="{0} AND ((2012[Date - Publication] : 2017[Date - Publication])) ".format(search_term[0]))
    handle_1 = Entrez.egquery(term = "{0} AND ((2012[Date - Publication] : 2017[Date - Publication])) AND {1}".format(search_term[0], search_term[2]))
    handle_2 = Entrez.egquery(term = "{0} AND ((2012[Date - Publication] : 2017[Date - Publication])) AND {1}".format(search_term[0], search_term[1]))
    record = Entrez.read(handle)
    record_1 = Entrez.read(handle_1)
    record_2 = Entrez.read(handle_2)

    pubmed_count = ['', '', '']

    for row in record["eGQueryResult"]:
        if row["DbName"] == "pubmed":
            pubmed_count[0] = row["Count"]
    for row in record_1["eGQueryResult"]:
        if row["DbName"] == "pubmed":
            pubmed_count[1] = row["Count"]
    for row in record_2["eGQueryResult"]:
        if row["DbName"] == "pubmed":
            pubmed_count[2] = row["Count"]

    print(pubmed_count)
    pubmed_counts.append(pubmed_count)

输出

['3', '0', '0']
['1', '0', '0']

所需的修改是使用format使查询变量。

其他一些不必要但可能有用的修改:

  • 仅在Excel工作表上循环
  • pubmed_count存储在预定义列表中,因为如果值返回为空,则输出的大小会有所不同,因此很难猜出哪个值属于哪个查询
  • 一切都可以进一步优化和美化,例如将查询存储在列表中并循环遍历它们,这将减少代码重复次数,但现在它可以完成工作。