| 1 |
=head1 LICENSE |
| 2 |
|
| 3 |
Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 4 |
|
| 5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
you may not use this file except in compliance with the License. |
| 7 |
You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software |
| 12 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
See the License for the specific language governing permissions and |
| 15 |
limitations under the License. |
| 16 |
|
| 17 |
=cut |
| 18 |
|
| 19 |
package Bio::DB::HTS::Tabix::Iterator; |
| 20 |
|
| 21 |
use Bio::DB::HTS; #load the XS |
| 22 |
$Bio::DB::HTS::Tabix::Iterator::VERSION = '3.02'; |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
use Scalar::Util qw/reftype/; |
| 27 |
|
| 28 |
#this class is just a wrapper around the tabix_iter_next method, |
| 29 |
#all the attributes it needs come from the main Tabix method |
| 30 |
|
| 31 |
sub new { |
| 32 |
my $class = shift; |
| 33 |
my (%args) = @_; |
| 34 |
my $tabix_iter = $args{_tabix_iter}; #a hts_itr_t pointer which is returned from Tabix::query |
| 35 |
my $htsfile = $args{_htsfile}; #an open htsFile pointer |
| 36 |
my $tabix_index = $args{_tabix_index}; |
| 37 |
|
| 38 |
my $self = bless { |
| 39 |
_tabix_iter => $tabix_iter, |
| 40 |
_htsfile => $htsfile, |
| 41 |
_tabix_index => $tabix_index, |
| 42 |
}, ref $class || $class; |
| 43 |
|
| 44 |
return $self; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
sub next { |
| 50 |
my $self = shift; |
| 51 |
|
| 52 |
#sometimes tabix_query doesn't return an iterator, just NULL so we have to allow |
| 53 |
#a null iterator |
| 54 |
return unless defined $self->{_tabix_iter}; |
| 55 |
|
| 56 |
#this is an xs method |
| 57 |
return tbx_iter_next($self->{_tabix_iter}, $self->{_htsfile}, $self->{_tabix_index}); |
| 58 |
} |
| 59 |
|
| 60 |
sub close { |
| 61 |
my $self = shift; |
| 62 |
|
| 63 |
#xs method |
| 64 |
if ( defined $self->{_tabix_iter} ) { |
| 65 |
tbx_iter_free($self->{_tabix_iter}); |
| 66 |
delete $self->{_tabix_iter}; # delete once you've removed it. Prevents bad re-issuing of code |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
sub DESTROY { |
| 71 |
my $self = shift; |
| 72 |
return if reftype($self) ne 'HASH'; |
| 73 |
$self->close(); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
1; |
| 78 |
|
| 79 |
__END__ |
| 80 |
|
| 81 |
=head1 NAME |
| 82 |
|
| 83 |
Bio::DB::HTS::Tabix::Iterator - XS module wrapping around a tabix hts_itr_t |
| 84 |
|
| 85 |
=head1 SYNOPSIS |
| 86 |
|
| 87 |
You shouldn't be instantiating one of these manually it needs a load of pointers. |
| 88 |
Usage would be through L<Bio::DB::HTS::Tabix>: |
| 89 |
|
| 90 |
use feature qw( say ); |
| 91 |
use Bio::DB::HTS::Tabix; |
| 92 |
|
| 93 |
my $tabix = Bio::DB::HTS::Tabix->new( filename => "gerp_plus_plus_31July2014.gz" ); |
| 94 |
|
| 95 |
say $tabix->header; |
| 96 |
my $iter = $tabix->query("1:4000005-4000009"); |
| 97 |
|
| 98 |
while ( my $n = $iter->next ) { |
| 99 |
say $n; |
| 100 |
} |
| 101 |
|
| 102 |
=head1 DESCRIPTION |
| 103 |
|
| 104 |
This is returned from L<Bio::DB::HTS::Tabix>, the only method you need to care about is 'next'. |
| 105 |
|
| 106 |
Don't go importing this and calling new on it if you value your sanity, it won't work. |
| 107 |
|
| 108 |
=head2 Methods |
| 109 |
|
| 110 |
=over 12 |
| 111 |
|
| 112 |
=item C<next> |
| 113 |
|
| 114 |
Returns a string with the line from the tabix iterator |
| 115 |
|
| 116 |
=back |
| 117 |
|
| 118 |
=head1 AUTHOR |
| 119 |
|
| 120 |
Alex Hodgkins |
| 121 |
Rishi Nag E<lt>[email protected]<gt> |
| 122 |
|
| 123 |
=cut |