$Bio::EnsEMBL::DBSQL::PredictionTranscriptAdaptor::VERSION
=
'113.0.0'
;
@ISA
=
qw( Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor )
;
sub
_tables {
my
$self
=
shift
;
return
[
'prediction_transcript'
,
'pt'
];
}
sub
_columns {
my
$self
=
shift
;
return
qw( pt.prediction_transcript_id
pt.seq_region_id
pt.seq_region_start
pt.seq_region_end
pt.seq_region_strand
pt.analysis_id
pt.display_label)
;
}
sub
fetch_by_stable_id {
my
$self
=
shift
;
my
$stable_id
=
shift
;
throw(
'Stable_id argument expected'
)
if
(!
$stable_id
);
my
$syn
=
$self
->_tables()->[1];
$self
->bind_param_generic_fetch(
$stable_id
,SQL_VARCHAR);
my
$pts
=
$self
->generic_fetch(
"$syn.display_label = ?"
);
return
(
@$pts
) ?
$pts
->[0] :
undef
;
}
sub
fetch_all_by_Slice {
my
$self
=
shift
;
my
$slice
=
shift
;
my
$logic_name
=
shift
;
my
$load_exons
=
shift
;
my
$transcripts
=
$self
->SUPER::fetch_all_by_Slice(
$slice
,
$logic_name
);
if
(!
$load_exons
||
@$transcripts
< 1) {
return
$transcripts
;
}
my
(
$min_start
,
$max_end
);
my
$ext_slice
;
unless
(
$slice
->is_circular()) {
foreach
my
$t
(
@$transcripts
) {
if
(!
defined
(
$min_start
) ||
$t
->seq_region_start() <
$min_start
) {
$min_start
=
$t
->seq_region_start();
}
if
(!
defined
(
$max_end
) ||
$t
->seq_region_end() >
$max_end
) {
$max_end
=
$t
->seq_region_end();
}
}
if
(
$min_start
>=
$slice
->start() &&
$max_end
<=
$slice
->end()) {
$ext_slice
=
$slice
;
}
else
{
my
$sa
=
$self
->db()->get_SliceAdaptor();
$ext_slice
=
$sa
->fetch_by_region(
$slice
->coord_system->name(),
$slice
->seq_region_name(),
$min_start
,
$max_end
,
$slice
->strand(),
$slice
->coord_system->version());
}
}
else
{
my
(
$min_start_feature
,
$max_end_feature
);
foreach
my
$t
(
@$transcripts
) {
if
(!
defined
(
$min_start
) || (
$t
->start() >= 0 &&
$t
->start() <
$min_start
)) {
$min_start
=
$t
->start();
$min_start_feature
=
$t
;
}
if
(!
defined
(
$max_end
) || (
$t
->end() >= 0 &&
$t
->end() >
$max_end
)) {
$max_end
=
$t
->end();
$max_end_feature
=
$t
;
}
}
$min_start
=
$min_start_feature
->seq_region_start();
$max_end
=
$max_end_feature
->seq_region_end();
my
$sa
=
$self
->db()->get_SliceAdaptor();
$ext_slice
=
$sa
->fetch_by_region(
$slice
->coord_system->name(),
$slice
->seq_region_name(),
$min_start
,
$max_end
,
$slice
->strand(),
$slice
->coord_system->version());
}
my
%tr_hash
=
map
{
$_
->
dbID
=>
$_
}
@$transcripts
;
my
$tr_id_str
=
'('
.
join
(
','
,
keys
%tr_hash
) .
')'
;
my
$sth
=
$self
->prepare
(
"SELECT prediction_transcript_id, prediction_exon_id, exon_rank "
.
"FROM prediction_exon "
.
"WHERE prediction_transcript_id IN $tr_id_str"
);
$sth
->execute();
my
(
$ex_id
,
$tr_id
,
$rank
);
$sth
->bind_columns(\
$tr_id
, \
$ex_id
, \
$rank
);
my
%ex_tr_hash
;
while
(
$sth
->fetch()) {
$ex_tr_hash
{
$ex_id
} ||= [];
push
@{
$ex_tr_hash
{
$ex_id
}}, [
$tr_hash
{
$tr_id
},
$rank
];
}
$sth
->finish();
my
$ea
=
$self
->db()->get_PredictionExonAdaptor();
my
$exons
=
$ea
->fetch_all_by_Slice(
$ext_slice
);
foreach
my
$ex
(
@$exons
) {
$ex
=
$ex
->transfer(
$slice
)
if
(
$slice
!=
$ext_slice
);
if
(!
$ex
) {
throw(
"Unexpected. PredictionExon could not be transfered onto "
.
"PredictionTranscript slice."
);
}
foreach
my
$row
(@{
$ex_tr_hash
{
$ex
->dbID()}}) {
my
(
$tr
,
$rank
) =
@$row
;
$tr
->add_Exon(
$ex
,
$rank
);
}
}
return
$transcripts
;
}
sub
fetch_by_prediction_exon_id {
my
(
$self
,
$prediction_exon_id
) =
@_
;
my
$sth
=
$self
->prepare(
qq(
SELECT pe.prediction_transcript_id
FROM prediction_exon pe
WHERE pe.prediction_exon_id = ?
)
);
$sth
->bind_param(1,
$prediction_exon_id
, SQL_INTEGER);
$sth
->execute();
my
(
$prediction_transcript_id
) =
$sth
->fetchrow_array();
$sth
->finish();
return
undef
if
(!
defined
$prediction_transcript_id
);
my
$prediction_transcript
=
$self
->fetch_by_dbID(
$prediction_transcript_id
);
return
$prediction_transcript
;
}
sub
_objs_from_sth {
my
(
$self
,
$sth
,
$mapper
,
$dest_slice
) =
@_
;
my
$sa
=
$self
->db()->get_SliceAdaptor();
my
$aa
=
$self
->db()->get_AnalysisAdaptor();
my
@ptranscripts
;
my
%analysis_hash
;
my
%slice_hash
;
my
%sr_name_hash
;
my
%sr_cs_hash
;
my
(
$prediction_transcript_id
,
$seq_region_id
,
$seq_region_start
,
$seq_region_end
,
$seq_region_strand
,
$analysis_id
,
$display_label
);
$sth
->bind_columns(\(
$prediction_transcript_id
,
$seq_region_id
,
$seq_region_start
,
$seq_region_end
,
$seq_region_strand
,
$analysis_id
,
$display_label
));
my
$dest_slice_start
;
my
$dest_slice_end
;
my
$dest_slice_strand
;
my
$dest_slice_length
;
my
$dest_slice_cs
;
my
$dest_slice_sr_name
;
my
$dest_slice_sr_id
;
my
$asma
;
if
(
$dest_slice
) {
$dest_slice_start
=
$dest_slice
->start();
$dest_slice_end
=
$dest_slice
->end();
$dest_slice_strand
=
$dest_slice
->strand();
$dest_slice_length
=
$dest_slice
->
length
();
$dest_slice_cs
=
$dest_slice
->coord_system();
$dest_slice_sr_name
=
$dest_slice
->seq_region_name();
$dest_slice_sr_id
=
$dest_slice
->get_seq_region_id();
$asma
=
$self
->db->get_AssemblyMapperAdaptor();
}
FEATURE:
while
(
$sth
->fetch()) {
my
$analysis
=
$analysis_hash
{
$analysis_id
} ||=
$aa
->fetch_by_dbID(
$analysis_id
);
$analysis_hash
{
$analysis_id
} =
$analysis
;
$seq_region_id
=
$self
->get_seq_region_id_internal(
$seq_region_id
);
my
$slice
=
$slice_hash
{
"ID:"
.
$seq_region_id
};
if
(!
$slice
) {
$slice
=
$sa
->fetch_by_seq_region_id(
$seq_region_id
);
$slice_hash
{
"ID:"
.
$seq_region_id
} =
$slice
;
$sr_name_hash
{
$seq_region_id
} =
$slice
->seq_region_name();
$sr_cs_hash
{
$seq_region_id
} =
$slice
->coord_system();
}
if
(!
$mapper
&&
$dest_slice
&& !
$dest_slice_cs
->equals(
$slice
->coord_system)) {
$mapper
=
$asma
->fetch_by_CoordSystems(
$dest_slice_cs
,
$slice
->coord_system);
}
my
$sr_name
=
$sr_name_hash
{
$seq_region_id
};
my
$sr_cs
=
$sr_cs_hash
{
$seq_region_id
};
if
(
$mapper
) {
if
(
defined
$dest_slice
&&
$mapper
->isa(
'Bio::EnsEMBL::ChainedAssemblyMapper'
) ) {
(
$seq_region_id
,
$seq_region_start
,
$seq_region_end
,
$seq_region_strand
) =
$mapper
->
map
(
$sr_name
,
$seq_region_start
,
$seq_region_end
,
$seq_region_strand
,
$sr_cs
, 1,
$dest_slice
);
}
else
{
(
$seq_region_id
,
$seq_region_start
,
$seq_region_end
,
$seq_region_strand
) =
$mapper
->fastmap(
$sr_name
,
$seq_region_start
,
$seq_region_end
,
$seq_region_strand
,
$sr_cs
);
}
next
FEATURE
if
(!
defined
(
$seq_region_id
));
$slice
=
$slice_hash
{
"ID:"
.
$seq_region_id
} ||=
$sa
->fetch_by_seq_region_id(
$seq_region_id
);
}
if
(
defined
(
$dest_slice
)) {
my
$seq_region_len
=
$dest_slice
->seq_region_length();
if
(
$dest_slice_strand
== 1 ) {
$seq_region_start
=
$seq_region_start
-
$dest_slice_start
+ 1;
$seq_region_end
=
$seq_region_end
-
$dest_slice_start
+ 1;
if
(
$dest_slice
->is_circular ) {
if
(
$seq_region_start
>
$seq_region_end
) {
if
(
$seq_region_end
>
$dest_slice_start
) {
$seq_region_start
-=
$seq_region_len
;
}
if
(
$seq_region_end
< 0 ) {
$seq_region_end
+=
$seq_region_len
;
}
}
else
{
if
(
$dest_slice_start
>
$dest_slice_end
&&
$seq_region_end
< 0) {
$seq_region_start
+=
$seq_region_len
;
$seq_region_end
+=
$seq_region_len
;
}
}
}
}
else
{
my
$start
=
$dest_slice_end
-
$seq_region_end
+ 1;
my
$end
=
$dest_slice_end
-
$seq_region_start
+ 1;
if
(
$dest_slice
->is_circular()) {
if
(
$dest_slice_start
>
$dest_slice_end
) {
if
(
$seq_region_start
>=
$dest_slice_start
) {
$end
+=
$seq_region_len
;
$start
+=
$seq_region_len
if
$seq_region_end
>
$dest_slice_start
;
}
elsif
(
$seq_region_start
<=
$dest_slice_end
) {
}
elsif
(
$seq_region_end
>=
$dest_slice_start
) {
$start
+=
$seq_region_len
;
$end
+=
$seq_region_len
;
}
elsif
(
$seq_region_end
<=
$dest_slice_end
) {
$end
+=
$seq_region_len
if
$end
< 0;
}
elsif
(
$seq_region_start
>
$seq_region_end
) {
$end
+=
$seq_region_len
;
}
}
else
{
if
(
$seq_region_start
<=
$dest_slice_end
and
$seq_region_end
>=
$dest_slice_start
) {
}
elsif
(
$seq_region_start
>
$seq_region_end
) {
if
(
$seq_region_start
<=
$dest_slice_end
) {
$start
-=
$seq_region_len
;
}
elsif
(
$seq_region_end
>=
$dest_slice_start
) {
$end
+=
$seq_region_len
;
}
}
}
}
$seq_region_start
=
$start
;
$seq_region_end
=
$end
;
$seq_region_strand
*= -1;
}
if
(
$seq_region_end
< 1
||
$seq_region_start
>
$dest_slice_length
|| (
$dest_slice_sr_id
!=
$seq_region_id
)) {
next
FEATURE;
}
$slice
=
$dest_slice
;
}
push
(
@ptranscripts
,
$self
->_create_feature(
'Bio::EnsEMBL::PredictionTranscript'
, {
'-start'
=>
$seq_region_start
,
'-end'
=>
$seq_region_end
,
'-strand'
=>
$seq_region_strand
,
'-adaptor'
=>
$self
,
'-slice'
=>
$slice
,
'-analysis'
=>
$analysis
,
'-dbID'
=>
$prediction_transcript_id
,
'-display_label'
=>
$display_label
} ) );
}
return
\
@ptranscripts
;
}
sub
store {
my
(
$self
,
@pre_transcripts
) =
@_
;
my
$ptstore_sth
=
$self
->prepare
(
qq{INSERT INTO prediction_transcript (seq_region_id, seq_region_start,
seq_region_end, seq_region_strand,
analysis_id, display_label)
VALUES( ?, ?, ?, ?, ?, ?)}
);
my
$ptupdate_sth
=
$self
->prepare
(
qq{UPDATE prediction_transcript SET display_label = ?
WHERE prediction_transcript_id = ?}
);
my
$db
=
$self
->db();
my
$analysis_adaptor
=
$db
->get_AnalysisAdaptor();
my
$pexon_adaptor
=
$db
->get_PredictionExonAdaptor();
FEATURE:
foreach
my
$pt
(
@pre_transcripts
) {
if
(!
ref
(
$pt
) || !
$pt
->isa(
'Bio::EnsEMBL::PredictionTranscript'
)) {
throw(
'Expected PredictionTranscript argument not ['
.
ref
(
$pt
).
']'
);
}
if
(
$pt
->is_stored(
$db
)) {
warning(
'Not storing already stored prediction transcript '
.
$pt
->dbID);
next
FEATURE;
}
my
$analysis
=
$pt
->analysis();
if
(!
$analysis
) {
throw(
'Prediction transcript must have analysis to be stored.'
);
}
if
(!
$analysis
->is_stored(
$db
)) {
$analysis_adaptor
->store(
$analysis
);
}
$pt
->recalculate_coordinates();
my
$original
=
$pt
;
my
$seq_region_id
;
(
$pt
,
$seq_region_id
) =
$self
->_pre_store(
$pt
);
$ptstore_sth
->bind_param(1,
$seq_region_id
,SQL_INTEGER);
$ptstore_sth
->bind_param(2,
$pt
->start,SQL_INTEGER);
$ptstore_sth
->bind_param(3,
$pt
->end,SQL_INTEGER);
$ptstore_sth
->bind_param(4,
$pt
->strand,SQL_TINYINT);
$ptstore_sth
->bind_param(5,
$analysis
->dbID,SQL_INTEGER);
$ptstore_sth
->bind_param(6,
$pt
->display_label,SQL_VARCHAR);
$ptstore_sth
->execute();
my
$pt_id
=
$self
->last_insert_id(
'prediction_transcript_id'
,
undef
,
'prediction_transcript'
);
$original
->dbID(
$pt_id
);
$original
->adaptor(
$self
);
my
$rank
= 1;
foreach
my
$pexon
(@{
$original
->get_all_Exons}) {
$pexon_adaptor
->store(
$pexon
,
$pt_id
,
$rank
++);
}
if
(!
defined
(
$pt
->display_label())) {
my
$zeros
=
'0'
x (11 -
length
(
$pt_id
));
my
$display_label
=
uc
(
$analysis
->logic_name()) .
$zeros
.
$pt_id
;
$ptupdate_sth
->bind_param(1,
$display_label
,SQL_VARCHAR);
$ptupdate_sth
->bind_param(2,
$pt_id
,SQL_INTEGER);
$ptupdate_sth
->execute();
$original
->display_label(
$display_label
);
}
}
}
sub
remove {
my
$self
=
shift
;
my
$pre_trans
=
shift
;
if
(!
ref
(
$pre_trans
)||!
$pre_trans
->isa(
'Bio::EnsEMBL::PredictionTranscript'
)){
throw(
'Expected PredictionTranscript argument.'
);
}
if
(!
$pre_trans
->is_stored(
$self
->db())) {
warning(
'PredictionTranscript is not stored in this DB - not removing.'
);
return
;
}
my
$pexon_adaptor
=
$self
->db()->get_PredictionExonAdaptor();
foreach
my
$pexon
(@{
$pre_trans
->get_all_Exons}) {
$pexon_adaptor
->remove(
$pexon
);
}
my
$sth
=
$self
->prepare( "DELETE FROM prediction_transcript
WHERE prediction_transcript_id = ?" );
$sth
->bind_param(1,
$pre_trans
->dbID,SQL_INTEGER);
$sth
->execute();
$pre_trans
->dbID(
undef
);
$pre_trans
->adaptor(
undef
);
}
sub
list_dbIDs {
my
(
$self
,
$ordered
) =
@_
;
return
$self
->_list_dbIDs(
"prediction_transcript"
,
undef
,
$ordered
);
}
1;