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