3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
37 # this object isn't instantiated directly but rather extended
43 This is the base
object for some of the objects used in the IdMapping
44 application. An
object that extends
BaseObject will have a ConfParser,
46 functions related to file and db access.
48 This isn
't very clean OO design but it's efficient and easy to use...
57 upload_file_into_table
65 package Bio::EnsEMBL::IdMapping::BaseObject;
69 no warnings
'uninitialized';
81 Example : my $object = Bio::EnsEMBL::IdMapping::BaseObjectSubclass->
new(
86 Description : Constructor
87 Return type : implementing subclass type
88 Exceptions : thrown on wrong or missing arguments
97 my $class = ref($caller) || $caller;
99 my ($logger, $conf, $cache) = rearrange([
'LOGGER',
'CONF',
'CACHE'], @_);
101 unless ($logger and ref($logger) and
102 $logger->isa(
'Bio::EnsEMBL::Utils::Logger')) {
103 throw(
"You must provide a Bio::EnsEMBL::Utils::Logger for logging.");
106 unless ($conf and ref($conf) and
107 $conf->isa(
'Bio::EnsEMBL::Utils::ConfParser')) {
108 throw(
"You must provide configuration as a Bio::EnsEMBL::Utils::ConfParser object.");
111 unless ($cache and ref($cache) and
112 $cache->isa(
'Bio::EnsEMBL::IdMapping::Cache')) {
113 throw(
"You must provide configuration as a Bio::EnsEMBL::IdMapping::Cache object.");
117 bless ($self, $class);
120 $self->logger($logger);
122 $self->cache($cache);
130 Arg[1] : String $filename - filename
for filehandle
131 Arg[2] : String $path_append - append subdirectory name to basedir
132 Arg[3] : String $mode - filehandle mode (<|>|>>)
133 Example : my $fh = $object->
get_filehandle('mapping_stats.txt', 'stats',
135 print $fh "Stats:\n";
136 Description : Returns a filehandle to a file for reading or writing. The file
137 is qualified with the basedir defined in the configuration and
138 an optional subdirectory name.
139 Return type : filehandle
140 Exceptions : thrown on missing filename
149 my $filename = shift;
150 my $path_append = shift;
153 throw(
"Need a filename for this filehandle.") unless (defined($filename));
155 my $path = $self->conf->param(
'basedir');
156 $path = path_append($path, $path_append)
if (defined($path_append));
160 open(my $fh, $mode,
"$path/$filename") or
161 throw("Unable to open $path/$filename: $!");
169 Arg[1] : String $filename - filename to test
170 Arg[2] : Boolean $path_append - turn on pre-pending of basedir
171 Example : unless ($object->file_exists('gene_mappings.ser', 1)) {
172 $object->do_gene_mapping;
174 Description : Tests
if a file exists and has non-zero size.
175 Return type : Boolean
185 my $filename = shift;
186 my $path_append = shift;
188 my $path = $self->conf->param(
'basedir');
189 $path = path_append($path, $path_append)
if (defined($path_append));
191 return (-s
"$path/$filename");
195 =head2 fetch_value_from_db
197 Arg[1] : DBI::db $dbh - a DBI database handle
198 Arg[2] : String $sql - SQL statement to execute
199 Example : my $num_genes = $object->fetch_value_from_db($dbh,
200 'SELECT count(*) FROM gene');
201 Description : Executes an SQL statement on a db handle and returns the first
202 column of the first row returned. Useful
for queries returning a
203 single value, like table counts.
204 Return type : Return type of SQL statement
205 Exceptions : thrown on wrong or missing arguments
212 sub fetch_value_from_db {
217 throw(
"Need a db handle.") unless ($dbh and $dbh->isa(
'DBI::db'));
218 throw(
"Need an SQL query to execute.") unless ($sql);
220 my $sth = $dbh->prepare($sql);
222 my ($retval) = $sth->fetchrow_array;
228 =head2 dump_table_to_file
230 Arg[1] : String $dbtype - db type (source|target)
231 Arg[2] : String $table - name of table to dump
232 Arg[3] : String $filename - name of dump file
233 Arg[4] : Boolean $check_existing - turn on test
for existing dump
234 Example : my $rows_dumped = $object->dump_table_to_file(
'source',
235 'stable_id_event',
'stable_id_event_existing.txt');
236 Description : Dumps the contents of a db table to a tab-delimited file. The
237 dump file will be written to a subdirectory called
'tables'
238 under the basedir from your configuration.
239 Return type : Int - the number of rows dumped
240 Exceptions : thrown on wrong or missing arguments
247 sub dump_table_to_file {
251 my $filename = shift;
252 my $check_existing = shift;
255 unless (($dbtype eq
'source') or ($dbtype eq
'target')) {
256 throw(
"Missing or unknown db type: $dbtype.");
258 throw(
"Need a table name.") unless ($table);
259 throw(
"Need a filename.") unless ($filename);
261 # conditionally check if table was already dumped
262 if ($check_existing and $self->file_exists($filename,
'tables')) {
263 $self->logger->info(
"$filename exists, won't dump again.\n");
267 my $fh = $self->get_filehandle($filename,
'tables');
269 my $dba = $self->cache->get_DBAdaptor($dbtype);
270 my $dbh = $dba->dbc->db_handle;
271 my $sth = $dbh->prepare(
"SELECT * FROM $table");
276 while (my @row = $sth->fetchrow_array) {
279 # use '\N' for NULL values
280 for (my $j = 0; $j < scalar(@row); $j++) {
281 $row[$j] =
'\N' unless (defined($row[$j]));
284 print $fh join(
"\t", @row);
294 =head2 upload_file_into_table
296 Arg[1] : String $dbtype - db type (source|target)
297 Arg[2] : String $table - name of table to upload the data to
298 Arg[3] : String $filename - name of dump file
299 Arg[4] : Boolean $no_check_empty - don
't check if table is empty
300 Example : my $rows_uploaded = $object->upload_file_into_table('target
',
301 'stable_id_event
', 'stable_id_event_new.txt
');
302 Description : Uploads a tab-delimited data file into a db table. The data file
303 will be taken from a subdirectory 'tables
' under your configured
304 basedir. If the db table isn't empty and $no_check_empty isn
't
305 set, no data is uploaded (and a warning is issued).
306 Return type : Int - the number of rows uploaded
307 Exceptions : thrown on wrong or missing arguments
314 sub upload_file_into_table {
318 my $filename = shift;
319 my $no_check_empty = shift;
322 unless ( ( $dbtype eq 'source
' ) or ( $dbtype eq 'target
' ) ) {
323 throw("Missing or unknown db type: $dbtype.");
325 throw("Need a table name.") unless ($table);
326 throw("Need a filename.") unless ($filename);
328 # sanity check for dry run
329 if ( $self->conf->param('dry_run
') ) {
330 $self->logger->warning(
331 "dry_run - skipping db upload for $filename.\n");
336 join( '/
', $self->conf->param('basedir
'), 'tables
', $filename );
341 $self->logger->debug( "$file -> $table\n", 1 );
343 my $dba = $self->cache->get_DBAdaptor($dbtype);
344 my $dbh = $dba->dbc->db_handle;
347 if ( $table =~ /^([^_]+)_stable_id/ ) {
348 # This is a stable_id table we're working with.
353 # check table is empty
355 unless ($no_check_empty) {
358 qq(SELECT count(*) FROM $table WHERE stable_id IS NOT NULL);
361 $sql = qq(SELECT count(*) FROM $table);
363 $sth = $dbh->prepare($sql);
365 my ($c) = $sth->fetchrow_array;
370 $self->logger->warning(
371 "Table $table contains $c stable IDs.\n",
375 $self->logger->warning(
376 "Table $table not empty: found $c entries.\n",
379 $self->logger->info(
"Data not uploaded!\n", 1 );
382 } ## end unless ($no_check_empty)
384 # now upload the data
386 # Create a temporary table, upload the data into it, and then
387 # update the main table.
389 qq( CREATE TABLE stable_id_$$ ( object_id INTEGER UNSIGNED,
390 stable_id VARCHAR(255),
391 version SMALLINT UNSIGNED,
392 created_date DATETIME,
393 modified_date DATETIME,
394 PRIMARY KEY(object_id) ) )
398 qq(LOAD DATA LOCAL INFILE
'$file' INTO TABLE stable_id_$$));
402 UPDATE $table, stable_id_$$
403 SET $table.stable_id=stable_id_$$.stable_id,
404 $table.version=stable_id_$$.version,
405 $table.created_date=stable_id_$$.created_date,
406 $table.modified_date=stable_id_$$.modified_date
407 WHERE $table.${table}_id = stable_id_$$.object_id )
410 $dbh->do(qq(DROP TABLE stable_id_$$));
411 } ## end
if ($idtable)
413 $dbh->do(qq(LOAD DATA LOCAL INFILE
'$file' INTO TABLE $table));
415 $dbh->do(qq(OPTIMIZE TABLE $table));
417 } ## end
if ( -s $file )
419 $self->logger->warning(
"No data found in file $filename.\n", 1 );
423 } ## end sub upload_file_into_table
429 Example : $object->logger->
info(
"Starting ID mapping.\n");
430 Description : Getter/setter
for logger
object
441 $self->{
'_logger'} = shift
if (@_);
442 return $self->{
'_logger'};
450 Example : my $basedir = $object->conf->
param(
'basedir');
451 Description : Getter/setter
for configuration
object
462 $self->{
'_conf'} = shift
if (@_);
463 return $self->{
'_conf'};
471 Description : Getter/setter
for cache
object
482 $self->{
'_cache'} = shift
if (@_);
483 return $self->{
'_cache'};