Biomart

github.com/dazoakley/biomart

rdoc.info/projects/dazoakley/biomart

Biomart provides a simple interface for working with Biomart servers (see www.biomart.org for more info on Biomart itself), so you don’t have to get down and dirty with the basic webservice calls yourself.

Install

gem install biomart

Usage

Include the module in your code:

require "rubygems"
require "biomart"

To basically connect to a Biomart server and have access to all of its information and meta data:

biomart = Biomart::Server.new( "http://www.biomart.org/biomart" )

# List all of the available datasets on this Biomart server
# (be patient - this will take a while on this server as there's 
# a lot there...)
p biomart.list_datasets

# Grab the "kermits" dataset
kermits = biomart.datasets["kermits"]

# List it's filters and attributes
p kermits.list_filters
p kermits.list_attributes

# Count the total number of records in the dataset
p kermits.count()

# Do a count with a filter added
p kermits.count( :filters => { "sponsor" => "EUCOMM" } )

# Do a search using the default filters and attributes
# - this will return a hash with :headers (an array of the headers) 
#   and :data (an array of arrays of results)
#
# Doing a search like this is generally a BAD idea as biomarts tend 
# to hold a LOT of data... Unless you have time to kill...
p kermits.search()

# Do a search using some specific filters (but the default attributes)
p kermits.search( :filters => { "marker_symbol" => "Cbx1" } )

# Do a search with specific filters and attributes
p kermits.search( 
    :filters => { "marker_symbol" => "Cbx1" }, 
    :attributes => ["marker_symbol"] 
  )

# If you would like to retrieve a more useful results object - i.e. an 
# array of hashes, where each hash represents a row of results (keyed 
# by the attribute name), add the :process_results argument
p kermits.search( 
    :filters => { "marker_symbol" => "Cbx1" }, 
    :process_results => true 
  )

Or if you know the dataset you wish to work with and would like to just get on with things…

htgt_targ = Biomart::Dataset.new( 
  "http://www.sanger.ac.uk/htgt/biomart", 
  { :name => "htgt_targ" } 
)
p htgt_targ.count( :filters => { "is_eucomm" => "1" } )

# etc. etc.

See Biomart module and Class docs for more detail.

Advanced Filtering

Most filters in biomart are just pure textual filters, i.e.

kermits.search( :filters => { "marker_symbol" => "Cbx1" } )

To filter on more than one term, simply pass a comma concatenated string, or an array (the following two searches are equivalent):

kermits.search( :filters => { "marker_symbol" => "Cbx1,Mysm1" } )
kermits.search( :filters => { "marker_symbol" => ["Cbx1","Mysm1"] } )

Finally, there are also boolean filters (shown in the standard MartView interface as a filter name and then two radio buttons: ‘Only’ or ‘Excluded’). These filers can only accept the following:

  • ‘Only’: true / ‘only’ / ‘included’

  • ‘Excluded’: false / ‘excluded’

Here’s some example usage:

snp_mart = Biomart::Dataset.new( "http://www.ensembl.org/biomart", { :name => "hsapiens_snp" } )

# These are equivalent to selecting 'Only' in MartView
snp_mart.search( :filters => { 'with_variation_annotation' => true,       'ensembl_gene' => 'ENSG00000244734' } )
snp_mart.search( :filters => { 'with_variation_annotation' => 'only',     'ensembl_gene' => 'ENSG00000244734' } )
snp_mart.search( :filters => { 'with_variation_annotation' => 'included', 'ensembl_gene' => 'ENSG00000244734' } )

# These are equivalent to selecting 'Excluded' in MartView
snp_mart.search( :filters => { 'with_variation_annotation' => false,      'ensembl_gene' => 'ENSG00000244734' } )
snp_mart.search( :filters => { 'with_variation_annotation' => 'excluded', 'ensembl_gene' => 'ENSG00000244734' } )

Federated Searches

To perform a federated search across two datasets…

htgt = Biomart::Server.new( "http://www.sanger.ac.uk/htgt/biomart" )

res = htgt.datasets["htgt_targ"].search(
  :filters => {
    "status"     => [
      "Mice - Genotype confirmed",
      "Mice - Germline transmission",
      "Mice - Microinjection in progress",
      "ES Cells - Targeting Confirmed"
    ]
  },
  :attributes => [
    "marker_symbol",
    "mgi_accession_id",
    "status"
  ],
  :federate => [
    {
      :dataset => htgt.datasets["mmusculus_gene_ensembl"],
      :filters => {
        "chromosome_name" => "1",
        "start"           => "1",
        "end"             => "10000000"
      },
      :attributes => []
    }
  ]
)

The above will perform a federated query for all genes with available mice knockout es cells in the first 10Mb of chromosome 1 from the IKMC projects (not really important, but it’s an example of a complex query).

The basic search arguments are the same as if we were searching across a single dataset, with the addition of the :federate option, which is an array of hashes for each dataset (and additional filters/attributes) that we want to federate our search with.

Note: at present you can only federate across two datasets, this is limitation in the current stable release of biomart (0.7). If you try federate across more than two datasets, a Biomart::ArgumentError will be raised. This limitation shall be removed from this API when it is possible to federate across more than two datasets in biomart itself.

Count queries are only allowed on single datasets.

Required Attributes

As biomarts are generally big de-normalised data stores, a common task after receiving a set of results is to filter out rows that don’t have information for a given attribute or set of attributes. A facility for doing this has been built into the gem.

htgt = Biomart::Server.new( "http://www.sanger.ac.uk/htgt/biomart" )

res = htgt.datasets["mmusculus_gene_ensembl"].search(
  :filters => {
    "chromosome_name" => "1",
    "start"           => "1",
    "end"             => "10000000"
  },
  :attributes => [
    "ensembl_gene_id", "ensembl_transcript_id",
    "mouse_paralog_ensembl_gene", "mouse_paralog_chromosome"
  ],
  :required_attributes => ["mouse_paralog_ensembl_gene"]
)

The above will perform a basic search and then automatically remove any result row that does not have a value for the “mouse_paralog_ensembl_gene” attribute.

Note: You can specify more than one required attribute. If more than one required attribute is specified, ALL of these attributes must be present for a data row to be returned (it is using AND logic).

Using a Proxy

If you need to channel all of your requests via a proxy, specify your proxy via Biomart.proxy:

Biomart.proxy = "http://proxy.example.com/"

Now all requests made through Biomart will be proxied via proxy.example.com.

Alternatively you can also set your proxy url in the environment variable ‘http_proxy’, and Biomart will automatically detect this.

Setting a Timeout Value

If you need to alter the default request timeout value, you can alter this globally via Biomart.timeout:

Biomart.timeout = 60     # For a 60 second timeout...

Or on a per-request basis by passing the :timeout option when searching:

kermits.search( 
  :filters         => { "marker_symbol" => "Cbx1" }, 
  :process_results => true, 
  :timeout         => 60
)

Meta

Written by Darren Oakley (daz dot oakley at gmail dot com)

github.com/dazoakley/biomart

rdoc.info/projects/dazoakley/biomart

License

(The MIT License)

Copyright © 2009 Darren Oakley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.