[Bioperl-l] Automatic typing in Annotation::Collection
Charles Tilford
charles.tilford@bms.com
Tue, 28 May 2002 18:13:52 -0400
Hmm, just investigated TypeManager. It looks like it would make more
sense to first modify that module:
modified method in TypeManager:
sub _add_type_map{
my ($self,$key,$type) = @_;
$self->{'_type'}->{$key} = $type;
# Add this line to get the reciprocal hash:
$self->{'_key'}->{$type} = $key;
}
new methods in TypeManager:
sub key_for_object {
my ($self,$object) = @_;
return $self->{'_key'}->{ref($object)};
}
sub key_for_type {
my ($self,$type) = @_;
return $self->{'_key'}->{$type};
}
Modification within Bio::Annotation::Collection->add_Annotation
if ( !defined $object ) {
# Check to see if the user passed a single object of known type:
my $tm = Bio::Annotation::TypeManager->new();
if (my $keycheck = $tm->key_for_object($key)) {
($key, $object) = ($keycheck, $key);
}
$self->throw("Must have at least key and object in add_Annotation")
if( !defined $object );
}
-Charles
> Charles Tilford wrote:
>
> Any objections to modifying Bio::Annotation::Collection so that the
> "common" annotation types can be passed on their own, and the code
> figure out the key value from them? Aside from allowing the user to be
> a little lazier, I think it would also help guarantee standardization
> of the hash keys in Collection - that is, if Collection.pm knew that
> DBLink objects should be put under the "dblink" key, I would not have
> to worry about such entries accidentally going to "DBlink" or
> "db_link" keys.
>
> The modification I was thinking of would look something like this:
>
> sub add_Annotation{
> my ($self,$key,$object,$archytype) = @_;
>
> if( !defined $object ) {
> # Check to see if the user passed a single object of known type:
> if (my $ref = ref($key)) {
> $object = $key;
> if ($ref eq "Bio::Annotation::Comment") {
> $key = "comment";
> } elsif ($ref eq "Bio::Annotation::DBLink") {
> $key = "dblink";
> } elsif ($ref eq "Bio::Annotation::Reference") {
> $key = "reference";
> } else {
> $object = undef;
> $self->throw("Must have at least key and object in
> add_Annotation - I was unsure how to classify the '$ref' object you
> provided.")
> }
> }
> $self->throw("Must have at least key and object in add_Annotation")
> if( !defined $object );
> }
>
> -Charles
>
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l@bioperl.org
> http://bioperl.org/mailman/listinfo/bioperl-l