File Coverage

blib/lib/Bio/Gonzales/Util/Text.pm
Criterion Covered Total %
statement 27 28 96.4
branch 4 8 50.0
condition n/a
subroutine 7 7 100.0
pod 1 2 50.0
total 39 45 86.6


line stmt bran cond sub pod time code
1             #Copyright (c) 2010 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2              
3             package Bio::Gonzales::Util::Text;
4              
5 1     1   114922 use warnings;
  1         12  
  1         34  
6 1     1   6 use strict;
  1         1  
  1         20  
7 1     1   5 use Carp;
  1         2  
  1         57  
8              
9 1     1   6 use base 'Exporter';
  1         2  
  1         443  
10             our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
11             our $VERSION = '0.083'; # VERSION
12              
13             @EXPORT = qw();
14             %EXPORT_TAGS = ();
15             @EXPORT_OK = qw(ccount ccount_iter);
16              
17             sub ccount_iter {
18 1     1 0 3 my ($args) = @_;
19              
20 1         2 my %counts;
21              
22             return sub {
23 1     1   12 my ($string) = @_;
24              
25 1 50       5 return \%counts unless($string);
26              
27 1         3 my @counts;
28              
29 1         8 for ( my $i = length($string) - 1; $i >= 0; $i-- ) {
30 4         14 $counts[ ord( substr( $string, $i, 1 ) ) ]++;
31             }
32              
33 1         7 for my $idx ( 0 .. $#counts ) {
34 85 50       128 if ( $args->{ignore_case} ) {
35 0 0       0 $counts{ lc( chr($idx) ) } += $counts[$idx] if exists $counts[$idx];
36             } else {
37 85 100       147 $counts{ chr($idx) } += $counts[$idx] if exists $counts[$idx];
38              
39             }
40             }
41              
42 1         14 return \%counts;
43 1         7 };
44             }
45              
46             sub ccount {
47 1     1 1 140 my ( $string, $args ) = @_;
48 1         4 my $counter = ccount_iter($args);
49              
50 1         5 return $counter->($string);
51             }
52              
53             1;
54              
55             __END__
56              
57             =head1 NAME
58              
59             Bio::Gonzales::Util::Text - text and string functions
60              
61             =head1 SYNOPSIS
62              
63             use Bio::Gonzales::Util::Text qw(ccount);
64              
65             =head1 DESCRIPTION
66              
67             Text and string functions that can be useful in a bioinformaticians daily life.
68              
69             =head1 SUBROUTINES
70              
71             =over 4
72              
73             =item B<< $counts = ccount($string) >>
74              
75             counts the character occurrences in C<$string> and returns a hash with
76             characters as keys and their corresponding counts as values.
77              
78             $counts = {
79             'A' => 34,
80             'G' => 234234,
81             'a' => 12,
82             'C' => 234234,
83             'T' => 46
84             };
85              
86             =back
87              
88             =head1 SEE ALSO
89              
90             =head1 AUTHOR
91              
92             jw bargsten, C<< <joachim.bargsten at wur.nl> >>
93              
94             =cut