die"ERROR: Could not find $name in external_db table please add this too continue\n";
}
}
$sth->finish;
######################################
# For each external_db to be updated #
# Delete the existing ones #
######################################
$sth=$self->xref->dbc->prepare('select s.name, count(*) from xref x, object_xref ox, source s where ox.xref_id = x.xref_id and x.source_id = s.source_id and ox_status = "DUMP_OUT" group by s.name');
$sth->execute();
my$count;
$sth->bind_columns(\$name,\$count);
my$synonym_sth=$self->core->dbc->prepare('DELETE external_synonym FROM external_synonym, xref WHERE external_synonym.xref_id = xref.xref_id AND xref.external_db_id = ?');
my$go_sth=$self->core->dbc->prepare('DELETE gx FROM xref x, object_xref ox LEFT JOIN go_xref gx ON ox.object_xref_id = gx.object_xref_id WHERE x.xref_id = ox.xref_id AND x.external_db_id = ? AND gx.linkage_type is not null');
my$identity_sth=$self->core->dbc->prepare('DELETE identity_xref FROM identity_xref, object_xref, xref WHERE identity_xref.object_xref_id = object_xref.object_xref_id AND object_xref.xref_id = xref.xref_id AND xref.external_db_id = ?');
my$object_sth=$self->core->dbc->prepare('DELETE object_xref FROM object_xref, xref WHERE object_xref.xref_id = xref.xref_id AND xref.external_db_id = ?');
# my $dependent_sth = $self->core->dbc->prepare('DELETE dependent_xref FROM dependent_xref, xref WHERE dependent_xref.dependent_xref_id = xref.xref_id and xref.external_db_id = ?');
my$xref_sth=$self->core->dbc->prepare('DELETE FROM xref WHERE xref.external_db_id = ?');
my$unmapped_sth=$self->core->dbc->prepare('DELETE FROM unmapped_object WHERE type="xref" and external_db_id = ?');
my$test=1;
if(!$test){
while($sth->fetch()){
my$ex_id=$name_to_external_db_id{$name};
print"Deleting data for $name from core before updating from new xref database\n";
$sth->execute||die"Could not create temp table dependent_xref\n";
$sth->finish;
##### Delete this ONLY after the gene/transcript display_xref and description calculations.
##########################################
# Get the offsets for object_xref, xref #
##########################################
$sth=$self->core->dbc->prepare('select MAX(xref_id) from xref');
my$xref_offset;
$sth->execute;
$sth->bind_columns(\$xref_offset);
$sth->fetch();
$sth->finish;
$sth=$self->core->dbc->prepare('select MAX(object_xref_id) from object_xref');
my$object_xref_offset;
$sth->execute;
$sth->bind_columns(\$object_xref_offset);
$sth->fetch();
$sth->finish;
####################
# Get analysis id's
####################
my%analysis_id=$self->get_analysis();#
print"xref offset is $xref_offset, object_xref offset is $object_xref_offset\n";
#####################################
# Now add the new ones #
#####################################
###########################
# SQL to get data from xref
###########################
my$direct_sth=$self->xref->dbc->prepare('select x.xref_id, x.accession, x.label, x.version, x.description, ox.object_xref_id, ox.ensembl_id, ox.ensembl_object_type from xref x, object_xref ox where ox.ox_status = "DUMP_OUT" and ox.xref_id = x.xref_id and x.source_id = ? and x.info_type = ? order by x.xref_id');
my$dependent_sth=$self->xref->dbc->prepare('select x.xref_id, x.accession, x.label, x.version, x.description, ox.object_xref_id, ox.ensembl_id, ox.ensembl_object_type, d.master_xref_id from xref x, object_xref ox, dependent_xref d where ox.ox_status = "DUMP_OUT" and ox.xref_id = x.xref_id and d.dependent_xref_id = x.xref_id and x.source_id = ? and x.info_type = ? order by x.xref_id, ox.ensembl_id');
$go_sth=$self->xref->dbc->prepare('select x.xref_id, x.accession, x.label, x.version, x.description, ox.object_xref_id, ox.ensembl_id, ox.ensembl_object_type, d.master_xref_id, g.linkage_type from xref x, object_xref ox, dependent_xref d, go_xref g where ox.ox_status = "DUMP_OUT" and g.object_xref_id = ox.object_xref_id and x.xref_id = ox.xref_id and d.object_xref_id = ox.object_xref_id and x.source_id = ? and x.info_type = ? order by x.xref_id, ox.ensembl_id');
my$seq_sth=$self->xref->dbc->prepare('select x.xref_id, x.accession, x.label, x.version, x.description, ox.object_xref_id, ox.ensembl_id, ox.ensembl_object_type, i.query_identity, i.target_identity, i.hit_start, i.hit_end, i.translation_start, i.translation_end, i.cigar_line, i.score, i.evalue from xref x, object_xref ox, identity_xref i where ox.ox_status = "DUMP_OUT" and i.object_xref_id = ox.object_xref_id and ox.xref_id = x.xref_id and x.source_id = ? and x.info_type = ? order by x.xref_id');
my$add_go_xref_sth=$self->core->dbc->prepare('insert into go_xref (object_xref_id, linkage_type) values (?, ?)');
my$add_dependent_xref_sth=$self->core->dbc->prepare('insert into dependent_xref (object_xref_id, master_xref_id, dependent_xref_id) values (?, ?, ?)');
my$add_syn_sth=$self->core->dbc->prepare('insert into external_synonym (xref_id, synonym) values (?, ?)');
$sth=$self->xref->dbc->prepare('select s.name, s.source_id, count(*), x.info_type from xref x, object_xref ox, source s where ox.xref_id = x.xref_id and x.source_id = s.source_id and ox_status = "DUMP_OUT" group by s.name, x.info_type');