| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | package Bio::EnsEMBL::Utils::Iterator; | ||
| 21 | $Bio::EnsEMBL::Utils::Iterator::VERSION = '114.0.0_50'; # TRIAL | ||
| 22 | $Bio::EnsEMBL::Utils::Iterator::VERSION = '114.0.050'; | ||
| 23 | |||
| 24 | =head1 CONTACT | ||
| 25 | |||
| 26 | Please email comments or questions to the public Ensembl | ||
| 27 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 28 | |||
| 29 | Questions may also be sent to the Ensembl help desk at | ||
| 30 | <http://www.ensembl.org/Help/Contact>. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Bio::EnsEMBL::Utils::Iterator | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | my $variation_iterator = | ||
| 41 | $variation_adaptor->fetch_Iterator_by_VariationSet($1kg_set); | ||
| 42 | |||
| 43 | while ( my $variation = $variation_iterator->next ) { | ||
| 44 | # operate on variation object | ||
| 45 | print $variation->name, "\n"; | ||
| 46 | } | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | Some adaptor methods may return more objects than can fit in memory at once, in these cases | ||
| 51 | you can fetch an iterator object instead of the usual array reference. The iterator object | ||
| 52 | allows you to iterate over the set of objects (using the next() method) without loading the | ||
| 53 | entire set into memory at once. You can tell if an iterator is exhausted with the has_next() | ||
| 54 | method. The peek() method allows you to fetch the next object from the iterator without | ||
| 55 | advancing the iterator - this is useful if you want to check some property of en element in | ||
| 56 | the set while leaving the iterator unchanged. | ||
| 57 | |||
| 58 | You can filter and transform an iterator in an analogous way to using map and grep on arrays | ||
| 59 | using the provided map() and grep() methods. These methods return another iterator, and only | ||
| 60 | perform the filtering and transformation on each element as it is requested, so again these | ||
| 61 | can be used without loading the entire set into memory. | ||
| 62 | |||
| 63 | Iterators can be combined together with the append() method which merges together the | ||
| 64 | iterator it is called on with the list of iterators passed in as arguments. This is | ||
| 65 | somewhat analogous to concatenating arrays with the push function. append() returns a new | ||
| 66 | iterator which iterates over each component iterator until it is exhausted before moving | ||
| 67 | on to the next iterator, in the order in which they are supplied to the method. | ||
| 68 | |||
| 69 | An iterator can be converted to an array (reference) containing all the elements in the | ||
| 70 | set with the to_arrayref() method, but note that this array may consume a lot of memory if | ||
| 71 | the set the iterator is iterating over is large and it is recommended that you do not call | ||
| 72 | this method unless there is no way of working with each element at a time. | ||
| 73 | |||
| 74 | =head1 METHODS | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | use strict; | ||
| 79 | use warnings; | ||
| 80 | |||
| 81 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 82 | |||
| 83 | =head2 new | ||
| 84 | |||
| 85 | Argument : either a coderef representing the iterator, in which case this | ||
| 86 | anonymous subroutine is assumed to return the next object in the | ||
| 87 | set when called and to return undef when the set is exhausted, | ||
| 88 | or an arrayref, in which case we return an iterator over this | ||
| 89 | array. If the argument is not defined then we return an 'empty' | ||
| 90 | iterator that immediately returns undef | ||
| 91 | |||
| 92 | Example : | ||
| 93 | |||
| 94 | my @dbIDs = fetch_relevant_dbIDs(); | ||
| 95 | |||
| 96 | my $iterator = Bio::EnsEMBL::Utils::Iterator->new( | ||
| 97 | sub { return $self->fetch_by_dbID(shift @dbIDs) } | ||
| 98 | ); | ||
| 99 | |||
| 100 | NB: this is a very simple example showing how to call the constructor | ||
| 101 | that would be rather inefficient in practice, real examples should | ||
| 102 | probably be smarter about batching up queries to minimise trips to | ||
| 103 | the database. See examples in the Variation API. | ||
| 104 | |||
| 105 | Description: Constructor, creates a new iterator object | ||
| 106 | Returntype : Bio::EnsEMBL::Utils::Iterator instance | ||
| 107 | Exceptions : thrown if the supplied argument is not the expected | ||
| 108 | Caller : general | ||
| 109 | Status : Experimental | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub new { | ||
| 114 | my $class = shift; | ||
| 115 | |||
| 116 | my $arg = shift; | ||
| 117 | |||
| 118 | my $coderef; | ||
| 119 | |||
| 120 | if (not defined $arg) { | ||
| 121 | # if the user doesn't supply an argument, we create a | ||
| 122 | # simple 'empty' iterator that immediately returns undef | ||
| 123 | |||
| 124 | $coderef = sub { return undef }; | ||
| 125 | } | ||
| 126 | elsif (ref $arg eq 'ARRAY') { | ||
| 127 | # if the user supplies an arrayref as an argument, we | ||
| 128 | # create an iterator over this array | ||
| 129 | |||
| 130 | $coderef = sub { return shift @$arg }; | ||
| 131 | } | ||
| 132 | elsif (ref $arg eq 'CODE'){ | ||
| 133 | $coderef = $arg; | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | throw("The supplied argument does not look like an arrayref or a coderef ".(ref $arg)) | ||
| 137 | } | ||
| 138 | |||
| 139 | my $self = {sub => $coderef}; | ||
| 140 | |||
| 141 | return bless $self, $class; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | =head2 _fetch_next_value_if_needed | ||
| 146 | |||
| 147 | Example : $iterator->_fetch_next_value_if_needed | ||
| 148 | Description: Helper method used to fill the internal buffer with the next value of the iterator | ||
| 149 | Returntype : none | ||
| 150 | Exceptions : none | ||
| 151 | Caller : general | ||
| 152 | Status : Experimental | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub _fetch_next_value_if_needed { | ||
| 157 | my $self = shift; | ||
| 158 | |||
| 159 | unless (exists $self->{next}) { | ||
| 160 | $self->{next} = $self->{sub}->(); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | =head2 next | ||
| 166 | |||
| 167 | Example : $obj = $iterator->next | ||
| 168 | Description: returns the next object from this iterator, or undef if the iterator is exhausted | ||
| 169 | Returntype : Object type will depend on what this iterator is iterating over | ||
| 170 | Exceptions : none | ||
| 171 | Caller : general | ||
| 172 | Status : Experimental | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub next { | ||
| 177 | my $self = shift; | ||
| 178 | |||
| 179 | $self->_fetch_next_value_if_needed(); | ||
| 180 | |||
| 181 | if (defined $self->{next}) { | ||
| 182 | return delete $self->{next}; | ||
| 183 | } | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 has_next | ||
| 188 | |||
| 189 | Example : if ($iterator->has_next) { my $obj = $iterator->next } | ||
| 190 | Description: Boolean - true if this iterator has more elements to fetch, false when | ||
| 191 | it is exhausted | ||
| 192 | Returntype : boolean | ||
| 193 | Exceptions : none | ||
| 194 | Caller : general | ||
| 195 | Status : Experimental | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub has_next { | ||
| 200 | my $self = shift; | ||
| 201 | |||
| 202 | $self->_fetch_next_value_if_needed(); | ||
| 203 | |||
| 204 | return defined $self->{next}; | ||
| 205 | } | ||
| 206 | |||
| 207 | =head2 peek | ||
| 208 | |||
| 209 | Example : $obj = $iterator->peek | ||
| 210 | Description: returns the next object from this iterator, or undef if the iterator is exhausted, | ||
| 211 | much like next but does not advance the iterator (so the same object will be | ||
| 212 | returned on the following call to next or peek) | ||
| 213 | Returntype : Object type will depend on what this iterator is iterating over | ||
| 214 | Exceptions : none | ||
| 215 | Caller : general | ||
| 216 | Status : Experimental | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub peek { | ||
| 221 | my $self = shift; | ||
| 222 | |||
| 223 | $self->_fetch_next_value_if_needed(); | ||
| 224 | |||
| 225 | return $self->{next}; | ||
| 226 | } | ||
| 227 | |||
| 228 | =head2 grep | ||
| 229 | |||
| 230 | Example : my $filtered_iterator = $original_iterator->grep(sub {$_->name =~ /^rs/}); | ||
| 231 | Description: filter this iterator, returning another iterator | ||
| 232 | Argument : a coderef which returns true if the element should be included in the | ||
| 233 | filtered set, or false if the element should be filtered out. $_ will be | ||
| 234 | set locally to each element in turn so you should be able to write a block | ||
| 235 | in a similar way as for the perl grep function (although it will need to be | ||
| 236 | preceded with the sub keyword). Otherwise you can pass in a reference to a | ||
| 237 | subroutine which expects a single argument with the same behaviour. | ||
| 238 | Returntype : Bio::EnsEMBL::Utils::Iterator | ||
| 239 | Exceptions : thrown if the argument is not a coderef | ||
| 240 | Caller : general | ||
| 241 | Status : Experimental | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub grep { | ||
| 246 | my ($self, $coderef) = @_; | ||
| 247 | |||
| 248 | throw('Argument should be a coderef') unless ref $coderef eq 'CODE'; | ||
| 249 | |||
| 250 | return Bio::EnsEMBL::Utils::Iterator->new(sub { | ||
| 251 | while ($self->has_next) { | ||
| 252 | local $_ = $self->next; | ||
| 253 | return $_ if $coderef->($_); | ||
| 254 | } | ||
| 255 | return undef; | ||
| 256 | }); | ||
| 257 | } | ||
| 258 | |||
| 259 | =head2 map | ||
| 260 | |||
| 261 | Example : my $transformed_iterator = $original_iterator->map(sub {$_->name}); | ||
| 262 | Description: transform the elements of this iterator, returning another iterator | ||
| 263 | Argument : a coderef which returns the desired transformation of each element. | ||
| 264 | $_ will be set locally set to each original element in turn so you | ||
| 265 | should be able to write a block in a similar way as for the perl map | ||
| 266 | function (although it will need to be preceded with the sub keyword). | ||
| 267 | Otherwise you can pass in a reference to a subroutine which expects a | ||
| 268 | single argument with the same behaviour. | ||
| 269 | Returntype : Bio::EnsEMBL::Utils::Iterator | ||
| 270 | Exceptions : thrown if the argument is not a coderef | ||
| 271 | Caller : general | ||
| 272 | Status : Experimental | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | |||
| 277 | sub map { | ||
| 278 | my ($self, $coderef) = @_; | ||
| 279 | |||
| 280 | throw('Argument should be a coderef') unless ref $coderef eq 'CODE'; | ||
| 281 | |||
| 282 | return Bio::EnsEMBL::Utils::Iterator->new(sub { | ||
| 283 | local $_ = $self->next; | ||
| 284 | return defined $_ ? $coderef->($_) : undef; | ||
| 285 | }); | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | =head2 each | ||
| 290 | |||
| 291 | Example : $iterator->each(sub { print $_->name, "\n"; }); | ||
| 292 | Description: Performs a full iteration of the current iterator instance. | ||
| 293 | Argument : a coderef which returns the desired transformation of each element. | ||
| 294 | $_ will be set locally set to each element. | ||
| 295 | Returntype : None | ||
| 296 | Exceptions : thrown if the argument is not a coderef | ||
| 297 | Caller : general | ||
| 298 | Status : Experimental | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | |||
| 303 | sub each { | ||
| 304 | my ($self, $coderef) = @_; | ||
| 305 | throw('Argument should be a coderef') unless ref $coderef eq 'CODE'; | ||
| 306 | while($self->has_next()) { | ||
| 307 | local $_ = $self->next(); | ||
| 308 | $coderef->($_); | ||
| 309 | } | ||
| 310 | return; | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | =head2 to_arrayref | ||
| 315 | |||
| 316 | Example : my $arrayref = $iterator->to_arrayref; | ||
| 317 | Description: return a reference to an array containing all elements from the | ||
| 318 | iterator. This is created by simply iterating over the iterator | ||
| 319 | until it is exhausted and adding each element in turn to an array. | ||
| 320 | Note that this may consume a lot of memory for iterators over | ||
| 321 | large collections | ||
| 322 | Returntype : arrayref | ||
| 323 | Exceptions : none | ||
| 324 | Caller : general | ||
| 325 | Status : Experimental | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub to_arrayref { | ||
| 330 | my ($self) = @_; | ||
| 331 | |||
| 332 | my @array; | ||
| 333 | |||
| 334 | while ($self->has_next) { | ||
| 335 | push @array, $self->next; | ||
| 336 | } | ||
| 337 | |||
| 338 | return \@array; | ||
| 339 | } | ||
| 340 | |||
| 341 | =head2 append | ||
| 342 | |||
| 343 | Example : my $combined_iterator = $iterator1->append($iterator2, $iterator3); | ||
| 344 | Description: return a new iterator that combines this iterator with the others | ||
| 345 | passed as arguments, this new iterator will iterate over each | ||
| 346 | component iterator (in the order supplied here) until it is | ||
| 347 | exhausted and then move on to the next iterator until all are | ||
| 348 | exhausted | ||
| 349 | Argument : an array of Bio::EnsEMBL::Utils::Iterator objects | ||
| 350 | Returntype : Bio::EnsEMBL::Utils::Iterator | ||
| 351 | Exceptions : thrown if any of the arguments are not iterators | ||
| 352 | Caller : general | ||
| 353 | Status : Experimental | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | sub append { | ||
| 358 | my ($self, @queue) = @_; | ||
| 359 | |||
| 360 | for my $iterator (@queue) { | ||
| 361 | throw("Argument to append doesn't look like an iterator") | ||
| 362 | unless UNIVERSAL::can($iterator, 'has_next') && UNIVERSAL::can($iterator, 'next'); | ||
| 363 | } | ||
| 364 | |||
| 365 | # push ourselves onto the front of the queue | ||
| 366 | unshift @queue, $self; | ||
| 367 | |||
| 368 | return Bio::EnsEMBL::Utils::Iterator->new(sub { | ||
| 369 | # shift off any exhausted iterators | ||
| 370 | while (@queue && not $queue[0]->has_next) { | ||
| 371 | shift @queue; | ||
| 372 | } | ||
| 373 | |||
| 374 | # and return the next object from the iterator at the | ||
| 375 | # head of the queue, or undef if the queue is empty | ||
| 376 | return @queue ? $queue[0]->next : undef; | ||
| 377 | }); | ||
| 378 | } | ||
| 379 | |||
| 380 | =head2 take | ||
| 381 | |||
| 382 | Example : my $limited_iterator = $iterator->take(5); | ||
| 383 | Description: return a new iterator that only iterates over the | ||
| 384 | first n elements of this iterator | ||
| 385 | Argument : a positive integer | ||
| 386 | Returntype : Bio::EnsEMBL::Utils::Iterator | ||
| 387 | Exceptions : thrown if the argument is negative | ||
| 388 | Caller : general | ||
| 389 | Status : Experimental | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | sub take { | ||
| 394 | my ($self, $n) = @_; | ||
| 395 | |||
| 396 | throw("Argument cannot be negative") if $n < 0; | ||
| 397 | |||
| 398 | my $cnt = 0; | ||
| 399 | |||
| 400 | return Bio::EnsEMBL::Utils::Iterator->new(sub { | ||
| 401 | return $cnt++ >= $n ? undef : $self->next; | ||
| 402 | }); | ||
| 403 | } | ||
| 404 | |||
| 405 | =head2 skip | ||
| 406 | |||
| 407 | Example : my $limited_iterator = $iterator->skip(5); | ||
| 408 | Description: skip over the first n elements of this iterator (and then return | ||
| 409 | the same iterator for your method chaining convenience) | ||
| 410 | Argument : a positive integer | ||
| 411 | Returntype : Bio::EnsEMBL::Utils::Iterator | ||
| 412 | Exceptions : thrown if the argument is negative | ||
| 413 | Caller : general | ||
| 414 | Status : Experimental | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub skip { | ||
| 419 | my ($self, $n) = @_; | ||
| 420 | |||
| 421 | throw("Argument cannot be negative") if $n < 0; | ||
| 422 | |||
| 423 | $self->next for (0 .. $n-1); | ||
| 424 | |||
| 425 | return $self; | ||
| 426 | } | ||
| 427 | |||
| 428 | =head2 reduce | ||
| 429 | |||
| 430 | Example : my $tot_length = $iterator->reduce(sub { $_[0] + $_[1]->length }, 0); | ||
| 431 | Description: reduce this iterator with the provided coderef, using the (optional) | ||
| 432 | second argument as the initial value of the accumulator | ||
| 433 | Argument[1]: a coderef that expects 2 arguments, the current accumulator | ||
| 434 | value and the next element in the set, and returns the next | ||
| 435 | accumulator value. Unless the optional second argument is | ||
| 436 | provided the first accumulator value passed in will be the | ||
| 437 | first element in the set | ||
| 438 | Argument[2]: (optional) an initial value to use for the accumulator instead | ||
| 439 | of the first value of the set | ||
| 440 | Returntype : returntype of the coderef | ||
| 441 | Exceptions : thrown if the argument is not a coderef | ||
| 442 | Caller : general | ||
| 443 | Status : Experimental | ||
| 444 | |||
| 445 | =cut | ||
| 446 | |||
| 447 | sub reduce { | ||
| 448 | my ($self, $coderef, $init_val) = @_; | ||
| 449 | |||
| 450 | throw('Argument should be a coderef') unless ref $coderef eq 'CODE'; | ||
| 451 | |||
| 452 | my $result = defined $init_val ? $init_val : $self->next; | ||
| 453 | |||
| 454 | while ($self->has_next) { | ||
| 455 | $result = $coderef->($result, $self->next); | ||
| 456 | } | ||
| 457 | |||
| 458 | return $result; | ||
| 459 | } | ||
| 460 | |||
| 461 | 1; | ||
| 462 |