ExtractMappings

From NCBO Wiki
Jump to: navigation, search

Extract Mappings - Sample code

#!/usr/bin/perl

#############################################################
# DESCRIPTION: Code to extract mapping data from 
# BioPortal(http://bioportal.bioontology.org). 
#
# NOTE: The Web services used in this code are prototype
# services and will change. Subscribe to the bioportal-announce
# mailing list (https://mailman.stanford.edu/mailman/listinfo/bioportal-announce)
# to be notified of changes in this Web service. 
#
# USAGE: perl extract_mappings.pl > outputfile.txt
#
# AUTHOR: Trish Whetzel (support@bioontology.org)
# DATE: Tue Oct 27 18:55:06 PDT 2009
#############################################################

use LWP::UserAgent;
use XML::LibXML;
use strict;
use warnings;

# Declare variables
my ($ontology_id, $term_id, $term, $ontology, $dest_ontology_id, $dest_ontology, $dest_term_id, $dest_term, $user_id);

# Main method
get_mappings();

# Subroutines
sub get_mappings {
	my $ua = new LWP::UserAgent;

	# make request
	# NOTE: ontology id is hard-coded, the ontology id can be found from the web interface ontology metadata page or 
	# the web service http://rest.bioontology.org/bioportal/ontologies?email=example@example.org
	my $req =  new HTTP::Request GET => 'http://bioportal.bioontology.org/mappings/service/1430/?email=bioportal-mapping';  #1023
	$req->content_type('application/x-www-form-urlencoded');

	# send request and get response.
	my $res = $ua->request($req);

	# If you want to get a large result. It is better to write to a file directly.
	# my $res = $ua->request($req,'file_name.txt');

	# Check the outcome of the response
	if ( $res->is_success ) {
		print STDERR "Call successful\n";

		# read XML file
		my $parser = XML::LibXML->new();
		my $xml = $parser->parse_string( $res->decoded_content );
		my $num_of_responses = 0;	

		# print output
		foreach my $mappingBlock ( $xml->findnodes('hash/mapping-from/mapping-from') ) {
			my ($source) = $mappingBlock->findnodes('./source-ont');
			my ($ontology_name) = $mappingBlock->findnodes('./source-ont-name');
			my ($source_id) = $mappingBlock->findnodes('./source-id');
			my ($source_name) = $mappingBlock->findnodes('./source-name');
			
			my ($id) = $mappingBlock->findnodes('./user-id');
			
			my ($dest_ont) = $mappingBlock->findnodes('./destination-ont');
			my ($dest_ontology_name) = $mappingBlock->findnodes('./destination-ont-name');
			my ($dest_source_id) = $mappingBlock->findnodes('./destination-id');
			my ($dest_source_name) = $mappingBlock->findnodes('./destination-name');
			
			$ontology_id = $source->to_literal;
			$ontology = $ontology_name->to_literal;
			$term_id = $source_id->to_literal;
			$term = $source_name->to_literal;
			
			$user_id = $id->to_literal;
			
			$dest_ontology_id = $dest_ont->to_literal;
			$dest_ontology = $dest_ontology_name->to_literal;
			$dest_term_id = $dest_source_id->to_literal;
			$dest_term = $dest_source_name->to_literal;
			
			#add line to check if mapping is to a specific ontology
			my $ontology_id_of_interest = '1341'; 
			my $user_of_interest = '';
			
			if ($dest_ontology_id == $ontology_id_of_interest) { #} && $user_id == $user_of_interest) {  #use for mapping-from
			#if ($ontology_id == $ontology_id_of_interest && $user_id == $user_of_interest) { #use for mapping-to
				$num_of_responses++;
				print ("$num_of_responses: S: $ontology_id, D: $dest_ontology_id; S: $ontology, D: $dest_ontology; S: $term_id, D: $dest_term_id; S: $term, D: $dest_term \n");
			}
			# For testing script
			else {
				#print STDERR ("**Not mapped to ontol of interest: $ontology_id_of_interest\t - $ontology_id, $ontology, $term_id, $term, $dest_ontology_id, $dest_ontology, $dest_term_id, $dest_term \n");
			}
		}
	}
}


Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox