ensembl-hive  2.7.0
update_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 
18 use strict;
19 use warnings;
20 
22 use Getopt::Long;
24 
25 my $help = 0;
26 my ( $host, $port, $user, $pass, $dbpattern );
27 
28 $port = '3306';
29 
30 my @table_names = qw(
31  assembly_exception
32  density_feature
33  ditag_feature
34  dna_align_feature
35  exon
36  gene
37  intron_supporting_evidence
38  karyotype
39  marker_feature
40  misc_feature
41  prediction_exon
42  prediction_transcript
43  protein_align_feature
44  repeat_feature
45  simple_feature
47 );
48 
49 sub usage {
50  print <<USAGE_END;
51 USAGE:
52 
53  $0 --dbhost=ens-staging1 [--dbport=3306] \\
54  \t--dbuser=ensadmin --dbpass=XXX \\
55  \t--dbpattern=core
56 
57  $0 --help
58 
59  --dbpattern Specifies a regular expression for (possibly) matching
60  multiple databases.
61 
62  --nobackup Do not backup the meta_coord table in a file.
63 
64  --help Displays this help text.
65 
66 This script will dump the current meta_coord table to a backup file in
67 the current directory. Then it will update the meta_coord table for the
68 data in the following tables:
69 
70 USAGE_END
71 
72  print( "\t", join( "\n\t", @table_names ), "\n" );
73 
74 }
75 
76 if ( scalar(@ARGV) == 0 ) {
77  usage();
78  exit 0;
79 }
80 
81 my $cli_helper = Bio::EnsEMBL::Utils::CliHelper->new();
82 
83 # get the basic options for connecting to a database server
84 my $optsd = $cli_helper->get_dba_opts();
85 push(@$optsd, 'backup!');
86 # process the command line with the supplied options plus a help subroutine
87 my $opts = $cli_helper->process_args( $optsd, \&usage );
88 
89 # use the command line options to get an array of database details
90 # only process each database name once (to avoid duplication for multispecies dbs)
91 for my $db_args ( @{ $cli_helper->get_dba_args_for_opts( $opts, 0 ) } ) {
92 
93  my $dba = new Bio::EnsEMBL::DBSQL::DBAdaptor(%$db_args);
94 
95  if (!exists $opts->{backup} or (exists $opts->{backup} and $opts->{backup})) {
96  my $file =
97  $dba->dbc()->dbname() . "_" . $dba->species_id() . ".meta_coord.backup";
98  my $sys_call = sprintf( "mysql "
99  . "--host=%s "
100  . "--port=%d "
101  . "--user=%s "
102  . "--pass='%s' "
103  . "--database=%s "
104  . "--skip-column-names "
105  . " --execute='SELECT * FROM meta_coord'"
106  . " > $file",
107  $dba->dbc->host(), $dba->dbc->port(),
108  $dba->dbc->username(), $dba->dbc->password(),
109  $dba->dbc->dbname() );
110  unless ( system($sys_call) == 0 ) {
111  warn "Can't dump the original meta_coord for back up "
112  . "(skipping this species)\n";
113  next;
114  } else {
115  print "Original meta_coord table backed up in $file\n";
116  }
117  }
118 
119  foreach my $table_name (@table_names) {
120  print("Updating $table_name table entries... ");
121 
122  $dba->dbc()->sql_helper()->execute_update(
123  -SQL =>
124 "DELETE mc.* FROM meta_coord mc, coord_system cs WHERE cs.coord_system_id=mc.coord_system_id AND table_name = ? AND cs.species_id=?",
125  -PARAMS => [ $table_name, $dba->species_id() ] );
126 
127  my $sql = "INSERT INTO meta_coord "
128  . "SELECT '$table_name', s.coord_system_id, "
129  . "MAX( t.seq_region_end - t.seq_region_start + 1 ) "
130  . "FROM $table_name t, seq_region s, coord_system c "
131  . "WHERE t.seq_region_id = s.seq_region_id AND c.coord_system_id=s.coord_system_id AND c.species_id=?"
132  . "GROUP BY s.coord_system_id";
133 
134  $dba->dbc()->sql_helper()->execute_update(
135  -SQL => $sql,
136  -PARAMS => [ $dba->species_id() ] );
137 
138  print("done\n");
139  }
140 
141 
142  print( "==> Done with "
143  . $dba->dbc->dbname() . "/"
144  . $dba->species_id()
145  . "\n" );
146 } ## end for my $db_args ( @{ $cli_helper...})
147 
148 print("==> All done.\n");
transcript
public transcript()
Bio::EnsEMBL::DBSQL::DBAdaptor
Definition: DBAdaptor.pm:40
Bio::EnsEMBL::Utils::CliHelper::get_dba_opts
public Arrayref get_dba_opts()
usage
public usage()
exon
public exon()
Bio::EnsEMBL::DBSQL::DBConnection
Definition: DBConnection.pm:42
Bio::EnsEMBL::Utils::CliHelper
Definition: CliHelper.pm:55