Debian Codegraph @

lib/Bio/DB/HTS/Tabix.pm

raw
1
2 =head1 LICENSE
3
4 Copyright [2015-2024] EMBL-European Bioinformatics Institute
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::DB::HTS::Tabix;
21
22 use Bio::DB::HTS;
23 use Bio::DB::HTS::Tabix::Iterator;
24 $Bio::DB::HTS::Tabix::VERSION = '3.02';
25 use strict;
26 use warnings;
27
28 use Scalar::Util qw/reftype/;
29 use File::Spec;
30 use Cwd;
31
32 sub new {
33 my $class = shift;
34 my (%args) = @_;
35 my $filename = $args{filename};
36 my $warnings = $args{warnings};
37 my $use_tmp_dir = $args{use_tmp_dir};
38
39 # filename checks
40 die "Tabix region lookup requires a gzipped, tabixed bedfile" unless $filename =~ /gz$/;
41 my $pdir = getcwd;
42 my $tmpdir = File::Spec->tmpdir;
43
44 #change to the tmp dir to allow the C library to download remote index files
45 if( Bio::DB::HTSfile->is_remote($filename) && ( !defined $use_tmp_dir || $use_tmp_dir != 0) )
46 {
47 chdir($tmpdir);
48 }
49
50 my $htsfile = Bio::DB::HTSfile->open($filename);
51 my $tabix_index = tbx_open($filename);
52 # use Data::Dumper; print $tabix_index; die;
53 die "Couldn't find index for file " . $filename unless $tabix_index;
54 my $header = tbx_header($htsfile, $tabix_index);
55 if( $header ) {
56 $header = join "\n", @{ $header } ;
57 } else {
58 $header = '';
59 }
60
61 chdir($pdir);
62
63 my $self = bless {
64 _htsfile => $htsfile,
65 _filename => $filename,
66 _tabix_index => $tabix_index,
67 _header => $header,
68 }, ref $class || $class;
69
70 return $self;
71 }
72
73 sub query_full {
74 my ($self, $chr, $start, $end) = @_;
75 my $region = $chr;
76 $region .= ':'; #trailing ':' required for odd chr containing ':'
77 if(defined $start) {
78 $region .= $start;
79 $region .= '-'.$end if(defined $end);
80 }
81
82 my $iter = tbx_query( $self->{_tabix_index}, $region );
83 unless ( $iter ) {
84 # this likely means the chromosome wasn't found in the tabix index, or it couldn't parse the provided region.
85 my $seqnames_hash = { map { $_ => 1 } @{ $self->seqnames } };
86
87 if ( not exists $seqnames_hash->{ $chr } ) {
88 # warn("Specified chromosome '$chr' does not exist in file " . $self->{_filename});
89 return undef;
90 } else {
91 die "Unable to get iterator for region '$region' in file ". $self->{_filename} . " -- htslib couldn't parse your region string";
92 }
93 }
94
95 return Bio::DB::HTS::Tabix::Iterator->new( _tabix_iter => $iter, _htsfile => $self->{_htsfile}, _tabix_index => $self->{_tabix_index} );
96 }
97
98 sub query {
99 my ( $self, $region ) = @_;
100
101 die "Please provide a region" unless defined $region;
102 my ( $chr, $start, $end ) = $region =~ /^([^:]+)(?::(\d+))?(?:-(\d+))?$/;
103 unless ( defined $chr ) {
104 die "You must specify a region in the format chr, chr:start or chr:start-end";
105 }
106
107 if ( defined $end ) {
108 die "End in $region is less than the start" if $end < $start;
109 }
110 else {
111 if ( defined $start ) {
112 #$self->log->warn("You have not specified an end, which actually means chr:start-end_of_chromosome");
113 }
114 }
115
116 return $self->query_full($chr, $start, $end);
117 }
118
119 sub seqnames {
120 my $self = shift;
121 return tbx_seqnames($self->{_tabix_index});
122 }
123
124 sub header {
125 my $self = shift;
126 return $self->{_header};
127 }
128
129 sub header_array {
130 my $self = shift;
131 if( $self->{_header} )
132 {
133 my @lines = split(/\n/,$self->{_header});
134 return @lines ;
135 }
136 return ;
137 }
138
139
140 #free up memory allocated in XS code
141 sub close {
142 my $self = shift;
143
144 if ( $self->{_tabix_index} ) {
145 tbx_close($self->{_tabix_index});
146 delete $self->{_tabix_index};
147 }
148 }
149
150 sub DESTROY {
151 my $self = shift;
152 return if reftype($self) ne 'HASH';
153 $self->close();
154 return;
155 }
156
157
158 1;
159
160 __END__
161
162 =head1 NAME
163
164 Bio::DB::HTS::Tabix - Object oriented access to the underlying tbx C methods
165
166 =head1 SYNOPSIS
167
168 use feature qw( say );
169 use Bio::DB::HTS::Tabix;
170
171 my $tabix = Bio::DB::HTS::Tabix->new( filename => "gerp_plus_plus_31July2014.gz" );
172
173 say $tabix->header;
174 my $iter = $tabix->query("1:4000005-4000009");
175
176 while ( my $n = $iter->next ) {
177 say $n;
178 }
179 $tabix->close;
180
181 =head1 DESCRIPTION
182
183 A high level object oriented interface to the htslib tabix (tbx.h) api. Currently it only supports
184 retrieving regions from a tabixed file, because that's all I needed it for.
185
186 =head2 Attributes
187
188 =over 12
189
190 =item C<filename>
191
192 The gzipped file you want to query. Must have a filename.tbi (the index is not created automatically)
193
194 =back
195
196 =head2 Methods
197
198 =over 12
199
200 =item C<header>
201
202 Returns all the header lines as a single scalar from the tabixed file
203
204 =item C<query>
205
206 Takes a single region like: '1:4000005-4000009' or '12:5000000'. The coordinate format is 0 or 1-based for start and stop positions depending on how the Tabix index file was created - by default this is 1.
207
208 The index for remote files will be downloaded to the system temporary directory by default. Set use_tmp_dir to 0 to download to the current working directory instead.
209
210
211 Here are some examples showing Tabix.
212
213 use Bio::DB::HTS::Tabix;
214
215 my $tabix = Bio::DB::HTS::Tabix->new(filename => $file, use_tmp_dir => 1);
216
217 # Calling region 1
218
219 $iter = $tabix->query("1");
220 printf("Calling region 1\n" );
221 while(my $l = $iter->next) {
222 print $l, "\n";
223 }
224 Gives:
225 1 4000000 4000000 -0.972
226 1 4000001 4000001 -0.153
227 1 4000002 4000002 -2.15
228 1 4000003 4000003 -1.17
229 1 4000003 4000006 -3.6
230 1 4000006 4000007 -6.7
231 1 4000007 4000009 -7.9
232
233
234 #Calling a range
235 $iter = $tabix->query("1:4000003-4000006");
236 gives
237 1 4000003 4000003 -1.17
238 1 4000003 4000006 -3.6
239 1 4000006 4000007 -6.7
240
241 #Calling region 1:4000003 to end of region 1
242 $iter = $tabix->query("1:4000003");
243 gives
244 1 4000003 4000003 -1.17
245 1 4000003 4000006 -3.6
246 1 4000006 4000007 -6.7
247 1 4000007 4000009 -7.9
248
249 #Calling single location 1:4000002
250 $iter = $tabix->query("1:4000002-4000002");
251 gives
252 1 4000002 4000002 -2.15
253
254
255 Returns a L<Bio::DB::HTS::Tabix::Iterator> for the specified region
256
257 =item C<seqnames>
258
259 Returns an array ref of chromosomes that are in the indexed file
260
261 =back
262
263 =head1 AUTHOR
264
265 Alex Hodgkins
266 Rishi Nag E<lt>[email protected]<gt>
267
268 =cut