Phylo/Matrices/Datatype/Mixed.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 171 CC 18 SCAR 30.6
CC
18
Cov
0%
is_same
56.0
is_valid
30.0
_new
12.0
CRAP 21.4
1 package Bio::Phylo::Matrices::Datatype::Mixed;
2 use strict;
3 use base 'Bio::Phylo::Matrices::Datatype';
4 use Bio::Phylo::Util::CONSTANT '/looks_like/';
5 use Bio::Phylo::Util::Exceptions 'throw';
6 {
7  
8 =head1 NAME
9  
10 Bio::Phylo::Matrices::Datatype::Mixed - Validator subclass,
11 no serviceable parts inside
12  
13 =head1 DESCRIPTION
14  
15 The Bio::Phylo::Matrices::Datatype::* classes are used to validate data
16 contained by L<Bio::Phylo::Matrices::Matrix> and L<Bio::Phylo::Matrices::Datum>
17 objects.
18  
19 =cut
20  
21 my @fields = \( my ( %range, %missing, %gap ) );
22  
23 sub _new {
24 my ( $package, $self, $ranges ) = @_;
25 if ( not looks_like_instance $ranges, 'ARRAY' ) {
26 throw 'BadArgs' =>
27 "No type ranges specified for 'mixed' data type!";
28 }
29 my $id = $self->get_id;
30 $range{$id} = [];
31 $missing{$id} = '?';
32 $gap{$id} = '-';
33 my $start = 0;
34 for ( my $i = 0 ; $i <= ( $#{$ranges} - 1 ) ; $i += 2 ) {
35 my $type = $ranges->[$i];
36 my $arg = $ranges->[ $i + 1 ];
37 my ( @args, $length );
38 if ( looks_like_instance $arg, 'HASH' ) {
39 $length = $arg->{'-length'};
40 @args = @{ $arg->{'-args'} };
41 }
42 else {
43 $length = $arg;
44 }
45 my $end = $length + $start - 1;
46 my $obj = Bio::Phylo::Matrices::Datatype->new( $type, @args );
47 $range{$id}->[$_] = $obj for ( $start .. $end );
48 $start = ++$end;
49 }
50 return bless $self, $package;
51 }
52  
53 =head1 METHODS
54  
55 =head2 MUTATORS
56  
57 =over
58  
59 =item set_missing()
60  
61 Sets the symbol for missing data.
62  
63 Type : Mutator
64 Title : set_missing
65 Usage : $obj->set_missing('?');
66 Function: Sets the symbol for missing data
67 Returns : Modified object.
68 Args : Argument must be a single
69 character, default is '?'
70  
71 =cut
72  
73 sub set_missing {
74 my ( $self, $missing ) = @_;
75 if ( not $missing eq $self->get_gap ) {
76 $missing{ $self->get_id } = $missing;
77 }
78 else {
79 throw 'BadArgs' =>
80 "Missing character '$missing' already in use as gap character";
81 }
82 return $self;
83 }
84  
85 =item set_gap()
86  
87 Sets the symbol for gaps.
88  
89 Type : Mutator
90 Title : set_gap
91 Usage : $obj->set_gap('-');
92 Function: Sets the symbol for gaps
93 Returns : Modified object.
94 Args : Argument must be a single
95 character, default is '-'
96  
97 =cut
98  
99 sub set_gap {
100 my ( $self, $gap ) = @_;
101 if ( not $gap eq $self->get_missing ) {
102 $gap{ $self->get_id } = $gap;
103 }
104 else {
105 throw 'BadArgs' =>
106 "Gap character '$gap' already in use as missing character";
107 }
108 return $self;
109 }
110  
111 =back
112  
113 =head2 ACCESSORS
114  
115 =over
116  
117 =item get_missing()
118  
119 Returns the object's missing data symbol.
120  
121 Type : Accessor
122 Title : get_missing
123 Usage : my $missing = $obj->get_missing;
124 Function: Returns the object's missing data symbol
125 Returns : A string
126 Args : None
127  
128 =cut
129  
130 sub get_missing { return $missing{ shift->get_id } }
131  
132 =item get_gap()
133  
134 Returns the object's gap symbol.
135  
136 Type : Accessor
137 Title : get_gap
138 Usage : my $gap = $obj->get_gap;
139 Function: Returns the object's gap symbol
140 Returns : A string
141 Args : None
142  
143 =cut
144  
145 sub get_gap { return $gap{ shift->get_id } }
146 my $get_ranges = sub { $range{ shift->get_id } };
147  
148 =item get_type()
149  
150 Returns the object's datatype as string.
151  
152 Type : Accessor
153 Title : get_type
154 Usage : my $type = $obj->get_type;
155 Function: Returns the object's datatype
156 Returns : A string
157 Args : None
158  
159 =cut
160  
161 sub get_type {
162 my $self = shift;
163 my $string = 'mixed(';
164 my $last;
165 my $range = $self->$get_ranges;
166 MODEL_RANGE_CHECK: for my $i ( 0 .. $#{$range} ) {
167 if ( $i == 0 ) {
168 $string .= $range->[$i]->get_type . ":1-";
169 $last = $range->[$i];
170 }
171 elsif ( $range->[$i] != $last ) {
172 $last = $range->[$i];
173 $string .= "$i, " . $last->get_type . ":" . ( $i + 1 ) . "-";
174 }
175 else {
176 next MODEL_RANGE_CHECK;
177 }
178 }
179 $string .= scalar( @{$range} ) . ")";
180 return $string;
181 }
182  
183 =item get_type_for_site()
184  
185 Returns type object for site number.
186  
187 Type : Accessor
188 Title : get_type_for_site
189 Usage : my $type = $obj->get_type_for_site(1);
190 Function: Returns data type object for site
191 Returns : A Bio::Phylo::Matrices::Datatype object
192 Args : None
193  
194 =cut
195  
196 sub get_type_for_site {
197 my ( $self, $i ) = @_;
198 if ( exists $range{ $self->get_id }->[$i] ) {
199 return $range{ $self->get_id }->[$i];
200 }
201 else {
202 return $range{ $self->get_id }->[-1];
203 }
204 }
205  
206 =back
207  
208 =head2 TESTS
209  
210 =over
211  
212 =item is_same()
213  
214 Compares data type objects.
215  
216 Type : Test
217 Title : is_same
218 Usage : if ( $obj->is_same($obj1) ) {
219 # do something
220 }
221 Function: Returns true if $obj1 contains the same validation rules
222 Returns : BOOLEAN
223 Args : A Bio::Phylo::Matrices::Datatype::* object
224  
225 =cut
226  
227 sub is_same {
228 my ( $self, $obj ) = @_;
229 my $id = $self->get_id;
230 return 1 if $id == $obj->get_id;
231 return 0 if $self->get_type ne $obj->get_type;
232 return 0 if $self->get_gap ne $obj->get_gap;
233 return 0 if $self->get_missing ne $obj->get_missing;
234 for my $i ( 0 .. $#{ $range{ $self->get_id } } ) {
235 if ( my $subtype = $range{ $self->get_id }->[$i] ) {
236 return 0
237 if not $subtype->is_same( $obj->get_type_for_site($i) );
238 }
239 }
240 return 1;
241 }
242  
243 =item is_valid()
244  
245 Returns true if argument only contains valid characters
246  
247 Type : Test
248 Title : is_valid
249 Usage : if ( $obj->is_valid($datum) ) {
250 # do something
251 }
252 Function: Returns true if $datum only contains valid characters
253 Returns : BOOLEAN
254 Args : A Bio::Phylo::Matrices::Datum object
255  
256 =cut
257  
258 sub is_valid {
259 my $self = shift;
260 my $datum = $_[0];
261 my $is_datum_object;
262 my ( $start, $end );
263 if (
264 looks_like_implementor $datum,
265 'get_position' and looks_like_implementor $datum,
266 'get_length'
267 )
268 {
269 ( $start, $end ) =
270 ( $datum->get_position - 1, $datum->get_length - 1 );
271 $is_datum_object = 1;
272 }
273 else {
274 $start = 0;
275 $end = $#_;
276 }
277 my $ranges = $self->$get_ranges;
278 my $type;
279 MODEL_RANGE_CHECK: for my $i ( $start .. $end ) {
280 if ( not $type ) {
281 $type = $ranges->[$i];
282 }
283 elsif ( $type != $ranges->[$i] ) {
284  
285 #die; # needs to slice
286 return 1; # TODO
287 }
288 else {
289 next MODEL_RANGE_CHECK;
290 }
291 }
292 if ($is_datum_object) {
293 return $type->is_valid($datum);
294 }
295 else {
296 return 1; # FIXME
297 }
298 }
299  
300 sub DESTROY {
301 my $self = shift;
302 my $id = $self->get_id;
303 for my $field (@fields) {
304 delete $field->{$id};
305 }
306 }
307 }
308  
309 =back
310  
311 =cut
312  
313 # podinherit_insert_token
314  
315 =head1 SEE ALSO
316  
317 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
318 for any user or developer questions and discussions.
319  
320 =over
321  
322 =item L<Bio::Phylo::Matrices::Datatype>
323  
324 This object inherits from L<Bio::Phylo::Matrices::Datatype>, so the methods defined
325 therein are also applicable to L<Bio::Phylo::Matrices::Datatype::Mixed>
326 objects.
327  
328 =item L<Bio::Phylo::Manual>
329  
330 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
331  
332 =back
333  
334 =head1 CITATION
335  
336 If you use Bio::Phylo in published research, please cite it:
337  
338 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
339 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
340 I<BMC Bioinformatics> B<12>:63.
341 L<http://dx.doi.org/10.1186/1471-2105-12-63>
342  
343 =cut
344  
345 1;