my ( $dbi, $type ) = @_;
my ( $highest_from_current, $highest_from_archive );
# Get highest stable ID from the relevant table.
my $sth = $dbi->prepare("SELECT MAX(stable_id) FROM $type WHERE stable_id LIKE 'ENS%'");
$sth->execute();
if ( my @row = $sth->fetchrow_array() ) {
$highest_from_current = $row[0];
} else {
die("Can't get max $type stable ID from $type\n");
}
if ( length($highest_from_current) == 0 ) {
print( STDERR
" Warning ! length of stable_id for $type is zero \n" );
}
if ( $type eq "exon" ) {
# Archive doesn't store information about exon_stable_ids so try
# without archive first.
if ( length($highest_from_current) == 0 ) {
print( "\n"
. "WARNING:\n"
. "No stable_id for exon found \n"
. "I got no prefix to generate new stable_ids for type $type!!! "
. "- I'll try to use gene_archive now\n" );
my $max =
my $prefix;
if ( length($max) > 0 ) {
( $prefix, my $suffix ) = $max =~ /([a-zA-Z]+)([0-9]+)/;
$prefix =~ s/G$
} else {
die( "ERROR: "
. "No entries in table exon and "
. "gene_archive tables found\n"
. "Don't know which species prefix to use for species.\n" );
$highest_from_current = sprintf( "%s%011d", $prefix, 0 );
}
} ## end if ( length($highest_from_current...))
# remove the 'E' from exon stable id prefix
my ( $prefix, $suffix ) = $highest_from_current =~ /([a-zA-Z]+)([0-9]+)/;
$prefix =~ s/E$
return $prefix . $suffix;
} ## end if ( $type eq "exon" )
# and from relevant archive
$highest_from_archive =
my $max =
( $highest_from_current ge $highest_from_archive )
? $highest_from_current
: $highest_from_archive;
if ( length($max) == 0 ) {
die( "ERROR: "
. "No stable_id in table gene_archive "
. "or found in $type\_stable_id - tables\n" );
}
# Assuming that this is a correctly formatted stable id -> remove the
# G/T/P for gene etc. (Exon dealt with above.)
my ( $prefix, $suffix ) = $max =~ /([a-zA-Z]+)([0-9]+)/;
if ( $type eq 'gene' ) {
$prefix =~ s/G$
} elsif ( $type eq 'transcript' ) {
$prefix =~ s/T$
} elsif ( $type eq 'translation' ) {
$prefix =~ s/P$
}
return $prefix . $suffix;