Skip to content

Instantly share code, notes, and snippets.

@sb25
Created November 17, 2009 15:38
Show Gist options
  • Select an option

  • Save sb25/237003 to your computer and use it in GitHub Desktop.

Select an option

Save sb25/237003 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
# Author:: Sébastien Briois (mailto:[email protected])
require "rubygems"
require "rest_client"
require "json"
IDCC_SITE = "http://user:password@localhost:3000"
# Generic helper method for handling the web calls to the repository.
def request(method, url, data = nil, datatype = "application/json", repository = IDCC_SITE)
resource = RestClient::Resource.new( repository )
response =
case method.upcase
when "GET" then resource[url].get
when "POST" then resource[url].post data, :content_type => datatype
when "PUT" then resource[url].put data, :content_type => datatype
when "DELETE" then resource[url].delete
else
raise "Method #{method} unknown when requesting url #{url}"
end
puts "#{method} #{url} - #{response.code} #{RestClient::STATUSES[response.code]}"
return response.body
end
##
## Scenario 1: search and update an allele
##
allele = {
:pipeline_id => 1,
:mgi_accession_id => 'MGI:123456',
:project_design_id => 1,
:cassette => 'L1L2_gt2',
:backbone => 'L3L4_pZero_kan',
:assembly => 'NCBIM37',
:chromosome => '11',
:strand => '+',
:design_type => 'Knock Out',
:design_subtype => 'Frameshift',
:homology_arm_start => 10,
:homology_arm_end => 10000,
:cassette_start => 50,
:cassette_end => 500,
:loxp_start => 1000,
:loxp_end => 1500
}
# Let's search for this allele using discriminating attributes
search_params = "mgi_accession_id=#{allele['mgi_accession_id']}"
search_params += "&project_design_id=#{allele['project_design_id']}"
# etc.
response = request( 'GET', "alleles.json?#{search_params}" )
alleles_found = JSON.parse( response ) # Returns a list of matches
# NOTE:
# The alleles_found might hold more than one allele in which case the
# search (above) could be run again with more details.
if alleles_found
# We now want to update our allele.
allele_found = alleles_found[0]
allele_json = JSON.generate( {:molecular_structure => allele} )
request( 'PUT', "alleles/#{allele_found['id']}", allele_json )
else
# No allele has been found, let's create our allele from the Hash we've made
allele_json = JSON.generate( {:molecular_structure => allele} )
response = request( 'POST', 'alleles', allele_json )
allele = JSON.parse( response )
# In this example we'll DELETE the created allele
request( 'DELETE', "alleles/#{allele['id']}")
end
##
## Scenario 2: Create an allele and all its products in a single request
## /!\ This trick will not work for an UPDATE.
##
allele_and_products = {
:pipeline_id => 1,
:mgi_accession_id => "MGI:456789",
:project_design_id => 2,
:cassette => "L1L2_gt2",
:backbone => "L3L4_pZero_kan",
:assembly => "NCBIM37",
:chromosome => "1",
:strand => "+",
:design_type => "Knock Out",
:design_subtype => "Frameshift",
:homology_arm_start => 10,
:homology_arm_end => 10000,
:cassette_start => 50,
:cassette_end => 500,
:loxp_start => 1000,
:loxp_end => 1500,
:targeting_vectors => [
{
:name => 'PRPGD001',
:intermediate_vector => 'PGS001',
:ikmc_project_id => 1,
:es_cells => [
{ :name => 'EPD001' },
{ :name => 'EPD002' },
{ :name => 'EPD003' },
]
},
{
:name => 'PRPGD002',
:intermediate_vector => 'PGS001',
:ikmc_project_id => 1,
:es_cells => [
{ :name => 'EPD004' },
{ :name => 'EPD005' },
{ :name => 'EPD006' },
]
}
],
:genbank_file => {
:escell_clone => "A GENBANK FILE IN PLAIN TEXT",
:targeting_vector => "A GENBANK FILE IN PLAIN TEXT"
}
}
allele_json = JSON.generate( {:molecular_structure => allele_and_products} )
response = request( 'POST', 'alleles.json', allele_json )
allele = JSON.parse( response )
# DELETE everything
allele['es_cells'].each do |es_cell|
request( 'DELETE', "es_cells/#{es_cell['id']}" )
end
allele['targeting_vectors'].each do |targ_vec|
request( 'DELETE', "targeting_vectors/#{targ_vec['id']}" )
end
request( 'DELETE', "alleles/#{allele['id']}" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment