File Coverage

blib/lib/Data/Babel/Client.pm
Criterion Covered Total %
statement 52 57 91.2
branch 9 18 50.0
condition 3 6 50.0
subroutine 14 14 100.0
pod 2 2 100.0
total 80 97 82.4


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