$Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::VERSION
=
'113.0.0'
;
no
warnings
qw(uninitialized)
;
our
@ISA
=
qw(Bio::EnsEMBL::DBSQL::BaseAdaptor)
;
sub
fetch_by_stable_id {
my
$self
=
shift
;
my
$stable_id
=
shift
;
my
$arch_id
=
$self
->_fetch_by_stable_id(
$stable_id
,
@_
);
if
(!
defined
(
$arch_id
)) {
my
$vindex
=
rindex
(
$stable_id
,
'.'
);
if
(
$vindex
!~ /^[0-9]{1,5}$/) {
return
$arch_id
; }
$arch_id
=
$self
->fetch_by_stable_id_version(
substr
(
$stable_id
,0,
$vindex
),
substr
(
$stable_id
,
$vindex
+1),
@_
);
}
return
$arch_id
;
}
sub
_fetch_by_stable_id {
my
$self
=
shift
;
my
$stable_id
=
shift
;
my
$arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$stable_id
,
-adaptor
=>
$self
);
@_
?
$arch_id
->type(
shift
) :
$self
->_resolve_type(
$arch_id
);
if
(
$self
->lookup_current(
$arch_id
)) {
$arch_id
->version(
$arch_id
->current_version);
$arch_id
->db_name(
$self
->dbc->dbname);
$arch_id
->release(
$self
->get_current_release);
$arch_id
->assembly(
$self
->get_current_assembly);
}
else
{
my
$extra_sql
=
defined
(
$arch_id
->{
'type'
}) ?
" AND sie.type = '@{[lc($arch_id->{'type'})]}'"
:
''
;
my
$r
=
$self
->_fetch_archive_id(
$stable_id
,
$extra_sql
,
$extra_sql
);
if
(
$r
->{
'new_stable_id'
} and
$r
->{
'new_stable_id'
} eq
$stable_id
) {
$arch_id
->version(
$r
->{
'new_version'
});
$arch_id
->release(
$r
->{
'new_release'
});
$arch_id
->assembly(
$r
->{
'new_assembly'
});
$arch_id
->db_name(
$r
->{
'new_db_name'
});
}
else
{
$arch_id
->version(
$r
->{
'old_version'
});
$arch_id
->release(
$r
->{
'old_release'
});
$arch_id
->assembly(
$r
->{
'old_assembly'
});
$arch_id
->db_name(
$r
->{
'old_db_name'
});
}
$arch_id
->type(
ucfirst
(
lc
(
$r
->{
'type'
})));
}
if
(!
defined
$arch_id
->db_name) {
return
undef
;
}
$arch_id
->is_latest(1);
return
$arch_id
;
}
sub
fetch_by_stable_id_version {
my
$self
=
shift
;
my
$stable_id
=
shift
;
my
$version
=
shift
;
my
$arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$stable_id
,
-version
=>
$version
,
-adaptor
=>
$self
);
if
(
$version
!~ /^[0-9]{1,5}$/) {
throw(
"$version is not valid, should be a small int"
);
}
@_
?
$arch_id
->type(
shift
) :
$self
->_resolve_type(
$arch_id
);
if
(
$self
->lookup_current(
$arch_id
) &&
$arch_id
->is_current) {
$arch_id
->db_name(
$self
->dbc->dbname);
$arch_id
->release(
$self
->get_current_release);
$arch_id
->assembly(
$self
->get_current_assembly);
}
else
{
my
$extra_sql1
=
qq(AND sie.old_version = "$version")
;
my
$extra_sql2
=
qq(AND sie.new_version = "$version")
;
my
$r
=
$self
->_fetch_archive_id(
$stable_id
,
$extra_sql1
,
$extra_sql2
);
if
(
$r
->{
'new_stable_id'
} and
$r
->{
'new_stable_id'
} eq
$stable_id
and
$r
->{
'new_version'
} ==
$version
) {
$arch_id
->release(
$r
->{
'new_release'
});
$arch_id
->assembly(
$r
->{
'new_assembly'
});
$arch_id
->db_name(
$r
->{
'new_db_name'
});
}
else
{
$arch_id
->release(
$r
->{
'old_release'
});
$arch_id
->assembly(
$r
->{
'old_assembly'
});
$arch_id
->db_name(
$r
->{
'old_db_name'
});
}
$arch_id
->type(
ucfirst
(
lc
(
$r
->{
'type'
})));
}
if
(!
defined
$arch_id
->db_name) {
return
undef
;
}
return
$arch_id
;
}
sub
fetch_by_stable_id_dbname {
my
$self
=
shift
;
my
$stable_id
=
shift
;
my
$db_name
=
shift
;
my
$arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$stable_id
,
-db_name
=>
$db_name
,
-adaptor
=>
$self
);
@_
?
$arch_id
->type(
shift
) :
$self
->_resolve_type(
$arch_id
);
if
(
$self
->lookup_current(
$arch_id
) and
$db_name
eq
$self
->dbc->dbname) {
$arch_id
->version(
$arch_id
->current_version);
$arch_id
->release(
$self
->get_current_release);
$arch_id
->assembly(
$self
->get_current_assembly);
}
else
{
my
$extra_sql
=
defined
(
$arch_id
->{
'type'
}) ?
" AND sie.type = '@{[lc($arch_id->{'type'})]}'"
:
''
;
my
$extra_sql1
=
$extra_sql
.
qq( AND ms.old_db_name = "$db_name")
;
my
$extra_sql2
=
$extra_sql
.
qq( AND ms.new_db_name = "$db_name")
;
my
$r
=
$self
->_fetch_archive_id(
$stable_id
,
$extra_sql1
,
$extra_sql2
);
if
(
$r
->{
'new_stable_id'
} and
$r
->{
'new_stable_id'
} eq
$stable_id
and
$r
->{
'new_db_name'
} eq
$db_name
) {
$arch_id
->release(
$r
->{
'new_release'
});
$arch_id
->assembly(
$r
->{
'new_assembly'
});
$arch_id
->version(
$r
->{
'new_version'
});
}
else
{
$arch_id
->release(
$r
->{
'old_release'
});
$arch_id
->assembly(
$r
->{
'old_assembly'
});
$arch_id
->version(
$r
->{
'old_version'
});
}
$arch_id
->type(
ucfirst
(
lc
(
$r
->{
'type'
})));
}
if
(!
defined
$arch_id
->version ) {
return
undef
;
}
return
$arch_id
;
}
sub
_fetch_archive_id {
my
$self
=
shift
;
my
$stable_id
=
shift
;
my
$extra_sql1
=
shift
;
my
$extra_sql2
=
shift
;
my
$sql
=
qq(
SELECT * FROM stable_id_event sie, mapping_session ms
WHERE sie.mapping_session_id = ms.mapping_session_id
AND sie.old_stable_id = ?
$extra_sql1
UNION
SELECT * FROM stable_id_event sie, mapping_session ms
WHERE sie.mapping_session_id = ms.mapping_session_id
AND sie.new_stable_id = ?
$extra_sql2
ORDER BY created DESC, score DESC
LIMIT 1
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->execute(
$stable_id
,
$stable_id
);
my
$r
=
$sth
->fetchrow_hashref;
$sth
->finish;
return
$r
;
}
sub
fetch_all_by_archive_id {
my
$self
=
shift
;
my
$archive_id
=
shift
;
my
$return_type
=
shift
;
my
@result
= ();
my
$lc_self_type
=
lc
(
$archive_id
->type);
my
$lc_return_type
=
lc
(
$return_type
);
my
$sql
=
qq(
SELECT
ga.${lc_return_type}_stable_id,
ga.${lc_return_type}_version,
m.old_db_name,
m.old_release,
m.old_assembly
FROM gene_archive ga, mapping_session m
WHERE ga.${lc_self_type}_stable_id = ?
AND ga.${lc_self_type}_version = ?
AND ga.mapping_session_id = m.mapping_session_id
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->bind_param(1,
$archive_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(2,
$archive_id
->version, SQL_SMALLINT);
$sth
->execute;
my
(
$stable_id
,
$version
,
$db_name
,
$release
,
$assembly
);
$sth
->bind_columns(\
$stable_id
, \
$version
, \
$db_name
, \
$release
, \
$assembly
);
while
(
$sth
->fetch) {
my
$new_arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$stable_id
,
-version
=>
$version
,
-db_name
=>
$db_name
,
-release
=>
$release
,
-assembly
=>
$assembly
,
-type
=>
$return_type
,
-adaptor
=>
$self
);
push
(
@result
,
$new_arch_id
);
}
$sth
->finish();
return
\
@result
;
}
sub
fetch_associated_archived {
my
$self
=
shift
;
my
$arch_id
=
shift
;
throw(
"Need a Bio::EnsEMBL::ArchiveStableId"
)
unless
(
$arch_id
and
ref
(
$arch_id
) and
$arch_id
->isa(
'Bio::EnsEMBL::ArchiveStableId'
));
my
$type
=
$arch_id
->type();
if
( !
defined
(
$type
) ) {
throw(
"Can't deduce ArchiveStableId type."
);
}
$type
=
lc
(
$type
);
my
$sql
=
qq(
SELECT ga.gene_stable_id,
ga.gene_version,
ga.transcript_stable_id,
ga.transcript_version,
ga.translation_stable_id,
ga.translation_version,
pa.peptide_seq,
ms.old_release,
ms.old_assembly,
ms.old_db_name
FROM (mapping_session ms, gene_archive ga)
LEFT JOIN peptide_archive pa
ON ga.peptide_archive_id = pa.peptide_archive_id
WHERE ga.mapping_session_id = ms.mapping_session_id
AND ga.${type}_stable_id = ?
AND ga.${type}_version = ?
);
my
$sth
=
$self
->prepare(
$sql
);
$sth
->bind_param(1,
$arch_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(2,
$arch_id
->version, SQL_SMALLINT);
$sth
->execute;
my
@result
= ();
while
(
my
$r
=
$sth
->fetchrow_hashref) {
my
@row
= ();
push
@row
, Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$r
->{
'gene_stable_id'
},
-version
=>
$r
->{
'gene_version'
},
-db_name
=>
$r
->{
'old_db_name'
},
-release
=>
$r
->{
'old_release'
},
-assembly
=>
$r
->{
'old_assembly'
},
-type
=>
'Gene'
,
-adaptor
=>
$self
);
push
@row
, Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$r
->{
'transcript_stable_id'
},
-version
=>
$r
->{
'transcript_version'
},
-db_name
=>
$r
->{
'old_db_name'
},
-release
=>
$r
->{
'old_release'
},
-assembly
=>
$r
->{
'old_assembly'
},
-type
=>
'Transcript'
,
-adaptor
=>
$self
);
if
(
$r
->{
'translation_stable_id'
}) {
push
@row
, Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$r
->{
'translation_stable_id'
},
-version
=>
$r
->{
'translation_version'
},
-db_name
=>
$r
->{
'old_db_name'
},
-release
=>
$r
->{
'old_release'
},
-assembly
=>
$r
->{
'old_assembly'
},
-type
=>
'Translation'
,
-adaptor
=>
$self
);
push
@row
,
$r
->{
'peptide_seq'
};
}
push
@result
, \
@row
;
}
return
\
@result
;
}
sub
fetch_predecessors_by_archive_id {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
@result
;
if
( ! (
defined
$arch_id
->stable_id() &&
defined
$arch_id
->db_name() )) {
throw(
"Need db_name for predecessor retrieval"
);
}
my
$sql
=
qq(
SELECT
sie.old_stable_id,
sie.old_version,
sie.type,
m.old_db_name,
m.old_release,
m.old_assembly
FROM mapping_session m, stable_id_event sie
WHERE sie.mapping_session_id = m.mapping_session_id
AND sie.new_stable_id = ?
AND m.new_db_name = ?
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->bind_param(1,
$arch_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(2,
$arch_id
->db_name, SQL_VARCHAR);
$sth
->execute();
my
(
$old_stable_id
,
$old_version
,
$type
,
$old_db_name
,
$old_release
,
$old_assembly
);
$sth
->bind_columns(\
$old_stable_id
, \
$old_version
, \
$type
, \
$old_db_name
, \
$old_release
, \
$old_assembly
);
while
(
$sth
->fetch) {
if
(
defined
$old_stable_id
) {
my
$old_arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$old_stable_id
,
-version
=>
$old_version
,
-db_name
=>
$old_db_name
,
-release
=>
$old_release
,
-assembly
=>
$old_assembly
,
-type
=>
$type
,
-adaptor
=>
$self
);
push
(
@result
,
$old_arch_id
);
}
}
$sth
->finish();
unless
(
@result
) {
$sql
=
qq(
SELECT
sie.new_stable_id,
sie.new_version,
sie.type,
m.new_db_name,
m.new_release,
m.new_assembly
FROM mapping_session m, stable_id_event sie
WHERE sie.mapping_session_id = m.mapping_session_id
AND sie.new_stable_id = ?
AND m.new_db_name = ?
)
;
$sth
=
$self
->prepare(
$sql
);
my
$curr_dbname
=
$arch_id
->db_name;
PREV:
while
(
my
$prev_dbname
=
$self
->previous_dbname(
$curr_dbname
)) {
$sth
->bind_param(1,
$arch_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(2,
$prev_dbname
, SQL_VARCHAR);
$sth
->execute();
$sth
->bind_columns(\
$old_stable_id
, \
$old_version
, \
$type
, \
$old_db_name
, \
$old_release
, \
$old_assembly
);
while
(
$sth
->fetch() ) {
if
(
defined
$old_stable_id
) {
my
$old_arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$old_stable_id
,
-version
=>
$old_version
,
-db_name
=>
$old_db_name
,
-release
=>
$old_release
,
-assembly
=>
$old_assembly
,
-type
=>
$type
,
-adaptor
=>
$self
);
push
(
@result
,
$old_arch_id
);
last
PREV;
}
}
$curr_dbname
=
$prev_dbname
;
}
$sth
->finish();
}
return
\
@result
;
}
sub
fetch_successors_by_archive_id {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
@result
;
if
( ! (
defined
$arch_id
->stable_id() &&
defined
$arch_id
->db_name() )) {
throw(
"Need db_name for successor retrieval"
);
}
my
$sql
=
qq(
SELECT
sie.new_stable_id,
sie.new_version,
sie.type,
m.new_db_name,
m.new_release,
m.new_assembly
FROM mapping_session m, stable_id_event sie
WHERE sie.mapping_session_id = m.mapping_session_id
AND sie.old_stable_id = ?
AND m.old_db_name = ?
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->bind_param(1,
$arch_id
->stable_id,SQL_VARCHAR);
$sth
->bind_param(2,
$arch_id
->db_name,SQL_VARCHAR);
$sth
->execute();
my
(
$new_stable_id
,
$new_version
,
$type
,
$new_db_name
,
$new_release
,
$new_assembly
);
$sth
->bind_columns(\
$new_stable_id
, \
$new_version
, \
$type
, \
$new_db_name
, \
$new_release
, \
$new_assembly
);
while
(
$sth
->fetch() ) {
if
(
defined
$new_stable_id
) {
my
$new_arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$new_stable_id
,
-version
=>
$new_version
,
-db_name
=>
$new_db_name
,
-release
=>
$new_release
,
-assembly
=>
$new_assembly
,
-type
=>
$type
,
-adaptor
=>
$self
);
push
(
@result
,
$new_arch_id
);
}
}
$sth
->finish();
unless
(
@result
) {
$sql
=
qq(
SELECT
sie.old_stable_id,
sie.old_version,
sie.type,
m.old_db_name,
m.old_release,
m.old_assembly
FROM mapping_session m, stable_id_event sie
WHERE sie.mapping_session_id = m.mapping_session_id
AND sie.old_stable_id = ?
AND m.old_db_name = ?
)
;
$sth
=
$self
->prepare(
$sql
);
my
$curr_dbname
=
$arch_id
->db_name;
NEXTDB:
while
(
my
$next_dbname
=
$self
->next_dbname(
$curr_dbname
)) {
$sth
->bind_param(1,
$arch_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(2,
$next_dbname
, SQL_VARCHAR);
$sth
->execute();
$sth
->bind_columns(\
$new_stable_id
, \
$new_version
, \
$type
, \
$new_db_name
, \
$new_release
, \
$new_assembly
);
while
(
$sth
->fetch() ) {
if
(
defined
$new_stable_id
) {
my
$new_arch_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$new_stable_id
,
-version
=>
$new_version
,
-db_name
=>
$new_db_name
,
-release
=>
$new_release
,
-assembly
=>
$new_assembly
,
-type
=>
$type
,
-adaptor
=>
$self
);
push
(
@result
,
$new_arch_id
);
last
NEXTDB;
}
}
$curr_dbname
=
$next_dbname
;
}
$sth
->finish();
}
return
\
@result
;
}
sub
fetch_history_tree_by_stable_id {
my
(
$self
,
$stable_id
,
$num_high_scorers
,
$max_rows
,
$time_limit
) =
@_
;
throw(
"Expecting a stable ID argument."
)
unless
$stable_id
;
$num_high_scorers
||= NUM_HIGH_SCORERS;
$max_rows
||= MAX_ROWS;
my
$sql
=
qq(
SELECT old_stable_id, old_version,
old_db_name, old_release, old_assembly,
new_stable_id, new_version,
new_db_name, new_release, new_assembly,
type, score
FROM stable_id_event sie, mapping_session ms
WHERE sie.mapping_session_id = ms.mapping_session_id
AND sie.old_stable_id = ?
UNION
SELECT old_stable_id, old_version,
old_db_name, old_release, old_assembly,
new_stable_id, new_version,
new_db_name, new_release, new_assembly,
type, score
FROM stable_id_event sie, mapping_session ms
WHERE sie.mapping_session_id = ms.mapping_session_id
AND sie.new_stable_id = ?
)
;
my
$sth
=
$self
->prepare(
$sql
);
my
$history
= Bio::EnsEMBL::StableIdHistoryTree->new(
-CURRENT_DBNAME
=>
$self
->dbc->dbname,
-CURRENT_RELEASE
=>
$self
->get_current_release,
-CURRENT_ASSEMBLY
=>
$self
->get_current_assembly,
);
my
%do
= (
$stable_id
=> 1);
my
%done
;
while
(
my
(
$id
) =
keys
(
%do
)) {
if
(
scalar
(
keys
(
%done
)) >
$max_rows
) {
$history
->is_incomplete(1);
$sth
->finish;
last
;
}
delete
$do
{
$id
};
$done
{
$id
} = 1;
$sth
->bind_param(1,
$id
, SQL_VARCHAR);
$sth
->bind_param(2,
$id
, SQL_VARCHAR);
$sth
->execute;
my
@events
;
while
(
my
$r
=
$sth
->fetchrow_hashref) {
my
(
$old_id
,
$new_id
);
if
(
$r
->{
'old_stable_id'
}) {
$old_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$r
->{
'old_stable_id'
},
-version
=>
$r
->{
'old_version'
},
-db_name
=>
$r
->{
'old_db_name'
},
-release
=>
$r
->{
'old_release'
},
-assembly
=>
$r
->{
'old_assembly'
},
-type
=>
$r
->{
'type'
},
-adaptor
=>
$self
);
}
if
(
$r
->{
'new_stable_id'
}) {
$new_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$r
->{
'new_stable_id'
},
-version
=>
$r
->{
'new_version'
},
-db_name
=>
$r
->{
'new_db_name'
},
-release
=>
$r
->{
'new_release'
},
-assembly
=>
$r
->{
'new_assembly'
},
-type
=>
$r
->{
'type'
},
-adaptor
=>
$self
);
}
my
$event
= Bio::EnsEMBL::StableIdEvent->new(
-old_id
=>
$old_id
,
-new_id
=>
$new_id
,
-score
=>
$r
->{
'score'
}
);
push
@events
,
$event
;
}
my
@others
;
foreach
my
$event
(
@events
) {
my
$old_id
=
$event
->old_ArchiveStableId;
my
$new_id
=
$event
->new_ArchiveStableId;
if
(!
$old_id
|| !
$new_id
|| (
$old_id
->stable_id eq
$new_id
->stable_id)) {
$history
->add_StableIdEvents(
$event
);
}
else
{
push
@others
,
$event
;
}
}
my
$k
= 0;
foreach
my
$event
(
sort
{
$b
->score <=>
$a
->score }
@others
) {
$history
->add_StableIdEvents(
$event
);
$do
{
$event
->old_ArchiveStableId->stable_id} = 1
unless
$done
{
$event
->old_ArchiveStableId->stable_id};
$do
{
$event
->new_ArchiveStableId->stable_id} = 1
unless
$done
{
$event
->new_ArchiveStableId->stable_id};
last
if
(++
$k
==
$num_high_scorers
);
}
}
$sth
->finish;
$history
->consolidate_tree;
$self
->add_all_current_to_history(
$history
);
$history
->calculate_coords(
$time_limit
);
return
$history
;
}
sub
add_all_current_to_history {
my
$self
=
shift
;
my
$history
=
shift
;
unless
(
$history
and
$history
->isa(
'Bio::EnsEMBL::StableIdHistoryTree'
)) {
throw(
"Need a Bio::EnsEMBL::StableIdHistoryTree."
);
}
my
@ids
= @{
$history
->get_unique_stable_ids };
my
$id_string
=
join
(
"', '"
,
@ids
);
my
$tmp_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$ids
[0]);
my
$type
=
lc
(
$self
->_resolve_type(
$tmp_id
));
return
unless
(
$type
);
my
$sql
=
qq(
SELECT stable_id, version FROM ${type}
WHERE stable_id IN ('$id_string')
);
my
$sth
=
$self
->prepare(
$sql
);
$sth
->execute;
while
(
my
(
$stable_id
,
$version
) =
$sth
->fetchrow_array) {
my
$new_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$stable_id
,
-version
=>
$version
,
-current_version
=>
$version
,
-db_name
=>
$self
->dbc->dbname,
-release
=>
$self
->get_current_release,
-assembly
=>
$self
->get_current_assembly,
-type
=>
$type
,
-adaptor
=>
$self
);
my
$event
=
$history
->get_latest_StableIdEvent(
$new_id
);
next
unless
(
$event
);
if
(
$event
->old_ArchiveStableId and
$event
->old_ArchiveStableId->stable_id eq
$stable_id
) {
$event
->new_ArchiveStableId(
$new_id
);
}
else
{
my
$new_event
= Bio::EnsEMBL::StableIdEvent->new(
-old_id
=>
$event
->new_ArchiveStableId,
-new_id
=>
$new_id
,
-score
=>
$event
->score,
);
$history
->add_StableIdEvents(
$new_event
);
}
}
$history
->flush_ArchiveStableIds;
$history
->add_ArchiveStableIds_for_events;
}
sub
fetch_successor_history {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
$current_db_name
=
$self
->list_dbnames->[0];
my
$dbname
=
$arch_id
->db_name;
if
(
$dbname
eq
$current_db_name
) {
return
[
$arch_id
];
}
my
$old
= [];
my
@result
= ();
push
@$old
,
$arch_id
;
while
(
$dbname
ne
$current_db_name
) {
my
$new
= [];
while
(
my
$asi
= (
shift
@$old
)) {
push
@$new
, @{
$asi
->get_all_successors };
}
if
(
@$new
) {
$dbname
=
$new
->[0]->db_name;
}
else
{
last
;
}
my
%unique
=
map
{
join
(
":"
,
$_
->stable_id,
$_
->version,
$_
->release) =>
$_
}
@$new
;
@$new
=
values
%unique
;
@$old
=
@$new
;
push
@result
,
@$new
;
}
return
\
@result
;
}
sub
fetch_predecessor_history {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
$oldest_db_name
=
$self
->list_dbnames->[-1];
my
$dbname
=
$arch_id
->db_name;
if
(
$dbname
eq
$oldest_db_name
) {
return
[
$arch_id
];
}
my
$old
= [];
my
@result
= ();
push
@$old
,
$arch_id
;
while
(
$dbname
ne
$oldest_db_name
) {
my
$new
= [];
while
(
my
$asi
= (
shift
@$old
)) {
push
@$new
, @{
$asi
->get_all_predecessors };
}
if
(
@$new
) {
$dbname
=
$new
->[0]->db_name;
}
else
{
last
;
}
my
%unique
=
map
{
join
(
":"
,
$_
->stable_id,
$_
->version,
$_
->release) =>
$_
}
@$new
;
@$new
=
values
%unique
;
@$old
=
@$new
;
push
@result
,
@$new
;
}
return
\
@result
;
}
sub
fetch_stable_id_event {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
$stable_id
=
shift
;
my
$event
;
my
$sql
=
qq(
SELECT sie.old_stable_id, sie.old_version, sie.new_stable_id, sie.new_version, sie.type, sie.score,
ms.old_db_name, ms.new_db_name, ms.old_release, ms.new_release, ms.old_assembly, ms.new_assembly
FROM stable_id_event sie, mapping_session ms
WHERE ms.mapping_session_id = sie.mapping_session_id
AND (old_stable_id = ? AND ms.old_db_name = ? AND old_release = ? AND old_assembly = ? AND new_stable_id = ?)
OR (new_stable_id = ? AND ms.new_db_name = ? AND new_release = ? AND new_assembly = ? AND old_stable_id = ?)
);
my
$sth
=
$self
->prepare(
$sql
);
$sth
->bind_param(1,
$arch_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(2,
$arch_id
->db_name, SQL_VARCHAR);
$sth
->bind_param(3,
$arch_id
->release, SQL_INTEGER);
$sth
->bind_param(4,
$arch_id
->assembly, SQL_VARCHAR);
$sth
->bind_param(5,
$stable_id
, SQL_VARCHAR);
$sth
->bind_param(6,
$arch_id
->stable_id, SQL_VARCHAR);
$sth
->bind_param(7,
$arch_id
->db_name, SQL_VARCHAR);
$sth
->bind_param(8,
$arch_id
->release, SQL_INTEGER);
$sth
->bind_param(9,
$arch_id
->assembly, SQL_VARCHAR);
$sth
->bind_param(10,
$stable_id
, SQL_VARCHAR);
$sth
->execute();
my
(
$old_stable_id
,
$old_version
,
$new_stable_id
,
$new_version
,
$type
,
$score
);
my
(
$old_db_name
,
$new_db_name
,
$old_release
,
$new_release
,
$old_assembly
,
$new_assembly
);
$sth
->bind_columns(\
$old_stable_id
, \
$old_version
, \
$new_stable_id
, \
$new_version
, \
$type
, \
$score
,
\
$old_db_name
, \
$new_db_name
, \
$old_release
, \
$new_release
, \
$old_assembly
, \
$new_assembly
);
while
(
$sth
->fetch) {
if
(
$new_stable_id
eq
$stable_id
) {
my
$alt_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$new_stable_id
,
-version
=>
$new_version
,
-db_name
=>
$new_db_name
,
-release
=>
$new_release
,
-assembly
=>
$new_assembly
,
-type
=>
$type
,
-adaptor
=>
$self
);
$event
= Bio::EnsEMBL::StableIdEvent->new(
-old_id
=>
$arch_id
,
-new_id
=>
$alt_id
,
-score
=>
$score
);
}
elsif
(
$old_stable_id
eq
$stable_id
) {
my
$alt_id
= Bio::EnsEMBL::ArchiveStableId->new(
-stable_id
=>
$old_stable_id
,
-version
=>
$old_version
,
-db_name
=>
$old_db_name
,
-release
=>
$old_release
,
-assembly
=>
$old_assembly
,
-type
=>
$type
,
-adaptor
=>
$self
);
$event
= Bio::EnsEMBL::StableIdEvent->new(
-old_id
=>
$alt_id
,
-new_id
=>
$arch_id
,
-score
=>
$score
);
}
}
$sth
->finish();
return
$event
;
}
sub
list_dbnames {
my
$self
=
shift
;
if
( !
defined
$self
->{
'dbnames'
} ) {
my
$sql
=
qq(
SELECT old_db_name, new_db_name
FROM mapping_session
ORDER BY created DESC
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->execute();
my
(
$old_db_name
,
$new_db_name
);
my
@dbnames
= ();
my
%seen
;
$sth
->bind_columns( \
$old_db_name
, \
$new_db_name
);
while
(
$sth
->fetch() ) {
push
(@{
$self
->{
'dbnames'
} },
$new_db_name
)
unless
(
$seen
{
$new_db_name
});
$seen
{
$new_db_name
} = 1;
push
(@{
$self
->{
'dbnames'
} },
$old_db_name
)
unless
(
$seen
{
$old_db_name
});
$seen
{
$old_db_name
} = 1;
}
$sth
->finish();
}
return
$self
->{
'dbnames'
};
}
sub
previous_dbname {
my
$self
=
shift
;
my
$dbname
=
shift
;
my
$curr_idx
=
$self
->_dbname_index(
$dbname
);
my
@dbnames
= @{
$self
->list_dbnames };
if
(
$curr_idx
==
@dbnames
) {
return
undef
;
}
else
{
return
$dbnames
[
$curr_idx
+1];
}
}
sub
next_dbname {
my
$self
=
shift
;
my
$dbname
=
shift
;
my
$curr_idx
=
$self
->_dbname_index(
$dbname
);
my
@dbnames
= @{
$self
->list_dbnames };
if
(
$curr_idx
== 0) {
return
undef
;
}
else
{
return
$dbnames
[
$curr_idx
-1];
}
}
sub
_dbname_index {
my
$self
=
shift
;
my
$dbname
=
shift
;
my
@dbnames
= @{
$self
->list_dbnames };
for
(
my
$i
= 0;
$i
<
@dbnames
;
$i
++) {
if
(
$dbnames
[
$i
] eq
$dbname
) {
return
$i
;
}
}
}
sub
get_peptide {
my
$self
=
shift
;
my
$arch_id
=
shift
;
if
(
lc
(
$arch_id
->type() ) ne
'translation'
) {
return
undef
;
}
my
$sql
=
qq(
SELECT pa.peptide_seq
FROM peptide_archive pa, gene_archive ga
WHERE ga.translation_stable_id = ?
AND ga.translation_version = ?
AND ga.peptide_archive_id = pa.peptide_archive_id
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->bind_param( 1,
$arch_id
->stable_id, SQL_VARCHAR );
$sth
->bind_param( 2,
$arch_id
->version, SQL_SMALLINT );
$sth
->execute();
my
(
$peptide_seq
) =
$sth
->fetchrow_array();
$sth
->finish();
return
$peptide_seq
;
}
sub
get_current_release {
my
$self
=
shift
;
unless
(
$self
->{
'current_release'
}) {
my
$mca
=
$self
->db->get_MetaContainer;
my
(
$release
) = @{
$mca
->list_value_by_key(
'schema_version'
) };
$self
->{
'current_release'
} =
$release
;
}
return
$self
->{
'current_release'
};
}
sub
get_current_assembly {
my
$self
=
shift
;
unless
(
$self
->{
'current_assembly'
}) {
my
$mca
=
$self
->db->get_MetaContainer;
my
(
$assembly
) = @{
$mca
->list_value_by_key(
'assembly.default'
) };
$self
->{
'current_assembly'
} =
$assembly
;
}
return
$self
->{
'current_assembly'
};
}
sub
lookup_current {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
$type
=
lc
(
$arch_id
->type );
unless
(
$type
) {
warning(
"Can't lookup current version without a type."
);
return
0;
}
my
$sql
=
qq(
SELECT version FROM ${type}
WHERE stable_id = ?
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->execute(
$arch_id
->stable_id );
my
(
$version
) =
$sth
->fetchrow_array;
$sth
->finish;
if
(
$version
) {
$arch_id
->current_version(
$version
);
return
1;
}
return
0;
}
sub
_resolve_type {
my
$self
=
shift
;
my
$arch_id
=
shift
;
my
$stable_id
=
$arch_id
->stable_id();
my
$id_type
;
if
(
$stable_id
=~ /^AGAP.*/) {
if
(
$stable_id
=~ /.*-RA/) {
$id_type
=
"Transcript"
;
}
elsif
(
$stable_id
=~ /.*-PA/) {
$id_type
=
"Translation"
;
}
else
{
$id_type
=
"Gene"
;
}
}
elsif
(
$stable_id
=~ /.
*G
\d+(\.\d+)?$/) {
$id_type
=
"Gene"
;
}
elsif
(
$stable_id
=~ /.
*T
\d+(\.\d+)?$/) {
$id_type
=
"Transcript"
;
}
elsif
(
$stable_id
=~ /.
*P
\d+(\.\d+)?$/) {
$id_type
=
"Translation"
;
}
elsif
(
$stable_id
=~ /.
*E
\d+(\.\d+)?$/) {
$id_type
=
"Exon"
;
}
else
{
my
$sql
=
qq(
SELECT type from stable_id_event
WHERE old_stable_id = ?
OR new_stable_id = ?
)
;
my
$sth
=
$self
->prepare(
$sql
);
$sth
->execute(
$stable_id
,
$stable_id
);
(
$id_type
) =
$sth
->fetchrow_array;
$sth
->finish;
}
warning(
"Couldn't resolve stable ID type."
)
unless
(
$id_type
);
$arch_id
->type(
$id_type
);
}
1;