DB/SoapEUtilities/GQueryAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 125 CC 9 SCAR 21.2
CC
9
Cov
0%
query_by_db
20.0
new
12.0
found_in_dbs
12.0
CRAP 8.4
1 # $Id$
2 #
3 # BioPerl module for Bio::DB::SoapEUtilities::GQueryAdaptor
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Mark A. Jensen <maj -at- fortinbras -dot- us>
8 #
9 # Copyright Mark A. Jensen
10 #
11 # You may distribute this module under the same terms as perl itself
12  
13 # POD documentation - main docs before the code
14  
15 =head1 NAME
16  
17 Bio::DB::SoapEUtilities::GQueryAdaptor - Handle for Entrez SOAP GlobalQuery items
18  
19 =head1 SYNOPSIS
20  
21 my $fac = Bio::DB::SoapEUtilities->new();
22 # run a query, returning a GQueryAdaptor
23 my $queries = $fac->egquery( -term => 'BRCA and human' )->run(-auto_adapt=>1);
24 # all databases with hits
25 my @dbs = $queries->found_in_dbs;
26 # queries by database
27 my $prot_count = $queries->query_by_db('prot')->count;
28 # iterate over gquery
29 while ( my $q = $queries->next_query ) {
30 my $db = $q->db;
31 my $count = $q->count;
32 my $status = $q->status;
33 }
34  
35 =head1 DESCRIPTION
36  
37 This adaptor provides an iterator (C<next_query()>) and other
38 convenience functions for parsing NCBI Entrez EUtility C<egquery>
39 SOAP results.
40  
41 =head1 SEE ALSO
42  
43 L<Bio::DB::SoapEUtilities>
44  
45 =head1 FEEDBACK
46  
47 =head2 Mailing Lists
48  
49 User feedback is an integral part of the evolution of this and other
50 Bioperl modules. Send your comments and suggestions preferably to
51 the Bioperl mailing list. Your participation is much appreciated.
52  
53 bioperl-l@bioperl.org - General discussion
54 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
55  
56 =head2 Support
57  
58 Please direct usage questions or support issues to the mailing list:
59  
60 L<bioperl-l@bioperl.org>
61  
62 rather than to the module maintainer directly. Many experienced and
63 reponsive experts will be able look at the problem and quickly
64 address it. Please include a thorough description of the problem
65 with code and data examples if at all possible.
66  
67 =head2 Reporting Bugs
68  
69 Report bugs to the Bioperl bug tracking system to help us keep track
70 of the bugs and their resolution. Bug reports can be submitted via
71 the web:
72  
73 http://redmine.open-bio.org/projects/bioperl/
74  
75 =head1 AUTHOR - Mark A. Jensen
76  
77 Email maj -at- fortinbras -dot- us
78  
79 =head1 APPENDIX
80  
81 The rest of the documentation details each of the object methods.
82 Internal methods are usually preceded with a _
83  
84 =cut
85  
86 # Let the code begin...
87  
88 package Bio::DB::SoapEUtilities::GQueryAdaptor;
89 use strict;
90  
91 # Object preamble - inherits from Bio::Root::Root
92  
93 use Bio::Root::Root;
94  
95 use base qw(Bio::Root::Root );
96  
97 =head2 new
98  
99 Title : new
100 Usage : my $obj = new Bio::DB::SoapEUtilities::GQueryAdaptor();
101 Function: Builds a new Bio::DB::SoapEUtilities::GQueryAdaptor object
102 Returns : an instance of Bio::DB::SoapEUtilities::GQueryAdaptor
103 Args :
104  
105 =cut
106  
107 sub new {
108 my ($class,@args) = @_;
109 my $self = $class->SUPER::new(@args);
110 my ($result) = $self->_rearrange([qw(RESULT)], @args);
111 $self->throw("GQueryAdaptor requires a SoapEUtilities::Result argument")
112 unless $result;
113 $self->throw("GQueryAdaptor only works with egquery results") unless
114 $result->util eq 'egquery';
115 $self->{'_result'} = $result;
116 $self->{'_query_by_db'} = {};
117 $self->{'_idx'} = 1;
118 return $self;
119 }
120  
121 sub result { shift->{'_result'} }
122  
123 =head2 next_query()
124  
125 Title : next_query
126 Usage :
127 Function: return the next global query from the attached Result
128 Returns :
129 Args :
130  
131 =cut
132  
133 sub next_query {
134 my $self = shift;
135 # my $stem = "//Body/".$self->result->result_type."/[".$self->{'_idx'}."]";
136 # not consistent, kludge as follows:
137 my $stem = "//eGQueryResult/[".$self->{'_idx'}."]";
138 my $som = $self->result->som;
139 return unless $som->valueof($stem);
140 my ($ret, %params);
141 my $get = sub { $som->valueof("$stem/".shift) };
142 my $toplev = $get->('');
143 my $get_tl = sub { $toplev->{ shift @_ } };
144
145 $params{'-term'} = $som->valueof("//Term");
146  
147 my $names = [];
148 $params{'-count'} = $get_tl->('Count');
149 $params{'-db'} = $get_tl->('DbName');
150 $params{'-status'} = $get_tl->('Status');
151  
152 my $class = ref($self)."::gquery";
153 $ret = $class->new(%params);
154 $self->{_query_by_db}->{$params{'-db'}} = $ret;
155 ($self->{'_idx'})++;
156 return $ret;
157 }
158  
159 sub next_obj { shift->next_query(@_) }
160  
161 sub rewind { shift->{'_idx'} = 1; };
162  
163 =head2 found_in_dbs()
164  
165 Title : found_in_dbs
166 Usage :
167 Function: Return list of db names containing hits for
168 the query term
169 Returns : array of scalar strings
170 Args : none
171  
172 =cut
173  
174 sub found_in_dbs {
175 my $self = shift;
176 return @{$self->{'_found_in_dbs'}} if $self->{'_found_in_dbs'};
177 my $som = $self->result->som;
178 $self->{'_found_in_dbs'} = [];
179 foreach ($som->valueof("//eGQueryResult/*")) {
180 push @{$self->{'_found_in_dbs'}}, $_->{'DbName'} if
181 $_->{'Count'};
182 }
183 return @{$self->{'_found_in_dbs'}};
184 }
185  
186 =head2 query_by_db()
187  
188 Title : query_by_db
189 Usage :
190 Function: get gquery object by db name
191 Returns :
192 Args : db name (scalar string)
193  
194 =cut
195  
196 sub query_by_db {
197 my $self = shift;
198 my $db = shift;
199  
200 $self->throw("db must be specified") unless $db;
201 return $self->{_query_by_db}->{$db} if $self->{_query_by_db}->{$db};
202 my $som = $self->result->som;
203 my $i;
204 for ($i = 1; my $val = $som->valueof("//eGQueryResult/[$i]/DbName"); $i++) {
205 last if $val eq $db;
206 }
207 my $curidx = $self->{_idx};
208 my $query;
209 {
210 local $self->{_idx} = $i;
211 $query = $self->next_query;
212 }
213 return $query;
214 }
215
216 1;
217  
218 ####
219 package Bio::DB::SoapEUtilities::GQueryAdaptor::gquery;
220 use strict;
221 use warnings;
222  
223 use base qw(Bio::Root::Root);
224  
225 sub new {
226 my ($class, @args) = @_;
227 my $self = $class->SUPER::new(@args);
228 my %args = @args;
229 $self->_set_from_args( \%args,
230 -methods => [map { /^-?(.*)/ } keys %args],
231 -create => 1,
232 -code =>
233 'my $self = shift;
234 my $d = shift;
235 my $k = \'_\'.$method;
236 $self->{$k} = $d if $d;
237 return (ref $self->{$k} eq \'ARRAY\') ?
238 @{$self->{$k}} : $self->{$k};'
239  
240 );
241 return $self;
242 }
243
244 1;