Bio::EnsEMBL::DBSQL::Funcgen::ExternalFeatureAdaptor - A database adaptor for fetching and
storing ExternalFeature objects.
sub _objs_from_sth
{ my ($self, $sth, $mapper, $dest_slice) = @_;
my ($sa, @features, $seq_region_id, %fset_hash, %slice_hash, %sr_name_hash, %sr_cs_hash, %ftype_hash);
$sa = $dest_slice->adaptor->db->get_SliceAdaptor() if($dest_slice); $sa ||= $self->db->get_SliceAdaptor();
my $fset_adaptor = $self->db->get_FeatureSetAdaptor();
my $ftype_adaptor = $self->db->get_FeatureTypeAdaptor();
my (
$external_feature_id, $efg_seq_region_id,
$seq_region_start, $seq_region_end,
$seq_region_strand, $fset_id,
$display_label, $ftype_id
);
$sth->bind_columns(\$
external_feature_id,\$ efg_seq_region_id,\$
seq_region_start,\$ seq_region_end,\$
seq_region_strand,\$ display_label,\$
ftype_id,\$ fset_id,
);
my ($asm_cs, $cmp_cs, $asm_cs_name, $asm_cs_vers, $cmp_cs_name, $cmp_cs_vers);
if ($mapper) {
$asm_cs = $mapper->assembled_CoordSystem();
$cmp_cs = $mapper->component_CoordSystem();
$asm_cs_name = $asm_cs->name();
$asm_cs_vers = $asm_cs->version();
$cmp_cs_name = $cmp_cs->name();
$cmp_cs_vers = $cmp_cs->version();
}
my ($dest_slice_start, $dest_slice_end, $dest_slice_strand, $dest_slice_length, $dest_slice_sr_name);
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_sr_name = $dest_slice->seq_region_name();
}
FEATURE: while ( $sth->fetch() ) {
$seq_region_id = $self->get_core_seq_region_id($efg_seq_region_id);
if(! $seq_region_id){
warn "Cannot get slice for eFG seq_region_id $efg_seq_region_id\n".
"The region you are using is not present in the current dna DB";
next;
}
$fset_hash{$fset_id} = $fset_adaptor->fetch_by_dbID($fset_id) if(! exists $fset_hash{$fset_id});
$ftype_hash{$ftype_id} = $ftype_adaptor->fetch_by_dbID($ftype_id) if(! exists $ftype_hash{$ftype_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();
}
my $sr_name = $sr_name_hash{$seq_region_id};
my $sr_cs = $sr_cs_hash{$seq_region_id};
if ($mapper) {
throw("Not yet implmented mapper, check equals are Funcgen calls too!");
($sr_name, $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 $sr_name;
if ( $asm_cs == $sr_cs || ( $cmp_cs != $sr_cs && $asm_cs->equals($sr_cs) ) ) {
$slice = $slice_hash{"NAME:$sr_name:$cmp_cs_name:$cmp_cs_vers"}
||= $sa->fetch_by_region($cmp_cs_name, $sr_name, undef, undef, undef, $cmp_cs_vers);
} else {
$slice = $slice_hash{"NAME:$sr_name:$asm_cs_name:$asm_cs_vers"}
||= $sa->fetch_by_region($asm_cs_name, $sr_name, undef, undef, undef, $asm_cs_vers);
}
}
if ($dest_slice) {
unless ($dest_slice_start == 1 && $dest_slice_strand == 1) {
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;
} else {
my $tmp_seq_region_start = $seq_region_start;
$seq_region_start = $dest_slice_end - $seq_region_end + 1;
$seq_region_end = $dest_slice_end - $tmp_seq_region_start + 1;
$seq_region_strand *= -1;
}
}
next FEATURE if $seq_region_end < 1 || $seq_region_start > $dest_slice_length
|| ( $dest_slice_sr_name ne $sr_name );
$slice = $dest_slice;
}
push @features, Bio::EnsEMBL::Funcgen::ExternalFeature->new_fast
({
'start' => $seq_region_start,
'end' => $seq_region_end,
'strand' => $seq_region_strand,
'slice' => $slice,
'analysis' => $fset_hash{$fset_id}->analysis(),
'adaptor' => $self,
'dbID' => $external_feature_id,
'display_label' => $display_label,
'feature_set' => $fset_hash{$fset_id},
'feature_type' => $ftype_hash{$ftype_id},
});
}
return\@ features; } |
sub store
{ my ($self, @efs) = @_;
if (scalar(@efs) == 0) {
throw('Must call store with a list of ExternalFeature objects');
}
my $sth = $self->prepare("
INSERT INTO external_feature (
seq_region_id, seq_region_start,
seq_region_end, seq_region_strand,
display_label, feature_type_id,
feature_set_id
) VALUES (?, ?, ?, ?, ?, ?, ?)
");
my $db = $self->db();
FEATURE: foreach my $ef (@efs) {
if(! (ref($ef) && $ef->isa('Bio::EnsEMBL::Funcgen::ExternalFeature') )) {
throw('Feature must be an ExternalFeature object');
}
if ( $ef->is_stored($db) ) {
warning('ExternalFeature [' . $ef->dbID() . '] is already stored in the database');
next FEATURE;
}
if ( ! $ef->analysis->is_stored($db)) {
throw('A stored Bio::EnsEMBL::Analysis must be attached to the ExternalFeature objects to be stored.');
}
if (! $ef->feature_set->is_stored($db)) {
throw('A stored Bio::EnsEMBL::Funcgen::FeatureSet must be attached to the ExternalFeature objects to be stored.');
}
my $seq_region_id;
($ef, $seq_region_id) = $self->_pre_store($ef);
$sth->bind_param(1, $seq_region_id, SQL_INTEGER);
$sth->bind_param(2, $ef->start(), SQL_INTEGER);
$sth->bind_param(3, $ef->end(), SQL_INTEGER);
$sth->bind_param(4, $ef->strand(), SQL_TINYINT);
$sth->bind_param(5, $ef->display_label(), SQL_VARCHAR);
$sth->bind_param(6, $ef->feature_type->dbID(), SQL_INTEGER);
$sth->bind_param(7, $ef->feature_set->dbID(), SQL_INTEGER);
$sth->execute();
$ef->dbID( $sth->{'mysql_insertid'} );
$ef->adaptor($self);
$self->store_associated_feature_types($ef) if (defined $ef->{'associated_feature_types'});
}
return\@ efs;
}
1; } |