
\begin{Schunk}
\begin{Sinput}
> require("cellHTS2")
> library("biomaRt")
\end{Sinput}
\end{Schunk}
%
By default, the \Rpackage{biomaRt} package will query the webservice at\newline 
http://www.ebi.ac.uk/biomart/martservice.  Let us check
which BioMart databases it covers:
%
\begin{Schunk}
\begin{Sinput}
> listMarts()
\end{Sinput}
\begin{Soutput}
                     biomart
1                    ensembl
2                        snp
3                       vega
4                        msd
5                       htgt
6                   QTL_MART
7       ENSEMBL_MART_ENSEMBL
8           ENSEMBL_MART_SNP
9          GRAMENE_MARKER_29
10            GRAMENE_MAP_29
11                  REACTOME
12          wormbase_current
13                     dicty
14                 rgd__mart
15             ipi_rat__mart
16                SSLP__mart
17                  g4public
18                     pride
19              uniprot_mart
20 ensembl_expressionmart_48
21                 biomartDB
22        Eurexpress Biomart
23      pepseekerGOLD_mart06
24     Pancreatic_Expression
                                                   version
1                             ENSEMBL 53 GENES (SANGER UK)
2                        ENSEMBL 53 VARIATION  (SANGER UK)
3                                     VEGA 34  (SANGER UK)
4                                   MSD PROTOTYPE (EBI UK)
5  HIGH THROUGHPUT GENE TARGETING AND TRAPPING (SANGER UK)
6                              GRAMENE 29 QTL DB (CSHL US)
7                               GRAMENE 29 GENES (CSHL US)
8                                GRAMENE 29 SNPs (CSHL US)
9                             GRAMENE 29 MARKERS (CSHL US)
10                           GRAMENE 29 MAPPINGS (CSHL US)
11                                      REACTOME (CSHL US)
12                                      WORMBASE (CSHL US)
13                             DICTYBASE (NORTHWESTERN US)
14                                      RGD GENES (MCW US)
15                                   RGD IPI MART (MCW US)
16                     RGD MICROSATELLITE MARKERS (MCW US)
17                                           HGNC (EBI UK)
18                                          PRIDE (EBI UK)
19                                        UNIPROT (EBI UK)
20                                      EURATMART (EBI UK)
21                         PARAMECIUM GENOME (CNRS FRANCE)
22                           EUREXPRESS (MRC EDINBURGH UK)
23                 PEPSEEKER (UNIVERSITY OF MANCHESTER UK)
24 PANCREATIC EXPRESSION DATABASE (INSTITUTE OF CANCER UK)
\end{Soutput}
\end{Schunk}
%
In this example, we use the Ensembl database~\cite{Ensembl2006}, from
which we select the \textit{D. melanogaster} dataset.
%
\begin{Schunk}
\begin{Sinput}
> mart <- useMart("ensembl", 
+                 dataset="dmelanogaster_gene_ensembl")
\end{Sinput}
\end{Schunk}
% 
We can query the available gene attributes and filters for the
selected dataset using the following functions.
\begin{Schunk}
\begin{Sinput}
> attrs <- listAttributes(mart)
> filts <- listFilters(mart)
\end{Sinput}
\end{Schunk}
%
In the BioMart system~\cite{Kasprzyk2004}, a \emph{filter} is a
property that can be used to select a gene or a set of genes (like the
``where'' clause in an SQL query), and an \emph{attribute} is a
property that can be queried (like the ``select'' clause in an SQL
query). We use the \Rfunction{getBM} function of the package
\Rpackage{biomaRt} to obtain the gene annotation from Ensembl.
%
\begin{Schunk}
\begin{Sinput}
> myGetBM <- function(att)
+     getBM(attributes=c("flybasecgid_gene", att), 
+           filter="flybasecgid_gene", 
+           values=unique(geneAnno(xsc)), mart=mart)
\end{Sinput}
\end{Schunk}
% 
For performance reasons, we split up our query in three subqueries,
which corresponds to different areas in the BioMart schema, and then
assemble the results together in R.  Alternatively, it would also be
possible to submit a single query for all of the attributes, but then
the result table will be enormous due to the 1:many mapping
especially from gene ID to GO categories~\cite{GO}.
%
\begin{Schunk}
\begin{Sinput}
> bm1 <- myGetBM(c("chromosome_name", "start_position", 
+                  "end_position", "description"))
> bm2 <- myGetBM(c("flybasename_gene", "flybase_gene_id"))
> bm3 = myGetBM(c("go_biological_process_id", 
+                 "go_biological_process_description"))
\end{Sinput}
\end{Schunk}
%
There are only a few CG-identifiers for which we were not able to
obtain chromosomal locations: 
%
\begin{Schunk}
\begin{Sinput}
> length(unique(setdiff(geneAnno(xsc), bm1$flybasecgid_gene)))
\end{Sinput}
\begin{Soutput}
[1] 389
\end{Soutput}
\end{Schunk}
%
Below, we add the results
to the dataframe \Robject{featureData} of \Robject{xsc}. 
Since the tables \Robject{bm1},
\Robject{bm2}, and \Robject{bm3} contain zero, one or several rows for
each gene ID, but in \Robject{featureData} we want exactly one row per
gene ID, the function \Rfunction{oneRowPerId} does the somewhat tedious
task of reformatting the tables: multiple entries are collapsed
into a single comma-separated string, and empty rows are inserted
where necessary.
%
\begin{Schunk}
\begin{Sinput}
> id <- geneAnno(xsc)
> bmAll <- cbind(
+    oneRowPerId(bm1, id),
+    oneRowPerId(bm2, id),
+    oneRowPerId(bm3, id)) 
> bdgpbiomart <- cbind(fData(xsc), bmAll)
> fData(xsc) <- bdgpbiomart
> fvarMetadata(xsc)[names(bmAll), "labelDescription"] <- 
+     sapply(names(bmAll), 
+            function(i) gsub("_", " ", i) )
\end{Sinput}
\end{Schunk}

%% This is how the object is then saved in the data subdirectory of the package:

