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

filter: Statementstmt 44.8% 56 / 125 Branchbran 25.0% 11 / 44 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 42.1% 8 / 19 Podpod 22.2% 2 / 9 totaltotal 38.1% 80 / 210 CC 28 SCAR 26.4
CC
28
Cov
39%
_pairwise_identity_generic
42.0
pairwise_identities
20.0
seqid_mapper
20.0
CRAP 14.0
« blib/lib/Bio/Gonzales/Util.pm ↑ Summary blib/lib/Bio/Gonzales/Var/IO/VCF.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  
10 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
12 5 our $VERSION = '0.058'; # VERSION
13  
14 5 @EXPORT = qw();
15 5 %EXPORT_TAGS = ();
16 5 @EXPORT_OK = qw(
17 pairwise_identity_l
18 pairwise_identity_s
19 pairwise_identity_gaps_l
20 pairwise_identity_gaps_s
21 pairwise_identities
22 map_seqids
23 seqid_mapper
24 crc64
25 strand_convert
26 );
27  
28 5 our %STRAND_CHAR_TABLE = (
29 '+' => 1,
30 '-' => -1,
31 '.' => 0,
32 -1 => '-',
33 1 => '+',
34 0 => '.',
35 );
36  
37 sub strand_convert {
38 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
39 0 return $STRAND_CHAR_TABLE{ $_[-1] };
40 } else {
41 141 return '.';
42 }
43 }
44  
45 sub pairwise_identity_l {
46 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_l: not called CC=1 SCAR=6.9
Pod undocumented
47 0 return _pairwise_identity_generic( $seq1, $seq2, 1, 0 );
48 }
49  
50 sub pairwise_identity_s {
51 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_s: not called CC=1 SCAR=6.9
Pod undocumented
52 0 return _pairwise_identity_generic( $seq1, $seq2, 0, 0 );
53 }
54  
55 sub pairwise_identity_gaps_l {
56 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_gaps_l: not called CC=1 SCAR=6.9
Pod undocumented
57 0 return _pairwise_identity_generic( $seq1, $seq2, 1, 1 );
58 }
59  
60 sub _pairwise_identity_generic {
61 0 ▶ my ( $seq1, $seq2, $use_longest, $include_gaps ) = @_;
Subroutine _pairwise_identity_generic: not called CC=6 SCAR=37.4
62  
63 0 ▶ $seq1 = $seq1->seq if ( blessed $seq1);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($seq1) 0 0 0
64 0 ▶ $seq2 = $seq2->seq if ( blessed $seq2);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($seq2) 0 0 0
65  
66 0 my $seq1_gaps = 0;
67 0 my $seq2_gaps = 0;
68 0 ▶ if ( !$include_gaps ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($include_gaps) 0 0 0
69 0 $seq1_gaps = $seq1 =~ y/-/./;
70 0 $seq2_gaps = () = $seq2 =~ /-/g;
71 }
72  
73 0 my $mask = $seq1 ^ $seq2;
74 0 my $matches = $mask =~ tr/\x0/\x0/;
75  
76 0 my $longest;
77 my $shortest;
78 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
79 0 $longest = length($seq1) - $seq1_gaps;
80 0 $shortest = length($seq2) - $seq2_gaps;
81 } else {
82 0 $shortest = length($seq1) - $seq1_gaps;
83 0 $longest = length($seq2) - $seq2_gaps;
84 }
85  
86 0 ▶ if ($use_longest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_longest) { } 0 0 0
87 0 return ( $matches / $longest );
88 } else {
89 0 return ( $matches / $shortest );
90 }
91 }
92  
93 sub pairwise_identity_gaps_s {
94 0 ▶ my ( $seq1, $seq2 ) = @_;
Subroutine pairwise_identity_gaps_s: not called CC=1 SCAR=6.9
Pod undocumented
95  
96 0 return _pairwise_identity_generic( $seq1, $seq2, 0, 1 );
97 }
98  
99 sub pairwise_identities {
100 0 ▶ my ( $sub, @seqs ) = @_;
Subroutine pairwise_identities: not called CC=4 SCAR=30.0
Pod undocumented
101  
102 #creating an upper triangular matrix
103  
104 0 my @dist;
105 0 ▶ for ( my $i = 0; $i < @seqs; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @seqs) 0 0 0
106 0 push @dist, [];
107 }
108  
109 0 my $i;
110 0 ▶ for ( $i = 0; $i < @seqs - 1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @seqs - 1) 0 0 0
111 0 $dist[$i][$i] = 1;
112 0 ▶ for ( my $j = $i + 1; $j < @seqs; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @seqs) 0 0 0
113 0 $dist[$j][$i] = $sub->( $seqs[$j], $seqs[$i] );
114 }
115 }
116 0 $dist[$i][$i] = 1;
117  
118 0 return \@dist;
119 }
120  
121 sub map_seqids {
122 0 ▶ my ( $seqs, $pattern ) = @_;
Subroutine map_seqids: not called CC=2 SCAR=17.9
Pod documented
123  
124 0 my %map;
125  
126 0 my $i = seqid_mapper($pattern);
127  
128 0 for my $s (@$seqs) {
129 0 my ( $new, $old ) = $i->($s);
130 0 $map{$new} = $old;
131 }
132  
133 0 return \%map;
134 }
135  
136 sub seqid_mapper {
137 0 ▶ my ( $pattern, @extra_args ) = @_;
Subroutine seqid_mapper: not called CC=4 SCAR=30.0
Pod documented
138 0 ▶ $pattern = 's%08d' unless defined $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 0 0
139  
140 0 my $handler;
141 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
142 0 $handler = $pattern;
143 } elsif ( ref $pattern eq 'HASH' ) {
144 0 my $i = 1;
145 $handler = sub {
146 0 ▶ my $id = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
147 0 ▶ return defined $pattern->{$id} ? $pattern->{$id} : 'unknown_' . $i++;
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern->{$id} ? : 0 0 0
148 0 };
149 } else {
150 0 my $i = 1;
151 $handler = sub {
152 0 ▶ return sprintf $pattern, $i++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
153  
154 0 };
155 }
156  
157 return sub {
158 0 ▶ my ($seq) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
159 0 ▶ return unless ($seq);
Branch: 1 branch
BranchTrueFalseTotal
unless $seq 0 0 0
160 0 ▶ unless ( blessed($seq) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($seq)) 0 0 0
161 0 ▶ if ( ref $seq eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $seq eq 'ARRAY') { } 0 0 0
162 0 croak "you supplied an array to the mapper function, use single seq Bio::Gonzales::Seq objects";
163 } else {
164 0 confess Dumper $seq ;
165 }
166 }
167  
168 0 my $orig_id = $seq->id;
169 0 my $id = $handler->( $orig_id, @extra_args );
170 0 $seq->id($id);
171  
172 0 return ( $id, $orig_id );
173 0 };
174 }
175  
176 {
177 5 my $POLY64REVh = 0xd8000000;
178 5 my @CRCTableh = 256;
179 5 my @CRCTablel = 256;
180 5 my $initialized;
181  
182 sub crc64 {
183 25 ▶ my $sequence = shift;
Subroutine crc64: called CC=8 SCAR=20.8
Pod undocumented
184 25 my $crcl = 0;
185 25 my $crch = 0;
186 25 ▶ if ( !$initialized ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($initialized) 1 24 25
187 1 $initialized = 1;
188 1 ▶ for ( my $i = 0; $i < 256; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 256) 256 1 257
189 256 my $partl = $i;
190 256 my $parth = 0;
191 256 ▶ for ( my $j = 0; $j < 8; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 8) 2048 256 2304
192 2048 my $rflag = $partl & 1;
193 2048 $partl >>= 1;
194 2048 ▶ $partl |= ( 1 << 31 ) if $parth & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $parth & 1 0 2048 2048
195 2048 $parth >>= 1;
196 2048 ▶ $parth ^= $POLY64REVh if $rflag;
Branch: 1 branch
BranchTrueFalseTotal
if $rflag 1024 1024 2048
197 }
198 256 $CRCTableh[$i] = $parth;
199 256 $CRCTablel[$i] = $partl;
200 }
201 }
202  
203 25 foreach ( split '', $sequence ) {
204 4581 my $shr = ( $crch & 0xFF ) << 24;
205 4581 my $temp1h = $crch >> 8;
206 4581 my $temp1l = ( $crcl >> 8 ) | $shr;
207 4581 my $tableindex = ( $crcl ^ ( unpack "C", $_ ) ) & 0xFF;
208 4581 $crch = $temp1h ^ $CRCTableh[$tableindex];
209 4581 $crcl = $temp1l ^ $CRCTablel[$tableindex];
210 }
211 25 ▶ return wantarray ? ( $crch, $crcl ) : sprintf( "%08X%08X", $crch, $crcl );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 25 25
212 }
213 }
214  
215 5 1;
216 __END__
217  
218 =head1 NAME
219  
220  
221  
222 =head1 SYNOPSIS
223  
224 use Bio::Gonzales::Seq::Util qw(
225 pairwise_identity_l
226 pairwise_identity_s
227 pairwise_identity_gaps_l
228 pairwise_identity_gaps_s
229 pairwise_identities
230 map_seqids
231 seqid_mapper
232 overlaps
233 cluster_overlapping_ranges
234 );
235  
236 =head1 DESCRIPTION
237  
238 =head1 SUBROUTINES
239  
240 =over 4
241  
242 =item B<< $map = map_seqids($seqs, I<$pattern>) >>
243  
244 Maps all sequence ids of C<$seqs> in situ. If C<$pattern> is not given, C<s%9d> is taken as default.
245  
246  
247 =item B<< $clustered_ranges = cluster_overlapping_ranges(\@ranges) >>
248  
249 This function takes some ranges and clusters them by overlap.
250  
251 @ranges = (
252 [ $start, $stop, $some, $custom, $elements ],
253 [ ... ],
254 ...
255 );
256  
257 $clustered_ranges = [
258 # first cluster
259 [
260 [ $start, $stop, $some, $custom, $elements ],
261 ...
262 ],
263 # next cluster
264 [
265 [ $start, $stop, $some, $custom, $elements ],
266 ...
267 ],
268 ...
269 ];
270  
271  
272 =item B<< $map = map_seqids(\@seqs!, $pattern) >>
273  
274 Wrapper around C<seqid_mapper()>, maps the ids of @seqs and returns the map.
275 This function works directly on the sequences.
276  
277 =item B<< $mapper = seqid_mapper(\%idmap) >>
278  
279 Create a mapper that maps given sequences to new ids generated by the argument
280 given. A hash as argument will be used as mapping base, taking the key as old and the
281 value as new id. In case the sequence id is non-existent in the hash, an artificial id
282 following the pattern C<unknown_$i> with C<$i> running from 0
283 onwards will be generated..
284  
285 =item B<< $mapper = seqid_mapper(\&handler) >>
286  
287 Create a mapper that maps given sequences to new ids generated by successive
288 calls to the handler. The handler will get the existing/original id as
289 argument and shall return a new id. A simple mapper would be:
290  
291  
292 my $i = 1;
293 my $mapper = seqid_mapper(sub { sprintf "id%d", $i++ });
294 or
295 my $mapper = seqid_mapper(sub { my $id = shift; $id =~ s/pep/cds/; return $id});
296  
297 my ($old_id, $new_id) = $mapper->($sequence_object);
298  
299 The sequence object WILL BE ALTERED IN SITU.
300  
301 =item B<< $mapper = seqid_mapper($pattern) >>
302  
303 Use pattern as basis for sequence id mapping, "%s" or "%d" must be included
304 ONLY ONCE and will be substituted by a couter, running from 0 to INF.
305  
306 =item B<< $mapper = seqid_mapper() >>
307  
308 Same as C<$mapper = seqid_mapper("s%9d")>
309  
310 =item B<< overlaps([$a_begin, $a_end], [$b_begin, $b_end]) >>
311  
312 Returns true if a overlaps with b, false otherwise.
313  
314 =back
315  
316 =head1 SEE ALSO
317  
318 =head1 AUTHOR
319  
320 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
321  
322 =cut
« blib/lib/Bio/Gonzales/Util.pm ↑ Summary blib/lib/Bio/Gonzales/Var/IO/VCF.pm »