Skip to content
Snippets Groups Projects
Commit 194eacc8 authored by Andreas Kusalananda Kähäri's avatar Andreas Kusalananda Kähäri
Browse files

Changes:

  * Pick up perl executable from environment.

  * use warnings;

  * Make usage() a subroutine rather than a string.

  * Use @table_names in usage() to avoid confusion when the list of
    tables is updated (the 'ditag' table was missing from the list, but
    not in the old usage string).

  * Better usage() text.

  * Better information messages when running the script.

  * When unable to dump the old table, skip to the next database rather
    than dying.

  * Always use long option names when calling external programs from scripts!

  * Use do() rather than prepare(), execute(), finish() for simple
    things like these.
parent 39b703eb
No related branches found
No related tags found
No related merge requests found
#!/usr/local/ensembl/bin/perl -w
#!/usr/bin/env perl
use strict;
use warnings;
use Bio::EnsEMBL::DBSQL::DBConnection;
use Getopt::Long;
......@@ -9,38 +11,54 @@ my ( $host, $port, $user, $pass, $dbpattern );
$port = '3306';
my $usage = qq(
$0 --host ens-staging --port 3306 --user ensadmin \\
--pass XXX --dbpattern core
[--help] displays this menu.
This script will dump the current meta_coord table in the latest
homo_sapiens_core.meta_coord file. Then it will update the meta_coord
table for all the following table names one by one
my @table_names = qw(
assembly_exception
gene
exon
density_feature
ditag
ditag_feature
dna_align_feature
exon
gene
karyotype
marker_feature
misc_feature
qtl_feature
prediction_exon
prediction_transcript
protein_align_feature
qtl_feature
repeat_feature
simple_feature
splicing_event
transcript
);
);
if ( scalar @ARGV == 0 ) {
print $usage, "\n";
sub usage {
print <<USAGE_END;
USAGE:
$0 --dbhost=ens-staging1 [--dbport=3306] \\
\t--dbuser=ensadmin --dbpass=XXX \\
\t--dbpattern=core
$0 --help
--dbpattern Specifies a regular expression for (possibly) matching
multiple databases.
--help Displays this help text.
This script will dump the current meta_coord table to a backup file in
the current directory. Then it will update the meta_coord table for the
data in the following tables:
USAGE_END
print( "\t", join( "\n\t", @table_names ), "\n" );
}
if ( scalar(@ARGV) == 0 ) {
usage();
exit 0;
}
......@@ -53,7 +71,7 @@ if ( !GetOptions( 'help!' => \$help,
) ||
$help )
{
print $usage, "\n";
usage();
exit;
}
......@@ -64,29 +82,11 @@ my $db = DBI->connect( $dsn, $user, $pass );
my @dbnames =
map { $_->[0] } @{ $db->selectall_arrayref("SHOW DATABASES") };
my @table_names = qw(
assembly_exception
gene
exon
density_feature
ditag_feature
dna_align_feature
karyotype
marker_feature
misc_feature
qtl_feature
prediction_exon
prediction_transcript
protein_align_feature
repeat_feature
simple_feature
splicing_event
transcript
);
for my $dbname (@dbnames) {
next if ( $dbname !~ /$dbpattern/ );
if ( $dbname !~ /$dbpattern/ ) { next }
print("==> Looking at $dbname...\n");
my $dbc =
new Bio::EnsEMBL::DBSQL::DBConnection( -host => $host,
......@@ -95,12 +95,15 @@ for my $dbname (@dbnames) {
-pass => $pass,
-dbname => $dbname );
if ( system( "mysql -h$host -P$port -u$user -p'$pass' -N " .
"-e 'SELECT * FROM meta_coord' $dbname " .
"> $dbname.meta_coord.backup" ) )
if ( system( "mysql --host=$host --port=$port " .
"--user=$user --password='$pass' " .
"--database=$dbname --skip-column-names " .
"--execute='SELECT * FROM meta_coord'" .
">$dbname.meta_coord.backup" ) )
{
print STDERR "Can't dump the original meta_coord for back up\n";
exit 1;
warn( "Can't dump the original meta_coord for back up " .
"(skipping this database)\n" );
next;
}
else {
print STDERR "Original meta_coord table backed up in " .
......@@ -108,22 +111,23 @@ for my $dbname (@dbnames) {
}
foreach my $table_name (@table_names) {
print STDERR "Updating $table_name table entries...";
my $sql = "DELETE FROM meta_coord WHERE table_name = ?";
my $sth = $dbc->prepare($sql);
$sth->execute($table_name);
$sth->finish;
print("Updating $table_name table entries... ");
my $sql = "DELETE FROM meta_coord WHERE table_name = '$table_name'";
$dbc->do($sql);
$sql =
"INSERT INTO meta_coord " .
"SELECT '$table_name', s.coord_system_id, " .
"MAX( t.seq_region_end - t.seq_region_start + 1 ) " .
"FROM $table_name t, seq_region s " .
"WHERE t.seq_region_id = s.seq_region_id " .
"FROM $table_name t JOIN seq_region s USING (seq_region_id) " .
"GROUP BY s.coord_system_id";
$sth = $dbc->prepare($sql);
$sth->execute;
$sth->finish;
print STDERR "Done\n";
$dbc->do($sql);
print("done\n");
}
print("==> Done with $dbname\n");
} ## end for my $dbname (@dbnames)
print("==> All done.\n");
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