$Bio::EnsEMBL::DBSQL::DataFileAdaptor::VERSION
=
'112.0_55'
;
$Bio::EnsEMBL::DBSQL::DataFileAdaptor::VERSION
=
'112.055'
;
my
$GLOBAL_BASE_PATH
;
sub
global_base_path {
my
(
$class
,
$base_path
) =
@_
;
return
$GLOBAL_BASE_PATH
unless
$base_path
;
$GLOBAL_BASE_PATH
=
$base_path
;
return
$GLOBAL_BASE_PATH
;
}
sub
get_base_path {
my
(
$self
,
$path
) =
@_
;
return
$path
if
defined
$path
;
my
$global_base_path
=
$self
->global_base_path();
return
$global_base_path
if
defined
$global_base_path
;
my
$meta_base_path
=
$self
->db()->get_MetaContainer()->single_value_by_key(
'data_file.base_path'
, 1);
return
$meta_base_path
if
defined
$meta_base_path
;
throw
"No base path discovered. Either provide a path, set a global using global_base_path() or specify 'data_file.base_path' in meta"
;
}
sub
DataFile_to_extensions {
my
(
$self
,
$df
) =
@_
;
my
$type
=
$df
->file_type();
my
$extensions
= {
BAM
=> [
'bam'
,
'bam.bai'
],
BAMCOV
=> [
'bam'
,
'bam.bai'
,
'bam.bw'
],
BIGBED
=> [
'bb'
],
BIGWIG
=> [
'bw'
],
VCF
=> [
'vcf.gz'
,
'vcf.gz.tbi'
],
}->{
$type
};
throw
sprintf
(
q{No extensions found for the type '%s'}
,
$type
)
if
!
$extensions
;
return
$extensions
;
}
sub
DataFile_to_adaptor {
my
(
$self
,
$df
,
$base
,
$requested_type
) =
@_
;
my
$type
=
$df
->file_type();
throw
sprintf
(
"Request for a '%s' adaptor, but file is of type '%s'"
,
$requested_type
,
$type
)
if
$type
ne
'BAMCOV'
and
$type
ne
$requested_type
;
SWITCH:
{
return
Bio::EnsEMBL::IO::Adaptor::BAMAdaptor->new(
$df
->path(
$base
))
if
$type
eq
'BAM'
;
return
Bio::EnsEMBL::IO::Adaptor::BigBedAdaptor->new(
$df
->path(
$base
))
if
$type
eq
'BIGBED'
;
return
Bio::EnsEMBL::IO::Adaptor::BigWigAdaptor->new(
$df
->path(
$base
))
if
$type
eq
'BIGWIG'
;
return
Bio::EnsEMBL::IO::Adaptor::VCFAdaptor->new(
$df
->path(
$base
))
if
$type
eq
'VCF'
;
if
(
$type
eq
'BAMCOV'
) {
return
Bio::EnsEMBL::IO::Adaptor::BAMAdaptor->new(
$df
->path(
$base
))
if
$requested_type
eq
'BAM'
or
$requested_type
eq
'BAMCOV'
;
return
Bio::EnsEMBL::IO::Adaptor::BigWigAdaptor->new(
$df
->get_all_paths(
$base
)->[2])
if
$requested_type
eq
'BIGWIG'
;
}
throw
sprintf
(
q{No '%s' handler found for the type '%s'}
,
$requested_type
,
$type
)
}
}
sub
fetch_all_by_logic_name {
my
(
$self
,
$logic_name
) =
@_
;
my
$analysis
=
$self
->db()->get_AnalysisAdaptor()->fetch_by_logic_name(
$logic_name
);
throw
"No analysis found for logic_name '${logic_name}'"
if
!
$analysis
;
return
$self
->fetch_all_by_Analysis(
$analysis
);
}
sub
fetch_all_by_Analysis {
my
(
$self
,
$analysis
) =
@_
;
assert_ref(
$analysis
,
'Bio::EnsEMBL::Analysis'
,
'analysis'
);
$self
->bind_param_generic_fetch(
$analysis
->dbID(), SQL_INTEGER);
return
$self
->generic_fetch(
'df.analysis_id =?'
);
}
sub
fetch_all_by_CoordSystem {
my
(
$self
,
$cs
) =
@_
;
assert_ref(
$cs
,
'Bio::EnsEMBL::CoordSystem'
,
'coord_system'
);
$self
->bind_param_generic_fetch(
$cs
->dbID(), SQL_INTEGER);
return
$self
->generic_fetch(
'df.coord_system_id =?'
);
}
sub
fetch_by_name_and_type {
my
(
$self
,
$name
,
$type
) =
@_
;
$self
->bind_param_generic_fetch(
$name
, SQL_VARCHAR);
$self
->bind_param_generic_fetch(
$type
, SQL_VARCHAR);
my
$results
=
$self
->generic_fetch(
'df.name =? and df.file_type =?'
);
return
$results
->[0]
if
@{
$results
};
return
;
}
sub
generic_fetch {
my
(
$self
,
$constraint
) =
@_
;
$constraint
||=
q{}
;
my
$sql
=
<<'SQL';
select df.data_file_id, df.coord_system_id, df.analysis_id, df.name, df.version_lock, df.absolute, df.url, df.file_type
from data_file df
join coord_system cs using (coord_system_id)
where cs.species_id =?
SQL
$sql
.=
'AND '
.
$constraint
if
$constraint
;
my
$params
=
$self
->bind_param_generic_fetch();
if
(
defined
$params
) {
$self
->{
'_bind_param_generic_fetch'
} = ();
}
else
{
$params
= [];
}
unshift
(@{
$params
},
$self
->db()->species_id());
my
$csa
=
$self
->db()->get_CoordSystemAdaptor();
my
$aa
=
$self
->db()->get_AnalysisAdaptor();
return
$self
->dbc()->sql_helper()->execute(
-SQL
=>
$sql
,
-PARAMS
=>
$params
,
-CALLBACK
=>
sub
{
my
(
$row
) =
@_
;
my
(
$data_file_id
,
$coord_system_id
,
$analysis_id
,
$name
,
$version_lock
,
$absolute
,
$url
,
$file_type
) = @{
$row
};
my
$hash
= {
dbID
=>
$data_file_id
,
adaptor
=>
$self
,
coord_system
=>
$csa
->fetch_by_dbID(
$coord_system_id
),
analysis
=>
$aa
->fetch_by_dbID(
$analysis_id
),
name
=>
$name
,
version_lock
=>
$version_lock
,
absolute
=>
$absolute
,
file_type
=>
$file_type
,
};
$hash
->{url} =
$url
if
$url
;
return
Bio::EnsEMBL::DataFile->new_fast(
$hash
);
});
}
sub
store {
my
(
$self
,
$df
) =
@_
;
assert_ref(
$df
,
'Bio::EnsEMBL::DataFile'
,
'datafile'
);
if
(
$df
->is_stored(
$self
->db())) {
return
$df
->dbID();
}
throw
'Analysis is not defined for this data file'
if
!
defined
$df
->analysis();
throw
'Coord system is not defined for this data file'
if
!
defined
$df
->coord_system();
my
$sql
=
<<'SQL';
INSERT INTO data_file (coord_system_id, analysis_id, name, version_lock, absolute, url, file_type)
VALUES (?,?,?,?,?,?,?)
SQL
my
$params
= [
[
$df
->coord_system()->dbID(), SQL_INTEGER],
[
$df
->analysis()->dbID(), SQL_INTEGER],
[
$df
->name(), SQL_VARCHAR],
[
$df
->version_lock(), SQL_INTEGER],
[
$df
->absolute(), SQL_INTEGER],
[
$df
->url(), SQL_VARCHAR],
[
$df
->file_type(), SQL_VARCHAR],
];
$self
->dbc()->sql_helper()->execute_update(
-SQL
=>
$sql
,
-PARAMS
=>
$params
,
-CALLBACK
=>
sub
{
my
(
$sth
,
$dbh
) =
@_
;
$df
->dbID(
$self
->last_insert_id());
return
;
});
$df
->adaptor(
$self
);
return
$df
->dbID();
}
sub
update {
my
(
$self
,
$df
) =
@_
;
assert_ref(
$df
,
'Bio::EnsEMBL::DataFile'
,
'datafile'
);
if
(!
$df
->is_stored(
$self
->db())) {
$self
->store(
$df
);
return
;
}
my
$sql
=
<<'SQL';
UPDATE data_file SET coord_system_id =?, analysis_id=?, name=?, version_lock=?, absolute=?, url=?, file_type=?
WHERE data_file_id =?
SQL
my
$params
= [
[
$df
->coord_system()->dbID(), SQL_INTEGER],
[
$df
->analysis()->dbID(), SQL_INTEGER],
[
$df
->name(), SQL_VARCHAR],
[
$df
->version_lock(), SQL_INTEGER],
[
$df
->absolute(), SQL_INTEGER],
[
$df
->url(), SQL_VARCHAR],
[
$df
->file_type(), SQL_VARCHAR],
[
$df
->dbID(), SQL_INTEGER],
];
$self
->dbc()->sql_helper()->execute_update(
-SQL
=>
$sql
,
-PARAMS
=>
$params
);
return
;
}
sub
delete
{
my
(
$self
,
$df
) =
@_
;
assert_ref(
$df
,
'Bio::EnsEMBL::DataFile'
,
'datafile'
);
if
(!
$df
->is_stored(
$self
->db())) {
throw
"Cannot delete the data file if it has not already been stored in this database"
;
}
$self
->dbc()->sql_helper()->execute_update(
-SQL
=>
'DELETE from data_file where data_file_id =?'
,
-PARAMS
=> [[
$df
->dbID(), SQL_INTEGER]],
);
return
;
}
sub
_tables {
my
(
$self
) =
@_
;
return
(
[
qw/data_file df/
]
);
}
1;