From 582c821351ec1fb54eb9cad3e90b32640de2a9d9 Mon Sep 17 00:00:00 2001 From: Paul R Alexander Date: Wed, 1 Oct 2014 16:06:56 -0700 Subject: [PATCH] Perl REST client example code --- perl/annotate_text.pl | 30 ++++++++++++++++++++++++++++++ perl/classes_search.pl | 28 ++++++++++++++++++++++++++++ perl/get_labels.pl | 47 +++++++++++++++++++++++++++++++++++++++++++++++ perl/list_ontologies.pl | 27 +++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100755 perl/annotate_text.pl create mode 100755 perl/classes_search.pl create mode 100755 perl/get_labels.pl create mode 100755 perl/list_ontologies.pl diff --git a/perl/annotate_text.pl b/perl/annotate_text.pl new file mode 100755 index 0000000..b38ccd0 --- /dev/null +++ b/perl/annotate_text.pl @@ -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"; +} diff --git a/perl/classes_search.pl b/perl/classes_search.pl new file mode 100755 index 0000000..da66b0a --- /dev/null +++ b/perl/classes_search.pl @@ -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"; +} diff --git a/perl/get_labels.pl b/perl/get_labels.pl new file mode 100755 index 0000000..9618b95 --- /dev/null +++ b/perl/get_labels.pl @@ -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"; +} + diff --git a/perl/list_ontologies.pl b/perl/list_ontologies.pl new file mode 100755 index 0000000..1ac1917 --- /dev/null +++ b/perl/list_ontologies.pl @@ -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"; +}