2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # Designed to sort a feature table by seq_region_id, start and end
25 use POSIX qw/strftime/;
27 my ($db_name,$db_host,$db_user,$db_pass,$db_port,$help);
30 my ($optimise, $nobackup, $nolock);
32 GetOptions (
"db_name|dbname|database=s" => \$db_name,
33 "db_host|dbhost|host=s" => \$db_host,
34 "db_user|dbuser|user|username=s" => \$db_user,
35 "db_pass|dbpass|pass|password=s" => \$db_pass,
36 "db_port|dbport|port=s" => \$db_port,
37 'table|tables=s@' => \@tables,
38 'optimise!' => \$optimise,
39 'nolock!' => \$nolock,
40 'nobackup!' => \$nobackup,
41 'columns=s@' => \@columns,
45 if ($help) {&
usage; exit 0;}
46 unless ($db_name and $db_host) {print
"Insufficient arguments\n"; &
usage; exit 1;}
49 @columns = qw/seq_region_id seq_region_start seq_region_end/;
65 # see bottom of file for this method call
68 foreach my $table (@tables) {
69 info(
'Processing %s', $table);
77 my ($table, $dba) = @_;
78 info(
"Starting sort");
79 my $s_table = $table.
'_sorted';
82 info(
"Locking table %s", $table);
83 $dba->dbc()->do(
"lock tables ${table} write");
90 info(
"Re-ordering table");
91 my $cols = join(
',', @columns);
92 $dba->dbc()->do(
"ALTER TABLE ${table} ORDER BY $cols");
95 info(
"Unlocking table %s", $table);
96 $dba->dbc()->do(
"unlock tables");
104 my ($table, $dba) = @_;
105 info(
"Optimising table");
106 $dba->dbc()->do(
"OPTIMIZE TABLE ${table}");
112 my ($table, $dba) = @_;
114 info(
"Backing up table to $bak_name");
115 $dba->dbc()->do(
"create table ${bak_name} like ${table}");
116 $dba->dbc()->do(
"alter table ${bak_name} disable keys");
117 $dba->dbc()->do(
"insert into ${bak_name} select * from ${table}");
118 $dba->dbc()->do(
"alter table ${bak_name} enable keys");
119 info(
"Finished backing up");
124 my ($table, $dba) = @_;
128 $new_name = $count == 0 ?
"${table}_bak" :
"${table}_bak_${count}";
129 my $r = $dba->dbc()->sql_helper()->execute(
130 -SQL =>
'show tables like ?', -PARAMS => [$new_name]
132 last
if scalar(@{$r}) == 0;
139 my ($msg, @args) = @_;
140 my $m = sprintf $msg, @args;
141 my $time = strftime(
'%c',localtime());
142 printf STDERR
'[%s] %s', $time, $m;
150 Sort a feature table by seq_region_id, seq_region_start. Also run optimise
151 if specified. Holds a backup of the table that has just been sorted
152 unless told otherwise.
156 perl sort_feature_table.pl -db_name NAME
160 -db_name The DB to add these features to
164 -db_host Hostname for the DB
168 -db_user Username for the DB
173 -db_pass Password for the DB
178 -db_port Port for the DB
182 -table The table(s) to sort. Specify multiple parameters
185 -optimise Optimise the table post sort
187 -nobackup Do not backup the original table
189 -nolock Stop the code from applying for table locks
191 -columns Specify the columns to sort on. Defaults to
192 seq_region_id, seq_region_start and seq_region_end.
193 Multiple parameters allowed