rentrez 是 E-utilities 的 R 包封装,用它可以在 R 语言代码简便地使用 E-utilities 省去创建 E-utilities 链接的麻烦。
NCBI 默认有访问限制,未设置 api key 时限制 3 次每秒,设置后限制 10 次每秒。如果有,通过 set_entrez_key
函数设置。
set_entrez_key("xxxxxx")
本质是设置环境变量 "ENTREZ_KEY" 直接设置环境变量也可以。
> set_entrez_key
function (key)
{
Sys.setenv(ENTREZ_KEY = key)
}
<bytecode: 0x5650672a7630>
<environment: namespace:rentrez>
列举可用的数据库 entrez_dbs
函数,常用的数据库大多都支持。
> entrez_dbs()
[1] "pubmed" "protein" "nuccore" "ipg"
[5] "nucleotide" "structure" "genome" "annotinfo"
[9] "assembly" "bioproject" "biosample" "blastdbinfo"
[13] "books" "cdd" "clinvar" "gap"
[17] "gapplus" "grasp" "dbvar" "gene"
[21] "gds" "geoprofiles" "homologene" "medgen"
[25] "mesh" "nlmcatalog" "omim" "orgtrack"
[29] "pmc" "popset" "proteinclusters" "pcassay"
[33] "protfam" "pccompound" "pcsubstance" "seqannot"
[37] "snp" "sra" "taxonomy" "biocollections"
[41] "gtr"
查看某数据库总结 entrez_db_summary
函数。
> entrez_db_summary("nucleotide")
DbName: nuccore
MenuName: Nucleotide
Description: Core Nucleotide db
DbBuild: Build221030-1815m.1
Count: 508239779
LastUpdate: 2022/11/01 21:31
查看某数据库支持的搜索字段(fields),可根据支持的字段定义搜索条件进行搜索。
> entrez_db_searchable("nucleotide")
Searchable fields for database 'nuccore'
ALL All terms from all searchable fields
UID Unique number assigned to each sequence
FILT Limits the records
WORD Free text associated with record
TITL Words in definition line
KYWD Nonstandardized terms provided by submitter
AUTH Author(s) of publication
JOUR Journal abbreviation of publication
VOL Volume number of publication
ISS Issue number of publication
... # 剩下省略
esearch 搜索用 entrez_search
函数,字段用 []
包含。
> es <- entrez_search(db = "nucleotide", term = "Human adenovirus 5[ORGN]", retmax = 5)
> es
Entrez search result with 681 hits (object contains 5 IDs and no web_history object)
Search term (as translated): "Human adenovirus 5"[Organism]
> es$ids
[1] "2295556242" "2295556240" "2295556238" "2287989107" "2260395970"
函数 entrez_link
获取不同数据库的链接。比如从物种链接到核酸序列,可以获取一个物种对应核酸序列 id 列表,后续用于获取序列信息或下载序列等。
> el <- entrez_link(dbfrom = "taxonomy", id = 28285, db = "nucleotide")
> el
elink object with contents:
$links: IDs for linked records from NCBI
> el$links
elink result with information from 2 databases:
[1] taxonomy_nuccore taxonomy_nucleotide_exp
> el$links$taxonomy_nuccore[1:5]
[1] "9652377" "9652366" "296210" "2295556242" "2295556240"
函数 entrez_summary
取得条目总结信息,设置 always_return_list = TRUE
让函数就算一个请求也返回列表,防止在 apply
或循环时出现非预期行为。
> es <- entrez_summary(db = "nucleotide", id = "2295556242")
> es
esummary result with 32 items:
[1] uid term caption title extra
[6] gi createdate updatedate flags taxid
[11] slen biomol moltype topology sourcedb
[16] segsetsize projectid genome subtype subname
[21] assemblygi assemblyacc tech completeness geneticcode
[26] strand organism strain biosample statistics
[31] properties oslt
> es$organism
[1] "Human adenovirus 5"
函数 extract_from_esummary
从返回的总结对象提取需要信息。
> head(extract_from_esummary(es, "biomol"))
1884989923 1884989922 1884989921 1884989920 1884989919 1041463157
"genomic" "genomic" "genomic" "genomic" "genomic" "genomic"
函数 entrez_fetch
获取条目完整信息,常用于下载数据,如选择 nucleotide 数据库并返回类型为 fasta 时可以下载对应 fasta 信息,返回为字符串写入到文件,即完成了核酸序列 fasta 文件下载。
> el <- entrez_link(dbfrom = "taxonomy", id = "28285", db = "nucleotide")
> nu5 <- el$links$taxonomy_nuccore[1:5]
> nu5
[1] "9652377" "9652366" "296210" "2295556242" "2295556240"
> ef <- entrez_fetch(db = "nucleotide", id = nu5, rettype = "fasta")
> temp <- tempfile()
> write(ef, temp)
web_history
NCBI 允许将搜索结果保存在服务器,然后调用结果,避免了下载结果、解析、上传解析结果的过程,也能避免产生过多的请求数目导致受限。如 entrez_link
设置参数 cmd = "neighbor_history"
将返回 web_history 对象。
以下载某物种核酸序列为例,默认的 entrez_link
返回对象需要自己取得核酸序列 id 列表(如前面的例子),然后用这些核酸序列输入到 entrez_fetch
从 NCBI 一条条下载核酸序列;如果用 web_history
因为对象保存在 NCBI 服务器,可以直接根据 web_history
下载物种的核酸序列,如下面的代码示例。
> el <- entrez_link(dbfrom = "taxonomy", id = "208893", db = "nucleotide", cmd = "neighbor_history")
> el
elink object with contents:
$web_histories: Objects containing web history information
> el$web_histories
$taxonomy_nuccore
Web history object (QueryKey = 1, WebEnv = MCID_637d8b3...)
$taxonomy_nucleotide_exp
Web history object (QueryKey = 2, WebEnv = MCID_637d8b3...)
# 下载所有的该物种核酸序列
> ef <- entrez_fetch(db = "nucleotide", web_history = el$web_histories$taxonomy_nuccore, rettype = "fasta")
> str(ef)
chr ">LC732340.1 Human respiratory syncytial virus A Fukui_S_258_2018 gene for attachment glycoprotein, partial cds\"| __truncated__
网友评论