$Bio::EnsEMBL::DBSQL::AnalysisAdaptor::VERSION
=
'113.0.0'
;
@ISA
=
qw( Bio::EnsEMBL::DBSQL::BaseAdaptor)
;
sub
new {
my
(
$class
,
$db
) =
@_
;
my
$self
=
$class
->SUPER::new(
$db
);
$self
->fetch_all;
return
$self
;
}
sub
fetch_all {
my
$self
=
shift
;
my
(
$analysis
,
$dbID
);
my
$rowHashRef
;
$self
->{_cache} = {};
$self
->{_logic_name_cache} = {};
my
$sth
=
$self
->prepare(
q {
SELECT analysis.analysis_id, logic_name,
program, program_version, program_file,
db, db_version, db_file,
module, module_version,
gff_source, gff_feature,
created, parameters, description, display_label, displayable, web_data
FROM analysis
LEFT JOIN analysis_description
ON analysis.analysis_id = analysis_description.analysis_id } );
$sth
->execute;
while
(
$rowHashRef
=
$sth
->fetchrow_hashref ) {
my
$analysis
=
$self
->_objFromHashref(
$rowHashRef
);
$self
->{_cache}->{
$analysis
->dbID} =
$analysis
;
$self
->{_logic_name_cache}->{
lc
(
$analysis
->logic_name())} =
$analysis
;
}
my
@ana
=
values
%{
$self
->{_cache}};
return
\
@ana
;
}
sub
fetch_all_by_feature_class {
my
$self
=
shift
;
my
$feat_class
=
shift
|| throw(
"Need a feature type, e.g. SimpleFeature"
);
if
(
$feat_class
=~ /Density/) {
$feat_class
=
'DensityType'
;
}
my
$adaptor
=
$self
->db->get_adaptor(
$feat_class
);
if
(!
$adaptor
) {
throw(
"$feat_class is not a know feature. No adaptor found"
);
}
my
$has_analysis
= 0;
my
@columns
=
$adaptor
->_columns;
foreach
my
$column
(
@columns
) {
if
(
$column
=~ /analysis/) {
$has_analysis
= 1;
last
;
}
}
if
(
$has_analysis
== 0) {
throw(
"$feat_class does not have an analysis column"
);
}
my
@tables
=
$adaptor
->_tables();
my
$table
=
$tables
[0]->[0];
my
$sql_t
=
qq|
SELECT DISTINCT analysis_id FROM %s |
;
my
$sql
=
sprintf
(
$sql_t
,
$table
);
my
$sth
=
$self
->prepare(
$sql
);
my
$rv
=
$sth
->execute();
my
$res
=
$sth
->fetchall_arrayref;
my
@analyses
;
foreach
my
$r
( @{
$res
} ){
my
$analysis
=
$self
->fetch_by_dbID(
$r
->[0])
|| throw(
"analysis_id $r->[0] from $table table "
.
"is not in the analysis table!"
);
push
@analyses
,
$analysis
;
}
return
[
@analyses
];
}
sub
fetch_by_dbID {
my
$self
=
shift
;
my
$id
=
shift
;
if
(
defined
$self
->{_cache}->{
$id
} ) {
return
$self
->{_cache}->{
$id
};
}
my
$query
=
q{
SELECT analysis.analysis_id, logic_name,
program, program_version, program_file,
db, db_version, db_file,
module, module_version,
gff_source, gff_feature,
created, parameters, description, display_label, displayable, web_data
FROM analysis
LEFT JOIN analysis_description
ON analysis.analysis_id = analysis_description.analysis_id
WHERE analysis.analysis_id = ? }
;
my
$sth
=
$self
->prepare(
$query
);
$sth
->bind_param(1,
$id
,SQL_INTEGER);
$sth
->execute();
my
$rowHashRef
=
$sth
->fetchrow_hashref;
if
( !
defined
$rowHashRef
) {
return
;
}
my
$anal
=
$self
->_objFromHashref(
$rowHashRef
);
$self
->{_cache}->{
$anal
->dbID} =
$anal
;
$self
->{_logic_name_cache}->{
lc
(
$anal
->logic_name())} =
$anal
;
return
$anal
;
}
sub
fetch_by_logic_name {
my
(
$self
,
$logic_name
) =
@_
;
my
$analysis
;
my
$rowHash
;
if
(
defined
(
$self
->{_logic_name_cache}{
lc
(
$logic_name
) } ) ) {
return
$self
->{_logic_name_cache}{
lc
(
$logic_name
) };
}
my
$sth
=
$self
->prepare(
qq(
SELECT analysis.analysis_id,
logic_name,
program,
program_version,
program_file,
db,
db_version,
db_file,
module,
module_version,
gff_source,
gff_feature,
created,
parameters,
description,
display_label,
displayable,
web_data
FROM analysis
LEFT JOIN analysis_description
ON ( analysis.analysis_id = analysis_description.analysis_id )
WHERE LOWER(logic_name) = ?)
);
$sth
->bind_param( 1,
lc
(
$logic_name
), SQL_VARCHAR );
$sth
->execute();
my
$rowHashRef
=
$sth
->fetchrow_hashref();
if
( !
defined
(
$rowHashRef
) ) {
return
}
$analysis
=
$self
->_objFromHashref(
$rowHashRef
);
$self
->{_cache}->{
$analysis
->dbID() } =
$analysis
;
$self
->{_logic_name_cache}->{
lc
(
$logic_name
) } =
$analysis
;
return
$analysis
;
}
sub
store {
my
$self
=
shift
;
my
$analysis
=
shift
;
if
(!
ref
(
$analysis
) || !
$analysis
->isa(
'Bio::EnsEMBL::Analysis'
)) {
throw(
"Bio::EnsEMBL::Analysis argument expected."
);
}
if
(
$analysis
->is_stored(
$self
->db())) {
return
$analysis
->dbID();
}
if
(!
$analysis
->logic_name()) {
throw(
"Analysis cannot be stored without a valid logic_name"
);
}
my
$insert_ignore
=
$self
->insert_ignore_clause();
my
$rows_inserted
= 0;
my
$sth
;
if
(
$analysis
->created() ) {
$sth
=
$self
->prepare(
qq{
${insert_ignore}
INTO analysis
(created, logic_name, db, db_version, db_file, program, program_version, program_file, parameters, module, module_version, gff_source, gff_feature)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
}
);
$sth
->bind_param( 1,
$analysis
->created(), SQL_DATETIME );
$sth
->bind_param( 2,
lc
(
$analysis
->logic_name() ), SQL_VARCHAR );
$sth
->bind_param( 3,
$analysis
->db(), SQL_VARCHAR );
$sth
->bind_param( 4,
$analysis
->db_version(), SQL_VARCHAR );
$sth
->bind_param( 5,
$analysis
->db_file(), SQL_VARCHAR );
$sth
->bind_param( 6,
$analysis
->program(), SQL_VARCHAR );
$sth
->bind_param( 7,
$analysis
->program_version(), SQL_VARCHAR );
$sth
->bind_param( 8,
$analysis
->program_file(), SQL_VARCHAR );
$sth
->bind_param( 9,
$analysis
->parameters(), SQL_VARCHAR );
$sth
->bind_param( 10,
$analysis
->module(), SQL_VARCHAR );
$sth
->bind_param( 11,
$analysis
->module_version(), SQL_VARCHAR );
$sth
->bind_param( 12,
$analysis
->gff_source(), SQL_VARCHAR );
$sth
->bind_param( 13,
$analysis
->gff_feature(), SQL_VARCHAR );
$rows_inserted
=
$sth
->execute();
}
else
{
$sth
=
$self
->prepare(
qq{
${insert_ignore}
INTO analysis
(created, logic_name, db, db_version, db_file, program, program_version, program_file, parameters, module, module_version, gff_source, gff_feature)
VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
}
);
$sth
->bind_param( 1,
$analysis
->logic_name, SQL_VARCHAR );
$sth
->bind_param( 2,
$analysis
->db, SQL_VARCHAR );
$sth
->bind_param( 3,
$analysis
->db_version, SQL_VARCHAR );
$sth
->bind_param( 4,
$analysis
->db_file, SQL_VARCHAR );
$sth
->bind_param( 5,
$analysis
->program, SQL_VARCHAR );
$sth
->bind_param( 6,
$analysis
->program_version, SQL_VARCHAR );
$sth
->bind_param( 7,
$analysis
->program_file, SQL_VARCHAR );
$sth
->bind_param( 8,
$analysis
->parameters, SQL_VARCHAR );
$sth
->bind_param( 9,
$analysis
->module, SQL_VARCHAR );
$sth
->bind_param( 10,
$analysis
->module_version, SQL_VARCHAR );
$sth
->bind_param( 11,
$analysis
->gff_source, SQL_VARCHAR );
$sth
->bind_param( 12,
$analysis
->gff_feature, SQL_VARCHAR );
$rows_inserted
=
$sth
->execute();
}
my
$dbID
;
if
( !
$analysis
->created() || !
$rows_inserted
||
$rows_inserted
== 0 )
{
my
$new_analysis
=
$self
->fetch_by_logic_name(
$analysis
->logic_name );
if
( !
$new_analysis
) {
throw(
"Could not retrieve just stored analysis from database.\n"
.
"Possibly incorrect db permissions or missing analysis table\n"
);
}
$dbID
=
$new_analysis
->dbID();
$analysis
->created(
$new_analysis
->created() );
}
$dbID
||=
$self
->last_insert_id(
'analysis_id'
,
undef
,
'analysis'
);
$sth
->finish();
if
(
defined
(
$analysis
->description() ) ||
defined
(
$analysis
->display_label() )||
defined
(
$analysis
->web_data() )) {
$self
->_store_description(
$analysis
,
$dbID
);
}
$self
->{_cache}->{
$dbID
} =
$analysis
;
$self
->{_logic_name_cache}{
lc
(
$analysis
->logic_name)} =
$analysis
;
$analysis
->adaptor(
$self
);
$analysis
->dbID(
$dbID
);
return
$dbID
;
}
sub
_store_description {
my
(
$self
,
$analysis
,
$dbID
) =
@_
;
my
$insert_ignore
=
$self
->insert_ignore_clause();
my
$sth
=
$self
->prepare(
"${insert_ignore} INTO analysis_description (analysis_id, display_label, description, displayable, web_data) "
.
"VALUES (?,?,?,?,?)"
);
my
$display_label
=
$analysis
->display_label();
$display_label
=
''
unless
defined
$display_label
;
$sth
->bind_param(1,
$dbID
,SQL_INTEGER);
$sth
->bind_param(2,
$display_label
,SQL_VARCHAR);
$sth
->bind_param(3,
$analysis
->description,SQL_LONGVARCHAR);
$sth
->bind_param(4,
$analysis
->displayable,SQL_TINYINT);
$sth
->bind_param(5,
$analysis
->web_data,SQL_LONGVARCHAR);
$sth
->execute();
$sth
->finish();
return
;
}
sub
update {
my
$self
=
shift
;
my
$a
=
shift
;
if
(!
ref
(
$a
) || !
$a
->isa(
'Bio::EnsEMBL::Analysis'
)) {
throw(
"Expected Bio::EnsEMBL::Analysis argument."
);
}
if
(!
$a
->is_stored(
$self
->db())) {
return
;
}
my
$sth
=
$self
->prepare
(
"UPDATE analysis "
.
"SET created = ?, logic_name = ?, db = ?, db_version = ?, db_file = ?, "
.
" program = ?, program_version = ?, program_file = ?, "
.
" parameters = ?, module = ?, module_version = ?, "
.
" gff_source = ?, gff_feature = ? "
.
"WHERE analysis_id = ?"
);
$sth
->bind_param(1,
$a
->created,SQL_DATETIME);
$sth
->bind_param(2,
$a
->logic_name,SQL_VARCHAR);
$sth
->bind_param(3,
$a
->db,SQL_VARCHAR);
$sth
->bind_param(4,
$a
->db_version,SQL_VARCHAR);
$sth
->bind_param(5,
$a
->db_file,SQL_VARCHAR);
$sth
->bind_param(6,
$a
->program,SQL_VARCHAR);
$sth
->bind_param(7,
$a
->program_version,SQL_VARCHAR);
$sth
->bind_param(8,
$a
->program_file,SQL_VARCHAR);
$sth
->bind_param(9,
$a
->parameters,SQL_VARCHAR);
$sth
->bind_param(10,
$a
->module,SQL_VARCHAR);
$sth
->bind_param(11,
$a
->module_version,SQL_VARCHAR);
$sth
->bind_param(12,
$a
->gff_source,SQL_VARCHAR);
$sth
->bind_param(13,
$a
->gff_feature,SQL_VARCHAR);
$sth
->bind_param(14,
$a
->dbID,SQL_INTEGER);
$sth
->execute();
$sth
->finish();
$sth
=
$self
->prepare(
"SELECT description FROM analysis_description WHERE analysis_id= ?"
);
$sth
->execute(
$a
->dbID);
if
(
$sth
->fetchrow_hashref) {
$sth
=
$self
->prepare
(
"UPDATE analysis_description SET description = ?, display_label = ?, displayable = ?, web_data = ? WHERE analysis_id = ?"
);
$sth
->bind_param(1,
$a
->description,SQL_LONGVARCHAR);
$sth
->bind_param(2,
$a
->display_label(),SQL_VARCHAR);
$sth
->bind_param(3,
$a
->displayable,SQL_TINYINT);
$sth
->bind_param(4,
$a
->web_data,SQL_LONGVARCHAR);
$sth
->bind_param(5,
$a
->dbID,SQL_INTEGER);
$sth
->execute();
}
else
{
if
(
$a
->description() ||
$a
->display_label() ||
$a
->web_data) {
$self
->_store_description(
$a
,
$a
->dbID);
}
}
$sth
->finish();
$self
->fetch_all();
return
1;
}
sub
remove {
my
(
$self
,
$analysis
) =
@_
;
if
(!
defined
$analysis
|| !
ref
$analysis
) {
throw(
"called remove on AnalysisAdaptor with a [$analysis]"
);
}
if
(!
$analysis
->is_stored(
$self
->db())) {
return
;
}
my
$sth
=
$self
->prepare(
"DELETE FROM analysis WHERE analysis_id = ?"
);
$sth
->bind_param(1,
$analysis
->dbID,SQL_INTEGER);
$sth
->execute();
$sth
=
$self
->prepare(
"DELETE FROM analysis_description WHERE analysis_id = ?"
);
$sth
->execute(
$analysis
->dbID());
delete
$self
->{
'_cache'
}->{
$analysis
->dbID()};
delete
$self
->{
'_logic_name_cache'
}->{
lc
(
$analysis
->logic_name)};
$analysis
->dbID(
undef
);
$analysis
->adaptor(
undef
);
return
;
}
sub
exists
{
my
(
$self
,
$anal
) =
@_
;
if
(!
ref
(
$anal
) || !
$anal
->isa(
"Bio::EnsEMBL::Analysis"
)) {
throw(
"Object is not a Bio::EnsEMBL::Analysis"
);
}
if
(
$anal
->is_stored(
$self
->db())) {
return
$anal
->dbID();
}
foreach
my
$cacheId
(
keys
%{
$self
->{_cache}}) {
if
(
$self
->{_cache}->{
$cacheId
}->compare(
$anal
) >= 0) {
return
$cacheId
;
}
}
return
;
}
sub
_objFromHashref {
my
$self
=
shift
;
my
$h
=
shift
;
return
Bio::EnsEMBL::Analysis->new_fast({
dbID
=>
$h
->{analysis_id},
adaptor
=>
$self
,
_db
=>
$h
->{db},
_db_file
=>
$h
->{db_file},
_db_version
=>
$h
->{db_version},
_program
=>
$h
->{program},
_program_version
=>
$h
->{program_version},
_program_file
=>
$h
->{program_file},
_gff_source
=>
$h
->{gff_source},
_gff_feature
=>
$h
->{gff_feature},
_module
=>
$h
->{module},
_module_version
=>
$h
->{module_version},
_parameters
=>
$h
->{parameters},
_created
=>
$h
->{created},
_logic_name
=>
$h
->{logic_name},
_description
=>
$h
->{description},
_display_label
=>
$h
->{display_label},
_displayable
=>
$h
->{displayable},
_web_data
=>
$h
->{web_data},
});
}
1;