Archive Ensembl HomeArchive Ensembl Home
NamedClusterSet.pm
Go to the documentation of this file.
00001 =pod 
00002 
00003 =head1 NAME
00004 
00005   Bio::EnsEMBL::Compara::StableId::NamedClusterSet
00006 
00007 =head1 SYNOPSIS
00008 
00009 =head1 DESCRIPTION
00010 
00011     A data container object (the only methods are getters/setters)
00012     that maintains membername-2-clusterid and clusterid-2-clustername relationships
00013 
00014 =cut
00015 
00016 package Bio::EnsEMBL::Compara::StableId::NamedClusterSet;
00017 
00018 use strict;
00019 use Bio::EnsEMBL::Utils::Argument;  # import 'rearrange()'
00020 use Bio::EnsEMBL::Compara::StableId::Map;
00021 
00022 sub new {
00023     my $class = shift @_;
00024 
00025     my $self = bless { }, $class;
00026 
00027     my ($type, $release) =
00028          rearrange([qw(type release) ], @_);
00029 
00030     $self->type($type)       if(defined($type));
00031     $self->release($release) if(defined($release));
00032 
00033     return $self;
00034 }
00035 
00036 sub apply_map {
00037     my $self = shift @_;
00038     my $map  = shift @_;
00039 
00040     foreach my $clid (@{$self->get_all_clids}) {
00041         if(my $new_name = $map->clid2clname($clid)) {
00042             $self->clid2clname($clid, $new_name);
00043         } else {
00044             my $old_name = $self->clid2clname($clid);
00045             die "Map does not contain mapping for '$old_name' (id=$clid)";
00046         }
00047     }
00048 }
00049 
00050 sub type {
00051     my $self = shift @_;
00052 
00053     if(@_) {
00054         $self->{'_type'} = shift @_;
00055     }
00056     return $self->{'_type'};
00057 }
00058 
00059 sub release {
00060     my $self = shift @_;
00061 
00062     if(@_) {
00063         $self->{'_release'} = shift @_;
00064     }
00065     return $self->{'_release'};
00066 }
00067 
00068 sub mname2clid {    # member_name -> class_id mapping (many-to-1)
00069     my $self       = shift @_;
00070     my $mname = shift @_;
00071 
00072     my $hash = $self->{'_mname2clid'} ||= {};
00073 
00074     if(@_) {
00075         $hash->{$mname} = shift @_;
00076     }
00077     return $hash->{$mname};
00078 }
00079 
00080 sub clid2clname {   # class_id -> class_name (1-to-1)
00081     my $self       = shift @_;
00082     my $clid  = shift @_;
00083 
00084     my $hash = $self->{'_clid2clname'} ||= {};
00085 
00086     if(@_) {
00087         $hash->{$clid} = shift @_;
00088     }
00089     return $hash->{$clid};
00090 }
00091 
00092 sub get_all_members {
00093     my $self       = shift @_;
00094 
00095     return [ keys %{ $self->{'_mname2clid'} ||= {} } ];
00096 }
00097 
00098 sub get_all_clids {
00099     my $self       = shift @_;
00100 
00101     return [ sort {$a<=>$b} keys %{ $self->{'_clid2clname'} ||= {} } ];
00102 }
00103 
00104 1;
00105