Archive Ensembl HomeArchive Ensembl Home
Generator.pm
Go to the documentation of this file.
00001 =pod 
00002 
00003 =head1 NAME
00004 
00005   Bio::EnsEMBL::Compara::StableId::Generator
00006 
00007 =head1 SYNOPSIS
00008 
00009 =head1 DESCRIPTION
00010 
00011     A name generator object (maintains the counter and knows the format of stable_ids).
00012 
00013 =cut
00014 
00015 package Bio::EnsEMBL::Compara::StableId::Generator;
00016 
00017 use strict;
00018 use Bio::EnsEMBL::Utils::Argument;  # import 'rearrange()'
00019 use Bio::EnsEMBL::Compara::StableId::Map;
00020 use Bio::EnsEMBL::Compara::StableId::NamedClusterSet;
00021 
00022 sub new {
00023     my $class = shift @_;
00024 
00025     my $self = bless { }, $class;
00026 
00027     my ($type, $release, $counter, $prefix, $map, $default_version) =
00028          rearrange([qw(type release counter prefix map default_version)], @_);
00029 
00030     $self->counter($counter || 0);
00031 
00032     $self->type($type)         if(defined($type));
00033     $self->release($release)   if(defined($release));
00034     $self->prefix($prefix)     if(defined($prefix));
00035     $self->init_from_map($map) if(defined($map));
00036 
00037     $self->default_version(defined($default_version) ? $default_version : 1);  # can be 0 or anything you would like to start with. But it must be set once and forever.
00038 
00039     return $self;
00040 }
00041 
00042 sub prefix {
00043     my $self = shift @_;
00044 
00045     if(@_) {
00046         $self->{'_prefix'} = shift @_;
00047     }
00048     return ($self->{'_prefix'} ||= { 'f' => 'ENSFM', 't' => 'ENSGT' }->{$self->type} || 'UnkType');
00049 }
00050 
00051 sub init_from_map {     # actually, we can initialize either from a Map or from a NamedClusterSet (they both have get_all_clids() & clid2clname() methods)
00052     my $self = shift @_;
00053     my $map  = shift @_;
00054     
00055     my $highest_counter = 0;
00056 
00057     foreach my $clid (@{ $map->get_all_clids }) {
00058         my $clname = $map->clid2clname($clid);
00059 
00060         if($clname=~/^(\w+)\d{4}(\d{10})\.\d+$/) {
00061             if(defined($self->{_prefix}) ? ($1 eq $self->prefix()) : $self->prefix($1)) { # make sure you completely understand this line if you're itching to change it :)
00062                 if($2 > $highest_counter) {
00063                     $highest_counter = $2;
00064                 }
00065             }
00066         }
00067     }
00068     $self->counter($highest_counter);
00069 }
00070 
00071 sub generate_new_name {
00072     my $self = shift @_;
00073 
00074     $self->counter($self->counter+1);
00075 
00076     return sprintf("%s%04d%010d.%d",$self->prefix, $self->release, $self->counter, $self->default_version);
00077 }
00078 
00079 sub type {
00080     my $self = shift @_;
00081 
00082     if(@_) {
00083         $self->{'_type'} = shift @_;
00084     }
00085     return $self->{'_type'};
00086 }
00087 
00088 sub release {
00089     my $self = shift @_;
00090 
00091     if(@_) {
00092         $self->{'_release'} = shift @_;
00093     }
00094     return $self->{'_release'};
00095 }
00096 
00097 sub counter {
00098     my $self = shift @_;
00099 
00100     if(@_) {
00101         $self->{'_counter'} = shift @_;
00102     }
00103     return $self->{'_counter'};
00104 }
00105 
00106 sub default_version {
00107     my $self = shift @_;
00108 
00109     if(@_) {
00110         $self->{'_default_version'} = shift @_;
00111     }
00112     return $self->{'_default_version'};
00113 }
00114 
00115 1;
00116