Skip to content
Browse files

Added in support for Boolean filters. [Closes #11]

  • Loading branch information...
1 parent 774b2e5 commit 73a5a59709e2bd0c8bc1baa07153dcaa9a74d049 Daz Oakley committed Feb 16, 2011
Showing with 66 additions and 11 deletions.
  1. +13 −3 lib/biomart/dataset.rb
  2. +2 −0 lib/biomart/filter.rb
  3. +51 −8 test/test_biomart.rb
View
16 lib/biomart/dataset.rb
@@ -248,10 +248,20 @@ def dataset_xml( xml, dataset, args )
if args[:filters]
args[:filters].each do |name,value|
- if value.is_a? Array
- value = value.join(",")
+ raise Biomart::ArgumentError, "The filter '#{name}' does not exist" if dataset.filters[name].nil?
+
+ if dataset.filters[name].type == 'boolean'
+ if [true,'included','only'].include?(value)
+ xml.Filter( :name => name, :excluded => '0' )
+ elsif [false,'excluded'].include?(value)
+ xml.Filter( :name => name, :excluded => '1' )
+ else
+ raise Biomart::ArgumentError, "The boolean filter '#{name}' can only accept 'true/included/only' or 'false/excluded' arguments."
+ end
+ else
+ value = value.join(",") if value.is_a? Array
+ xml.Filter( :name => name, :value => value )
end
- xml.Filter( :name => name, :value => value )
end
else
dataset.filters.each do |name,filter|
View
2 lib/biomart/filter.rb
@@ -13,6 +13,8 @@ def initialize(args)
@qualifier = args["qualifier"]
@type = args["type"]
@multiple_values = args["multipleValues"] ? true : false
+
+ @type.downcase! unless @type.nil?
end
# Convenience method to see if this filter is hidden from
View
59 test/test_biomart.rb
@@ -42,12 +42,13 @@ def setup
context "A Biomart::Dataset instance" do
setup do
- @htgt_targ = @htgt.datasets["htgt_targ"]
- @htgt_trap = @htgt.datasets["htgt_trap"]
- @kermits = @htgt.datasets["kermits"]
- @ensembl = @htgt.datasets["mmusculus_gene_ensembl"]
- @emma = Biomart::Dataset.new( "http://www.emmanet.org/biomart", { :name => "strains" } )
- @dcc = Biomart::Dataset.new( "http://www.i-dcc.org/biomart", { :name => "dcc" } )
+ @htgt_targ = @htgt.datasets["htgt_targ"]
+ @htgt_trap = @htgt.datasets["htgt_trap"]
+ @kermits = @htgt.datasets["kermits"]
+ @ensembl = @htgt.datasets["mmusculus_gene_ensembl"]
+ @ensembl_var = Biomart::Dataset.new( "http://www.ensembl.org/biomart", { :name => "hsapiens_snp" } )
+ @emma = Biomart::Dataset.new( "http://www.emmanet.org/biomart", { :name => "strains" } )
+ @dcc = Biomart::Dataset.new( "http://www.knockoutmouse.org/biomart", { :name => "dcc" } )
end
should "have basic metadata" do
@@ -252,6 +253,47 @@ def setup
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
+ }
+
+ 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
context "A Biomart::Attribute instance" do
@@ -282,6 +324,7 @@ def setup
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." )
@@ -294,7 +337,7 @@ def setup
@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.i-dcc.org/biomart" )
+ @good_biomart = Biomart::Server.new( "http://www.knockoutmouse.org/biomart" )
end
should "allow you to ping a server" do
@@ -308,7 +351,7 @@ def setup
end
should "handle biomart server errors gracefully" do
- assert_raise( Biomart::FilterError ) { @htgt_targ.count( :filters => { "wibbleblibbleblip" => "1" } ) }
+ 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() }

0 comments on commit 73a5a59

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