#!/usr/bin/env rubyrequire'rubygems'require'rest_client'require'json'$server='http://rest.ensembl.org'defget_variants(species,symbol)genes=rest_get("/xrefs/symbol/#{species}/#{symbol}?object_type=gene")raise"No genes for species '#{species}' and symbol '#{symbol}'"ifgenes.size==0returnrest_get("/overlap/id/#{genes[0]['id']}?feature=variation")enddefrest_get(url)$request_counter||=0# Initialise if unset$last_request_time||=0# Initialise if unset# Rate limiting: Sleep for the remainder of a second since the last request on every third request$request_counter+=1if$request_counter==15diff=Time.now-$last_request_timesleep(1-diff)ifdiff<1$request_counter=0endbeginresponse=RestClient.get"#{$server}/#{url}",{:accept=>:json}$last_request_time=Time.nowJSON.parse(response)rescueRestClient::Exception=>eputs"Failed for #{url}! #{response?"Status code: #{response}. ":''}Reason: #{e.message}"# Sleep for specified number of seconds if there is a Retry-After headerife.response.headers[:retry_after]sleep(e.response.headers[:retry_after].to_f)retry# This retries from the start of the begin blockelseabort("Quitting... #{e.inspect}")endendendspecies='human'symbol='BRAF'get_variants(species,symbol).eachdo|variant|puts"#{variant['seq_region_name']}:#{variant['start']}-#{variant['end']}:#{variant['strand']} ==> #{variant['id']} (#{variant['consequence_type']})"end