Class: MartSearch::BiomartDataSource

Inherits:
DataSource show all
Defined in:
lib/martsearch/data_source_biomart.rb

Overview

Custom DataSource class for interacting with BioMart based datasources.

Author:

  • Darren Oakley

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DataSource

#name

Methods included from Utils

#build_http_client, #convert_array_to_hash

Constructor Details

#initialize(conf) ⇒ BiomartDataSource



13
14
15
16
# File 'lib/martsearch/data_source_biomart.rb', line 13

def initialize( conf )
  super
  @ds = Biomart::Dataset.new( @url, { :name => @conf[:dataset] } )
end

Instance Attribute Details

#dsObject (readonly)

The Biomart::Dataset object for the BiomartDataSource



10
11
12
# File 'lib/martsearch/data_source_biomart.rb', line 10

def ds
  @ds
end

Instance Method Details

#data_origin_url(query, conf) ⇒ Object

Function to provide a link URL to the original datasource given a dataset query.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/martsearch/data_source_biomart.rb', line 90

def data_origin_url( query, conf )
  MartSearch::Controller.instance().logger.debug("[MartSearch::BiomartDataSource] '#{self.name}' ::data_origin_url - running data_origin_url( '#{query}', conf )")

  url = @url + "/martview?VIRTUALSCHEMANAME=default&VISIBLEPANEL=resultspanel"

  # Filters...
  filters = []

  filters_to_build = { conf[:joined_filter] => query }
  filters_to_build.merge!( conf[:filters] ) unless conf[:filters].nil?

  filters_to_build.each do |key,value|
    filter = "#{@conf[:dataset]}.default.filters.#{key}.""

    if value.is_a?(Array) then filter << "#{CGI::escape(value.join(","))}&quot;"
    else                       filter << "#{CGI::escape(value)}&quot;"
    end

    filters.push(filter)
  end

  url << "&FILTERS=#{filters.join("|")}"

  # Attributes...
  attrs = []

  url << "&ATTRIBUTES="
  conf[:attributes].each do |attribute|
    attrs.push("#{@conf[:dataset]}.default.attributes.#{attribute}")
  end

  url << attrs.join("|")

  MartSearch::Controller.instance().logger.debug("[MartSearch::BiomartDataSource] '#{self.name}' ::data_origin_url - running data_origin_url( '#{query}', conf ) - DONE")
  return url
end

#ds_attributesObject



18
19
20
21
# File 'lib/martsearch/data_source_biomart.rb', line 18

def ds_attributes
  @ds_attributes = @ds.attributes if @ds_attributes.nil?
  return @ds_attributes
end

#fetch_all_terms_for_indexing(conf) ⇒ Object

Function to query a biomart datasource and return all of the data ready for indexing.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/martsearch/data_source_biomart.rb', line 33

def fetch_all_terms_for_indexing( conf )
  MartSearch::Controller.instance().logger.debug("[MartSearch::BiomartDataSource] '#{self.name}' ::fetch_all_terms_for_indexing - running fetch_all_terms_for_indexing()")

  attributes = []
  conf[:attribute_map].each do |map|
    attributes.push(map[:attr])
  end

  filters = conf[:filters]
  filters.stringify_keys! unless filters.nil?

  biomart_search_params = {
    :filters    => filters,
    :attributes => attributes.uniq,
    :timeout    => MartSearch::Server.settings.biomart_search_params_timeout
  }

  @ds.search(biomart_search_params)
end

#is_alive?Boolean

Simple heartbeat function to check that the datasource is online.



26
27
28
# File 'lib/martsearch/data_source_biomart.rb', line 26

def is_alive?
  @ds.alive?
end

#search(query, conf) ⇒ Object

Function to search a biomart datasource given an appropriate configuration.

Raises:

See Also:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/martsearch/data_source_biomart.rb', line 57

def search( query, conf )
  MartSearch::Controller.instance().logger.debug("[MartSearch::BiomartDataSource] '#{self.name}' ::search - running search( '#{query}', conf )")

  filters = { conf[:joined_filter] => query.join(',') }
  filters.merge!( conf[:filters] ) unless conf[:filters].nil? or conf[:filters].empty?
  filters.stringify_keys!

  search_options = {
    :filters         => filters,
    :attributes      => conf[:attributes],
    :process_results => true,
    :timeout         => MartSearch::Server.settings.biomart_search_options_timeout
  }

  if conf[:required_attributes]
    search_options[:required_attributes] = conf[:required_attributes]
  end

  begin
    results = @ds.search(search_options)
    results.recursively_symbolize_keys!
  rescue Biomart::BiomartError => error
    raise MartSearch::DataSourceError, "Biomart::BiomartError: #{error.message}"
  end

  MartSearch::Controller.instance().logger.debug("[MartSearch::BiomartDataSource] '#{self.name}' ::search - running search( '#{query}', conf ) - DONE")
  return results
end