###################################################
### chunk number 1: bmCon
###################################################
library("biomaRt")
head(listMarts())


###################################################
### chunk number 2: choseMart
###################################################
mart = useMart("ensembl")


###################################################
### chunk number 3: bmDataset
###################################################
head(listDatasets(mart))


###################################################
### chunk number 4: bmChoseset
###################################################
ensembl = useDataset("hsapiens_gene_ensembl", 
    mart=mart)


###################################################
### chunk number 5: bmutrDo
###################################################
EGs = c("1001", "1002")
utr = getSequence(id=EGs, seqType="3utr", 
    mart=ensembl, type="entrezgene")
utr[1,]


###################################################
### chunk number 6: bmfilt
###################################################
head(listFilters(ensembl))


###################################################
### chunk number 7: bmatt
###################################################
head(listAttributes(ensembl))


###################################################
### chunk number 8: bmDom
###################################################
domains = getBM(attributes=c("entrezgene", "pfam", 
    "prosite", "interpro"), filters="entrezgene", 
    value=EGs, mart=ensembl) 
interpro = split(domains$interpro, domains$entrezgene)
interpro[1]


###################################################
### chunk number 9: startoverwithmouse
###################################################
listDatasets(ensembl)[1:10,]
##Then set up so that you use that for this session 
##(we will choose the mouse one from NCBI build 37.1):
ensembl = useDataset("mmusculus_gene_ensembl",mart=ensembl)

##List attributes
attributes = listAttributes(ensembl)
attributes[1:10,]

##And filters
filters = listFilters(ensembl)
filters[1:10,]

##Some entrez gene IDs
EGs = c("18392","18414","56513")

##1st a Simple example to just get some gene names:
getBM(attributes = "external_gene_id", 
      filters = "entrezgene", 
      values = EGs, 
      mart=ensembl)


###################################################
### chunk number 10: biomartDemoContinued
###################################################
##Transcript starts and ends:
getBM(attributes = c("entrezgene","transcript_start","transcript_end"), 
      filters = "entrezgene", 
      values = EGs, 
      mart=ensembl)


###################################################
### chunk number 11: biomartDemoContinued2
###################################################
##Additionally, you can get exon boundaries.
##But 1st you have to find out what the attributes are called...
pages = attributePages(ensembl)  

##Lets zoom in on the structure attributes
listAttributes(ensembl, page = "structure")


###################################################
### chunk number 12: biomartDemoContinued3
###################################################
##Find the exon starts and stops for "56513"
getBM(attributes = c("ensembl_exon_id","exon_chrom_start","exon_chrom_end"), 
      filters = "entrezgene", 
      values = "56513", 
      mart=ensembl)


###################################################
### chunk number 13: GO and BiomaRt Example
###################################################
library("GO.db") 
library("org.Mm.eg.db")
GOTERM[["GO:0016564"]]

##here is what we have for EGs affiliated with that term
GOEGs = unique(org.Mm.egGO2EG[["GO:0016564"]])
GOEGs


###################################################
### chunk number 14: getGeneLocs
###################################################
geneLocs <- getBM(c("ensembl_gene_id", "transcript_start", 
        "transcript_end", "chromosome_name"), "entrezgene", 
         GOEGs, mart=ensembl)


