| 1 | package Data::Babel::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use vars qw(@AUTO_ATTRIBUTES @CLASS_ATTRIBUTES %DEFAULTS @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Class::AutoClass qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use base qw(Class::AutoClass); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | @AUTO_ATTRIBUTES=qw(ua base_url); | |||||||||||||||||||||||||||
| 20 | 5 | %DEFAULTS=(base_url=>'http://babel.gdxbase.org/cgi-bin/translate.cgi', | |||||||||||||||||||||||||||
| 21 | ua=>LWP::UserAgent->new | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | Class::AutoClass::declare(__PACKAGE__); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub _init_self { | ||||||||||||||||||||||||||
|
Subroutine
_init_self: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 27 | 4 | my($self,$class,$args)=@_; | |||||||||||||||||||||||||||
| 28 | 4 | ▶ | return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # object initialization goes here as needed | ||||||||||||||||||||||||||||
| 31 | 4 | $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # get the available idtypes | ||||||||||||||||||||||||||||
| 36 | # returns results as perl array | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | sub idtypes { | ||||||||||||||||||||||||||
|
Subroutine
idtypes: called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 2 | my ($self)=@_; | |||||||||||||||||||||||||||
| 39 | 2 | my %args=(request_type=>'idtypes', | |||||||||||||||||||||||||||
| 40 | output_format=>'json'); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | my $content=$self->_fetch(%args); | |||||||||||||||||||||||||||
| 43 | 0 | my $table=decode_json($content); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | wantarray? @$table:$table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # request translations | ||||||||||||||||||||||||||||
| 50 | # return results as perl array | ||||||||||||||||||||||||||||
| 51 | 4 | ▶ | sub translate { | ||||||||||||||||||||||||||
|
Subroutine
translate: called CC=9 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 4 | my ($self,%argHash)=@_; | |||||||||||||||||||||||||||
| 53 | 4 | my %args=(request_type=>'translate', output_format=>'json'); | |||||||||||||||||||||||||||
| 54 | 4 | delete @argHash{keys %args}; # override values | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | @args{keys %argHash}=values %argHash if %argHash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # check for missing args: | ||||||||||||||||||||||||||||
| 58 | 4 | my @missing_args; | |||||||||||||||||||||||||||
| 59 | 4 | my @required_args=qw(request_type input_type output_types output_format); | |||||||||||||||||||||||||||
| 60 | 4 | ▶ | push @missing_args, grep /\w/, map {$args{$_}? '' : $_} @required_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | push @missing_args, "input_ids or input_ids_all" unless $args{input_ids} || $args{input_ids_all}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"input_ids"} or $args{"input_ids_all"}
MC/DC50%1/2
$args{"input_ids"} or $args{"input_ids_all"}
$args{"input_ids"}
$args{"input_ids_all"}
Truth table66%2/3
A or B
A= $args{"input_ids"}B= $args{"input_ids_all"}
| |||||||||||||||||||||||||||||
| 62 | 4 | ▶ | die sprintf("translate: missing args: %s\n", join(', ',@missing_args)) if @missing_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | die sprintf("translate: cannot request both input_ids and input_ids_all") if $args{input_ids} && $args{input_ids_all}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args{"input_ids"} and $args{"input_ids_all"}
MC/DC0%0/2
$args{"input_ids"} and $args{"input_ids_all"}
$args{"input_ids"}
$args{"input_ids_all"}
Truth table33%1/3
A and B
A= $args{"input_ids"}B= $args{"input_ids_all"}
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | my $json=$self->_fetch(%args); | |||||||||||||||||||||||||||
| 66 | 0 | my $table=decode_json($json); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | wantarray? @$table:$table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub _fetch { | ||||||||||||||||||||||||||
|
Subroutine
_fetch: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 72 | 3 | my ($self,%args)=@_; | |||||||||||||||||||||||||||
| 73 | 3 | my $req=POST($self->base_url,\%args); | |||||||||||||||||||||||||||
| 74 | 3 | my $res=$self->ua->request($req); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | die sprintf("babel webservice error: %s\n",$res->status_line) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | unless $res->is_success; | ||||||||||||||||||||||||||||
| 78 | 0 | $res->content; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 NAME | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Data::Babel::Client - A Client to access the Babel web service. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 VERSION | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Version 0.02 | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | use Data::Babel::Client; | ||||||||||||||||||||||||||||
| 95 | my $bc=new BabelClient; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Get a list of valid id types: each element is a two-element array containing the literal type and an English description | ||||||||||||||||||||||||||||
| 98 | my @idtypes=$bc->idtypes; | ||||||||||||||||||||||||||||
| 99 | my %idtypes=map {($_->[0],$_->[1])} @idtypes; # convert to hash form | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Translate some Entrez gene ids to various types; $table contains an array of arrays | ||||||||||||||||||||||||||||
| 102 | my %args=(input_type=>'gene_entrez', | ||||||||||||||||||||||||||||
| 103 | input_ids=>[2983,1829,589,20383,293883], | ||||||||||||||||||||||||||||
| 104 | output_types=>[qw(protein_ensembl peptide_pepatlas reaction_ec function_go gene_symbol_synonym)]); | ||||||||||||||||||||||||||||
| 105 | my $table=$bc->translate(%args); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 Description | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | BabelClient.pm provides access to the babel web service. The Babel web service provides | ||||||||||||||||||||||||||||
| 112 | translations between biological identifiers of various types. For example, given a list | ||||||||||||||||||||||||||||
| 113 | of Entrez gene ids, it can provide the corresponding Ensembl gene ids, UniProt | ||||||||||||||||||||||||||||
| 114 | protien ids, and so forth. The full list of available identifiers, and accompanying | ||||||||||||||||||||||||||||
| 115 | English descriptions, can be obtained by making a call to the 'idtypes' method. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | This web service client provides two calls, idtypes() and translate(). Both of these calls mimic | ||||||||||||||||||||||||||||
| 118 | calls of the same names found in Data::Babel, which also provides full documentation. It is intended | ||||||||||||||||||||||||||||
| 119 | that the API's of the corresponding calls be exactly the same. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | The main method is 'translate'. It makes a request to the web service to translate identifiers. | ||||||||||||||||||||||||||||
| 122 | The parameters to translate are: | ||||||||||||||||||||||||||||
| 123 | - input_type: a string describing the type of the input identifiers. This type must match | ||||||||||||||||||||||||||||
| 124 | exactly with one of the values returned by the 'idtypes' method. | ||||||||||||||||||||||||||||
| 125 | - input_ids: a listref containing the actual identifiers to be translated. | ||||||||||||||||||||||||||||
| 126 | - output_types: a listref containing the output types desired, ie, the translations from | ||||||||||||||||||||||||||||
| 127 | 'input_type'. For example, if you have a list of Uniprot ids and you | ||||||||||||||||||||||||||||
| 128 | would like to know what are the corresponding Ensembl gene ids, gene symbols, | ||||||||||||||||||||||||||||
| 129 | and associated OMIM numbers, you would pass the list [qw(gene_ensembl gene_symbol function_omim)] | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | As mentioned, the method 'idtypes' provides a list of all valid id types for use in the 'translate' method | ||||||||||||||||||||||||||||
| 132 | (for both 'input_type' and 'output_type'). The 'idtypes' method takes no parameters. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | NOTE: not all translations are 1-to-1. Many of the translations will return more than one output value | ||||||||||||||||||||||||||||
| 135 | for a given input value. For example, there are multiple Affymetrix probeset ids for many genes. | ||||||||||||||||||||||||||||
| 136 | In this case, there will be one row for each unique combination of return values, so if there were | ||||||||||||||||||||||||||||
| 137 | six Affymetrix probe ids for a given Entrez gene id, there would be six rows in the returned array | ||||||||||||||||||||||||||||
| 138 | for that Entrez gene id, with the value for the Entrez gene id repeated in each row. Were you to | ||||||||||||||||||||||||||||
| 139 | request two non-unique translations to a call to 'translate', the returned array would contain all | ||||||||||||||||||||||||||||
| 140 | the different combinations of values, one on each row. In this way the returned array can grow in | ||||||||||||||||||||||||||||
| 141 | size so as to overwhelm the capabilities of the server, the web, and so forth, and caution must be used | ||||||||||||||||||||||||||||
| 142 | in making requests to the server. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 Location of the web service: | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | The current URL for the web service is http://babel.gdxbase.org/cgi-bin/translate.cgi. It | ||||||||||||||||||||||||||||
| 147 | is encoded into this client, but can be overridden by passing the named argument 'base_url' | ||||||||||||||||||||||||||||
| 148 | into the constructor, as in: | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my $bc=new BabelClient(base_url=>'http://some.other.url'); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | This assumes, of course, that there is another instance of the web service at the location | ||||||||||||||||||||||||||||
| 153 | mentioned. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 translate() | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | $table=translate(\%args) | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | %args: | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head3 inputs_ids: | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | an arrayref containing the ids you want translated. They must all be | ||||||||||||||||||||||||||||
| 170 | of the type specified by $args{input_types}. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head3 input_type: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | a string describing the type of the inputs. Must be one of the known | ||||||||||||||||||||||||||||
| 175 | values. For a list of known (legal) values, use the idtypes() | ||||||||||||||||||||||||||||
| 176 | function. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head3 output_types | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | a list (ARRAY ref) of desired output types. Similarly to | ||||||||||||||||||||||||||||
| 181 | $args{input_type}, each value must be one of | ||||||||||||||||||||||||||||
| 182 | the known (legal) types, as obtained by a call to idtypes(). | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head3 return value | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | translate() returns an array (or arrayref) to a table of translated | ||||||||||||||||||||||||||||
| 187 | values. Each row in the table contains one column for the | ||||||||||||||||||||||||||||
| 188 | passed-in input, and one column for each output_type desired, in | ||||||||||||||||||||||||||||
| 189 | the order that they were passed in. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 idtypes() | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | $table=idtypes() | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | idtypes() takes no arguments. It returns a table containing a list of | ||||||||||||||||||||||||||||
| 197 | all the legal identifier types. Each row in the table is a | ||||||||||||||||||||||||||||
| 198 | 2-element arrayref; the first element is the actual idtype, and | ||||||||||||||||||||||||||||
| 199 | the second element is a short description of the idtype. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Victor Cassen, C<< <vcassen at systemsbiology.org> >> | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head1 BUGS | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Please report any bugs or feature requests to C<bug-data-babel-client at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 210 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Babel-Client>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 211 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | perldoc Data::Babel::Client | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | You can also look for information at: | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =over 4 | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Babel-Client> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | L<http://annocpan.org/dist/Data-Babel-Client> | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | L<http://cpanratings.perl.org/d/Data-Babel-Client> | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | L<http://search.cpan.org/dist/Data-Babel-Client/> | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =back | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Copyright 2010 Victor Cassen. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 254 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 255 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =cut | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 5 | 1; # End of Data::Babel::Client | |||||||||||||||||||||||||||