Permalink
Browse files

Perl REST client example code

1 parent a4dcf6e commit 582c821351ec1fb54eb9cad3e90b32640de2a9d9 @palexander palexander committed Oct 1, 2014
Showing with 132 additions and 0 deletions.
  1. +30 −0 perl/annotate_text.pl
  2. +28 −0 perl/classes_search.pl
  3. +47 −0 perl/get_labels.pl
  4. +27 −0 perl/list_ontologies.pl
View
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+use LWP::Simple; # From CPAN
+use JSON qw( decode_json ); # From CPAN
+use Data::Dumper; # Perl core module
+use strict; # Good practice
+use warnings; # Good practice
+
+my $rest_url = "http://data.bioontology.org/";
+my $api_key = "";
+
+my $text_to_annotate = "Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye.";
+
+# Get the top-level resources
+my $json = get( $rest_url . "?apikey=" . $api_key );
+die "Could not get $rest_url!" unless defined $json;
+my $resources = decode_json( $json );
+
+# Follow the links to the ontologies
+my $annotate_link = $resources->{'links'}->{'annotator'};
+
+# Get the ontologies
+my $annotate_json = get( $annotate_link . "?text=" . $text_to_annotate . "&include=prefLabel&apikey=" . $api_key );
+die "Could not get $annotate_link!" unless defined $annotate_json;
+my @classes = @{ decode_json( $annotate_json ) };
+
+foreach my $ann (@classes) {
+ my $cls = $ann->{'annotatedClass'};
+ print $cls->{'prefLabel'} . "\t" . $cls->{'@id'} . "\t" . $cls->{'links'}->{'ontology'} . "\n";
+}
View
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use LWP::Simple; # From CPAN
+use JSON qw( decode_json ); # From CPAN
+use Data::Dumper; # Perl core module
+use strict; # Good practice
+use warnings; # Good practice
+
+my $rest_url = "http://data.bioontology.org/";
+my $api_key = "";
+
+# Get the top-level resources
+my $json = get( $rest_url . "?apikey=" . $api_key );
+die "Could not get $rest_url!" unless defined $json;
+my $resources = decode_json( $json );
+
+# Follow the links to the ontologies
+my $search_link = $resources->{'links'}->{'search'};
+
+# Get the ontologies
+my $search_json = get( $search_link . "?q=heart&apikey=" . $api_key );
+die "Could not get $search_link!" unless defined $search_json;
+my $results = decode_json( $search_json );
+my @classes = @{ $results->{'collection'} };
+
+foreach my $cls (@classes) {
+ print $cls->{'prefLabel'} . "\t" . $cls->{'@id'} . "\t" . $cls->{'links'}->{'ontology'} . "\n";
+}
View
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+use LWP::Simple; # From CPAN
+use JSON qw( decode_json ); # From CPAN
+use Data::Dumper; # Perl core module
+use strict; # Good practice
+use warnings; # Good practice
+
+my $rest_url = "http://data.bioontology.org/";
+my $api_key = "";
+
+# Get the top-level resources
+my $json = get( $rest_url . "?apikey=" . $api_key );
+die "Could not get $rest_url!" unless defined $json;
+my $resources = decode_json( $json );
+
+# Follow the links to the ontologies
+my $ont_link = $resources->{'links'}->{'ontologies'};
+
+# Get the ontologies
+my $ont_json = get( $ont_link . "?apikey=" . $api_key );
+die "Could not get $ont_link!" unless defined $ont_json;
+my @ontologies = @{ decode_json( $ont_json ) };
+
+my $bro;
+foreach my $ont (@ontologies) {
+ if ($ont->{'acronym'} eq "BRO") {
+ $bro = $ont;
+ }
+}
+
+my $cls_page_link = $bro->{'links'}->{'classes'} . "?apikey=" . $api_key;
+
+# Get the labels for BRO
+my @classes;
+while ($cls_page_link) {
+ my $page_json = get( $cls_page_link );
+ die "Could not get $cls_page_link!" unless defined $page_json;
+ my $cls_page = decode_json( $page_json );
+ push @classes, @{ $cls_page->{'collection'} };
+ $cls_page_link = $cls_page->{'links'}->{'nextPage'};
+}
+
+foreach my $cls (@classes) {
+ print $cls->{'prefLabel'} . "\n";
+}
+
View
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+use LWP::Simple; # From CPAN
+use JSON qw( decode_json ); # From CPAN
+use Data::Dumper; # Perl core module
+use strict; # Good practice
+use warnings; # Good practice
+
+my $rest_url = "http://data.bioontology.org/";
+my $api_key = "";
+
+# Get the top-level resources
+my $json = get( $rest_url . "?apikey=" . $api_key );
+die "Could not get $rest_url!" unless defined $json;
+my $resources = decode_json( $json );
+
+# Follow the links to the ontologies
+my $ont_link = $resources->{'links'}->{'ontologies'};
+
+# Get the ontologies
+my $ont_json = get( $ont_link . "?apikey=" . $api_key );
+die "Could not get $ont_link!" unless defined $ont_json;
+my @ontologies = @{ decode_json( $ont_json ) };
+
+foreach my $ont (@ontologies) {
+ print $ont->{'name'} . " (" . $ont->{'acronym'} . ")\n";
+}

0 comments on commit 582c821

Please sign in to comment.