Commit 1c925668 authored by James Allen's avatar James Allen
Browse files

Enable the script to create collection databases for testing. The code already...

Enable the script to create collection databases for testing. The code already iterates over multiple species in a single json config file, so just need to note when the first one is done, then avoid doing db creation and 'global' table population on subsequent iterations.
parent a80c06d4
...@@ -141,6 +141,7 @@ sub process { ...@@ -141,6 +141,7 @@ sub process {
my $dbc = $self->target_dbc(); my $dbc = $self->target_dbc();
my $config_hash = $self->{json}; my $config_hash = $self->{json};
my $is_dna = 1; my $is_dna = 1;
my %created;
foreach my $species (keys %{$config_hash}) { foreach my $species (keys %{$config_hash}) {
foreach my $group (keys %{$config_hash->{$species}}) { foreach my $group (keys %{$config_hash->{$species}}) {
...@@ -150,7 +151,8 @@ sub process { ...@@ -150,7 +151,8 @@ sub process {
my $info = $config_hash->{$species}->{$group}; my $info = $config_hash->{$species}->{$group};
my $regions = $info->{regions}; my $regions = $info->{regions};
my $adaptors = $info->{adaptors}; my $adaptors = $info->{adaptors};
my $to = $self->copy_database_structure($species, $group, $dbc); my $exists = exists $created{$group};
my $to = $self->copy_database_structure($species, $group, $dbc, $exists);
if ($group ne 'core') { if ($group ne 'core') {
$is_dna = 0; $is_dna = 0;
my $dnadb = $registry->get_DBAdaptor($species, 'core'); my $dnadb = $registry->get_DBAdaptor($species, 'core');
...@@ -161,14 +163,16 @@ sub process { ...@@ -161,14 +163,16 @@ sub process {
$from->dnadb($dnadb); $from->dnadb($dnadb);
$to->dnadb($dnadb); $to->dnadb($dnadb);
} }
$self->copy_globals($from, $to); $self->copy_globals($from, $to) if ! $exists;
my $slices = $self->copy_regions($from, $to, $regions, $is_dna); my $slices = $self->copy_regions($from, $to, $regions, $is_dna);
my $filter_exceptions = $info->{filter_exceptions}; my $filter_exceptions = $info->{filter_exceptions};
foreach my $adaptor_info (@{$adaptors}) { foreach my $adaptor_info (@{$adaptors}) {
$self->copy_features($from, $to, $slices, $adaptor_info, $filter_exceptions); $self->copy_features($from, $to, $slices, $adaptor_info, $filter_exceptions);
} }
$self->dump_database($to); $self->dump_database($to);
$self->drop_database($to); if (! $self->drop_database($to)) {
$created{$group}++;
}
} }
} }
} }
...@@ -193,8 +197,9 @@ sub drop_database { ...@@ -193,8 +197,9 @@ sub drop_database {
$dbc->do('drop database '.$db); $dbc->do('drop database '.$db);
delete $dbc->{dbname}; delete $dbc->{dbname};
$dbc->disconnect_if_idle(); $dbc->disconnect_if_idle();
return 1;
} }
return; return 0;
} }
...@@ -329,26 +334,31 @@ sub copy_features { ...@@ -329,26 +334,31 @@ sub copy_features {
} }
sub copy_database_structure { sub copy_database_structure {
my ($self, $species, $group, $target_dbc) = @_; my ($self, $species, $group, $target_dbc, $exists) = @_;
my $dba = Bio::EnsEMBL::Registry->get_DBAdaptor($species, $group); my $dba = Bio::EnsEMBL::Registry->get_DBAdaptor($species, $group);
my $dbc = $dba->dbc(); my $dbc = $dba->dbc();
my $target_name = $self->new_dbname($dba->dbc()->dbname()); my $target_name = $self->new_dbname($dba->dbc()->dbname());
my $source_name = $dba->dbc->dbname(); my $source_name = $dba->dbc->dbname();
print STDERR "Copying schema from ${source_name} into '${target_name}'\n"; if (! $exists) {
$target_dbc->do('drop database if exists '.$target_name); print STDERR "Copying schema from ${source_name} into '${target_name}'\n";
$target_dbc->do('create database '.$target_name); $target_dbc->do('drop database if exists '.$target_name);
my $cmd_tmpl = 'mysqldump --host=%s --port=%d --user=%s --no-data --skip-add-locks --skip-lock-tables %s | mysql --host=%s --port=%d --user=%s --password=%s %s'; $target_dbc->do('create database '.$target_name);
my @src_args = map { $dbc->$_() } qw/host port username dbname/; my $cmd_tmpl = 'mysqldump --host=%s --port=%d --user=%s --no-data --skip-add-locks --skip-lock-tables %s | mysql --host=%s --port=%d --user=%s --password=%s %s';
my @trg_args = ((map { $target_dbc->$_() } qw/host port username password/), $target_name); my @src_args = map { $dbc->$_() } qw/host port username dbname/;
my $cmd = sprintf($cmd_tmpl, @src_args, @trg_args); my @trg_args = ((map { $target_dbc->$_() } qw/host port username password/), $target_name);
system($cmd); my $cmd = sprintf($cmd_tmpl, @src_args, @trg_args);
my $rc = $? >> 8; system($cmd);
if($rc != 0 ) { my $rc = $? >> 8;
die "Could not execute command '$cmd'; got return code of $rc"; if($rc != 0 ) {
die "Could not execute command '$cmd'; got return code of $rc";
}
$target_dbc->dbname($target_name);
$target_dbc->do('use '.$target_name);
print STDERR "Finished population\n";
} else {
print STDERR "Using existing database '${target_name}'\n";
} }
$target_dbc->dbname($target_name);
$target_dbc->do('use '.$target_name);
print STDERR "Finished population\n";
my $dbadaptor; my $dbadaptor;
if ($group eq 'funcgen') { if ($group eq 'funcgen') {
$dbadaptor = Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor->new( $dbadaptor = Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor->new(
...@@ -361,7 +371,9 @@ sub copy_database_structure { ...@@ -361,7 +371,9 @@ sub copy_database_structure {
$dbadaptor = Bio::EnsEMBL::DBSQL::DBAdaptor->new( $dbadaptor = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
-DBCONN => $target_dbc, -DBCONN => $target_dbc,
-GROUP => $group, -GROUP => $group,
-SPECIES => $target_name, -SPECIES => $species,
-SPECIES_ID => $dba->species_id,
-MULTISPECIES_DB => $dba->is_multispecies,
); );
} }
return $dbadaptor; return $dbadaptor;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment