| Bio-Phylo documentation | Contained in the Bio-Phylo distribution. |
Bio::Phylo::Matrices::TypeSafeData - Superclass for objects that contain character data
# No direct usage
This is a superclass for objects holding character data. Objects that inherit from this class (typically matrices and datum objects) yield functionality to handle datatype objects and use them to validate data such as DNA sequences, continuous data etc.
TypeSafeData constructor.
Type : Constructor
Title : new
Usage : No direct usage, is called by child class;
Function: Instantiates a Bio::Phylo::Matrices::TypeSafeData
Returns : a Bio::Phylo::Matrices::TypeSafeData child class
Args : -type => (data type - required)
Optional:
-missing => (the symbol for missing data)
-gap => (the symbol for gaps)
-lookup => (a character state lookup hash)
-type_object => (a datatype object)
Set data type.
Type : Mutator
Title : set_type
Usage : $obj->set_type($type);
Function: Sets the object's datatype.
Returns : Modified object.
Args : Argument must be a string, one of
continuous, custom, dna, mixed,
protein, restriction, rna, standard
Set missing data symbol.
Type : Mutator
Title : set_missing
Usage : $obj->set_missing('?');
Function: Sets the symbol for missing data
Returns : Modified object.
Args : Argument must be a single
character, default is '?'
Set gap data symbol.
Type : Mutator
Title : set_gap
Usage : $obj->set_gap('-');
Function: Sets the symbol for gaps
Returns : Modified object.
Args : Argument must be a single
character, default is '-'
Set ambiguity lookup table.
Type : Mutator
Title : set_lookup
Usage : $obj->set_gap($hashref);
Function: Sets the symbol for gaps
Returns : Modified object.
Args : Argument must be a hash
reference that maps allowed
single character symbols
(including ambiguity symbols)
onto the equivalent set of
non-ambiguous symbols
Set data type object.
Type : Mutator
Title : set_type_object
Usage : $obj->set_gap($obj);
Function: Sets the datatype object
Returns : Modified object.
Args : Argument must be a subclass
of Bio::Phylo::Matrices::Datatype
Get data type.
Type : Accessor Title : get_type Usage : my $type = $obj->get_type; Function: Returns the object's datatype Returns : A string Args : None
Get missing data symbol.
Type : Accessor Title : get_missing Usage : my $missing = $obj->get_missing; Function: Returns the object's missing data symbol Returns : A string Args : None
Get gap symbol.
Type : Accessor Title : get_gap Usage : my $gap = $obj->get_gap; Function: Returns the object's gap symbol Returns : A string Args : None
Get ambiguity lookup table.
Type : Accessor Title : get_lookup Usage : my $lookup = $obj->get_lookup; Function: Returns the object's lookup hash Returns : A hash reference Args : None
Get data type object.
Type : Accessor Title : get_type_object Usage : my $obj = $obj->get_type_object; Function: Returns the object's linked datatype object Returns : A subclass of Bio::Phylo::Matrices::Datatype Args : None
Clones invocant.
Type : Utility method Title : clone Usage : my $clone = $object->clone; Function: Creates a copy of the invocant object. Returns : A copy of the invocant. Args : NONE
Validates the object's contents
Type : Interface method
Title : validate
Usage : $obj->validate
Function: Validates the object's contents
Returns : True or throws Bio::Phylo::Util::Exceptions::InvalidData
Args : None
Comments: This is an abstract method, i.e. this class doesn't
implement the method, child classes have to
This object inherits from Bio::Phylo::Listable, so the methods defined therein are also applicable to Bio::Phylo::Matrices::TypeSafeData objects.
Also see the manual: Bio::Phylo::Manual and http://rutgervos.blogspot.com.
If you use Bio::Phylo in published research, please cite it:
Rutger A Vos, Jason Caravas, Klaas Hartmann, Mark A Jensen and Chase Miller, 2011. Bio::Phylo - phyloinformatic analysis using Perl. BMC Bioinformatics 12:63. http://dx.doi.org/10.1186/1471-2105-12-63
$Id: TypeSafeData.pm 1660 2011-04-02 18:29:40Z rvos $
| Bio-Phylo documentation | Contained in the Bio-Phylo distribution. |
# $Id: TypeSafeData.pm 1660 2011-04-02 18:29:40Z rvos $ package Bio::Phylo::Matrices::TypeSafeData; use strict; use base 'Bio::Phylo::Listable'; use Bio::Phylo::Util::Exceptions 'throw'; use Bio::Phylo::Util::CONSTANT qw'_MATRIX_ /looks_like/'; use Bio::Phylo::Matrices::Datatype; { my $logger = __PACKAGE__->get_logger; my %type; my $MATRIX_CONSTANT = _MATRIX_;
sub new {
# is child class
my $class = shift;
# process args
my %args = looks_like_hash @_;
# notify user
if ( not $args{'-type'} and not $args{'-type_object'} ) {
$logger->info("No data type provided, will use 'standard'");
unshift @_, '-type', 'standard';
}
if ( $args{'-characters'} ) {
if ( $args{'-type'} ) {
$args{'-characters'}->set_type( $args{'-type'} );
}
elsif ( $args{'-type_object'} ) {
$args{'-characters'}->set_type_object( $args{'-type_object'} );
}
}
# notify user
$logger->debug("constructor called for '$class'");
# go up inheritance tree, eventually get an ID
return $class->SUPER::new(@_);
}
sub set_type {
my $self = shift;
my $arg = shift;
my ( $type, @args );
if ( looks_like_instance( $arg, 'ARRAY' ) ) {
@args = @{$arg};
$type = shift @args;
}
else {
@args = @_;
$type = $arg;
}
$logger->info("setting type '$type'");
my $obj = Bio::Phylo::Matrices::Datatype->new( $type, @args );
$self->set_type_object($obj);
eval { looks_like_object $self, $MATRIX_CONSTANT };
if ( not $@ ) {
for my $row ( @{ $self->get_entities } ) {
$row->set_type_object($obj);
}
}
else {
undef($@);
}
return $self;
}
sub set_missing {
my ( $self, $missing ) = @_;
if ( $self->can('get_matchchar') and $missing eq $self->get_matchchar )
{
throw 'BadArgs' =>
"Missing character '$missing' already in use as match character";
}
$logger->info("setting missing '$missing'");
$self->get_type_object->set_missing($missing);
$self->validate;
return $self;
}
sub set_gap {
my ( $self, $gap ) = @_;
if ( $self->can('get_matchchar') and $gap eq $self->get_matchchar ) {
throw 'BadArgs' =>
"Gap character '$gap' already in use as match character";
}
$logger->info("setting gap '$gap'");
$self->get_type_object->set_gap($gap);
$self->validate;
return $self;
}
sub set_lookup {
my ( $self, $lookup ) = @_;
$logger->info("setting character state lookup hash");
$self->get_type_object->set_lookup($lookup);
$self->validate;
return $self;
}
sub set_type_object {
my ( $self, $obj ) = @_;
$logger->info("setting character type object");
$type{ $self->get_id } = $obj;
eval { $self->validate };
if ($@) {
undef($@);
if ( my @char = $self->get_char ) {
$self->clear;
$logger->warn(
"Data contents of $self were invalidated by new type object."
);
}
}
return $self;
}
sub get_type {
my $to = shift->get_type_object;
if ($to) {
return $to->get_type;
}
else {
throw 'API' => "Missing data type object!";
}
}
sub get_missing {
my $to = shift->get_type_object;
if ($to) {
return $to->get_missing;
}
else {
throw 'API' => "Missing data type object!";
}
}
sub get_gap { shift->get_type_object->get_gap }
sub get_lookup { shift->get_type_object->get_lookup }
sub get_type_object { $type{ $_[0]->get_id } }
sub clone {
my $self = shift;
$logger->info("cloning $self");
my %subs = @_;
# we'll create type object during construction
$subs{'set_type'} = 0;
$subs{'set_missing'} = 0;
$subs{'set_gap'} = 0;
$subs{'set_lookup'} = 0;
# we'll override this, the type object is created from scratch
$subs{'set_type_object'} = 0;
# this will create type object during construction
$subs{'new'} = [
'-type' => $self->get_type,
'-missing' => $self->get_missing,
'-gap' => $self->get_gap,
'-lookup' => $self->get_lookup,
];
return $self->SUPER::clone(%subs);
}
sub validate {
shift->_validate;
}
sub _validate {
throw 'NotImplemented' => 'Not implemented!';
}
sub _cleanup {
my $self = shift;
if ( $self and defined( my $id = $self->get_id ) ) {
$logger->debug("cleaning up '$self'");
delete $type{ $self->get_id };
}
}
}
# podinherit_insert_token
1;