blib/lib/Bio/Gonzales/Seq/Util.pm

filter: Statementstmt 39.1% 67 / 171 Branchbran 16.6% 11 / 66 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 45.8% 11 / 24 Podpod 18.1% 2 / 11 totaltotal 32.4% 94 / 290 CC 42 SCAR 35.4
CC
42
Cov
33%
seq_lengths
132.0
_pairwise_identity_generic
42.0
seq_apply
30.0
CRAP 34.6
« blib/lib/Bio/Gonzales/Feat/IO/GFF3.pm ↑ Summary blib/lib/Bio/Gonzales/Matrix/Util.pm »
1 package Bio::Gonzales::Seq::Util;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Bio::Gonzales::Matrix::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Which qw/which/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Bio::Gonzales::Seq::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
15 5 our $VERSION = '0.062'; # VERSION
16  
17 5 @EXPORT = qw();
18 5 %EXPORT_TAGS = ();
19 5 @EXPORT_OK = qw(
20 pairwise_identity_l
21 pairwise_identity_s
22 pairwise_identity_gaps_l
23 pairwise_identity_gaps_s
24 pairwise_identities
25 map_seqids
26 seqid_mapper
27 crc64
28 strand_convert
29 seq_lengths
30 seq_apply
31 );
32  
33 5 our %STRAND_CHAR_TABLE = (
34 '+' => 1,
35 '-' => -1,
36 '.' => 0,
37 -1 => '-',
38 1 => '+',
39 0 => '.',
40 );
41  
42 5 our $BLASTDB_CMD = which('blastdbcmd');
43 5 our $SAMTOOLS_CMD = which('samtools');
44  
45 sub strand_convert {
46 141 ▶ if ( @_ && @_ > 0 && $_[-1] && exists( $STRAND_CHAR_TABLE{ $_[-1] } ) ) {
Subroutine strand_convert: called CC=5 SCAR=22.7
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and @_ > 0 and $_[-1] and exists $Bio::Gonzales::Seq::Util::STRAND_CHAR_TABLE{$_[-1]}) { } 0 141 141
Condition33%3/9
@_ and @_ > 0 and $_[-1] and exists $Bio::Gonzales::Seq::Util::STRAND_CHAR_TABLE{$_[-1]}
!ll&&!rl&&r
14100
@_ and @_ > 0 and $_[-1]
!ll&&!rl&&r
01410
@_ and @_ > 0
!ll&&!rl&&r
00141
MC/DC0%0/4
@_ and @_ > 0 and $_[-1] and exists $Bio::Gonzales::Seq::Util::STRAND_CHAR_TABLE{$_[-1]}
@_ @_ > 0 $_[-1] exists $Bio::Gonzales::Seq::Util::STRAND_CHAR_TABLE{$_[-1]}
Truth table20%1/5
A and B and C and D
A= @_B= @_ > 0C= $_[-1]D= exists $Bio::Gonzales::Seq::Util::STRAND_CHAR_TABLE{$_[-1]}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
47 0 return $STRAND_CHAR_TABLE{ $_[-1] };
48 } else {
49 141 return '.';
50 }
51 }
52  
53 sub seq_lengths {
54 0 ▶ my $f = shift;
Subroutine seq_lengths: not called CC=11 SCAR=48.8
Pod undocumented
55  
56 0 my $d;
57 0 ▶ if ( -f $f . ".fai" ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-f $f . '.fai') { } 0 0 0
elsif (-f $f . '.nhr') { } 0 0 0
elsif ($Bio::Gonzales::Seq::Util::SAMTOOLS_CMD) { } 0 0 0
58 0 $d = mslurp( $f . ".fai", { header => undef } );
59 } elsif ( -f $f . ".nhr" ) {
60 0 ▶ open my $fh, '-|', $BLASTDB_CMD, '-db', $f, '-entry', 'all', '-outfmt', "%a\t%l"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", $Bio::Gonzales::Seq::Util::BLASTDB_CMD, "-db", $f, "-entry", "all", "-outfmt", "%a\t%l" or die "Can't open filehandle: $!" 0 0 0
61 or die "Can't open filehandle: $!";
62 0 $d = mslurp( $fh, { header => undef } );
63 0 close $fh;
64 } elsif ($SAMTOOLS_CMD) {
65 0 ▶ system( 'samtools', 'faidx', $f ) == 0 or die "system failed: $?";
Branch: 1 branch
BranchTrueFalseTotal
system("samtools", "faidx", $f) == 0 or die "system failed: $?" 0 0 0
66 0 ▶ $d = mslurp( $f . ".fai", { header => undef } ) if ( -f $f . ".fai" );
Branch: 1 branch
BranchTrueFalseTotal
if -f $f . '.fai' 0 0 0
67 }
68  
69 # nothing worked so far
70 0 ▶ unless ($d) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d) 0 0 0
71 0 say STDERR "could not use samtools or blast indices, using std method";
72 0 my @lengths;
73 0 my $fit = faiterate($f);
74 0 ▶ while ( my $seq = $fit->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = &$fit()) 0 0 0
75 0 push @lengths, [ $seq->id, $seq->length ];
76 }
77 0 $d = \@lengths;
78 }
79  
80 0 my %sl;
81 0 for my $r (@$d) {
82 0 ▶ die "double ID" if ( $sl{ $r->[0] } );
Branch: 1 branch
BranchTrueFalseTotal
if $sl{$r->[0]} 0 0 0
83 0 $sl{ $r->[0] } = $r->[1];
84 }
85  
86 0 return \%sl;
87 }
88  
89 sub seq_apply {
90 0 ▶ my ( $f, $sub, $pattern ) = @_;
Subroutine seq_apply: not called CC=5 SCAR=34.0
Pod undocumented
91  
92 0 ▶ die "file or sub ref not correct" unless ( ref $sub eq 'CODE' && -f $f );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" and -f $f 0 0 0
Condition0%0/3
ref $sub eq "CODE" and -f $f
!ll&&!rl&&r
000
MC/DC0%0/2
ref $sub eq "CODE" and -f $f
ref $sub eq "CODE" -f $f
Truth table0%0/3
A and B
A= ref $sub eq "CODE"B= -f $f
ABresult
0X0
100
111
93 0 my $seq_lengths = seq_lengths($f);
94 0 my @res;
95 0 my $num = keys %$seq_lengths;
96 0 for my $sid ( keys %$seq_lengths ) {
97 0 my $spat;
98 0 ▶ if ($pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern) 0 0 0
99 0 $spat = $pattern;
100 0 $spat =~ s/\{id\}/$sid/g;
101 0 $spat =~ s/\{begin\}/1/g;
102 0 $spat =~ s/\{end\}/$seq_lengths->{$sid}/g;
103 }
104 0 push @res, $sub->( { id => $sid, begin => 1, end => $seq_lengths->{$sid}, num => $num }, $spat );
105 }
106 0 return \@res;
107 }
108  
109 sub pairwise_identity_l {
110 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_l: not called CC=1 SCAR=6.9
Pod undocumented
111 0 return _pairwise_identity_generic( $seq1, $seq2, 1, 0 );
112 }
113  
114 sub pairwise_identity_s {
115 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_s: not called CC=1 SCAR=6.9
Pod undocumented
116 0 return _pairwise_identity_generic( $seq1, $seq2, 0, 0 );
117 }
118  
119 sub pairwise_identity_gaps_l {
120 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_gaps_l: not called CC=1 SCAR=6.9
Pod undocumented
121 0 return _pairwise_identity_generic( $seq1, $seq2, 1, 1 );
122 }
123  
124 sub _pairwise_identity_generic {
125 0 ▶ my ( $seq1, $seq2, $use_longest, $include_gaps ) = @_;
Subroutine _pairwise_identity_generic: not called CC=6 SCAR=37.4
126  
127 0 ▶ $seq1 = $seq1->seq if ( blessed $seq1);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($seq1) 0 0 0
128 0 ▶ $seq2 = $seq2->seq if ( blessed $seq2);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($seq2) 0 0 0
129  
130 0 my $seq1_gaps = 0;
131 0 my $seq2_gaps = 0;
132 0 ▶ if ( !$include_gaps ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($include_gaps) 0 0 0
133 0 $seq1_gaps = $seq1 =~ y/-/./;
134 0 $seq2_gaps = () = $seq2 =~ /-/g;
135 }
136  
137 0 my $mask = $seq1 ^ $seq2;
138 0 my $matches = $mask =~ tr/\x0/\x0/;
139  
140 0 my $longest;
141 my $shortest;
142 0 ▶ if ( length($seq2) - $seq2_gaps < length($seq1) - $seq1_gaps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($seq2) - $seq2_gaps < length($seq1) - $seq1_gaps) { } 0 0 0
143 0 $longest = length($seq1) - $seq1_gaps;
144 0 $shortest = length($seq2) - $seq2_gaps;
145 } else {
146 0 $shortest = length($seq1) - $seq1_gaps;
147 0 $longest = length($seq2) - $seq2_gaps;
148 }
149  
150 0 ▶ if ($use_longest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_longest) { } 0 0 0
151 0 return ( $matches / $longest );
152 } else {
153 0 return ( $matches / $shortest );
154 }
155 }
156  
157 sub pairwise_identity_gaps_s {
158 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_gaps_s: not called CC=1 SCAR=6.9
Pod undocumented
159  
160 0 return _pairwise_identity_generic( $seq1, $seq2, 0, 1 );
161 }
162  
163 sub pairwise_identities {
164 0 ▶ my ( $sub, @seqs ) = @_;
Subroutine pairwise_identities: not called CC=4 SCAR=30.0
Pod undocumented
165  
166 #creating an upper triangular matrix
167  
168 0 my @dist;
169 0 ▶ for ( my $i = 0; $i < @seqs; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @seqs) 0 0 0
170 0 push @dist, [];
171 }
172  
173 0 my $i;
174 0 ▶ for ( $i = 0; $i < @seqs - 1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @seqs - 1) 0 0 0
175 0 $dist[$i][$i] = 1;
176 0 ▶ for ( my $j = $i + 1; $j < @seqs; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @seqs) 0 0 0
177 0 $dist[$j][$i] = $sub->( $seqs[$j], $seqs[$i] );
178 }
179 }
180 0 $dist[$i][$i] = 1;
181  
182 0 return \@dist;
183 }
184  
185 sub map_seqids {
186 0 ▶ my ( $seqs, $pattern ) = @_;
Subroutine map_seqids: not called CC=2 SCAR=17.9
Pod documented
187  
188 0 my %map;
189  
190 0 my $i = seqid_mapper($pattern);
191  
192 0 for my $s (@$seqs) {
193 0 my ( $new, $old ) = $i->($s);
194 0 $map{$new} = $old;
195 }
196  
197 0 return \%map;
198 }
199  
200 sub seqid_mapper {
201 0 ▶ my ( $pattern, @extra_args ) = @_;
Subroutine seqid_mapper: not called CC=4 SCAR=30.0
Pod documented
202 0 ▶ $pattern = 's%08d' unless defined $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 0 0
203  
204 0 my $handler;
205 0 ▶ if ( ref $pattern eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pattern eq 'CODE') { } 0 0 0
elsif (ref $pattern eq 'HASH') { } 0 0 0
206 0 $handler = $pattern;
207 } elsif ( ref $pattern eq 'HASH' ) {
208 0 my $i = 1;
209 $handler = sub {
210 0 ▶ my $id = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
211 0 ▶ return defined $pattern->{$id} ? $pattern->{$id} : 'unknown_' . $i++;
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern->{$id} ? : 0 0 0
212 0 };
213 } else {
214 0 my $i = 1;
215 $handler = sub {
216 0 ▶ return sprintf $pattern, $i++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
217  
218 0 };
219 }
220  
221 return sub {
222 0 ▶ my ($seq) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
223 0 ▶ return unless ($seq);
Branch: 1 branch
BranchTrueFalseTotal
unless $seq 0 0 0
224 0 ▶ unless ( blessed($seq) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($seq)) 0 0 0
225 0 ▶ if ( ref $seq eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $seq eq 'ARRAY') { } 0 0 0
226 0 croak "you supplied an array to the mapper function, use single seq Bio::Gonzales::Seq objects";
227 } else {
228 0 confess Dumper $seq ;
229 }
230 }
231  
232 0 my $orig_id = $seq->id;
233 0 my $id = $handler->( $orig_id, @extra_args );
234 0 $seq->id($id);
235  
236 0 return ( $id, $orig_id );
237 0 };
238 }
239  
240 {
241 5 my $POLY64REVh = 0xd8000000;
242 5 my @CRCTableh = 256;
243 5 my @CRCTablel = 256;
244 5 my $initialized;
245  
246 sub crc64 {
247 25 ▶ my $sequence = shift;
Subroutine crc64: called CC=8 SCAR=20.8
Pod undocumented
248 25 my $crcl = 0;
249 25 my $crch = 0;
250 25 ▶ if ( !$initialized ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($initialized) 1 24 25
251 1 $initialized = 1;
252 1 ▶ for ( my $i = 0; $i < 256; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 256) 256 1 257
253 256 my $partl = $i;
254 256 my $parth = 0;
255 256 ▶ for ( my $j = 0; $j < 8; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 8) 2048 256 2304
256 2048 my $rflag = $partl & 1;
257 2048 $partl >>= 1;
258 2048 ▶ $partl |= ( 1 << 31 ) if $parth & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $parth & 1 0 2048 2048
259 2048 $parth >>= 1;
260 2048 ▶ $parth ^= $POLY64REVh if $rflag;
Branch: 1 branch
BranchTrueFalseTotal
if $rflag 1024 1024 2048
261 }
262 256 $CRCTableh[$i] = $parth;
263 256 $CRCTablel[$i] = $partl;
264 }
265 }
266  
267 25 foreach ( split '', $sequence ) {
268 4581 my $shr = ( $crch & 0xFF ) << 24;
269 4581 my $temp1h = $crch >> 8;
270 4581 my $temp1l = ( $crcl >> 8 ) | $shr;
271 4581 my $tableindex = ( $crcl ^ ( unpack "C", $_ ) ) & 0xFF;
272 4581 $crch = $temp1h ^ $CRCTableh[$tableindex];
273 4581 $crcl = $temp1l ^ $CRCTablel[$tableindex];
274 }
275 25 ▶ return wantarray ? ( $crch, $crcl ) : sprintf( "%08X%08X", $crch, $crcl );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 25 25
276 }
277 }
278  
279 5 1;
280 __END__
281  
282 =head1 NAME
283  
284  
285  
286 =head1 SYNOPSIS
287  
288 use Bio::Gonzales::Seq::Util qw(
289 pairwise_identity_l
290 pairwise_identity_s
291 pairwise_identity_gaps_l
292 pairwise_identity_gaps_s
293 pairwise_identities
294 map_seqids
295 seqid_mapper
296 overlaps
297 cluster_overlapping_ranges
298 );
299  
300 =head1 DESCRIPTION
301  
302 =head1 SUBROUTINES
303  
304 =over 4
305  
306 =item B<< $map = map_seqids($seqs, I<$pattern>) >>
307  
308 Maps all sequence ids of C<$seqs> in situ. If C<$pattern> is not given, C<s%9d> is taken as default.
309  
310  
311 =item B<< $clustered_ranges = cluster_overlapping_ranges(\@ranges) >>
312  
313 This function takes some ranges and clusters them by overlap.
314  
315 @ranges = (
316 [ $start, $stop, $some, $custom, $elements ],
317 [ ... ],
318 ...
319 );
320  
321 $clustered_ranges = [
322 # first cluster
323 [
324 [ $start, $stop, $some, $custom, $elements ],
325 ...
326 ],
327 # next cluster
328 [
329 [ $start, $stop, $some, $custom, $elements ],
330 ...
331 ],
332 ...
333 ];
334  
335  
336 =item B<< $map = map_seqids(\@seqs!, $pattern) >>
337  
338 Wrapper around C<seqid_mapper()>, maps the ids of @seqs and returns the map.
339 This function works directly on the sequences.
340  
341 =item B<< $mapper = seqid_mapper(\%idmap) >>
342  
343 Create a mapper that maps given sequences to new ids generated by the argument
344 given. A hash as argument will be used as mapping base, taking the key as old and the
345 value as new id. In case the sequence id is non-existent in the hash, an artificial id
346 following the pattern C<unknown_$i> with C<$i> running from 0
347 onwards will be generated..
348  
349 =item B<< $mapper = seqid_mapper(\&handler) >>
350  
351 Create a mapper that maps given sequences to new ids generated by successive
352 calls to the handler. The handler will get the existing/original id as
353 argument and shall return a new id. A simple mapper would be:
354  
355  
356 my $i = 1;
357 my $mapper = seqid_mapper(sub { sprintf "id%d", $i++ });
358 or
359 my $mapper = seqid_mapper(sub { my $id = shift; $id =~ s/pep/cds/; return $id});
360  
361 my ($old_id, $new_id) = $mapper->($sequence_object);
362  
363 The sequence object WILL BE ALTERED IN SITU.
364  
365 =item B<< $mapper = seqid_mapper($pattern) >>
366  
367 Use pattern as basis for sequence id mapping, "%s" or "%d" must be included
368 ONLY ONCE and will be substituted by a couter, running from 0 to INF.
369  
370 =item B<< $mapper = seqid_mapper() >>
371  
372 Same as C<$mapper = seqid_mapper("s%9d")>
373  
374 =item B<< overlaps([$a_begin, $a_end], [$b_begin, $b_end]) >>
375  
376 Returns true if a overlaps with b, false otherwise.
377  
378 =back
379  
380 =head1 SEE ALSO
381  
382 =head1 AUTHOR
383  
384 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
385  
386 =cut
« blib/lib/Bio/Gonzales/Feat/IO/GFF3.pm ↑ Summary blib/lib/Bio/Gonzales/Matrix/Util.pm »