Module: EnsemblRest

Defined in:
lib/bio-ensembl-rest/lookup.rb,
lib/bio-ensembl-rest/mapping.rb,
lib/bio-ensembl-rest/features.rb,
lib/bio-ensembl-rest/sequence.rb,
lib/bio-ensembl-rest/taxonomy.rb,
lib/bio-ensembl-rest/variation.rb,
lib/bio-ensembl-rest/ontologies.rb,
lib/bio-ensembl-rest/information.rb,
lib/bio-ensembl-rest/cross-reference.rb,
lib/bio-ensembl-rest/ensembl-rest-main.rb,
lib/bio-ensembl-rest/comparative-genomics.rb

Defined Under Namespace

Modules: ComparativeGenomics, CrossReference, Features, Information, Lookup, Mapping, Ontologies, Sequence, Taxonomy, Variation

Class Method Summary collapse

Class Method Details

.build_path(home, opts) ⇒ Object

HTTP request stuff ##



57
58
59
60
61
62
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 57

def self.build_path(home, opts) # :nodoc:
  path = home + '?'
  opts.each { |k,v| path << "#{k}=#{v};"  if k != 'content-type' }
  path[-1] = '' if not opts
  URI::encode path
end

.check_response(response) ⇒ Object

:nodoc:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 86

def self.check_response(response) # :nodoc:
  case response.code 
  when '200'
    return response.body
  when '400'
    raise 'Bad request: ' + response.body
  when '404' 
    raise 'Not Found'
  when '415'
    raise 'Unsupported Media Type'
  when '429' 
    raise 'Too many requests'
  when '503'
    raise 'Service Unavailable'
  else
    raise "Bad response code: #{response.code}"
  end
end

.connect_dbObject

start HTTP database connection ##



4
5
6
7
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 4

def self.connect_db 
  $SERVER = URI.parse 'http://beta.rest.ensembl.org'
  $HTTP_CONNECTION = Net::HTTP.new($SERVER.host, $SERVER.port)
end

.fetch_data(path, opts, mod) ⇒ Object

:nodoc:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 64

def self.fetch_data(path, opts, mod) # :nodoc:
  # what we should set as content-type in the header
  # to keep ensembl happy when the the user does not
  # use the format parameter to set the return type
  default_types = {
    'sequence' => 'text/plain',
    'compara' => 'text/xml',
    'crossreference' => 'application/json',
    'features' => 'application/json',
    'information' => 'application/json',
    'lookup' => 'application/json',
    'mapping' => 'application/json',
    'ontologies' => 'application/json',
    'taxonomy' => 'application/json',
    'variation' => 'application/json'
  }
  request = Net::HTTP::Get.new path
  request.content_type = opts['content-type'] || default_types[mod]
  response = $HTTP_CONNECTION.request request
  return check_response response
end

.parse_format(opts) ⇒ Object

:nodoc:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 29

def self.parse_format(opts) # :nodoc:
  supported_formats = {
    'text' => 'text/plain',
    'fasta' => 'text/x-fasta',
    'gff3' => 'ext/x-gff3',
    'json' => 'application/json',
    'msgpack' => 'application/x-msgpack',
    'nh' => 'text/x-nh',
    'seqxml' => 'text/x-seqxml+xml', 
    'sereal' => 'application/x-sereal',
    'phyloxml' => 'text/x-phyloxml+xml',
    'xml' => 'text/xml',
    'yaml' => 'text/x-yaml'
  }
  if opts.has_key?('response')
    req = opts['response']
    if supported_formats[req]
      opts['content-type'] = supported_formats[req]
    else
      opts['content-type'] = req
    end
    opts.delete 'response'
  end
  opts
end

.parse_options(opts) ⇒ Object

parse options stuff ##



11
12
13
14
15
16
17
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 11

def self.parse_options(opts) # :nodoc:
  parsed_opts = {}
  opts.each {|k, v| parsed_opts[k.to_s] = v}

  parse_format parsed_opts
  parse_true_false parsed_opts
end

.parse_true_false(opts) ⇒ Object

:nodoc:



19
20
21
22
23
24
25
26
27
# File 'lib/bio-ensembl-rest/ensembl-rest-main.rb', line 19

def self.parse_true_false(opts) # :nodoc:
  opts.each do |k, v|
    if v.instance_of?(TrueClass)
      opts[k] = 1
    elsif v.instance_of?(FalseClass)
      opts[k] = 0
    end
  end
end