File Coverage

blib/lib/Bio/Translator/Validations.pm
Criterion Covered Total %
statement 27 27 100.0
branch 7 10 70.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             package Bio::Translator::Validations;
2              
3 9     9   49 use strict;
  9         16  
  9         314  
4 9     9   46 use warnings;
  9         16  
  9         316  
5              
6             =head1 NAME
7              
8             Bio::Translator::Validations - validation methods and objects
9              
10             =cut
11              
12 9     9   48 use Carp;
  9         18  
  9         617  
13 9     9   51 use Params::Validate;
  9         17  
  9         557  
14 9     9   47 use Exporter 'import';
  9         19  
  9         6344  
15              
16             our %EXPORT_TAGS = (
17             defaults => [
18             qw(
19             $DEFAULT_STRAND
20             $DEFAULT_START
21             $DEFAULT_OFFSET
22             )
23             ],
24             regexes => [
25             qw(
26             $RE_BOOLEAN
27             $RE_NON_NEG_INT
28             $RE_STRAND
29             $RE_SEARCH_STRAND
30             $RE_012
31             )
32             ],
33             validations => [
34             qw(
35             $VAL_NON_NEG_INT
36             $VAL_STRAND
37             $VAL_SEARCH_STRAND
38             $VAL_START
39             $VAL_OFFSET
40              
41             validate_seq_params
42             validate_lower_upper
43             )
44             ]
45             );
46              
47             our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
48              
49             =head1 DEFAULTS
50              
51             =cut
52              
53             our $DEFAULT_STRAND = 1;
54             our $DEFAULT_SEARCH_STRAND = 0;
55             our $DEFAULT_START = 1;
56             our $DEFAULT_OFFSET = 0;
57              
58             =head1 REGULAR EXPRESSIONS
59              
60             =cut
61              
62             our $RE_BOOLEAN = qr/^[01]$/;
63             our $RE_NON_NEG_INT = qr/^\+?\d+$/;
64             our $RE_STRAND = qr/^[+-]?1$/;
65             our $RE_SEARCH_STRAND = qr/^[+-]?[01]$/;
66             our $RE_012 = qr/^[012]$/;
67              
68             =head1 VALIDATIONS
69              
70             =cut
71              
72             our $VAL_NON_NEG_INT = {
73             optional => 1,
74             regex => $RE_NON_NEG_INT,
75             type => Params::Validate::SCALAR,
76             };
77              
78             # Make sure strand is 1 or -1 and set default
79             our $VAL_STRAND = {
80             default => $DEFAULT_STRAND,
81             regex => $RE_STRAND,
82             type => Params::Validate::SCALAR
83             };
84              
85             # Make sure strand is 0, 1 or -1 and set default
86             our $VAL_SEARCH_STRAND = {
87             default => $DEFAULT_SEARCH_STRAND,
88             regex => $RE_SEARCH_STRAND,
89             type => Params::Validate::SCALAR
90             };
91              
92             # Make sure partial is boolean and set default
93             our $VAL_START = {
94             default => $DEFAULT_START,
95             type => Params::Validate::SCALAR
96             };
97              
98             # Make sure offset is 0, 1 or 2 and set default
99             our $VAL_OFFSET = {
100             default => $DEFAULT_OFFSET,
101             regex => $RE_012,
102             type => Params::Validate::SCALAR
103             };
104              
105             =head1 VALIDATION METHODS
106              
107             =cut
108              
109             =head2 validate_seq_params
110              
111             my ( $seq_ref, @p ) = validate_seq_params(@_);
112              
113             Do validations for methods expecting to be called as:
114              
115             method( $sequence, \%params ); # or
116             method( \$sequence, \%params );
117              
118             =cut
119              
120             sub validate_seq_params (\@) {
121 14         289 my ( $seq_ref, @p ) = validate_pos(
122 14     14 1 22 @{ $_[0] },
123             { type => Params::Validate::SCALARREF | Params::Validate::SCALAR },
124             { type => Params::Validate::HASHREF, default => {} }
125             );
126              
127 14 50       77 $seq_ref = \$seq_ref unless ( ref $seq_ref );
128 14         50 return ( $seq_ref, @p );
129             }
130              
131             =head2 validate_lower_upper
132              
133             my ( $lower, $upper ) = validate_lower_upper( $lower, $upper, $seq_ref );
134             my ( $lower, $upper ) = validate_lower_upper( delete( @p{qw/ lower upper /} ), $seq_ref );
135            
136             Validate lower and upper bounds. Assumes that they have already passed
137             $VAL_NON_NEG_INT.
138              
139             =cut
140              
141             sub validate_lower_upper {
142 11     11 1 21 my ( $lower, $upper, $seq_ref ) = @_;
143              
144 11 100       24 if ($upper) {
145 3 50       10 croak 'upper bound is out range'
146             if ( $upper > length($$seq_ref) );
147             }
148 8         16 else { $upper = length($$seq_ref) }
149              
150 11 100       24 if ($lower) {
151 2 50       6 croak 'lower bound is greater than upper bound'
152             if ( $lower > $upper );
153             }
154 9         15 else { $lower = 0 }
155              
156 11         40 return ( $lower, $upper );
157             }
158              
159             1;