blib/lib/Bio/Gonzales/Util.pm

filter: Statementstmt 55.2% 42 / 76 Branchbran 25.0% 8 / 32 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 69.2% 9 / 13 Podpod 50.0% 4 / 8 totaltotal 46.5% 67 / 144 CC 23 SCAR 27.6
CC
23
Cov
45%
invslice
42.0
sys_fmt
30.0
as_arrayref
20.0
CRAP 15.8
« blib/lib/Bio/Gonzales/Domain/Identification/HMMER/SeqMarks.pm ↑ Summary blib/lib/Bio/Gonzales/Stat/Util.pm »
1 #Copyright (c) 2011 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2  
3 package Bio::Gonzales::Util;
4  
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
13 15 our $VERSION = '0.062'; # VERSION
14  
15 15 @EXPORT = qw();
16 15 %EXPORT_TAGS = ();
17 15 @EXPORT_OK = qw(undef_slice slice invslice flatten hash_merge as_arrayref sys_pipe sys_fmt);
18  
19 sub slice {
20 1 ▶ my ( $hr, @k ) = @_;
Subroutine slice: called CC=3 SCAR=11.8
Pod documented
21  
22 1 my $kk;
23 1 ▶ if ( @k == 1 && ref( $k[0] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k == 1 and ref $k[0] eq 'ARRAY') { } 0 1 1
Condition33%1/3
@k == 1 and ref $k[0] eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
@k == 1 and ref $k[0] eq "ARRAY"
@k == 1 ref $k[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @k == 1B= ref $k[0] eq "ARRAY"
ABresult
0X0
100
111
24 0 $kk = $k[0];
25 } else {
26 1 $kk = \@k;
27 }
28  
29 1 my @valid_keys = grep { exists( $hr->{$_} ) } @$kk;
30  
31 1 return undef_slice( $hr, \@valid_keys );
32 }
33  
34 sub invslice {
35 0 ▶ my ( $hr, @k ) = @_;
Subroutine invslice: not called CC=6 SCAR=37.4
Pod documented
36  
37 0 my $kk;
38 0 ▶ if ( @k == 1 && ref( $k[0] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k == 1 and ref $k[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
@k == 1 and ref $k[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@k == 1 and ref $k[0] eq "ARRAY"
@k == 1 ref $k[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @k == 1B= ref $k[0] eq "ARRAY"
ABresult
0X0
100
111
39 0 $kk = $k[0];
40 } else {
41 0 $kk = \@k;
42 }
43  
44 0 my %inv = map { $_ => 1 } @$kk;
45  
46 0 my %ret;
47 0 for my $k ( keys %$hr ) {
48 0 ▶ next if ( exists( $inv{$k} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $inv{$k} 0 0 0
49 0 $ret{$k} = $hr->{$k};
50 }
51  
52 0 ▶ return wantarray ? %ret : \%ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
53 }
54  
55 sub undef_slice {
56 2 ▶ my ( $hr, @k ) = @_;
Subroutine undef_slice: called CC=4 SCAR=14.0
Pod documented
57  
58 2 my $kk;
59 2 ▶ if ( @k == 1 && ref( $k[0] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k == 1 and ref $k[0] eq 'ARRAY') { } 1 1 2
Condition66%2/3
@k == 1 and ref $k[0] eq "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
@k == 1 and ref $k[0] eq "ARRAY"
@k == 1 ref $k[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @k == 1B= ref $k[0] eq "ARRAY"
ABresult
0X0
100
111
60 1 $kk = $k[0];
61 } else {
62 1 $kk = \@k;
63 }
64  
65 2 my %ret;
66 2 @ret{@$kk} = @$hr{@$kk};
67  
68 2 ▶ return wantarray ? %ret : \%ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
69 }
70  
71 sub flatten {
72 6 ▶ return map { ref eq 'ARRAY' ? flatten(@$_) : $_ } @_;
Subroutine flatten: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 3 12 15
73 }
74  
75 sub hash_merge {
76 14 ▶ my ( $target, $source ) = @_;
Subroutine hash_merge: called CC=4 SCAR=17.9
Pod undocumented
77  
78 14 for ( keys %$source ) {
79 1442 ▶ if ( 'ARRAY' eq ref $target->{$_} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $target->{$_}) { } 0 1442 1442
elsif ('HASH' eq ref $target->{$_}) { } 0 1442 1442
80 0 push @{ $target->{$_} }, @{ $source->{$_} };
81 } elsif ( 'HASH' eq ref $target->{$_} ) {
82 0 merge( $source->{$_}, $target->{$_} );
83 } else {
84 1442 $target->{$_} = $source->{$_};
85 }
86 }
87 }
88  
89 sub as_arrayref {
90 0 ▶ my ($item) = @_;
Subroutine as_arrayref: not called CC=4 SCAR=30.0
Pod undocumented
91  
92 0 ▶ return unless ( defined($item) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 0 0
93  
94 0 ▶ if ( ref $item eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 0 0 0
elsif (not ref $item) { } 0 0 0
95 0 return $item;
96 } elsif ( !ref $item ) {
97 0 return [$item];
98 } else {
99 0 return $item;
100 }
101 }
102  
103 sub sys_fmt {
104 0 ▶ my $cmd;
Subroutine sys_fmt: not called CC=5 SCAR=34.0
Pod undocumented
105  
106 0 for my $e (@_) {
107 0 ▶ if ( ref $e eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $e eq 'ARRAY') { } 0 0 0
elsif ($e =~ /^>>|\d?>|<|<<|\||\d?>\&\d$/) { } 0 0 0
elsif (defined $e) { } 0 0 0
108 0 $cmd .= shell_quote(@$e) . " ";
109 } elsif ( $e =~ /^>>|\d?>|<|<<|\||\d?>\&\d$/ ) {
110 0 $cmd .= $e . " ";
111 } elsif(defined $e) {
112 0 $cmd .= shell_quote($e) . " ";
113 } else {
114 0 next;
115 }
116 }
117 0 chomp $cmd;
118  
119 0 return $cmd;
120 }
121  
122 sub sys_pipe {
123 0 ▶ my $cmd = sys_fmt(@_);
Subroutine sys_pipe: not called CC=2 SCAR=17.9
Pod undocumented
124 0 ▶ system($cmd) == 0 or confess "system failed: $?\n$cmd";
Branch: 1 branch
BranchTrueFalseTotal
system($cmd) == 0 or confess("system failed: $?\n$cmd") 0 0 0
125 }
126  
127 15 1;
128  
129 __END__
130  
131 =head1 NAME
132  
133 Bio::Gonzales::Util - Utility functions for common tasks
134  
135 =head1 SYNOPSIS
136  
137 use Bio::Gonzales::Util qw(undef_slice slice invslice flatten hash_merge as_arrayref);
138  
139 =head1 SUBROUTINES
140  
141 =over 4
142  
143 =item B<< %sliced_hash = slice(\%hash, @keys_to_slice) >>
144  
145 =item B<< $sliced_hash = slice(\%hash, \@keys_to_slice) >>
146  
147 return a new hash with all keys removed that are not in C<@keys_to_slice>.
148  
149 =item B<< $sliced_hash = undef_slice(\%hash, \@keys_to_slice) >>
150  
151 =item B<< %sliced_hash = undef_slice(\%hash, @keys_to_slice) >>
152  
153 same as slice, but if a key in C<@keys_to_slice> does not exist in C<%hash>,
154 it will result in a additional entry with its value undefined
155
156 my %hash = (
157 a => 1,
158 b => 2,
159 c => 3
160 );
161  
162 my %sliced_hash = undef_slice(\%hash, qw/a b d/);
163  
164 # will result in
165 %sliced_hash = (
166 a => 1,
167 b => 2,
168 d => undef
169 );
170  
171 =item B<< %sliced_hash = invslice(\%hash, @keys_to_exclude) >>
172  
173 =item B<< \%sliced_hash = invslice(\%hash, \@keys_to_exclude) >>
174  
175 =item B<< @elements = flatten($nested_array1, $nested_array2) >>
176  
177 =back
178  
179 =head1 SEE ALSO
180  
181 =head1 AUTHOR
182  
183 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
184  
185 =cut
« blib/lib/Bio/Gonzales/Domain/Identification/HMMER/SeqMarks.pm ↑ Summary blib/lib/Bio/Gonzales/Stat/Util.pm »