了解一个研究方向和相关研究领域的大佬,收集文献是必不可少的操作。 作为一名生信人,我们可以通过编程来自动化实现以上流程,今后只需要一行代码,研究领域情报尽在囊中。
还不熟悉 Python 环境搭建的小伙伴,参考之前发的文章
在终端执行
pip install biopython
接下来通过BioPython提供的接口来实现快速的文献情报的收集下载。 下面例子是利用PubMed数据库来查询所有关于小鼠的文献资料,为了展示基础的流程,这里采用逐条下载的方式。
from Bio import Entrez
from Bio import Medline
# 参数设置
Entrez.email = "your_email@163.com"
Entrez.tool = "PaperScript"
# 用 esearch 在 pubmed 库中搜索关键字为 "mouse" 的文章
# RetMax 这个参数为每次返回的最大个数,因此如果把Count的值赋给RetMax就会获取全部的mouse的文章,这里为实例设置为100
hd_esearch = Entrez.esearch(db="pubmed", term="mouse", RetMax="100")
read_esearch = Entrez.read(hd_esearch)
idlist = read_esearch["IdList"]
print ("Total: ", read_esearch["Count"])
# 用 efetch下载
hd_efetch = Entrez.efetch(db="pubmed", id=idlist, rettype="medline", retmode="text", )
# 用 Medline 来解析
parse_medline = Medline.parse(hd_efetch)
with open("res/mouse_pubmed.xls", "w") as file:
file.write("title\tauthors\tsource\tPubMed\n")
for i, ele in enumerate(list(parse_medline)):
line = ele['TI'] + "\t" + ",".join(ele['AU']) + "\t" + ele['SO'] + "\t" + ele['PMID'] + "\n"
file.write(line)
print (i, line)
这里我们来查询近一年的关于 Sus scrofa 的综述。
from Bio import Entrez
# 参数设置
Entrez.email = "example@163.com"
Entrez.tool = "exampleScript"
# 搜索
hd_esearch = Entrez.esearch(db="pubmed", term="Sus scrofa", reldate=365, ptyp="Review", usehistory="y")
read_esearch = Entrez.read(hd_esearch)
total = int(read_esearch["Count"])
webenv = read_esearch["WebEnv"]
query_key = read_esearch["QueryKey"]
# 这里演示设定total为 10, 实际文献数量不止这么多
total = 10
step = 5
print("Result items: ", total)
with open("res/recent_review_sus_scrofa.txt", "w") as file:
for start in range(0, total, step):
print("Download record %i to %i" % (start + 1, int(start+step)))
hd_efetch = Entrez.efetch(db="pubmed", retstart=start, retmax=step, webenv=webenv, query_key=query_key, rettype="medline", retmode="text")
file.write(hd_efetch.read())
附赠一个小彩蛋:如何自动化查询某个物种的分类学位置?
下面的例子是查询我们人类在分类学中的位置。
# =====查看物种谱系=====
from Bio import Entrez
# 参数设置
Entrez.email = "your_email@163.com"
Entrez.tool = "TaxScript"
# 在 Taxonomy 库中搜索 Homo sapiens
hd_esearch = Entrez.esearch(db="Taxonomy", term="Homo sapiens")
read_esearch = Entrez.read(hd_esearch)
id = read_esearch["IdList"][0]
hd_eftech = Entrez.efetch(db="Taxonomy", id=id, retmode="xml")
read_eftech = Entrez.read(hd_eftech)
print(read_eftech[0]["Lineage"])
NCBI 提供了很多生物相关数据库,用法几乎差不多,可以根据自身研究或者感兴趣的方向自行选择。
参考
官方文档:https://biopython-cn.readthedocs.io/zh_CN/latest/ 我的博客:https://blog.csdn.net/u011262253/article/details/88542879