Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created May 18, 2010 13:07
Show Gist options
  • Save j1n3l0/404967 to your computer and use it in GitHub Desktop.
Save j1n3l0/404967 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Author:: Nelo Onyiah (mailto:io1@sanger.ac.uk)
use JSON;
use REST::Client;
my $domain = 'localhost:3000';
my $user = 'user';
my $pass = 'pass';
#
# Generic helper function for handling the web requests to the repository.
sub request {
my ( $method, $url, $data ) = @_;
die "Method $method unknown when requesting URL $url"
unless $method =~ m/DELETE|GET|POST|PUT/;
my @args = $data if $data;
my $client = REST::Client->new( { host => "http://$user:$pass\@$domain" } );
# Set the Content-Type and call the method with @args
$client->addHeader( content_type => "application/json" );
$client->$method( $url, @args );
# A small update message
warn join( " ", $method, $url, '-', $client->responseCode ), "\n";
# Handle failures here -- only code 200 | 201 are OK
die "Bad HTTP response ", $client->responseCode
unless $client->responseCode =~ m/20[01]/;
return $client->responseContent;
}
##
## Scenario 1: search and update an allele
##
my %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
my $search_params = "mgi_accession_id=$allele{mgi_accession_id}";
$search_params .= "&project_design_id=$allele{project_design_id}";
# etc.
my $response = request( "GET", "alleles.json?$search_params" );
my $alleles_found = from_json($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.
my $allele_found = $alleles_found->[0];
my $allele_json = to_json( { 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
my $allele_json = to_json( { molecular_structure => \%allele } );
my $response = request( "POST", 'alleles', $allele_json );
my $allele = from_json($response);
# In this example we'll DELETE the created allele
request( "DELETE", "alleles/$allele->{id}.json" );
}
##
## Scenario 2: Create an allele and all its products in a single request
## /!\ This trick will not work for an UPDATE.
##
my %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 => 'EPD00001' },
{ name => 'EPD00002' },
{ name => 'EPD00003' },
],
},
{
name => 'PRPGD002',
intermediate_vector => 'PGS001',
ikmc_project_id => 1,
es_cells => [
{ name => 'EPD00004' },
{ name => 'EPD00005' },
{ name => 'EPD00006' },
],
}
],
genbank_file => {
escell_clone => "A GENBANK FILE IN PLAIN TEXT",
targeting_vector => "A GENBANK FILE IN PLAIN TEXT",
},
);
my $allele_json = to_json( { molecular_structure => \%allele_and_products } );
$response = request( "POST", 'alleles.json', $allele_json );
my $allele = from_json($response);
#
# DELETE everything
for my $es_cell ( @{ $allele->{es_cells} } ) {
request( "DELETE", "es_cells/$es_cell->{id}" );
}
for my $targ_vec ( @{ $allele->{targeting_vectors} } ){
request( "DELETE", "targeting_vectors/$targ_vec->{id}" );
}
request( "DELETE", "alleles/$allele->{id}" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment