ensembl-hive  2.8.1
populate_meta_coord.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 # this script will repopulate the meta coord table
18 # it makes an attempt to pick up all the right tables
19 # (all those which have seq_region_id, seq_region_start and seq_region_end)
20 
21 # normally the API will populate the table ...
22 
23 
24 use strict;
25 use warnings;
26 use DBI;
27 
28 use Getopt::Long;
29 
30 my ($host, $user, $pass, $port, $dbname); #ensembl core db
31 GetOptions('host=s' => \$host,
32  'user=s' => \$user,
33  'pass=s' => \$pass,
34  'port=i' => \$port,
35  'dbname=s' => \$dbname,
36  );
37 
38 
39 my $dsn = "DBI:mysql:host=$host;dbname=$dbname";
40 if( $port ) {
41  $dsn .= ";port=$port";
42 }
43 
44 my $db = DBI->connect( $dsn, $user, $pass );
45 
46 $db->do( "delete from meta_coord" );
47 
48 my $res = $db->selectall_arrayref( "show tables" );
49 
50 my @tables = map { $_->[0] } @$res;
51 
52 my %need_cols = ( "seq_region_id" => 1,
53  "seq_region_start" => 1,
54  "seq_region_end" => 1 );
55 
56 for my $tablename ( @tables ) {
57  # skip empty tables
58  $res = $db->selectall_arrayref( "select count(*) from $tablename" );
59  next if( $res->[0]->[0] == 0 );
60 
61  $res = $db->selectall_arrayref( "desc $tablename" );
62  my @columns = map { $_->[0] } @$res;
63  if( 3 == scalar( grep { exists $need_cols{$_}} @columns )) {
64  meta_coord_query( $db, $tablename );
65  }
66 }
67 
68 
69 
70 
71 sub meta_coord_query {
72  my $db = shift;
73  my $tablename = shift;
74 
75  $db->do( qq{
76  INSERT INTO meta_coord
77  SELECT '$tablename', sr.coord_system_id,
78  MAX( f.seq_region_end - f.seq_region_start + 1)
79  FROM $tablename f, seq_region sr
80  WHERE sr.seq_region_id = f.seq_region_id
81  GROUP BY sr.coord_system_id
82  } );
83 }
84 
85 
map
public map()
meta_coord_query
public meta_coord_query()