File Coverage

blib/lib/Data/Babel/Client.pm
Criterion Covered Total %
statement 60 60 100.0
branch 14 18 77.7
condition 4 6 66.6
subroutine 15 15 100.0
pod 2 2 100.0
total 95 101 94.0


line stmt bran cond sub pod time code
1             package Data::Babel::Client;
2              
3 5     5   579593 use warnings;
  5         9  
  5         159  
4 5     5   23 use strict;
  5         7  
  5         145  
5              
6 5     5   21 use vars qw($VERSION);
  5         12  
  5         198  
7             $VERSION = '0.02';
8              
9 5     5   25 use Carp;
  5         6  
  5         331  
10 5     5   25 use Data::Dumper;
  5         8  
  5         214  
11 5     5   4963 use LWP::UserAgent;
  5         295367  
  5         194  
12 5     5   5118 use HTTP::Request::Common;
  5         11785  
  5         470  
13 5     5   1547 use JSON;
  5         26828  
  5         46  
14              
15 5     5   1115 use vars qw(@AUTO_ATTRIBUTES @CLASS_ATTRIBUTES %DEFAULTS @EXPORT_OK);
  5         10  
  5         376  
16              
17 5     5   6404 use Class::AutoClass qw(:all);
  5         109071  
  5         163  
18 5     5   42 use base qw(Class::AutoClass);
  5         12  
  5         2944  
19             @AUTO_ATTRIBUTES=qw(ua base_url);
20             %DEFAULTS=(base_url=>'http://babel.gdxbase.org/cgi-bin/translate.cgi',
21             ua=>LWP::UserAgent->new
22             );
23              
24             Class::AutoClass::declare(__PACKAGE__);
25              
26             sub _init_self {
27 4     4   7237 my($self,$class,$args)=@_;
28 4 50       23 return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this
29              
30             # object initialization goes here as needed
31 4         13 $self;
32             }
33              
34              
35             # get the available idtypes
36             # returns results as perl array
37             sub idtypes {
38 5     5 1 14005 my ($self)=@_;
39 5         28 my %args=(request_type=>'idtypes',
40             output_format=>'json');
41            
42 5         24 my $content=$self->_fetch(%args);
43 5         401 my $table=decode_json($content);
44            
45 5 100       55 wantarray? @$table:$table;
46             }
47              
48              
49             # request translations
50             # return results as perl array
51             sub translate {
52 12     12 1 131666 my ($self,%argHash)=@_;
53 12         102 my %args=(request_type=>'translate', output_format=>'json');
54 12         38 delete @argHash{keys %args}; # override values
55 12 50       78 @args{keys %argHash}=values %argHash if %argHash;
56              
57             # check for missing args:
58 12         28 my @missing_args;
59 12         37 my @required_args=qw(request_type input_type output_types output_format);
60 12 100       31 push @missing_args, grep /\w/, map {$args{$_}? '' : $_} @required_args;
  48         155  
61 12 100 66     96 push @missing_args, "input_ids or input_ids_all" unless $args{input_ids} || $args{input_ids_all};
62 12 100       64 die sprintf("translate: missing args: %s\n", join(', ',@missing_args)) if @missing_args;
63 9 100 66     92 die sprintf("translate: cannot request both input_ids and input_ids_all") if $args{input_ids} && $args{input_ids_all};
64              
65 8         37 my $json=$self->_fetch(%args);
66 8         8012 my $table=decode_json($json);
67              
68 8 50       189 wantarray? @$table:$table;
69             }
70              
71             sub _fetch {
72 13     13   49 my ($self,%args)=@_;
73 13         472 my $req=POST($self->base_url,\%args);
74 13         39229 my $res=$self->ua->request($req);
75              
76 13 50       10755931 die sprintf("babel webservice error: %s\n",$res->status_line)
77             unless $res->is_success;
78 13         280 $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<< >>
206              
207             =head1 BUGS
208              
209             Please report any bugs or feature requests to C, or through
210             the web interface at L. 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
230              
231             =item * AnnoCPAN: Annotated CPAN documentation
232              
233             L
234              
235             =item * CPAN Ratings
236              
237             L
238              
239             =item * Search CPAN
240              
241             L
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             1; # End of Data::Babel::Client