From 2e9b9ca7d9f959c7a321d0014e1485cd6061191b Mon Sep 17 00:00:00 2001
From: Arne Stabenau <stabenau@sanger.ac.uk>
Date: Wed, 8 Dec 2004 12:50:39 +0000
Subject: [PATCH] deletes and reloads the meta_coord table with all featrue
 tables it can find

---
 sql/populate_meta_coord.pl | 68 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 sql/populate_meta_coord.pl

diff --git a/sql/populate_meta_coord.pl b/sql/populate_meta_coord.pl
new file mode 100644
index 0000000000..09b2314d0f
--- /dev/null
+++ b/sql/populate_meta_coord.pl
@@ -0,0 +1,68 @@
+# this script will repopulate the meta coord table
+# it makes an attempt to pick up all the right tables
+# (all those which have seq_region_id, seq_region_start and seq_region_end)
+
+# normally the API will populate the table ...
+
+
+use strict;
+use DBI;
+
+use Getopt::Long;
+
+my ($host, $user, $pass, $port, $dbname); #ensembl core db
+GetOptions('host=s'   => \$host,
+	   'user=s'   => \$user,
+	   'pass=s'   => \$pass,
+	   'port=i'   => \$port,
+	   'dbname=s' => \$dbname,
+	  );
+
+
+my $dsn = "DBI:mysql:host=$host;dbname=$dbname";
+if( $port ) {
+  $dsn .= ";port=$port";
+}
+
+my $db = DBI->connect( $dsn, $user, $pass );
+
+$db->do( "delete from meta_coord" );
+
+my $res = $db->selectall_arrayref( "show tables" );
+
+my @tables = map { $_->[0] } @$res;
+
+my %need_cols = ( "seq_region_id" => 1,
+		  "seq_region_start" => 1,
+		  "seq_region_end" => 1 );
+
+for my $tablename ( @tables ) {
+  # skip empty tables
+  $res = $db->selectall_arrayref( "select count(*) from $tablename" );
+  next if( $res->[0]->[0] == 0 );
+
+  $res = $db->selectall_arrayref( "desc $tablename" );
+  my @columns = map { $_->[0] } @$res;
+  if( 3 == scalar( grep { exists $need_cols{$_}} @columns )) {
+    meta_coord_query( $db, $tablename );
+  }
+}
+
+
+
+
+sub meta_coord_query {
+  my $db = shift;
+  my $tablename = shift;
+
+  $db->do( qq{
+	      INSERT INTO meta_coord
+	      SELECT '$tablename', sr.coord_system_id,
+	      MAX( f.seq_region_end -  f.seq_region_start + 1)
+	      FROM   $tablename f, seq_region sr
+	      WHERE  sr.seq_region_id = f.seq_region_id
+	      GROUP BY sr.coord_system_id
+	      } );
+}
+
+
-- 
GitLab