/usr/share/perl5/Bio/Index/Abstract.pm is in libbio-perl-perl 1.7.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | #
#
# BioPerl module for Bio::Index::Abstract
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Ewan Birney <birney@sanger.ac.uk>
# and James Gilbert <jgrg@sanger.ac.uk>
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::Index::Abstract - Abstract interface for indexing a flat file
=head1 SYNOPSIS
You should not be using this module directly
=head1 USING DB_FILE
To use DB_File and not SDBM for this index, pass the value:
-dbm_package => 'DB_File'
to new (see below).
=head1 DESCRIPTION
This object provides the basic mechanism to associate positions
in files with names. The position and filenames are stored in DBM
which can then be accessed later on. It is the equivalent of flat
file indexing (eg, SRS or efetch).
This object is the guts to the mechanism, which will be used by the
specific objects inheriting from it.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
https://github.com/bioperl/bioperl-live/issues
=head1 AUTHOR - Ewan Birney, James Gilbert
Email - birney@sanger.ac.uk, jgrg@sanger.ac.uk
=head1 APPENDIX
The rest of the documentation details each of the object methods. Internal
methods are usually preceded with an "_" (underscore).
=cut
# Let the code begin...
package Bio::Index::Abstract;
use strict;
use Fcntl qw( O_RDWR O_CREAT O_RDONLY );
use vars qw( $TYPE_AND_VERSION_KEY
$USE_DBM_TYPE $DB_HASH );
use Bio::Root::IO;
use Symbol;
use base qw(Bio::Root::Root);
# Generate accessor methods for simple object fields
BEGIN {
foreach my $func (qw(filename write_flag)) {
no strict 'refs';
my $field = "_$func";
*$func = sub {
my( $self, $value ) = @_;
if (defined $value) {
$self->{$field} = $value;
}
return $self->{$field};
}
}
}
=head2 new
Usage : $index = Bio::Index::Abstract->new(
-filename => $dbm_file,
-write_flag => 0,
-dbm_package => 'DB_File',
-verbose => 0);
Function: Returns a new index object. If filename is
specified, then open_dbm() is immediately called.
Bio::Index::Abstract->new() will usually be called
directly only when opening an existing index.
Returns : A new index object
Args : -filename The name of the dbm index file.
-write_flag TRUE if write access to the dbm file is
needed.
-dbm_package The Perl dbm module to use for the
index.
-verbose Print debugging output to STDERR if
TRUE.
=cut
sub new {
my($class, @args) = @_;
my $self = $class->SUPER::new(@args);
my( $filename, $write_flag, $dbm_package, $cachesize, $ffactor, $pathtype ) =
$self->_rearrange([qw(FILENAME
WRITE_FLAG
DBM_PACKAGE
CACHESIZE
FFACTOR
PATHTYPE
)], @args);
# Store any parameters passed
$self->filename($filename) if $filename;
$self->cachesize($cachesize) if $cachesize;
$self->ffactor($ffactor) if $ffactor;
$self->write_flag($write_flag) if $write_flag;
$self->dbm_package($dbm_package) if $dbm_package;
#If user doesn't give a path, we default it to absolute
$pathtype ? $self->pathtype($pathtype) : $self->pathtype('absolute');
$self->{'_filehandle'} = []; # Array in which to cache SeqIO objects
$self->{'_DB'} = {}; # Gets tied to the DBM file
# Open database
$self->open_dbm() if $filename;
return $self;
}
=pod
=head2 filename
Title : filename
Usage : $value = $self->filename();
$self->filename($value);
Function: Gets or sets the name of the dbm index file.
Returns : The current value of filename
Args : Value of filename if setting, or none if
getting the value.
=head2 write_flag
Title : write_flag
Usage : $value = $self->write_flag();
$self->write_flag($value);
Function: Gets or sets the value of write_flag, which
is whether the dbm file should be opened with
write access.
Returns : The current value of write_flag (default 0)
Args : Value of write_flag if setting, or none if
getting the value.
=head2 dbm_package
Usage : $value = $self->dbm_package();
$self->dbm_package($value);
Function: Gets or sets the name of the Perl dbm module used.
If the value is unset, then it returns the value of
the package variable $USE_DBM_TYPE or if that is
unset, then it chooses the best available dbm type,
choosing 'DB_File' in preference to 'SDBM_File'.
Bio::Abstract::Index may work with other dbm file
types.
Returns : The current value of dbm_package
Args : Value of dbm_package if setting, or none if
getting the value.
=cut
sub dbm_package {
my( $self, $value ) = @_;
my $to_require = 0;
if( $value || ! $self->{'_dbm_package'} ) {
my $type = $value || $USE_DBM_TYPE || 'DB_File';
if( $type =~ /DB_File/i ) {
eval {
require DB_File;
};
$type = ( $@ ) ? 'SDBM_File' : 'DB_File';
}
if( $type ne 'DB_File' ) {
eval { require "$type.pm"; };
$self->throw($@) if( $@ );
}
$self->{'_dbm_package'} = $type;
if( ! defined $USE_DBM_TYPE ) {
$USE_DBM_TYPE = $self->{'_dbm_package'};
}
}
return $self->{'_dbm_package'};
}
=head2 db
Title : db
Usage : $index->db
Function: Returns a ref to the hash which is tied to the dbm
file. Used internally when adding and retrieving
data from the database.
Example : $db = $index->db();
$db->{ $some_key } = $data
$data = $index->db->{ $some_key };
Returns : ref to HASH
Args : NONE
=cut
sub db {
return $_[0]->{'_DB'};
}
=head2 get_stream
Title : get_stream
Usage : $stream = $index->get_stream( $id );
Function: Returns a file handle with the file pointer
at the approprite place
This provides for a way to get the actual
file contents and not an object
WARNING: you must parse the record deliminter
*yourself*. Abstract won't do this for you
So this code
$fh = $index->get_stream($myid);
while( <$fh> ) {
# do something
}
will parse the entire file if you don't put in
a last statement in, like
while( <$fh> ) {
/^\/\// && last; # end of record
# do something
}
Returns : A filehandle object
Args : string represents the accession number
Notes : This method should not be used without forethought
=cut
#'
sub get_stream {
my ($self,$id) = @_;
my ($desc,$acc,$out);
my $db = $self->db();
if (my $rec = $db->{ $id }) {
my( @record );
my ($file, $begin, $end) = $self->unpack_record( $rec );
# Get the (possibly cached) filehandle
my $fh = $self->_file_handle( $file );
# move to start
seek($fh, $begin, 0);
return $fh;
} else {
$self->throw("Unable to find a record for $id in the flat file index");
}
}
=head2 cachesize
Usage : $index->cachesize(1000000)
Function: Sets the dbm file cache size for the index.
Needs to be set before the DBM file gets opened.
Example : $index->cachesize(1000000)
Returns : size of the curent cache
=cut
sub cachesize {
my( $self, $size ) = @_;
if(defined $size){
$self->{'_cachesize'} = $size;
}
return ( $self->{'_cachesize'} );
}
=head2 ffactor
Usage : $index->ffactor(1000000)
Function: Sets the dbm file fill factor.
Needs to be set before the DBM file gets opened.
Example : $index->ffactor(1000000)
Returns : size of the curent cache
=cut
sub ffactor {
my( $self, $size ) = @_;
if(defined $size){
$self->{'_ffactor'} = $size;
}
return ( $self->{'_ffactor'} );
}
=head2 open_dbm
Usage : $index->open_dbm()
Function: Opens the dbm file associated with the index
object. Write access is only given if explicitly
asked for by calling new(-write => 1) or having set
the write_flag(1) on the index object. The type of
dbm file opened is that returned by dbm_package().
The name of the file to be is opened is obtained by
calling the filename() method.
Example : $index->_open_dbm()
Returns : 1 on success
=cut
sub open_dbm {
my( $self ) = @_;
my $filename = $self->filename()
or $self->throw("filename() not set");
my $db = $self->db();
# Close the dbm file if already open (maybe we're getting
# or dropping write access
if (ref($db) ne 'HASH') {
untie($db);
}
# What kind of DBM file are we going to open?
my $dbm_type = $self->dbm_package;
# Choose mode for opening dbm file (read/write+create or read-only).
my $mode_flags = $self->write_flag ? O_RDWR|O_CREAT : O_RDONLY;
# Open the dbm file
if ($dbm_type eq 'DB_File') {
my $hash_inf = DB_File::HASHINFO->new();
my $cache = $self->cachesize();
my $ffactor = $self->ffactor();
if ($cache){
$hash_inf->{'cachesize'} = $cache;
}
if ($ffactor){
$hash_inf->{'ffactor'} = $ffactor;
}
tie( %$db, $dbm_type, $filename, $mode_flags, 0644, $hash_inf )
or $self->throw("Can't open '$dbm_type' dbm file '$filename' : $!");
} else {
tie( %$db, $dbm_type, $filename, $mode_flags, 0644 )
or $self->throw("Can't open '$dbm_type' dbm file '$filename' : $!");
}
# The following methods access data in the dbm file:
# Now, if we're a Bio::Index::Abstract caterpillar, then we
# transform ourselves into a Bio::Index::<something> butterfly!
if( ref($self) eq "Bio::Index::Abstract" ) {
my $pkg = $self->_code_base();
bless $self, $pkg;
}
# Check or set this is the right kind and version of index
$self->_type_and_version();
# Check files haven't changed size since they were indexed
$self->_check_file_sizes();
return 1;
}
=head2 _version
Title : _version
Usage : $type = $index->_version()
Function: Returns a string which identifes the version of an
index module. Used to permanently identify an index
file as having been created by a particular version
of the index module. Must be provided by the sub class
Example :
Returns :
Args : none
=cut
sub _version {
my $self = shift;
$self->throw("In Bio::Index::Abstract, no _version method in sub class");
}
=head2 _code_base
Title : _code_base
Usage : $code = $db->_code_base();
Function:
Example :
Returns : Code package to be used with this
Args :
=cut
sub _code_base {
my ($self) = @_;
my $code_key = '__TYPE_AND_VERSION';
my $record;
$record = $self->db->{$code_key};
my($code,$version) = $self->unpack_record($record);
if( wantarray ) {
return ($code,$version);
} else {
return $code;
}
}
=head2 _type_and_version
Title : _type_and_version
Usage : Called by _initalize
Function: Checks that the index opened is made by the same index
module and version of that module that made it. If the
index is empty, then it adds the information to the
database.
Example :
Returns : 1 or exception
Args : none
=cut
sub _type_and_version {
my $self = shift;
my $key = '__TYPE_AND_VERSION';
my $version = $self->_version();
my $type = ref $self;
# Run check or add type and version key if missing
if (my $rec = $self->db->{ $key }) {
my( $db_type, $db_version ) = $self->unpack_record($rec);
$self->throw("This index file is from version [$db_version] - You need to rebuild it to use module version [$version]")
unless $db_version == $version;
$self->throw("This index file is type [$db_type] - Can't access it with module for [$type]")
unless $db_type eq $type;
} else {
$self->add_record( $key, $type, $version )
or $self->throw("Can't add Type and Version record");
}
return 1;
}
=head2 _check_file_sizes
Title : _check_file_sizes
Usage : $index->_check_file_sizes()
Function: Verifies that the files listed in the database
are the same size as when the database was built,
or throws an exception. Called by the new()
function.
Example :
Returns : 1 or exception
Args :
=cut
sub _check_file_sizes {
my $self = shift;
my $num = $self->_file_count() || 0;
for (my $i = 0; $i < $num; $i++) {
my( $file, $stored_size ) = $self->unpack_record( $self->db->{"__FILE_$i"} );
my $size = -s $file;
unless ($size == $stored_size) {
$self->throw("file $i [ $file ] has changed size $stored_size -> $size. This probably means you need to rebuild the index.");
}
}
return 1;
}
=head2 make_index
Title : make_index
Usage : $index->make_index( FILE_LIST )
Function: Takes a list of file names, checks that they are
all fully qualified, and then calls _filename() on
each. It supplies _filename() with the name of the
file, and an integer which is stored with each record
created by _filename(). Can be called multiple times,
and can be used to add to an existing index file.
Example : $index->make_index( '/home/seqs1', '/home/seqs2', '/nfs/pub/big_db' );
Returns : Number of files indexed
Args : LIST OF FILES
=cut
sub make_index {
my($self, @files) = @_;
my $count = 0;
my $recs = 0;
# blow up if write flag is not set. EB fix
if( !defined $self->write_flag ) {
$self->throw("Attempting to make an index on a read-only database. What about a WRITE flag on opening the index?");
}
# We're really fussy/lazy, expecting all file names to be fully qualified
$self->throw("No files to index provided") unless @files;
for(my $i=0;$i<scalar @files; $i++) {
if( $Bio::Root::IO::FILESPECLOADED && File::Spec->can('rel2abs') ) {
if( ! File::Spec->file_name_is_absolute($files[$i])
&& $self->pathtype() ne 'relative') {
$files[$i] = File::Spec->rel2abs($files[$i]);
}
} else {
if( $^O =~ /MSWin/i ) {
($files[$i] =~ m|^[A-Za-z]:/|) ||
$self->throw("Not an absolute file path '$files[$i]'");
} else {
($files[$i] =~ m|^/|) ||
$self->throw("Not an absolute file path '$files[$i]'");
}
}
$self->throw("File does not exist '$files[$i]'") unless -e $files[$i];
}
# Add each file to the index
FILE :
foreach my $file (@files) {
my $i; # index for this file
# Get new index for this file and increment file count
if ( defined(my $count = $self->_file_count) ) {
$i = $count;
} else {
$i = 0; $self->_file_count(0);
}
# see whether this file has been already indexed
my ($record,$number,$size);
if( ($record = $self->db->{"__FILENAME_$file"}) ) {
($number,$size) = $self->unpack_record($record);
# if it is the same size - fine. Otherwise die
if( -s $file == $size ) {
$self->warn("File $file already indexed. Skipping...");
next FILE;
} else {
$self->throw("In index, $file has changed size ($size). Indicates that the index is out of date");
}
}
# index this file
$self->debug("Indexing file $file\n");
# this is supplied by the subclass and does the serious work
$recs += $self->_index_file( $file, $i ); # Specific method for each type of index
# Save file name and size for this index
$self->add_record("__FILE_$i", $file, -s $file)
or $self->throw("Can't add data to file: $file");
$self->add_record("__FILENAME_$file", $i, -s $file)
or $self->throw("Can't add data to file: $file");
# increment file lines
$i++; $self->_file_count($i);
my $temp;
$temp = $self->_file_count();
}
return ($count, $recs);
}
=head2 pathtype
Title : pathtype
Usage : $index->pathtype($pathtype)
Function: Set the type of the file path
Only two values are supported, 'relative' or 'absolute'.
If the user does not give any value, it is set to
absolute by default. Thus it mimics the default
behavior of Bio::Index::Abstract module.
Example : my $index = Bio::Index::Abstract->(-pathtype => 'relative',
-file => $file.inx,
);
or
$index->pathtype('relative');
Returns : Type of the path.
Args : String (relative|absolute)
=cut
sub pathtype {
my($self, $type) = @_;
if(defined($type)){
if($type ne 'absolute' && $type ne 'relative'){
$self->throw("Type of path can only be 'relative' or 'absolute', not [$type].");
}
$self->{'_filepathtype'} = $type;
}
return $self->{'_filepathtype'};
}
=head2 _filename
Title : _filename
Usage : $index->_filename( FILE INT )
Function: Indexes the file
Example :
Returns :
Args :
=cut
sub _index_file {
my $self = shift;
my $pkg = ref($self);
$self->throw("Error: '$pkg' does not provide the _index_file() method");
}
=head2 _file_handle
Title : _file_handle
Usage : $fh = $index->_file_handle( INT )
Function: Returns an open filehandle for the file
index INT. On opening a new filehandle it
caches it in the @{$index->_filehandle} array.
If the requested filehandle is already open,
it simply returns it from the array.
Example : $first_file_indexed = $index->_file_handle( 0 );
Returns : ref to a filehandle
Args : INT
=cut
sub _file_handle {
my( $self, $i ) = @_;
unless ($self->{'_filehandle'}[$i]) {
my @rec = $self->unpack_record($self->db->{"__FILE_$i"})
or $self->throw("Can't get filename for index : $i");
my $file = $rec[0];
open my $fh, '<', $file or $self->throw("Could not read file '$file': $!");
$self->{'_filehandle'}[$i] = $fh; # Cache filehandle
}
return $self->{'_filehandle'}[$i];
}
=head2 _file_count
Title : _file_count
Usage : $index->_file_count( INT )
Function: Used by the index building sub in a sub class to
track the number of files indexed. Sets or gets
the number of files indexed when called with or
without an argument.
Example :
Returns : INT
Args : INT
=cut
sub _file_count {
my $self = shift;
if (@_) {
$self->db->{'__FILE_COUNT'} = shift;
}
return $self->db->{'__FILE_COUNT'};
}
=head2 add_record
Title : add_record
Usage : $index->add_record( $id, @stuff );
Function: Calls pack_record on @stuff, and adds the result
of pack_record to the index database under key $id.
If $id is a reference to an array, then a new entry
is added under a key corresponding to each element
of the array.
Example : $index->add_record( $id, $fileNumber, $begin, $end )
Returns : TRUE on success or FALSE on failure
Args : ID LIST
=cut
sub add_record {
my( $self, $id, @rec ) = @_;
$self->debug( "Adding key $id\n");
if( exists $self->db->{$id} ) {
$self->warn("overwriting a current value stored for $id\n");
}
$self->db->{$id} = $self->pack_record( @rec );
return 1;
}
=head2 pack_record
Title : pack_record
Usage : $packed_string = $index->pack_record( LIST )
Function: Packs an array of scalars into a single string
joined by ASCII 034 (which is unlikely to be used
in any of the strings), and returns it.
Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
Returns : STRING or undef
Args : LIST
=cut
sub pack_record {
my( $self, @args ) = @_;
# Silence undefined warnings
@args = map {
$_ = (defined $_) ? $_ : '';
$_ ;
} @args;
return join "\034", @args;
}
=head2 unpack_record
Title : unpack_record
Usage : $index->unpack_record( STRING )
Function: Splits the sting provided into an array,
splitting on ASCII 034.
Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
Returns : A 3 element ARRAY
Args : STRING containing ASCII 034
=cut
sub unpack_record {
my( $self, @args ) = @_;
return split /\034/, $args[0];
}
=head2 count_records
Title : count_records
Usage : $recs = $seqdb->count_records()
Function: return count of all recs in the index
Example :
Returns : a scalar
Args : none
=cut
sub count_records {
my ($self,@args) = @_;
my $db = $self->db;
my $c = 0;
while (my($id, $rec) = each %$db) {
if( $id =~ /^__/ ) {
# internal info
next;
}
$c++;
}
return ($c);
}
=head2 DESTROY
Title : DESTROY
Usage : Called automatically when index goes out of scope
Function: Closes connection to database and handles to
sequence files
Returns : NEVER
Args : NONE
=cut
sub DESTROY {
my $self = shift;
untie($self->{'_DB'});
# An additional undef was the only way to force
# the object to drop the open filehandles for ActivePerl
undef $self->{'_DB'};
}
1;
|