| 1 | # $Id$ | ||
| 2 | # | ||
| 3 | # BioPerl module for Bio::DB::SoapEUtilities::LinkAdaptor | ||
| 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::LinkAdaptor - Handle for Entrez SOAP LinkSets | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | my $fac = Bio::DB::SoapEUtilities->new(); | ||
| 22 | # run a query, returning a LinkAdaptor | ||
| 23 | $fac->elink( -db => 'nucleotide', | ||
| 24 | -dbfrom => 'protein', | ||
| 25 | -id => [qw(828392 790 470338)]); | ||
| 26 | my $links = $fac->elink->run( -auto_adapt => 1); | ||
| 27 | # get the linked ids corresponding to the submitted ids | ||
| 28 | # (may be arrays if multiple crossrefs, or undef if none) | ||
| 29 | my @nucids = $links->id_map(828392); | ||
| 30 | # iterate over linksets | ||
| 31 | while ( my $ls = $links->next_linkset ) { | ||
| 32 | my @from_ids = $ls->submitted_ids; | ||
| 33 | my @to_ids = $ls->ids; | ||
| 34 | my $from_db = $ls->db_from; | ||
| 35 | my $to_db = $ls->db_to; | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This adaptor provides an iterator (C<next_linkset()>) and other | ||
| 41 | convenience functions for parsing NCBI Entrez EUtility C<elink> | ||
| 42 | SOAP results. | ||
| 43 | |||
| 44 | =head1 SEE ALSO | ||
| 45 | |||
| 46 | L<Bio::DB::SoapEUtilities> | ||
| 47 | |||
| 48 | =head1 FEEDBACK | ||
| 49 | |||
| 50 | =head2 Mailing Lists | ||
| 51 | |||
| 52 | User feedback is an integral part of the evolution of this and other | ||
| 53 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 54 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 55 | |||
| 56 | bioperl-l@bioperl.org - General discussion | ||
| 57 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 58 | |||
| 59 | =head2 Support | ||
| 60 | |||
| 61 | Please direct usage questions or support issues to the mailing list: | ||
| 62 | |||
| 63 | L<bioperl-l@bioperl.org> | ||
| 64 | |||
| 65 | rather than to the module maintainer directly. Many experienced and | ||
| 66 | reponsive experts will be able look at the problem and quickly | ||
| 67 | address it. Please include a thorough description of the problem | ||
| 68 | with code and data examples if at all possible. | ||
| 69 | |||
| 70 | =head2 Reporting Bugs | ||
| 71 | |||
| 72 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 73 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 74 | the web: | ||
| 75 | |||
| 76 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 77 | |||
| 78 | =head1 AUTHOR - Mark A. Jensen | ||
| 79 | |||
| 80 | Email maj -at- fortinbras -dot- us | ||
| 81 | |||
| 82 | =head1 APPENDIX | ||
| 83 | |||
| 84 | The rest of the documentation details each of the object methods. | ||
| 85 | Internal methods are usually preceded with a _ | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | # Let the code begin... | ||
| 90 | |||
| 91 | package Bio::DB::SoapEUtilities::LinkAdaptor; | ||
| 92 | use strict; | ||
| 93 | use warnings; | ||
| 94 | |||
| 95 | # Object preamble - inherits from Bio::Root::Root | ||
| 96 | |||
| 97 | use Bio::Root::Root; | ||
| 98 | |||
| 99 | use base qw(Bio::Root::Root ); | ||
| 100 | |||
| 101 | =head2 new | ||
| 102 | |||
| 103 | Title : new | ||
| 104 | Usage : my $obj = new Bio::DB::SoapEUtilities::LinkAdaptor(); | ||
| 105 | Function: Builds a new Bio::DB::SoapEUtilities::LinkAdaptor object | ||
| 106 | Returns : an instance of Bio::DB::SoapEUtilities::LinkAdaptor | ||
| 107 | Args : | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub new { | ||
| 112 | my ($class,@args) = @_; | ||
| 113 | my $self = $class->SUPER::new(@args); | ||
| 114 | my ($result) = $self->_rearrange([qw(RESULT)], @args); | ||
| 115 | $self->throw("LinkAdaptor requires a SoapEUtilities::Result argument") | ||
| 116 | unless $result; | ||
| 117 | $self->throw("LinkAdaptor only works with elink results") unless | ||
| 118 | $result->util eq 'elink'; | ||
| 119 | $self->{'_result'} = $result; | ||
| 120 | $self->{'_idx'} = 1; | ||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub result { shift->{'_result'} } | ||
| 125 | |||
| 126 | =head2 next_linkset() | ||
| 127 | |||
| 128 | Title : next_linkset | ||
| 129 | Usage : | ||
| 130 | Function: return the next LinkSet from the attached Result | ||
| 131 | Returns : | ||
| 132 | Args : | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub next_linkset { | ||
| 137 | my $self = shift; | ||
| 138 | my $stem = "//Body/".$self->result->result_type."/[".$self->{'_idx'}."]"; | ||
| 139 | return unless $self->result->som and $self->result->som->valueof($stem); | ||
| 140 | my $som = $self->result->som; | ||
| 141 | my ($ret, %params); | ||
| 142 | my $get = sub { $som->valueof("$stem/".shift) }; | ||
| 143 | |||
| 144 | $params{'-db_from'} = $get->('DbFrom'); | ||
| 145 | $params{'-db_to'} = $get->('LinkSetDb/DbTo'); | ||
| 146 | $params{'-link_name'} = $get->('LinkSetDb/LinkName'); | ||
| 147 | $params{'-submitted_ids'} = [$get->('IdList/*')]; | ||
| 148 | $params{'-ids'} = [$get->('LinkSetDb/Link/*')]; | ||
| 149 | $params{'-webenv'} = $get->('WebEnv'); | ||
| 150 | my $class = ref($self)."::linkset"; | ||
| 151 | $ret = $class->new(%params); | ||
| 152 | ($self->{'_idx'})++; | ||
| 153 | return $ret; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub next_obj { shift->next_linkset(@_) } | ||
| 157 | |||
| 158 | sub rewind { shift->{'_idx'} = 1; }; | ||
| 159 | |||
| 160 | =head2 id_map() | ||
| 161 | |||
| 162 | Title : id_map | ||
| 163 | Usage : $to_id = $adaptor->id_map($from_id) | ||
| 164 | Function: Return 'to-database' ids corresponding to | ||
| 165 | given specified 'from-database' or | ||
| 166 | submitted ids | ||
| 167 | Returns : array of scalars (to-database ids or arrayrefs of ids) | ||
| 168 | Args : array of scalars (from-database ids) | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub id_map { | ||
| 173 | my $self = shift; | ||
| 174 | my @from_ids = @_; | ||
| 175 | my $som = $self->result->som; | ||
| 176 | my $stem = "//Body/".$self->result->result_type."/"; | ||
| 177 | if (!defined $self->{'_id_map'}) { | ||
| 178 | my $h = {}; | ||
| 179 | for (my $i=1; $som->valueof($stem."[$i]"); $i++) { | ||
| 180 | # note this assumes that in the elink query, | ||
| 181 | # ids were provided individually (not as a comma-sep | ||
| 182 | # list). This is the standard behavior for elink | ||
| 183 | # in SoapEUtilities. | ||
| 184 | my @to_ids = $som->valueof($stem."[$i]/LinkSetDb/Link/*"); | ||
| 185 | $$h{$som->valueof($stem."[$i]/IdList/[1]")} = | ||
| 186 | (@to_ids == 1 ? $to_ids[0] : \@to_ids); | ||
| 187 | } | ||
| 188 | $self->{'_id_map'} = $h; | ||
| 189 | } | ||
| 190 | return @{$self->{'_id_map'}}{@from_ids}; | ||
| 191 | } | ||
| 192 | |||
| 193 | package Bio::DB::SoapEUtilities::LinkAdaptor::linkset; | ||
| 194 | use strict; | ||
| 195 | use warnings; | ||
| 196 | |||
| 197 | use base qw(Bio::Root::Root); | ||
| 198 | |||
| 199 | sub new { | ||
| 200 | my ($class, @args) = @_; | ||
| 201 | my $self = $class->SUPER::new(@args); | ||
| 202 | my %args = @args; | ||
| 203 | $self->_set_from_args( \%args, | ||
| 204 | -methods => [map { /^-?(.*)/ } keys %args], | ||
| 205 | -create => 1, | ||
| 206 | -code => | ||
| 207 | 'my $self = shift; | ||
| 208 | my $d = shift; | ||
| 209 | my $k = \'_\'.$method; | ||
| 210 | $self->{$k} = $d if $d; | ||
| 211 | return (ref $self->{$k} eq \'ARRAY\') ? | ||
| 212 | @{$self->{$k}} : $self->{$k};' | ||
| 213 | |||
| 214 | ); | ||
| 215 | return $self; | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; |