这个包很久以前我就给它写过教程(点击阅读),但是排版不好,可读性很差,所以我用R Markdown重新来一个。 当然了,它本身有官方的英文版教程(点击阅读),我在翻译的基础上面,加入了自己的理解, 下面是正文:
biomaRt是一个超级网络资源库,里面的信息非常之多,就是网页版的biomaRt的R语言接口。谷歌搜索 the biomart user’s guide filetype:pdf 这个关键词,就看到关于这个包的详细介绍以及例子,我这里简单总结一下它的用法。
包的安装
Bioconductor系列包的安装方法都一样
source("http://bioconductor.org/biocLite.R")
biocLite(“biomaRt”)
install.packages('DT')
选择数据库
然后我们就可以加载这个包来看看它的一些性质啦,大家也可以根据这个包的说明书里面的代码一行行代码敲进去看看效果,这样学习最快也最容易记住。
library("biomaRt")
library(DT)
listMarts() #看看有多少数据库资源
## biomart version
## 1 ENSEMBL_MART_ENSEMBL Ensembl Genes 91
## 2 ENSEMBL_MART_MOUSE Mouse strains 91
## 3 ENSEMBL_MART_SNP Ensembl Variation 91
## 4 ENSEMBL_MART_FUNCGEN Ensembl Regulation 91
ensembl=useMart("ENSEMBL_MART_ENSEMBL")
all_datasets <- listDatasets(ensembl) #看看选择的数据库里面有多少数据表,这个跟物种相关
library(DT)
datatable(all_datasets, options = list(
searching = FALSE,
pageLength = 5,
lengthMenu = c(5, 10, 15, 20)
))
Show 5101520 entries
dataset | description | version | |
---|---|---|---|
1 | oaries_gene_ensembl | Sheep genes (Oar_v3.1) | Oar_v3.1 |
2 | mnemestrina_gene_ensembl | Pig-tailed macaque genes (Mnem_1.0) | Mnem_1.0 |
3 | mfascicularis_gene_ensembl | Crab-eating macaque genes (Macaca_fascicularis_5.0) | Macaca_fascicularis_5.0 |
4 | ngalili_gene_ensembl | Upper Galilee mountains blind mole rat genes (S.galili_v1.0) | S.galili_v1.0 |
5 | oprinceps_gene_ensembl | Pika genes (OchPri2.0-Ens) | OchPri2.0-Ens |
ensembl = useDataset("hsapiens_gene_ensembl",mart=ensembl)
# 这是一步法选择人类的ensembl数据库代码.
ensembl = useMart("ENSEMBL_MART_ENSEMBL",dataset="hsapiens_gene_ensembl")
三个主要函数getBM,getSequence,getLDS
我们选择好了数据库就要开始干活啦,这个数据库的检索主要是三个函数getBM,getSequence,getLDS, 其中getBM这个函数可以部分用select语句替代。
首先我们讲讲getBM函数,它就四个参数。
- filter来控制根据什么东西来过滤,可是不同数据库的ID,也可以是染色体定位系统坐标
- Attributes来控制我们想获得什么,一般是不同数据库的ID
- Values是我们用来检索的关键词向量
- Mart是我们前面选择好的数据库,一般都是ensembl = useMart(“ensembl”,dataset=“hsapiens_gene_ensembl”)
getBM函数唯一的用处,就是做各种ID转换。
可以查看filter和attribute有哪些东西。
filters = listFilters(ensembl)
#filters[1:5,]
library(DT)
datatable(filters, options = list(
searching = FALSE,
pageLength = 5,
lengthMenu = c(5, 10, 15, 20)
))
Show 5101520 entries
name | description | |
---|---|---|
1 | chromosome_name | Chromosome/scaffold name |
2 | start | Start |
3 | end | End |
4 | band_start | Band Start |
5 | band_end | Band End |
attributes = listAttributes(ensembl)
#attributes[1:5,]
datatable(attributes, options = list(
searching = FALSE,
pageLength = 5,
lengthMenu = c(5, 10, 15, 20)
))
Show 5101520 entries
name | description | page | |
---|---|---|---|
1 | ensembl_gene_id | Gene stable ID | feature_page |
2 | ensembl_gene_id_version | Gene stable ID version | feature_page |
3 | ensembl_transcript_id | Transcript stable ID | feature_page |
4 | ensembl_transcript_id_version | Transcript stable ID version | feature_page |
5 | ensembl_peptide_id | Protein stable ID | feature_page |
getSequence函数大同小异,建议大家活用help函数去查看每个函数的帮助文档。
简单讲几个例子咯: Ps:这些都是在线注释,所以都是要网络的,网速慢的会非常坑
几个实用的例子
一.对几个芯片探针的ID号,注释它所捕获的基因的entrezID
# ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")
affyids=c("202763_at","209310_s_at","207500_at")
getBM(attributes=c('affy_hg_u133_plus_2', 'entrezgene'),
filters = 'affy_hg_u133_plus_2',
values = affyids,
mart = ensembl
)
可以看到结果里面已经成功的把affymetrix的芯片探针ID,转为了对应的基因的entrez ID
二.对刚才的那三个探针ID号进行多个内容注释,每个探针都对应着基因名已经染色体及起始终止坐标。
affyids=c("202763_at","209310_s_at","207500_at")
getBM(attributes=c('affy_hg_u133_plus_2', 'hgnc_symbol',
'chromosome_name','start_position','end_position', 'band'),
filters = 'affy_hg_u133_plus_2',
values = affyids,
mart = ensembl
)
三.对给定的基因ID号进行GO注释
# library("biomaRt")
# ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")
entrez=c("673","837")
goids = getBM(attributes = c('entrezgene', 'go_id'),
filters = 'entrezgene',
values = entrez,
mart = ensembl)
head(goids)
四.通过染色体及起始终止坐标来挑选基因
getBM(c('affy_hg_u133_plus_2','ensembl_gene_id'),
filters = c('chromosome_name','start','end'),
values=list(16,1100000,1250000),
mart=ensembl
)
五.对特定的GO ID号来查询该go通路上面的基因是哪些。
getBM(c('entrezgene','hgnc_symbol'), filters='go', values='GO:0004707', mart=ensembl)
六.根据refseq数据库的NM系列ID号来获取信息
refseqids = c("NM_005359","NM_000546")
ipro = getBM(attributes=c("refseq_dna","interpro","interpro_description"), filters="refseq_dna",values=refseqids, mart=ensembl)
这个例子的代码有错误,因为refseq的信息没有 refseq_dna Error in getBM(attributes = c(“refseq_dna”, “interpro”, “interpro_description”), : Invalid attribute(s): refseq_dna Please use the function ‘listAttributes’ to get valid attribute names 我简单检查了一下,发现需要更正为 refseq_mrna
tmp=listAttributes(ensembl)
grep("refseq",tmp[,1])
## [1] 82 83 84 85 86 87
# [1] 81 82 83 84 85 86
tmp[grep("refseq",tmp[,1]),]
## name description page
## 82 refseq_mrna RefSeq mRNA ID feature_page
## 83 refseq_mrna_predicted RefSeq mRNA predicted ID feature_page
## 84 refseq_ncrna RefSeq ncRNA ID feature_page
## 85 refseq_ncrna_predicted RefSeq ncRNA predicted ID feature_page
## 86 refseq_peptide RefSeq peptide ID feature_page
## 87 refseq_peptide_predicted RefSeq peptide predicted ID feature_page
很明显,是没有 refseq_dna 的,只有 refseq_mrna
然后我用了新的代码
refseqids = c("NM_005359","NM_000546")
ipro = getBM(attributes=c("refseq_mrna","interpro","interpro_description"), filters="refseq_mrna",values=refseqids, mart=ensembl)
ipro
## refseq_mrna interpro
## 1 NM_000546 IPR002117
## 2 NM_000546 IPR008967
## 3 NM_000546 IPR010991
## 4 NM_000546 IPR011615
## 5 NM_000546 IPR012346
## 6 NM_000546 IPR013872
## 7 NM_005359 IPR001132
## 8 NM_005359 IPR003619
## 9 NM_005359 IPR008984
## 10 NM_005359 IPR013019
## 11 NM_005359 IPR013790
## 12 NM_005359 IPR017855
## interpro_description
## 1 p53 tumour suppressor family
## 2 p53-like transcription factor, DNA-binding
## 3 p53, tetramerisation domain
## 4 p53, DNA-binding domain
## 5 p53/RUNT-type transcription factor, DNA-binding domain
## 6 p53 transactivation domain
## 7 SMAD domain, Dwarfin-type
## 8 MAD homology 1, Dwarfin-type
## 9 SMAD/FHA domain
## 10 MAD homology, MH1
## 11 Dwarfin
## 12 SMAD domain-like
就成功的完成了转换
七.根据基因的entrez ID号来挑选该基因的指定上下游区域信息或者蛋白序列
entrez=c("673","7157","837")
getSequence(id = entrez, type="entrezgene",seqType="coding_gene_flank",upstream=100, mart=ensembl)
## coding_gene_flank
## 1 CCTCCGCCTCCGCCTCCGCCTCCGCCTCCCCCAGCTCTCCGCCTCCCTTCCCCCTCCCCGCCCGACAGCGGCCGCTCGGGCCCCGGCTCTCGGTTATAAG
## 2 CACGTTTCCGCCCTTTGCAATAAGGAAATACATAGTTTACTTTCATTTTTGACTCTGAGGCTCTTTCCAACGCTGTAAAAAAGGACAGAGGCTGTTCCCT
## 3 TCCTTCTCTGCAGGCCCAGGTGACCCAGGGTTGGAAGTGTCTCATGCTGGATCCCCACTTTTCCTCTTGCAGCAGCCAGACTGCCTTCCGGGTCACTGCC
## entrezgene
## 1 673
## 2 837
## 3 7157
这样就得到了三条序列,是给定基因的上游100bp序列 coding_gene_flank entrezgene
其实这个getSequence函数还有非常多的用法,当然主要的变化在其readme上面可以看到,主要是seqType可以有多个选择。
utr5 = getSequence(chromosome=3, start=185514033, end=185535839,
type="entrezgene",seqType="5utr", mart=ensembl)
utr5
## 5utr
## 1 Sequence unavailable
## 2 AGTCCCTAGGGAACTTCCTGTTGTCACCACACCTCTGAGTCGTCTGAGCTCACTGTGAGCAAAATCCCACAGTGGAAACTCTTAAGCCTCTGCGAAGTAAATCATTCTTGTGAATGTGACACACGATCTCTCCAGTTTCCAT
## 3 TGAGCAAAATCCCACAGTGGAAACTCTTAAGCCTCTGCGAAGTAAATCATTCTTGTGAATGTGACACACGATCTCTCCAGTTTCCAT
## 4 ATTCTTGTGAATGTGACACACGATCTCTCCAGTTTCCAT
## entrezgene
## 1 200879
## 2 200879
## 3 200879
## 4 200879
#这样就查询到了 chromosome=3, start=185514033, end=185535839,这个定位里面的所有utr5信息。
# 返回的urt5这个对象是一个data.frame,可以用exportFASTA()函数导出成fasta文件。
protein = getSequence(id=c(100, 5728),type="entrezgene",
seqType="peptide", mart=ensembl)
protein
## peptide
## 1 MAQTPAFDKPKVELHVHLDGSIKPETILYYGRRRGIALPANTAEGLLNVIGMDKPLTLPDFLAKFDYYMPAIARL*
## 2 Sequence unavailable
## 3 ALLFHKMMFETIPMFSGGTCNPQFVVCQLKVKIYSSNSGPTRREDKFMYFEFPQPLPVCGDIKVEFFHKQNKMLKKDKMFHFWVNTFFIPGPEETSEKVENGSLCDQEIDSICSIERADNDKEYLVLTLTKNDLDKANKDKANRYFSPNFKVS*
## 4 MAQTPAFDKPKVELHVHLDGSIKPETILYYGRRRGIALPANTAEGLLNVIGMDKPLTLPDFLAKFDYYMPAIAGCREAIKRIAYEFVEMKAKEGVVYVEVRYSPHLLANSKVEPIPWNQAEGDLTPDEVVALVGQGLQEGERDFGVKARSILCCMRHQPNWSPKVVELCKKYQQQTVVAIDLAGDETIPGSSLLPGHVQAYQAVDILKTERLGHGYHTLEDQALYNRLRQENMHFEICPWSSYLTGAWKPDTEHAVIRLKNDQANYSLNTDDPLIF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TSVTPDVSDNEPDHYRYSDTTDSDPENEPFDEDQHTQITKV*
## entrezgene
## 1 100
## 2 100
## 3 5728
## 4 100
## 5 100
## 6 5728
## 7 100
## 8 5728
#这样就查到了这两个基因转录本的所有蛋白序列,100这个基因有5条序列,5728这个基因有四条序列。
getSequence函数返回的对象是一个data.frame,可以用exportFASTA()函数导出成fasta文件。
八,选择其它数据库来进行查询,比如snp数据库
当然还有一些数据库的小技巧,第一个是参数 archive = TRUE,设置只用能获取的数据库
然后是设置特定选取hg19对应的信息。
ensembl = useMart("ensembl_mart_46", dataset="hsapiens_gene_ensembl", archive = TRUE)
listMarts(host='may2009.archive.ensembl.org')
ensembl54=useMart(host='may2009.archive.ensembl.org', biomart='ENSEMBL_MART_ENSEMBL')
ensembl54=useMart(host='may2009.archive.ensembl.org', biomart='ENSEMBL_MART_ENSEMBL', dataset='hsapiens_gene_ensembl')
还可以选取其它物种,比如秀丽隐杆线虫
下面这个代码是过时了的,你们需要自己去看说明书找到现在最新的代码
wormbase=useMart("WS220",dataset="wormbase_gene")
listFilters(wormbase)
listAttributes(wormbase)
getBM(attributes = c("public_name","rnai","rnai_phenotype_phenotype_label"),
filters="gene_name", values=c("unc-26","his-33"),
mart=wormbase)
写在最后
最后我简单提一下select函数是如何部分替代getBM函数的,因为biomaRt是在线数据库,本来只能用它自己的getBM系列函数,但是为了对接其它bioconductor系列包,也可以用select函数来操作这个在线数据库。
mart<-useMart("ENSEMBL_MART_ENSEMBL",dataset="hsapiens_gene_ensembl")
head(keytypes(mart), n=3)
## [1] "affy_hc_g110" "affy_hg_focus" "affy_hg_u133_plus_2"
length(keytypes(mart))
## [1] 334
length(columns(mart))
## [1] 1918
head(columns(mart), n=3)
## [1] "3_utr_end" "3_utr_end" "3_utr_start"
k = keys(mart, keytype="chromosome_name")
head(k, n=3)
## [1] "1" "2" "3"
k = keys(mart, keytype="chromosome_name", pattern="LRG")
head(k, n=3)
## character(0)
affy=c("202763_at","209310_s_at","207500_at")
select(mart, keys=affy, columns=c('affy_hg_u133_plus_2','entrezgene'), keytype='affy_hg_u133_plus_2')
## affy_hg_u133_plus_2 entrezgene
## 1 202763_at 836
## 2 209310_s_at 837
## 3 207500_at 838
而且这个包更新也比较频繁,所以大家如果看到比较老旧的教程,可能无法模仿,或者多年以后,你看到我这个教程,也会发现,重复不出来咯。
sessionInfo()
## R version 3.4.2 (2017-09-28)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] DT_0.2 biomaRt_2.34.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.14 AnnotationDbi_1.40.0 knitr_1.18
## [4] magrittr_1.5 progress_1.1.2 IRanges_2.12.0
## [7] BiocGenerics_0.24.0 bit_1.1-12 R6_2.2.2
## [10] rlang_0.1.2 stringr_1.2.0 blob_1.1.0
## [13] tools_3.4.2 parallel_3.4.2 Biobase_2.38.0
## [16] DBI_0.7 htmltools_0.3.6 assertthat_0.2.0
## [19] yaml_2.1.16 bit64_0.9-7 rprojroot_1.2
## [22] digest_0.6.13 tibble_1.3.4 htmlwidgets_0.9
## [25] S4Vectors_0.16.0 bitops_1.0-6 RCurl_1.95-4.8
## [28] memoise_1.1.0 evaluate_0.10.1 RSQLite_2.0
## [31] rmarkdown_1.6 stringi_1.1.5 compiler_3.4.2
## [34] prettyunits_1.0.2 backports_1.1.1 stats4_3.4.2
## [37] XML_3.98-1.9 jsonlite_1.5