EnsEMBL/DBSQL/FastaSequenceAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 85 CC 5 SCAR 15.2
CC
5
Cov
0%
fetch_by_Slice_start_end_strand
12.0
faindex
6.0
can_access_Slice
6.0
CRAP 4.6
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 =head1 CONTACT
19  
20 Please email comments or questions to the public Ensembl
21 developers list at <dev@ensembl.org>.
22  
23 Questions may also be sent to the Ensembl help desk at
24 <helpdesk@ensembl.org>.
25  
26 =cut
27  
28 =head1 NAME
29  
30 Bio::EnsEMBL::DBSQL::FastaSequenceAdaptor
31  
32 =head1 DESCRIPTION
33  
34 This sequence adaptor extends the BaseSequenceAdaptor code to provide an
35 implementation of the .fai index lookup as defined by samtools. The code uses
36 this indexing system to access portions of sequence and translates Slice requests
37 into sensible locations for our FASTA query layer.
38  
39 The adaptor must be initalised with access to a Faidx compatible object and the FASTA
40 file backing must use the same seq_region_name as the querying slices otherwise
41 we cannot return the required data.
42  
43 =cut
44  
45 package Bio::EnsEMBL::DBSQL::FastaSequenceAdaptor;
46 $Bio::EnsEMBL::DBSQL::FastaSequenceAdaptor::VERSION = '114.0.0_50'; # TRIAL
47 $Bio::EnsEMBL::DBSQL::FastaSequenceAdaptor::VERSION = '114.0.050';
48 use strict;
49 use warnings;
50  
51 use base qw/Bio::EnsEMBL::DBSQL::BaseSequenceAdaptor/;
52  
53 use Bio::EnsEMBL::Utils::Exception qw/throw/;
54 use Bio::EnsEMBL::Utils::Scalar qw/assert_ref/;
55 use Bio::EnsEMBL::Utils::Sequence qw/reverse_comp/;
56 use English qw/-no_match_vars/;
57 require bytes;
58  
59 =head2 new
60
61 Arg [1] : FileFaidx; $faindex. A FileFaidx object or a compatible version
62 Arg [2] : Integer; $chunk_power. Size of the region to cache
63 Arg [3] : Integer; $cache_size. Number of regions to cache
64 Description : Builds an instance of the FastaSequenceAdaptor
65  
66 =cut
67  
68 sub new {
69 my ($class, $faindex, $chunk_power, $cache_size) = @_;
70 my $self = $class->SUPER::new($chunk_power, $cache_size);
71 $self->faindex($faindex);
72 return $self;
73 }
74  
75 =head2 fetch_by_Slice_start_end_strand
76
77 Arg [1] : Bio::EnsEMBL::Slice; $slice. Slice to fetch sequence for
78 Arg [2] : Integer; $start. Start of region to retrieve relative to the Slice (defaults to 1)
79 Arg [3] : Integer; $end. End of region to retreive relative to the Slice (defaults to length)
80 Arg [4] : Integer; $strand. Strand to fetch (defaults to 1)
81 Description : Fetches sequence for the given slice. Unlike the normal SequenceAdaptor we assume
82 Sequence is held in a FASTA file under the Slice's seq_region_name.
83 Exception : Thrown if we are given a circular slice
84 =cut
85  
86 sub fetch_by_Slice_start_end_strand {
87 my ( $self, $slice, $start, $end, $strand ) = @_;
88
89 # Input checks
90 assert_ref($slice, 'Bio::EnsEMBL::Slice', 'slice');
91 if(defined $end && $start > $end && $slice->is_circular()) {
92 throw "Currently we do not support circular requests";
93 }
94
95 #Get a new slice that spans the exact region to retrieve dna from.
96 #Then constrain to seq region if it's gone negative or over the end
97 $slice = $self->expand_Slice($slice, $start, $end, $strand);
98 $slice = $slice->constrain_to_seq_region();
99
100 # This call is likely to barf if we try to query using a chr name
101 # we do not understand. Use can_access_Slice() to make sure
102 my $seq_ref = $self->_fetch_seq($slice->seq_region_name(), $slice->start(), $slice->length());
103 reverse_comp($seq_ref) if $strand == -1;
104 return $seq_ref;
105 }
106  
107 =head2 faindex
108  
109 Description : Holds a reference to the Faindex object to use for sequence access
110  
111 =cut
112  
113 sub faindex {
114 my ($self, $faindex) = @_;
115 if(defined $faindex) {
116 assert_ref($faindex, 'Bio::EnsEMBL::Utils::IO::FileFaidx', 'faidx');
117 $self->{faindex} = $faindex;
118 }
119 return $self->{faindex};
120 }
121  
122 =head2 can_access_Slice
123  
124 Description : Checks the lookup to see if we have access to the Slice given (using
125 seq region name as the ID). We reject any Circular Slice
126  
127 =cut
128  
129 sub can_access_Slice {
130 my ($self, $slice) = @_;
131 return 0 if $slice->is_circular();
132 return $self->faindex()->can_access_id($slice->seq_region_name());
133 }
134  
135 =head2 store
136
137 Description : Unsupported operation. Please use a FASTA serialiser
138  
139 =cut
140  
141 sub store {
142 throw "Unsupported operation. Cannot store sequence in a fasta file";
143 }
144  
145 =head _fetch_raw_seq
146  
147 Description : Provides access to the underlying faindex object and returns a sequence scalar ref
148  
149 =cut
150  
151 sub _fetch_raw_seq {
152 my ($self, $id, $start, $length) = @_;
153 my $seq_ref = $self->faindex()->fetch_seq($id, $start, $length);
154 return $seq_ref;
155 }
156  
157 1;