Skip to content
Browse files

Whitespace cleanup

  • Loading branch information...
1 parent bd9aa03 commit f9ca04a1bad11b92b2098823e7f679b2f20f0116 @dazoakley committed Jan 27, 2012
View
2 biomart.gemspec
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
-
+
s.add_runtime_dependency "builder", [">= 3.0"]
s.add_development_dependency "rake"
s.add_development_dependency "shoulda", [">= 2.10"]
View
72 lib/biomart.rb
@@ -8,42 +8,42 @@
require "builder"
module Biomart
- # This is the base Biomart error/exception class. Rescue it if
+ # This is the base Biomart error/exception class. Rescue it if
# you want to catch any exceptions that this code might raise.
class BiomartError < StandardError
attr_reader :data
-
+
def initialize(data)
@data = data
super
end
end
-
+
# Error class representing HTTP errors.
class HTTPError < BiomartError; end
-
- # Error class representing biomart filter errors. Usually raised
- # when a request is made for a incorrectly named (or non-existent)
+
+ # Error class representing biomart filter errors. Usually raised
+ # when a request is made for a incorrectly named (or non-existent)
# filter.
class FilterError < BiomartError; end
-
- # Error class representing biomart attribute errors. Usually raised
- # when a request is made for a incorrectly named (or non-existent)
+
+ # Error class representing biomart attribute errors. Usually raised
+ # when a request is made for a incorrectly named (or non-existent)
# attribute.
class AttributeError < BiomartError; end
-
- # Error class representing biomart dataset errors. Usually raised
- # when a request is made for a incorrectly named (or non-existent)
+
+ # Error class representing biomart dataset errors. Usually raised
+ # when a request is made for a incorrectly named (or non-existent)
# dataset.
class DatasetError < BiomartError; end
-
- # Error class representing errors in the arguments being passed
+
+ # Error class representing errors in the arguments being passed
# to the api.
class ArgumentError < BiomartError; end
-
- # Centralised request function for handling all of the HTTP requests
+
+ # Centralised request function for handling all of the HTTP requests
# to the biomart servers.
- #
+ #
# @param [Hash] params Parameters to be passed to the request
# @return [String] The response body
#
@@ -63,50 +63,50 @@ class ArgumentError < BiomartError; end
# @raise Biomart::BiomartError Raised for any other unhandled error
def request( params )
raise ArgumentError if !params.is_a?(Hash) || params.empty?
-
+
if params[:url] =~ / /
params[:url].gsub!(" ","+")
end
-
+
uri = URI.parse( params[:url] )
client = net_http_client()
req = nil
response = nil
-
+
case params[:method]
when 'post'
req = Net::HTTP::Post.new(uri.path)
req.form_data = { "query" => params[:query] }
else
req = Net::HTTP::Get.new(uri.request_uri)
end
-
+
client.start(uri.host, uri.port) do |http|
if Biomart.timeout or params[:timeout]
http.read_timeout = params[:timeout] ? params[:timeout] : Biomart.timeout
http.open_timeout = params[:timeout] ? params[:timeout] : Biomart.timeout
end
response = http.request(req)
end
-
+
response_code = response.code
response_body = response.body
-
+
if defined? Encoding && response_body.encoding == Encoding::ASCII_8BIT
response_body = response_body.force_encoding(Encoding::UTF_8).encode
end
-
+
check_response( response_body, response_code )
-
+
return response_body
end
-
+
class << self
attr_accessor :proxy, :timeout
end
-
+
private
-
+
# Utility function to create a Net::HTTP object.
def net_http_client
client = Net::HTTP
@@ -116,28 +116,28 @@ def net_http_client
end
return client
end
-
- # Utility function to determine if we need to use a proxy. If yes,
+
+ # Utility function to determine if we need to use a proxy. If yes,
# returns the proxy url, if no, returns false.
def proxy_url
if Biomart.proxy or ENV['http_proxy'] or ENV['HTTP_PROXY']
proxy_uri = Biomart.proxy
proxy_uri ||= ENV['http_proxy']
proxy_uri ||= ENV['HTTP_PROXY']
-
+
return proxy_uri
else
return false
end
end
-
- # Utility function to test the response from a http request.
+
+ # Utility function to test the response from a http request.
# Raises errors if appropriate.
def check_response( body, code )
# Process the response code/body to catch errors.
if code.is_a?(String) then code = code.to_i end
-
- if code != 200
+
+ if code != 200
raise HTTPError.new(code), "HTTP error #{code}, please check your biomart server and URL settings."
else
if body =~ /ERROR/
@@ -153,7 +153,7 @@ def check_response( body, code )
end
end
end
-
+
end
directory = File.expand_path(File.dirname(__FILE__))
View
14 lib/biomart/attribute.rb
@@ -1,30 +1,30 @@
module Biomart
- # Class representation for a biomart attribute.
+ # Class representation for a biomart attribute.
# Will belong to a Biomart::Dataset.
class Attribute
attr_reader :name, :display_name
-
+
def initialize(args)
@name = args["internalName"]
@display_name = args["displayName"]
@default = args["default"] ? true : false
@hidden = args["hideDisplay"] ? true : false
end
-
- # Convenience method to see if this attribute is hidden from
+
+ # Convenience method to see if this attribute is hidden from
# the standard MartView interface.
#
# @return [Boolean] true/false
def hidden?
@hidden
end
-
- # Convenience method to see if this attribute would be
+
+ # Convenience method to see if this attribute would be
# enabled by default in the standard MartView interface.
#
# @return [Boolean] true/false
def default?
@default
end
end
-end
+end
View
38 lib/biomart/database.rb
@@ -3,68 +3,68 @@ module Biomart
# Will contain many Biomart::Dataset objects, and belong to a Biomart::Server.
class Database
include Biomart
-
+
attr_reader :name, :display_name, :visible
-
+
def initialize( url, args )
@url = url or raise ArgumentError, "must pass :url"
unless @url =~ /martservice/
@url = @url + "/martservice"
end
-
+
@name = args["name"] || args[:name]
@display_name = args["displayName"] || args[:display_name]
@visible = ( args["visible"] || args[:visible] ) ? true : false
@datasets = {}
end
-
- # Returns an array of the dataset names (biomart 'name')
+
+ # Returns an array of the dataset names (biomart 'name')
# for this dataset.
- #
+ #
# @return [Array] An array of dataset names
def list_datasets
if @datasets.empty?
fetch_datasets
end
return @datasets.keys
end
-
- # Returns a hash (keyed by the biomart 'name' for the dataset)
+
+ # Returns a hash (keyed by the biomart 'name' for the dataset)
# of all of the Biomart::Dataset objects belonging to this server.
- #
+ #
# @return [Hash] A hash of Biomart::Dataset objects keyed by 'name'
def datasets
if @datasets.empty?
fetch_datasets
end
return @datasets
end
-
- # Returns true / false if this database is visbile in the
+
+ # Returns true / false if this database is visbile in the
# default MartView interface.
- #
+ #
# @return [Boolean] true/false
def visible?
@visible
end
-
+
private
-
- # Utility method to do the webservice call to the biomart server
+
+ # Utility method to do the webservice call to the biomart server
# and collate/build the information about the datasets.
def fetch_datasets
url = @url + "?type=datasets&mart=#{@name}"
document = request( :url => url )
tsv_data = []
-
+
if CSV.const_defined? :Reader
# Ruby < 1.9 CSV code
tsv_data = CSV.parse( document, "\t" )
else
# Ruby >= 1.9 CSV code
tsv_data = CSV.parse( document, { :col_sep => "\t" } )
end
-
+
tsv_data.each do |t|
if t[1] and ( t[3] === "1" )
dataset_attr = {
@@ -76,6 +76,6 @@ def fetch_datasets
end
end
end
-
+
end
-end
+end
View
148 lib/biomart/dataset.rb
@@ -3,9 +3,9 @@ module Biomart
# Can belong to a Biomart::Database and a Biomart::Server.
class Dataset
include Biomart
-
+
attr_reader :name, :display_name, :visible
-
+
# Creates a new Biomart::Dataset object.
#
# @param [String] url The URL location of the biomart server.
@@ -17,106 +17,106 @@ class Dataset
# :name => String, #
# "name" => String, #
# :display_name => {} #
- #
+ #
# }
def initialize( url, args )
@url = url or raise ArgumentError, "must pass :url"
unless @url =~ /martservice/
@url = @url + "/martservice"
end
-
+
@name = args["name"] || args[:name]
@display_name = args["displayName"] || args[:display_name]
@visible = ( args["visible"] || args[:visible] ) ? true : false
-
+
@filters = {}
@attributes = {}
@importables = {}
@exportables = {}
end
-
- # Returns a hash (keyed by the biomart 'internal_name' for the filter)
+
+ # Returns a hash (keyed by the biomart 'internal_name' for the filter)
# of all of the Biomart::Filter objects belonging to this dataset.
- #
+ #
# @return [Hash] A hash of Biomart::Filter objects keyed by 'internal_name'
def filters
if @filters.empty?
fetch_configuration()
end
return @filters
end
-
- # Returns an array of the filter names (biomart 'internal_name')
+
+ # Returns an array of the filter names (biomart 'internal_name')
# for this dataset.
- #
+ #
# @return [Array] An array of filters (their 'internal_name's)
def list_filters
if @filters.empty?
fetch_configuration()
end
return @filters.keys
end
-
- # Returns a hash (keyed by the biomart 'internal_name' for the attribute)
+
+ # Returns a hash (keyed by the biomart 'internal_name' for the attribute)
# of all of the Biomart::Attribute objects belonging to this dataset.
- #
+ #
# @return [Hash] A hash of Biomart::Attribute objects keyed by 'internal_name'
def attributes
if @attributes.empty?
fetch_configuration()
end
return @attributes
end
-
- # Returns an array of the attribute names (biomart 'internal_name')
+
+ # Returns an array of the attribute names (biomart 'internal_name')
# for this dataset.
- #
+ #
# @return [Array] An array of attributes (their 'internal_name's)
def list_attributes
if @attributes.empty?
fetch_configuration()
end
return @attributes.keys
end
-
- # Function to perform a Biomart count. Returns an integer value for
+
+ # Function to perform a Biomart count. Returns an integer value for
# the result of the count query.
- #
+ #
# arguments:
- #
+ #
# {
# :timeout => integer, # set a timeout length for the request (secs) - optional
# :filters => {} # hash of key-value pairs (filter => search term) - optional
# }
- #
+ #
# @param [Hash] args The arguments hash
# @raise Biomart::ArgumentError Raised when un-supported arguments are passed
def count( args={} )
if args[:federate]
raise Biomart::ArgumentError, "You cannot federate a count query."
end
-
+
if args[:required_attributes]
raise Biomart::ArgumentError, "The :required_attributes option is not allowed on count queries."
end
-
+
result = request(
:method => 'post',
:url => @url,
:timeout => args[:timeout],
:query => generate_xml(
- :filters => args[:filters],
- :attributes => args[:attributes],
+ :filters => args[:filters],
+ :attributes => args[:attributes],
:count => "1"
)
)
return result.to_i
end
-
+
# Function to perform a Biomart search.
- #
+ #
# optional arguments:
- #
+ #
# {
# :process_results => true/false, # convert search results to object
# :timeout => integer, # set a timeout length for the request (secs)
@@ -132,86 +132,86 @@ def count( args={} )
# ]
# }
#
- # Note, if you do not pass any filters or attributes arguments, the defaults
+ # Note, if you do not pass any filters or attributes arguments, the defaults
# for the dataset shall be used.
#
- # Also, using the :required_attributes option - this performs AND logic and will require
+ # Also, using the :required_attributes option - this performs AND logic and will require
# data to be returned in all of the listed attributes in order for it to be returned.
#
# By default will return a hash with the following:
- #
+ #
# {
# :headers => [], # array of headers
# :data => [] # array of arrays containing search results
# }
#
- # But with the :process_results option will return an array of hashes,
+ # But with the :process_results option will return an array of hashes,
# where each hash represents a row of results (keyed by the attribute name).
- #
+ #
# @param [Hash] args The arguments hash
# @return [Hash/Array] Will return a hash by default (of unprocessed data), or will return an array of hashes
# @raise Biomart::ArgumentError Raised if incorrect arguments are passed
def search( args={} )
if args[:required_attributes] and !args[:required_attributes].is_a?(Array)
raise Biomart::ArgumentError, "The :required_attributes option must be passed as an array."
end
-
+
response = request(
:method => 'post',
:url => @url,
:timeout => args[:timeout],
:query => generate_xml( process_xml_args(args) )
)
-
+
result = process_tsv( args, response )
result = filter_data_rows( args, result ) if args[:required_attributes]
result = conv_results_to_a_of_h( result ) if args[:process_results]
return result
end
-
+
# Utility function to build the Biomart query XML - used by #count and #search.
#
# @see #count
# @see #search
def generate_xml( args={} )
biomart_xml = ""
xml = Builder::XmlMarkup.new( :target => biomart_xml, :indent => 2 )
-
+
xml.instruct!
xml.declare!( :DOCTYPE, :Query )
xml.Query( :virtualSchemaName => "default", :formatter => "TSV", :header => "0", :uniqueRows => "1", :count => args[:count], :datasetConfigVersion => "0.6" ) {
dataset_xml( xml, self, { :filters => args[:filters], :attributes => args[:attributes] } )
-
+
if args[:federate]
args[:federate].each do |joined_dataset|
unless joined_dataset[:dataset].is_a?(Biomart::Dataset)
raise Biomart::ArgumentError, "You must pass a Biomart::Dataset object to the :federate[:dataset] option."
end
-
+
dataset_xml(
xml,
joined_dataset[:dataset],
{ :filters => joined_dataset[:filters], :attributes => joined_dataset[:attributes] }
)
end
end
-
+
}
-
+
return biomart_xml
end
-
+
# Simple heartbeat function to test that a Biomart server is online.
#
# @return [Boolean] true/false
def alive?
server = Biomart::Server.new( @url )
return server.alive?
end
-
+
private
-
- # Utility function to retrieve and process the configuration
+
+ # Utility function to retrieve and process the configuration
# xml for a dataset
def fetch_configuration
url = @url + "?type=configuration&dataset=#{@name}"
@@ -230,14 +230,14 @@ def fetch_configuration
end
end
end
-
+
# Attributes are much simpler...
REXML::XPath.each( document, '//AttributeDescription' ) do |a|
@attributes[ a.attributes["internalName"] ] = Attribute.new( a.attributes )
end
end
-
- # Utility function to process and test the arguments passed for
+
+ # Utility function to process and test the arguments passed for
# the xml query.
def process_xml_args( args={} )
xml_args = {
@@ -259,16 +259,16 @@ def process_xml_args( args={} )
return xml_args
end
-
- # Helper function to produce the portion of the biomart xml for
+
+ # Helper function to produce the portion of the biomart xml for
# a dataset query.
def dataset_xml( xml, dataset, args )
xml.Dataset( :name => dataset.name, :interface => "default" ) {
if args[:filters]
args[:filters].each do |name,value|
raise Biomart::ArgumentError, "The filter '#{name}' does not exist" if dataset.filters[name].nil?
-
+
if dataset.filters[name].type == 'boolean'
value = value.downcase if value.is_a? String
if [true,'included','only'].include?(value)
@@ -311,13 +311,13 @@ def dataset_xml( xml, dataset, args )
}
end
-
- # Utility function to transform the tab-separated data retrieved
+
+ # Utility function to transform the tab-separated data retrieved
# from the Biomart search query into a ruby object.
def process_tsv( args, tsv )
headers = []
parsed_data = []
-
+
append_header_attributes_for_tsv( headers, self, args[:attributes] )
if args[:federate]
@@ -342,14 +342,14 @@ def process_tsv( args, tsv )
parsed_data = parse_tsv_line_by_line( headers.size, tsv )
end
end
-
+
return {
:headers => headers,
:data => parsed_data
}
end
-
- # Helper function to append the attribute names to the 'headers' array
+
+ # Helper function to append the attribute names to the 'headers' array
# for processing the returned results.
def append_header_attributes_for_tsv( headers, dataset, attributes )
if attributes
@@ -364,18 +364,18 @@ def append_header_attributes_for_tsv( headers, dataset, attributes )
end
end
end
-
- # Utility function to process TSV formatted data that raises errors. (Biomart
- # has a habit of serving out this...) First attempts to use the CSV modules
- # 'parse_line' function to read in the data, if that fails, tries to use split
+
+ # Utility function to process TSV formatted data that raises errors. (Biomart
+ # has a habit of serving out this...) First attempts to use the CSV modules
+ # 'parse_line' function to read in the data, if that fails, tries to use split
# to recover the data.
def parse_tsv_line_by_line( expected_row_size, tsv )
parsed_data = []
-
+
data_by_line = tsv.split("\n")
data_by_line.each do |line|
elements = []
-
+
if CSV.const_defined? :Reader
# Ruby < 1.9 CSV code
elements = CSV::parse_line( line, "\t" ) || []
@@ -387,17 +387,17 @@ def parse_tsv_line_by_line( expected_row_size, tsv )
elements = []
end
end
-
+
if elements.size == 0
# This is a bad line (causing the above Exception), try and use split to recover.
elements = line.split("\t")
if line =~ /\t$/
# If the last attribute resturn is empty add a nil
- # value to the array as it would have been missed
+ # value to the array as it would have been missed
# by the split function!
elements.push(nil)
end
-
+
# Substitute blank strings for nils
elements.map! do |elem|
if elem === ""
@@ -406,7 +406,7 @@ def parse_tsv_line_by_line( expected_row_size, tsv )
elem
end
end
-
+
# Add a safety clause...
if elements.size === expected_row_size
parsed_data.push(elements)
@@ -415,12 +415,12 @@ def parse_tsv_line_by_line( expected_row_size, tsv )
parsed_data.push(elements)
end
end
-
+
return parsed_data
end
-
+
# Utility function to quickly convert a search result into an array of hashes
- # (keyed by the attribute name) for easier processing - this is not done by
+ # (keyed by the attribute name) for easier processing - this is not done by
# default on all searches as this can cause a large overhead on big data returns.
def conv_results_to_a_of_h( search_results )
result_objects = []
@@ -435,8 +435,8 @@ def conv_results_to_a_of_h( search_results )
return result_objects
end
-
- # Utility function to remove data rows from a search result that do not include
+
+ # Utility function to remove data rows from a search result that do not include
# the :required_attributes.
def filter_data_rows( args, result )
# Get the list of attributes searched for...
@@ -477,4 +477,4 @@ def filter_data_rows( args, result )
}
end
end
-end
+end
View
22 lib/biomart/filter.rb
@@ -1,9 +1,9 @@
module Biomart
- # Class representation for a biomart filter.
+ # Class representation for a biomart filter.
# Will belong to a Biomart::Dataset.
class Filter
attr_reader :name, :display_name, :default_value, :qualifier, :type, :pointer_dataset, :pointer_filter
-
+
def initialize(args)
@name = args["internalName"]
@display_name = args["displayName"]
@@ -13,36 +13,36 @@ def initialize(args)
@qualifier = args["qualifier"]
@type = args["type"]
@multiple_values = args["multipleValues"] ? true : false
-
+
@pointer_dataset = args["pointerDataset"]
@pointer_filter = args["pointerFilter"]
@pointer_interface = args["pointerInterface"]
-
+
@type.downcase! unless @type.nil?
end
-
- # Convenience method to see if this filter is hidden from
+
+ # Convenience method to see if this filter is hidden from
# the standard MartView interface.
#
# @return [Boolean] true/false
def hidden?
@hidden
end
-
- # Convenience method to see if this filter would be
+
+ # Convenience method to see if this filter would be
# enabled by default in the standard MartView interface.
#
# @return [Boolean] true/false
def default?
@default
end
-
- # Convenience method to see if this filter allows multiple
+
+ # Convenience method to see if this filter allows multiple
# values to be passed to it.
#
# @return [Boolean] true/false
def multiple_values?
@multiple_values
end
end
-end
+end
View
38 lib/biomart/server.rb
@@ -3,20 +3,20 @@ module Biomart
# Will contain many Biomart::Database and Biomart::Dataset objects.
class Server
include Biomart
-
+
attr_reader :url
-
+
def initialize( url )
@url = url or raise ArgumentError, "must pass :url"
unless @url =~ /martservice/
@url = @url + "/martservice"
end
-
+
@databases = {}
@datasets = {}
end
-
- # Returns an array of the database names (biomart 'name')
+
+ # Returns an array of the database names (biomart 'name')
# for this dataset.
#
# @return [Array] An array of database names
@@ -26,8 +26,8 @@ def list_databases
end
return @databases.keys
end
-
- # Returns a hash (keyed by the biomart 'name' for the database)
+
+ # Returns a hash (keyed by the biomart 'name' for the database)
# of all of the Biomart::Database objects belonging to this server.
#
# @return [Hash] A hash of Biomart::Database objects keyed by the database name
@@ -37,25 +37,25 @@ def databases
end
return @databases
end
-
- # Returns an array of the dataset names (biomart 'name')
+
+ # Returns an array of the dataset names (biomart 'name')
# for this dataset.
def list_datasets
if @datasets.empty?
fetch_datasets
end
return @datasets.keys
end
-
- # Returns a hash (keyed by the biomart 'name' for the dataset)
+
+ # Returns a hash (keyed by the biomart 'name' for the dataset)
# of all of the Biomart::Dataset objects belonging to this server.
def datasets
if @datasets.empty?
fetch_datasets
end
return @datasets
end
-
+
# Simple heartbeat function to test that a Biomart server is online.
# Returns true/false.
def alive?
@@ -68,10 +68,10 @@ def alive?
return true
end
end
-
+
private
-
- # Utility method to do the webservice call to the biomart server
+
+ # Utility method to do the webservice call to the biomart server
# and collate/build the information about the databases.
def fetch_databases
url = @url + '?type=registry'
@@ -80,14 +80,14 @@ def fetch_databases
@databases[ d.attributes["name"] ] = Database.new( @url, d.attributes )
end
end
-
- # Utility function to collate all of the Biomart::Dataset objects
+
+ # Utility function to collate all of the Biomart::Dataset objects
# contained within the Biomart::Database objects.
def fetch_datasets
self.databases.each do |name,database|
@datasets.merge!( database.datasets )
end
end
-
+
end
-end
+end
View
16 test/test_biomart.rb
@@ -6,42 +6,42 @@ def setup
@htgt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
@htgt_alt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
end
-
+
def teardown
VCR.eject_cassette
end
-
+
context "The Biomart module" do
setup do
@not_biomart = Biomart::Server.new( "http://www.sanger.ac.uk" )
@htgt_targ = @htgt.datasets["htgt_targ"]
@bad_dataset = Biomart::Dataset.new( "http://www.sanger.ac.uk/htgt/biomart", { :name => "wibble" } )
@good_biomart = Biomart::Server.new( "http://www.knockoutmouse.org/biomart" )
end
-
+
should "allow you to ping a server" do
assert( @good_biomart.alive?, "A good biomart does not respond 'true' to .alive?." )
assert( @htgt_targ.alive?, "A good biomart datasetdoes not respond 'true' to .alive?." )
assert_equal( false, @not_biomart.alive?, "A non-biomart server does not respond 'false' to .alive?." )
end
-
+
should "handle user/configuration errors (i.e. incorrect URLs etc)" do
assert_raise( Biomart::HTTPError ) { @not_biomart.list_databases }
end
-
+
should "handle biomart server errors gracefully" do
assert_raise( Biomart::ArgumentError ) { @htgt_targ.count( :filters => { "wibbleblibbleblip" => "1" } ) }
assert_raise( Biomart::AttributeError ) { @htgt_targ.search( :attributes => ["wibbleblibbleblip"] ) }
assert_raise( Biomart::DatasetError ) { @bad_dataset.count() }
-
+
begin
@bad_dataset.count()
rescue Biomart::BiomartError => e
general_error = e
end
-
+
assert( general_error.is_a?(Biomart::BiomartError), "Biomart.request is not handling general Biomart errors correctly." )
end
end
-
+
end
View
14 test/test_biomart_attribute.rb
@@ -6,26 +6,26 @@ def setup
@htgt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
@htgt_alt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
end
-
+
def teardown
VCR.eject_cassette
end
-
+
context "A Biomart::Attribute instance" do
setup do
@kermits = @htgt.datasets["kermits"]
end
-
+
should "have basic metadata" do
true_false = [true,false]
ens_gene_id = @kermits.attributes["ensembl_gene_id"]
-
+
assert( !ens_gene_id.name.nil?, "Biomart::Attribute.name is nil." )
assert( !ens_gene_id.display_name.nil?, "Biomart::Attribute.display_name is nil." )
-
+
assert( true_false.include?( ens_gene_id.hidden? ), "Biomart::Attribute.hidden? is not returning true/false." )
assert( true_false.include?( ens_gene_id.default? ), "Biomart::Attribute.default? is not returning true/false." )
end
end
-
-end
+
+end
View
12 test/test_biomart_database.rb
@@ -6,29 +6,29 @@ def setup
@htgt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
@htgt_alt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
end
-
+
def teardown
VCR.eject_cassette
end
-
+
context "A Biomart::Database instance" do
setup do
@htgt_database = @htgt.databases["htgt"]
end
-
+
should "have basic metadata" do
true_false = [true,false]
assert( @htgt_database.display_name, "Biomart::Database does not have a 'display name'." )
assert( @htgt_database.name, "Biomart::Database does not have a 'name'." )
assert( @htgt_database.visible != nil, "Biomart::Database does not have a 'visible' flag." )
assert( true_false.include?( @htgt_database.visible? ), "Biomart::Database.visible? is not returning true/false." )
end
-
+
should "have datasets" do
assert( @htgt_database.list_datasets.is_a?( Array ), "Biomart::Database.list_datasets is not returning an array." )
assert( @htgt_database.list_datasets.include?( "htgt_targ" ), "Biomart::Database dataset parsing is off - htgt_targ is not in htgt!" )
assert( @htgt_database.datasets["htgt_targ"].is_a?( Biomart::Dataset ), "Biomart::Database is not creating Biomart::Dataset objects." )
end
end
-
-end
+
+end
View
72 test/test_biomart_dataset.rb
@@ -6,11 +6,11 @@ def setup
@htgt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
@htgt_alt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
end
-
+
def teardown
VCR.eject_cassette
end
-
+
context "A Biomart::Dataset instance" do
setup do
@htgt_targ = @htgt.datasets["htgt_targ"]
@@ -22,26 +22,26 @@ def teardown
@dcc = Biomart::Dataset.new( "http://www.knockoutmouse.org/biomart", { :name => "dcc" } )
@mgi = Biomart::Dataset.new( "http://biomart.informatics.jax.org/biomart", { :name => "markers" } )
end
-
+
should "have basic metadata" do
assert( @htgt_targ.display_name, "Biomart::Dataset does not have a 'display name'." )
assert( @htgt_targ.name, "Biomart::Dataset does not have a 'name'." )
assert( @htgt_targ.visible != nil, "Biomart::Dataset does not have a 'visible' flag." )
end
-
+
should "have filters" do
assert( @htgt_targ.list_filters.is_a?( Array ), "Biomart::Dataset.list_filters is not returning an array." )
assert( @htgt_targ.list_filters.include?( "ensembl_gene_id" ), "Biomart::Dataset filter parsing is off - ensembl_gene_id is not in htgt_targ!" )
assert( @mgi.list_filters.include?( "ancestor_term_1023_filter" ), "Biomart::Dataset filter parsing is off - ancestor_term_1023_filter is not in mgi markers!" )
assert( @kermits.filters["ensembl_gene_id"].is_a?( Biomart::Filter ), "Biomart::Dataset is not creating Biomart::Filter objects." )
end
-
+
should "have attributes" do
assert( @htgt_targ.list_attributes.is_a?( Array ), "Biomart::Dataset.list_attributes is not returning an array." )
assert( @htgt_targ.list_attributes.include?( "ensembl_gene_id" ), "Biomart::Dataset attribute parsing is off - ensembl_gene_id is not in htgt_targ!" )
assert( @kermits.attributes["ensembl_gene_id"].is_a?( Biomart::Attribute ), "Biomart::Dataset is not creating Biomart::Attribute objects." )
end
-
+
should "perform count queries" do
htgt_count = @htgt_targ.count()
assert( htgt_count.is_a?( Integer ), "Biomart::Dataset.count is not returning integers." )
@@ -55,7 +55,7 @@ def teardown
assert( htgt_count_single_filter_group_value.is_a?( Integer ), "Biomart::Dataset.count (with single filter, group value) is not returning integers." )
assert( htgt_count_single_filter_group_value > 0, "Biomart::Dataset.count (with single filter, group value) is returning zero - this is wrong!" )
end
-
+
should "perform search queries" do
search = @htgt_trap.search()
assert( search.is_a?( Hash ), "Biomart::Dataset.search (no options) is not returning a hash." )
@@ -71,18 +71,18 @@ def teardown
assert( search2.first.is_a?( Hash ), "Biomart::Dataset.search (filters and attributes defined with processing) is not returning an array of hashes." )
assert( search2.first["marker_symbol"] == "Cbx1", "Biomart::Dataset.search (filters and attributes defined with processing) is not returning the correct info." )
end
-
+
should "perform search queries whilst altering the timeout property" do
search = @htgt_trap.search( :timeout => 60 )
assert( search.is_a?( Hash ), "Biomart::Dataset.search (no options except per-request timeout) is not returning a hash." )
assert( search[:data].is_a?( Array ), "Biomart::Dataset.search[:data] (no options except per-request timeout) is not returning an array." )
-
+
Biomart.timeout = 60
search = @htgt_trap.search()
assert( search.is_a?( Hash ), "Biomart::Dataset.search (no options except global timeout) is not returning a hash." )
assert( search[:data].is_a?( Array ), "Biomart::Dataset.search[:data] (no options except global timeout) is not returning an array." )
end
-
+
should "handle search queries that will generate poorly formatted TSV data" do
search = @htgt_targ.search(
:filters => { "mgi_accession_id" => [ "MGI:1921569", "MGI:1913402", "MGI:1913300" ] },
@@ -101,21 +101,21 @@ def teardown
assert( search.is_a?( Hash ), "Biomart::Dataset.search (no options) is not returning a hash. (HTGT Query)" )
assert( search[:data].is_a?( Array ), "Biomart::Dataset.search[:data] (no options) is not returning an array. (HTGT Query)" )
assert( search[:data].size > 20, "Biomart::Dataset.search[:data] for poorly formatted TSV data is empty. (HTGT Query)" )
-
+
search2 = @emma.search(
:filters => { "emma_id" => ["EM:03629"] },
:attributes => [
- "emma_id", "international_strain_name", "synonym", "maintained_background",
- "mutation_main_type", "mutation_sub_type", "alls_form", "genetic_description",
+ "emma_id", "international_strain_name", "synonym", "maintained_background",
+ "mutation_main_type", "mutation_sub_type", "alls_form", "genetic_description",
"phenotype_description", "reference", "pubmed_id", "availability", "owner"
]
)
-
+
assert( search2.is_a?( Hash ), "Biomart::Dataset.search (no options) is not returning a hash. (EMMA Query)" )
assert( search2[:data].is_a?( Array ), "Biomart::Dataset.search[:data] (no options) is not returning an array. (EMMA Query)" )
assert( search2[:data].size > 0, "Biomart::Dataset.search[:data] for poorly formatted TSV data is empty. (EMMA Query)" )
end
-
+
should "perform federated search queries" do
search_opts = {
:filters => {
@@ -133,16 +133,16 @@ def teardown
}
]
}
-
+
results = @htgt_targ.search( search_opts )
-
+
assert( results.is_a?(Hash), "Biomart::Dataset.search is not returning a hash. [federated search]" )
assert( results[:data].is_a?(Array), "Biomart::Dataset.search[:data] is not returning an array. [federated search]" )
assert( results[:data][0].size === 3, "Biomart::Dataset.search[:data] is not returning 3 attributes. [federated search]" )
assert( results[:headers].size === 3, "Biomart::Dataset.search[:headers] is not returning 3 elements. [federated search]" )
assert_raise( Biomart::ArgumentError ) { @htgt_targ.count( search_opts ) }
-
+
assert_raise Biomart::ArgumentError do
search_opts[:federate] = [
{
@@ -153,7 +153,7 @@ def teardown
]
results = @htgt_targ.search( search_opts )
end
-
+
assert_raise Biomart::ArgumentError do
search_opts[:federate] = {
:dataset => "mmusculus_gene_ensembl",
@@ -162,7 +162,7 @@ def teardown
}
results = @htgt_targ.search( search_opts )
end
-
+
assert_raise Biomart::ArgumentError do
search_opts[:federate] = [
{
@@ -179,7 +179,7 @@ def teardown
results = @htgt_targ.search( search_opts )
end
end
-
+
should "perform search queries with the :required_attributes option" do
search_opts = {
:filters => {
@@ -193,21 +193,21 @@ def teardown
],
:required_attributes => ["mouse_paralog_ensembl_gene"]
}
-
+
results = @ensembl.search( search_opts )
-
+
assert( results.is_a?(Hash), "Biomart::Dataset.search is not returning a hash. [required_attributes search]" )
assert( results[:data].is_a?(Array), "Biomart::Dataset.search[:data] is not returning an array. [required_attributes search]" )
results[:data].each do |data_row|
assert_equal( false, data_row[2].nil?, "The required_attributes search has not filtered out nil values." )
end
-
+
assert_raise( Biomart::ArgumentError ) { @ensembl.count( search_opts ) }
assert_raise Biomart::ArgumentError do
search_opts[:required_attributes] = "mouse_paralog_ensembl_gene"
@ensembl.search( search_opts )
end
-
+
results = @dcc.search(
:filters => {
"marker_symbol" => [
@@ -220,54 +220,54 @@ def teardown
:required_attributes => ["ikmc_project","ikmc_project_id"],
:process_results => true
)
-
+
results.each do |data_row|
assert_equal( false, data_row["ikmc_project"].nil?, "The required_attributes search has not filtered out nil values." )
assert_equal( false, data_row["ikmc_project_id"].nil?, "The required_attributes search has not filtered out nil values." )
end
end
-
+
should "perform searches that involve boolean filters" do
search_opts = {
:filters => { 'with_variation_annotation' => true, 'ensembl_gene' => 'ENSG00000244734' },
:attributes => [ 'refsnp_id','chr_name','chrom_start' ],
:process_results => true,
:timeout => 2000
}
-
+
true_results = {}
assert_nothing_raised( Biomart::BiomartError ) { true_results = @ensembl_var.search( search_opts ) }
assert( !true_results.empty?, "The search using a boolean filter is empty." )
-
+
search_opts[:filters].merge!({ 'with_variation_annotation' => 'included' })
true_results2 = {}
assert_nothing_raised( Biomart::BiomartError ) { true_results2 = @ensembl_var.search( search_opts ) }
assert( !true_results2.empty?, "The search using a boolean filter is empty." )
assert_equal( true_results, true_results2, "Using 'included' for a boolean filter does not give the same result as 'true'." )
-
+
search_opts[:filters].merge!({ 'with_variation_annotation' => 'only' })
true_results3 = {}
assert_nothing_raised( Biomart::BiomartError ) { true_results3 = @ensembl_var.search( search_opts ) }
assert( !true_results3.empty?, "The search using a boolean filter is empty." )
assert_equal( true_results, true_results3, "Using 'only' for a boolean filter does not give the same result as 'true'." )
-
+
search_opts[:filters].merge!({ 'with_variation_annotation' => false })
false_results = {}
assert_nothing_raised( Biomart::BiomartError ) { false_results = @ensembl_var.search( search_opts ) }
assert( !false_results.empty?, "The search using a boolean filter is empty." )
-
+
search_opts[:filters].merge!({ 'with_variation_annotation' => 'excluded' })
false_results2 = {}
assert_nothing_raised( Biomart::BiomartError ) { false_results2 = @ensembl_var.search( search_opts ) }
assert( !false_results2.empty?, "The search using a boolean filter is empty." )
assert_equal( false_results, false_results2, "Using 'excluded' for a boolean filter does not give the same result as 'false'." )
-
+
search_opts[:filters].merge!({ 'with_variation_annotation' => 'flibble' })
assert_raise( Biomart::ArgumentError ) { @ensembl_var.search( search_opts ) }
-
+
search_opts[:filters].merge!({ 'with_variation_annot' => true })
assert_raise( Biomart::ArgumentError ) { @ensembl_var.search( search_opts ) }
end
end
-
-end
+
+end
View
14 test/test_biomart_filter.rb
@@ -6,28 +6,28 @@ def setup
@htgt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
@htgt_alt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
end
-
+
def teardown
VCR.eject_cassette
end
-
+
context "A Biomart::Filter instance" do
setup do
@kermits = @htgt.datasets["kermits"]
end
-
+
should "have basic metadata" do
true_false = [true,false]
ens_gene_id = @kermits.filters["ensembl_gene_id"]
-
+
assert( !ens_gene_id.name.nil?, "Biomart::Filter.name is nil." )
assert( !ens_gene_id.display_name.nil?, "Biomart::Filter.display_name is nil." )
assert( !ens_gene_id.type.nil?, "Biomart::Filter.type is nil." )
-
+
assert( true_false.include?( ens_gene_id.hidden? ), "Biomart::Filter.hidden? is not returning true/false." )
assert( true_false.include?( ens_gene_id.default? ), "Biomart::Filter.default? is not returning true/false." )
assert( true_false.include?( ens_gene_id.multiple_values? ), "Biomart::Filter.multiple_values? is not returning true/false." )
end
end
-
-end
+
+end
View
10 test/test_biomart_server.rb
@@ -6,23 +6,23 @@ def setup
@htgt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
@htgt_alt = Biomart::Server.new('http://www.sanger.ac.uk/htgt/biomart')
end
-
+
def teardown
VCR.eject_cassette
end
-
+
context "A Biomart::Server instance" do
should "have databases" do
assert( @htgt_alt.list_databases.is_a?( Array ), "Biomart::Server.list_databases is not returning an array." )
assert( @htgt_alt.list_databases.include?( "htgt" ), "Biomart::Server datbase parsing is off - htgt is not in htgt!" )
assert( @htgt.databases["htgt"].is_a?( Biomart::Database ), "Biomart::Server is not creating Biomart::Database objects." )
end
-
+
should "have datasets" do
assert( @htgt_alt.list_datasets.is_a?( Array ), "Biomart::Server.list_datasets is not returning an array." )
assert( @htgt_alt.list_datasets.include?( "htgt_targ" ), "Biomart::Server dataset parsing is off - htgt_targ is not in htgt!" )
assert( @htgt.datasets["htgt_targ"].is_a?( Biomart::Dataset ), "Biomart::Server is not creating Biomart::Dataset objects." )
end
end
-
-end
+
+end
View
6 test/test_helper.rb
@@ -40,11 +40,11 @@ def format(result)
else
c.cassette_library_dir = 'test/vcr_cassettes_ruby1.9.2+'
end
-
+
c.stub_with :webmock
c.ignore_localhost = true
- c.default_cassette_options = {
- :record => :new_episodes,
+ c.default_cassette_options = {
+ :record => :new_episodes,
:match_requests_on => [:uri, :method, :body]
}
end

0 comments on commit f9ca04a

Please sign in to comment.
Something went wrong with that request. Please try again.