Created
May 20, 2010 10:27
-
-
Save j1n3l0/407431 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Author:: Nelo Onyiah (mailto:io1@sanger.ac.uk) | |
# | |
# In this example we are going to update all the alleles | |
# for the KOMP-Regeneron pipeline. We are updating the | |
# "allele_symbol_superscript" for each Es Cell we find | |
# that needs updating. | |
# | |
# This example demonstrates that data is retrieved from | |
# the repository one page at a time. It also makes use | |
# of a generic "request" function (see earlier examples). | |
# | |
use strict; | |
use warnings FATAL => 'all'; | |
use JSON; | |
use REST::Client; | |
my $domain = 'localhost:3000'; | |
my $user = 'user'; | |
my $pass = 'pass'; | |
my $page = 0; | |
# | |
# Retrieve the available pipelines | |
my $response = request( "GET", "pipelines.json" ); | |
my $pipelines = from_json($response); | |
# | |
# We are only interested in the KOMP-Regeneron pipeline, so let's get that | |
my ($regeneron) = grep { $_->{name} eq 'KOMP-Regeneron' } @{$pipelines}; | |
# | |
# Due to size limits, data is returned from the repository in pages. | |
# Therefore, we need to process the data as such ... one page at a time. | |
while (1) { | |
my $alleles = update_es_cells_on_page( $regeneron, ++$page ); | |
last unless @{$alleles}; | |
} | |
exit 0; | |
# | |
# 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; | |
} | |
# | |
# Generic function to process the data retrieved from a specified page | |
sub update_es_cells_on_page { | |
my ( $pipeline, $page ) = @_; | |
# | |
# Now let's fetch all the alleles from $page (this may take a while) | |
my $search_params = "pipeline_id=$pipeline->{id}&page=$page"; | |
my $response = request( "GET", "alleles.json?$search_params" ); | |
my $alleles = from_json($response); | |
# | |
# Loop through the alleles ... | |
for my $allele (@{$alleles}) { | |
for my $es_cell ( @{ $allele->{es_cells} } ) { | |
# ... updating the es_cells that need fixing along the way | |
if ( $es_cell->{allele_symbol_superscript} =~ m/^.+\<(.+)\>$/ ) { | |
$es_cell->{allele_symbol_superscript} = $1; | |
my $es_cell_json = to_json( { es_cell => $es_cell } ); | |
request( "PUT", "es_cells/$es_cell->{id}.json", $es_cell_json ); | |
} | |
} | |
} | |
# | |
# When there is no data, we are on the last page | |
warn "Found 0 KOMP-Regeneron alleles on page $page\n" unless scalar @{$alleles}; | |
# Return the list of alleles | |
return $alleles; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment