python的Bio下的Entrez使用

biopython文档

在这里插入图片描述

http://biopython.org/DIST/docs/tutorial/Tutorial.html
在这里插入图片描述

https://biopython-cn.readthedocs.io/zh_CN/latest/index.html
https://biopython-cn.readthedocs.io/zh_CN/latest/cn/chr09.html#entrez

进入NCBI的Entrez数据库

Entrez (https://www.ncbi.nlm.nih.gov/Web/Search/entrezfs.html)是一个数据检索系统,为用户提供对PubMed、GenBank、GEO等NCBI数据库的访问。您可以通过网络浏览器访问Entrez,手动输入查询,也可以使用Biopython s Bio。以规划方式进入Entrez的模块。例如,后者允许您搜索PubMed或从Python脚本中下载GenBank记录。生物。Entrez模块使用Entrez编程实用工具(也称为EUtils),由8个工具组成,在https://www.ncbi.nlm.nih.gov/books/NBK25501/的NCBI页面上有详细描述。这些工具中的每一个都对应于Bio中的一个Python函数。Entrez模块,如下节所述。此模块确保查询使用了正确的URL,并遵循NCBI负责数据访问的指导原则。————来自官方文档机翻

注意事项

如果你使用一个API键,你每秒最多可以做10个查询,否则每秒最多3个查询。这是由Biopython自动执行的。在参数列表中包含api_key="MyAPIkey"或将其设置为模块级变量。

from Bio import Entrez
Entrez.api_key = "MyAPIkey"

使用可选的email参数,NCBI可以在有问题时联系您。您可以在每次调用Entrez时显式地将其设置为一个参数(例如,在参数列表中包含email=“123@example.com”),或者您可以设置一个全局电子邮件地址。

from Bio import Entrez
Entrez.email = "123@example.com"

如果您在一些较大的软件套件中使用了Biopython,请使用工具参数指定此参数。你可以在每次调用Entrez时显式地设置工具名作为参数(例如在参数列表中包括tool=“MyLocalScript”),或者你可以设置一个全局工具名

from Bio import Entrez
Entrez.tool = "MyLocalScript"

方法

**

(注意Bio.Entrez官方文档的方法并没有展示全部属性,比如maxdata、mindata等,所以要看NCBI的The E-utilities In-Depth: Parameters, Syntax and More

**

Entrez.read()

这个函数解析由NCBI的Entrez实用程序创建的XML文件,返回Python列表和字典的多级数据结构。只要DTD可用,NCBI的Entrez实用程序返回的大多数XML文件都可以通过该函数进行解析。Biopython包括最常用的Entrez实用程序的DTD。——来自代码注释机翻

返回是:
Entrez.Parser.DictionaryElement类型,继承dict类型
NCBI Entrez XML元素映射到字典。
所以dict字典的方法应该都能用

将查询结果XML数据格式转换成python对象

Entrez.einfo()

EInfo为每个NCBI的数据库提供了条目索引,最近更新的时间以及可用的链接。此外,你可以很容易的使用EInfo通过 Entrez获取所有数据库名字的列表一个具有注脚的文本。

from Bio import Entrez
Entrez.email = "123@example.com"     # Always tell NCBI who you are
handle = Entrez.einfo()
result = handle.read()

Entrez.esearch()

我们可以使用 Bio.Entrez.esearch() 来搜索任意的数据库,返回pmid等信息。

from Bio import Entrez
Entrez.email = "123@example.com"
search_results = Entrez.read(
    Entrez.esearch(
        db="pubmed", term="mutation", reldate=365, datetype="pdat", usehistory="y"
    )
)
count = int(search_results["Count"])
print("Found %i results" % count)

参数:

参数列表意义
db数据库名称
term查询内容
retmax检索获取的结果条目数最大值
datetype设置查询文献的日期类型,是按照修改日期检索、还是出版日期、还是Entrez日期
reldate
mindate, maxdate两个参数必须一起使用,设置检索日期范围
usehistory
retmode返回的数据格式;XML是唯一支持的值,它实际上返回TXT。——源代码注释


python程序里只需要db和term两个参数即可使用。其他参数可以参考:EDirect
在这里插入图片描述

Entrez.efetch()

从Entrez中提取完整的记录,可以通过efetch()下载特点格式的文章数据。
通过传入id参数精确查找文献信息。

from Bio import Entrez, Medline
Entrez.email = "123@example.com"     # Always tell NCBI who you are
handle = Entrez.efetch(db="nucleotide", id="186972394", rettype="gb", retmode="text")

handle = Entrez.efetch(db="pubmed", id=id, rettype="medline", retmode="text")
# record = Entrez.read(handle)#这种方法不好用
record = list(Medline.parse(handle))[0] #检验使用Bio.Medline包
title = record['TI'] if 'TI' in record else 'nan' #标题
year = record['DP'].split(' ')[0] if 'DP' in record else 'nan'#年份
author = record['FAU'] if 'FAU' in record else 'nan'#作者
abtxt = record['AB'] if 'AB' in record else 'nan'#摘要

具体哪个缩写对应哪个信息,Medline源码里有注释
在这里插入图片描述

Entrez.elink()

很神奇的函数,上传两个参数db和id,返回查询结果(id)。
功能是:Similar articles,也就是返回和id文献相似的文献()。
功能等同下图。
在这里插入图片描述

from Bio import Entrez
Entrez.email = "qwe@example.com"
pmid = "19304878"
record = Entrez.read(Entrez.elink(dbfrom="pubmed", id=pmid))

Entrez.esummary()

从主id检索摘要
其实这个摘要,不是Abstract!!!
所以这个函数的功能esearch(),efetch()都能实现,显得没太大用。

参考

https://www.jianshu.com/p/30bff68b1dda
https://blog.csdn.net/sinat_32872729/article/details/103189573

https://www.jianshu.com/p/378678a19703

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值