|
| 1 | +#!/usr/bin/perl -w |
| 2 | + |
| 3 | +## usage: perl crosswalk.pl -u your-umls-username -p your-umls-password -v source vocabulary -i source vocabulary identifier [ -r specify target vocabulary] |
| 4 | +## Performs a crosswalk using the latest UMLS version and retrieves all codes that share a UMLS CUI with a particular code. |
| 5 | +## Example: |
| 6 | +## perl crosswalk.pl -u username -p password -v HPO -i HP:0001947 |
| 7 | +## perl crosswalk.pl -u username -p password -v HPO -i HP:0001947 -r SNOMEDCT_US |
| 8 | +## More information: https://documentation.uts.nlm.nih.gov/rest/source-asserted-identifiers/crosswalk/index.html |
| 9 | + |
| 10 | +use lib "lib"; |
| 11 | +use strict; |
| 12 | +use URI; |
| 13 | +use Authentication::TicketClient; |
| 14 | +use JSON; |
| 15 | +use REST::Client; |
| 16 | +use Data::Dumper; |
| 17 | +use Getopt::Std; |
| 18 | + |
| 19 | +## parse command line arguments |
| 20 | +our ($opt_u,$opt_p,$opt_v,$opt_i, $opt_r); |
| 21 | +getopt('upvir'); |
| 22 | +my $username = $opt_u || die "please provide username"; |
| 23 | +my $password = $opt_p || die "please provide password"; |
| 24 | +my $source = $opt_v || die "Please provide a source vocabulary"; |
| 25 | +my $identifier = $opt_i || die "Please provide a source vocabulary identifier"; |
| 26 | + |
| 27 | +## Create a ticket granting ticket for the session |
| 28 | +my $ticketClient = |
| 29 | + new TicketClient(username=>$opt_u,password=>$opt_p,service=>"http://umlsks.nlm.nih.gov",tgt=>"") || die "could not create TicketClient() object"; |
| 30 | +my $tgt = $ticketClient->getTgt(); |
| 31 | +my $uri = new URI("https://uts-ws.nlm.nih.gov"); |
| 32 | +my $json; |
| 33 | +my $client = REST::Client->new(); |
| 34 | +my %parameters = (); |
| 35 | + |
| 36 | +my $path = sprintf("/rest/crosswalk/current/source/%s/%s", $opt_v, $opt_i); |
| 37 | + |
| 38 | +if ( defined $opt_r && $opt_r ){ |
| 39 | + $parameters{'targetSource'} = $opt_r |
| 40 | +} |
| 41 | + |
| 42 | +## Query the API |
| 43 | +$json = run_query($path,\%parameters); |
| 44 | +my $ra_results = $json->{'result'}; |
| 45 | + |
| 46 | +## Loop through results |
| 47 | +foreach my $result( @$ra_results ) { |
| 48 | + printf("%s %s %s\n", $result->{'rootSource'}, $result->{'ui'}, $result->{'name'} ) |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +sub format_json { |
| 53 | + my $json_in = shift; |
| 54 | + my $json = JSON->new; |
| 55 | + my $obj = $json->decode($json_in); |
| 56 | + return $obj; |
| 57 | +} |
| 58 | + |
| 59 | +sub run_query { |
| 60 | + my ($path, $parameters) = @_; |
| 61 | + $parameters{ticket} = $ticketClient->getServiceTicket(); |
| 62 | + $uri->path($path); |
| 63 | + $uri->query_form($parameters); |
| 64 | + print qq{$uri\n\n}; |
| 65 | + my $query = $client->GET($uri) || die "Could not execute query $!\n"; |
| 66 | + my $results = $query->responseCode() eq '200'? $query->responseContent: die "Could not execute query $!\n"; |
| 67 | + my $json = format_json($results); |
| 68 | + return $json; |
| 69 | +} |
0 commit comments