Seq/Fetch.pm

filter: Statementstmt 98.0% 147 / 150 Branchbran 81.4% 56 / 70 (1 uncoverable) Conditioncond 88.0% 22 / 25 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 90.9% 250 / 276 (1 uncoverable) CC 45 SCAR 25.5
CC
45
Cov
92%
fetch_seq
19.4
write_index
16.1
new
7.0
CRAP 12.8
↑ Summary Seq/Stream.pm »
1 package BioX::Seq::Fetch;
2  
3 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use BioX::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use constant MAGIC_GZIP => pack('C3', (0x1f, 0x8b, 0x08));
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
13  
14 10 my ($class,$fn,%args) = @_;
15  
16 10 ▶ die "Must define input filename for random access"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn 1 9 10
17 if (! defined $fn);
18  
19 9 my $self = bless {fn => $fn}, $class;
20  
21 9 ▶ open my $fh, '<', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $fn or die "Error opening $fn for reading: $!\n" 8 1 9
22 or die "Error opening $fn for reading: $!\n";
23  
24 # read magic bytes and reset filehandle
25 8 my $old_layers = join '', map {":$_"} PerlIO::get_layers($fh);
26 8 binmode($fh);
27 8 read( $fh, my $magic_start, 3 );
28 8 read( $fh, my $magic_end, 1 );
29 8 binmode($fh,$old_layers);
30 8 seek($fh,0,0);
31  
32 # detect compression (must be BGZIP)
33 8 ▶ if ($magic_start eq MAGIC_GZIP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic_start eq "\37\213\b") 3 5 8
34 3 ▶ die "Gzip files must be compressed with bgzip for non-sequential access"
Branch: 1 branch
BranchTrueFalseTotal
if ord $magic_end != 4 1 2 3
35 if (ord($magic_end) != 0x04);
36 # TODO: find way to test missing library during unit testing
37 require Compress::BGZF::Reader
38 2 ▶ or die "Compress::BGZF::Reader not installed, no gzip support";
Branch: 1 branch
BranchTrueFalseTotal
require Compress::BGZF::Reader or die "Compress::BGZF::Reader not installed, no gzip support" 2 0 2
39 2 close $fh;
40 2 $fh = Compress::BGZF::Reader->new_filehandle($fn);
41 }
42  
43 7 ▶ $self->{with_description} = 1 if ($args{with_description});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"with_description"} 1 6 7
44  
45 7 $self->{fh} = $fh;
46  
47 7 $self->_load_faidx;
48  
49 3 return $self;
50  
51 }
52  
53 8 ▶sub write_index {
Subroutine write_index: called CC=16 SCAR=27.8
Pod documented
54
55 8 my ($self, $fn_idx) = @_;
56  
57 8 ▶ $fn_idx //= $self->{fn} . '.fai';
Condition100%2/2
$fn_idx //= $self->{"fn"} . '.fai'
l!l
17
MC/DC100%1/1
$fn_idx //= $self->{"fn"} . '.fai'
$fn_idx
Truth table100%2/2
A
A= $fn_idx
Aresult
11
00
58  
59 8 ▶ if (-e $fn_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fn_idx) 0 8 8
60 0 warn "Index file exists, won't overwrite\n";
61 0 return 0;
62 }
63  
64 8 ▶ open my $idx, '>:raw', $fn_idx
Branch: 1 branch
BranchTrueFalseTotal
open my $idx, ">:raw", $fn_idx or die "Error opening $fn_idx for writing: $!\n" 8 0 8
65 or die "Error opening $fn_idx for writing: $!\n";
66  
67 8 my $fh = $self->{fh};
68  
69 8 my $curr_id;
70 my $curr_len;
71 8 my $curr_bases;
72 8 my $curr_bytes;
73 8 my $curr_offset;
74 8 my $bl_mismatch = 0;
75 8 my $ll_mismatch = 0;
76 8 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 65 5 70
77 65 ▶ if ($line =~ /^>(\S+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^>(\S+)/u) { } 25 40 65
elsif ($line =~ /^[A-Za-z\-\*\.]+(\r?\n?)$/u) { } 37 3 40
elsif ($line =~ /\S/u) { } 1 2 3
78 25 ▶ if (defined $curr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $curr_id) 17 8 25
79 17 say {$idx} join "\t",
80 $curr_id,
81 $curr_len,
82 $curr_offset,
83 $curr_bases,
84 $curr_bytes,
85 ;
86 }
87 25 $curr_id = $1;
88 25 $curr_offset = tell $fh;
89 25 $curr_len = 0;
90 25 $curr_bases = undef;
91 25 $curr_bytes = undef;
92 25 $bl_mismatch = 0;
93 25 $ll_mismatch = 0;
94 }
95 elsif ($line =~ /^[A-Za-z\-\*\.]+(\r?\n?)$/) {
96 37 ▶ if ($bl_mismatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bl_mismatch) 1 36 37
97 1 close $idx;
98 1 unlink $fn_idx;
99 1 die "Base length mismatch\n";
100 }
101 36 ▶ if ($ll_mismatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ll_mismatch) 1 35 36
102 1 close $idx;
103 1 unlink $fn_idx;
104 1 die "Line length mismatch\n";
105 }
106 35 my $ll = length $line;
107 35 my $bl = $ll - length $1;
108 35 $curr_len += $bl;
109 35 ▶ $curr_bases //= $bl;
Condition66%2/3
$curr_bases //= $bl
l!l&&r!l&&!r
10250
MC/DC0%0/2
$curr_bases //= $bl
$curr_bases $bl
Truth table66%2/3
A //= B
A= $curr_basesB= $bl
ABresult
1X1
011
000
110 35 ▶ $curr_bytes //= $ll;
Condition66%2/3
$curr_bytes //= $ll
l!l&&r!l&&!r
10250
MC/DC0%0/2
$curr_bytes //= $ll
$curr_bytes $ll
Truth table66%2/3
A //= B
A= $curr_bytesB= $ll
ABresult
1X1
011
000
111 35 ▶ $bl_mismatch = 1 if ($bl != $curr_bases);
Branch: 1 branch
BranchTrueFalseTotal
if $bl != $curr_bases 9 26 35
112 35 ▶ $ll_mismatch = 1 if ($ll != $curr_bytes);
Branch: 1 branch
BranchTrueFalseTotal
if $ll != $curr_bytes 10 25 35
113 }
114 elsif ($line =~ /\S/) {
115 1 close $idx;
116 1 unlink $fn_idx;
117 1 die "Unexpected content: $line";
118 }
119
120 }
121  
122 # write remaining index
123 # uncoverable branch false
124 5 ▶ if (defined $curr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $curr_id) 5 0 5
125 5 say {$idx} join "\t",
126 $curr_id,
127 $curr_len,
128 $curr_offset,
129 $curr_bases,
130 $curr_bytes,
131 ;
132 }
133  
134 5 close $idx;
135  
136 #reset filehandle
137 5 seek $self->{fh}, 0, 0;
138  
139 5 return 1;
140  
141 }
142
143  
144 7 ▶sub _load_faidx {
Subroutine _load_faidx: called CC=6 SCAR=18.1
145
146 7 my ($self) = @_;
147  
148 7 my $fn_idx = $self->{fn} . '.fai';
149 7 ▶ $self->write_index if (! -e $fn_idx);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $fn_idx 7 0 7
150  
151 # make sure FASTA file is not newer than its index; otherwise index file
152 # may be out of date and cause silent corruption downstream
153 4 my $mtime_fas = stat($self->{fn})->mtime;
154 4 my $mtime_idx = stat($fn_idx)->mtime;
155 4 ▶ if ($mtime_fas > $mtime_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime_fas > $mtime_idx) 0 4 4
156 0 die "Index file exists but is older than FASTA file and probably out"
157 . " of date. Refresh or remove the existing index before"
158 . " proceeding.\n"
159 }
160  
161 4 my @ids;
162  
163 4 ▶ open my $in, '<', $fn_idx
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $fn_idx or die "Error opening index file: $!\n" 4 0 4
164 or die "Error opening index file: $!\n";
165 4 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 15 3 18
166 15 chomp $line;
167 15 my ($name, $len, $offset, $bases_per_line, $bytes_per_line)
168 = split "\t", $line;
169 die "ERROR: $fn_idx contains duplicate entries"
170 15 ▶ if ( defined $self->{idx}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"idx"}{$name} 1 14 15
171 14 my $eol = $bytes_per_line - $bases_per_line;
172 14 $self->{idx}->{$name} = [$len, $offset, $bases_per_line, $eol];
173 14 push @ids, $name;
174 }
175 3 close $in;
176 3 $self->{ids} = \@ids;
177  
178 3 return;
179  
180 }
181  
182 1 ▶sub ids {
Subroutine ids: called CC=1 SCAR=0.0
Pod documented
183  
184 1 my ($self) = @_;
185 1 return @{ $self->{ids} };
186  
187 }
188  
189 2 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
190  
191 2 my ($self, $id) = @_;
192 2 return $self->{idx}->{$id}->[0];
193  
194 }
195  
196 11 ▶sub fetch_seq {
Subroutine fetch_seq: called CC=19 SCAR=29.6
Pod documented
197
198 11 my ($self, $id, $start_bp, $end_bp) = @_;
199  
200 11 ▶ return if (! defined $self->{idx}->{$id});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"idx"}{$id} 0 11 11
201 11 my ($len, $off, $bpl, $eol) = @{ $self->{idx}->{$id} };
202  
203 11 my $fh = $self->{fh};
204  
205 11 ▶ $start_bp = $start_bp // 1;
Condition100%2/2
$start_bp // 1
l!l
74
MC/DC100%1/1
$start_bp // 1
$start_bp
Truth table100%2/2
A
A= $start_bp
Aresult
11
00
206 11 ▶ $end_bp = $end_bp // $len;
Condition66%2/3
$end_bp // $len
l!l&&r!l&&!r
740
MC/DC0%0/2
$end_bp // $len
$end_bp $len
Truth table66%2/3
A // B
A= $end_bpB= $len
ABresult
1X1
011
000
207 11 ▶ die "Coordinates out of bounds" if (
Branch: 1 branch
BranchTrueFalseTotal
if $start_bp < 1 or $end_bp < 1 or $start_bp > $end_bp or $end_bp > $len 5 6 11
Condition100%9/9
$start_bp < 1 or $end_bp < 1 or $start_bp > $end_bp or $end_bp > $len
l!l&&r!l&&!r
416
$start_bp < 1 or $end_bp < 1 or $start_bp > $end_bp
l!l&&r!l&&!r
317
$start_bp < 1 or $end_bp < 1
l!l&&r!l&&!r
218
MC/DC100%4/4
$start_bp < 1 or $end_bp < 1 or $start_bp > $end_bp or $end_bp > $len
$start_bp < 1 $end_bp < 1 $start_bp > $end_bp $end_bp > $len
Truth table100%5/5
A or B or C or D
A= $start_bp < 1B= $end_bp < 1C= $start_bp > $end_bpD= $end_bp > $len
ABCDresult
1XXX1
01XX1
001X1
00011
00000
208 $start_bp < 1 || $end_bp < 1 || $start_bp > $end_bp || $end_bp > $len
209 );
210 6 --$start_bp; #make 0-based
211 6 my $l = $end_bp - $start_bp;
212  
213 6 seek $fh, $off + $start_bp + int(($start_bp)/$bpl)*$eol, 0;
214 6 read($fh, my $seq, $l + int(($l+$start_bp%$bpl)/$bpl)*$eol);
215 6 $seq =~ s/\s//g;
216  
217 6 ++$start_bp;
218  
219 6 my $desc;
220 6 ▶ if ($start_bp > 1 || $end_bp < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_bp > 1 or $end_bp < $len) 2 4 6
Condition100%3/3
$start_bp > 1 or $end_bp < $len
l!l&&r!l&&!r
114
MC/DC100%2/2
$start_bp > 1 or $end_bp < $len
$start_bp > 1 $end_bp < $len
Truth table100%3/3
A or B
A= $start_bp > 1B= $end_bp < $len
ABresult
1X1
011
000
221 2 $desc = "($start_bp-$end_bp)";
222 }
223  
224 # backtrack to find defline, if asked
225 6 ▶ if ($self->{with_description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"with_description"}) 1 5 6
226  
227 1 my $string = '';
228 1 my $p = $off;
229 1 my $chr;
230  
231 BACK:
232 1 ▶ while ($p > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > 0) 65 0 65
233 65 --$p;
234 65 seek $fh, $p, 0;
235 65 ▶ read($fh, $chr, 1)
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $chr, 1 or die "Error during backtrack read: $@\n" 65 0 65
236 or die "Error during backtrack read: $@\n";
237 # stop at record separator
238 65 ▶ if ($chr eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chr eq ">") 2 63 65
239 2 ▶ last BACK if ($p == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $p == 0 0 2 2
240 2 my $prev;
241 2 seek $fh, $p-1, 0;
242 2 read($fh, $prev, 1);
243 2 ▶ last BACK if ($prev =~ /[\r\n]/);
Branch: 1 branch
BranchTrueFalseTotal
if $prev =~ /[\r\n]/u 1 1 2
244 }
245  
246 64 $string = $chr . $string;
247 }
248  
249 1 ▶ die "Backtracking inexplicably failed\n"
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $string 0 1 1
250 if (! CORE::length $string);
251 1 ▶ if ($string =~ /^(\S+)(?:\s+(.*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(\S+)(?:\s+(.*))?$/u) 1 0 1
252 1 my $nid = $1;
253 1 $desc = join ' ', grep {defined $_} $2, $desc;
254 1 ▶ die "ID mismatch ($id vs $nid)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if $nid ne $id 0 1 1
255 if ($nid ne $id);
256 }
257
258 }
259  
260 6 return BioX::Seq->new($seq, $id, $desc);
261  
262 }
263  
264  
265 2 1;
266  
267  
268 __END__
↑ Summary Seq/Stream.pm »