Bio::DB FileCache
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Summary
Bio::DB::FileCache - In file cache for BioSeq objects
Package variables
No package variables defined.
Included modules
Bio::Annotation::Collection
Bio::Location::Fuzzy
Bio::Location::Split
Bio::Seq
Bio::Seq::RichSeq
Bio::SeqFeature::Generic
Bio::Species
DB_File
Fcntl qw ( O_CREAT O_RDWR O_RDONLY )
File::Temp ' tmpnam '
Storable qw ( freeze thaw )
Inherit
Bio::DB::SeqI Bio::Root::Root
Synopsis
  $cachedb = Bio::DB::FileCache->new($real_db);
# # $real_db is a Bio::DB::RandomAccessI database # $seq = $cachedb->get_Seq_by_id('ROA1_HUMAN'); # # $seq is a Bio::Seq object # # more control provided with named-parameter form $cachedb = Bio::DB::FileCache->new( -seqdb => $real_db, -file => $path, -keep => $flag, );
Description
This is a disk cache system which saves the objects returned by
Bio::DB::RandomAccessI on disk. The disk cache grows without limit,
while the process is running, but is automatically unlinked at process
termination unless the -keep flag is set.
This module requires DB_File and Storable.
Methods
newDescriptionCode
get_Seq_by_idDescriptionCode
get_Seq_by_accDescriptionCode
seqdbDescriptionCode
file_nameDescriptionCode
keepDescriptionCode
dbDescriptionCode
flushDescriptionCode
_get
No description
Code
_store
No description
Code
get_Seq_by_versionDescriptionCode
DESTROY
No description
Code
_open_database
No description
Code
Methods description
newcode    nextTop
 Title   : new
Usage : $db = Bio::DB::FileCache->new(
-seqdb => $db, # Bio::DB::RandomAccessI database
-file => $path, # path to index file
-keep => $flag, # don't unlink index file
)
Function: creates a new on-disk cache
Returns : a Bio::DB::RandomAccessI database
Args : as above
Throws : "Must be a randomaccess database" exception
"Could not open primary index file" exception
If no index file is specified, will create a temporary file in your
system's temporary file directory. The name of this temporary file
can be retrieved using file_name().
get_Seq_by_idcodeprevnextTop
 Title   : get_Seq_by_id
Usage : $seq = $db->get_Seq_by_id('ROA1_HUMAN')
Function: Gets a Bio::Seq object by its name
Returns : a Bio::Seq object
Args : the id (as a string) of a sequence
Throws : "id does not exist" exception
get_Seq_by_acccodeprevnextTop
 Title   : get_Seq_by_acc
Usage : $seq = $db->get_Seq_by_acc('X77802');
Function: Gets a Bio::Seq object by accession number
Returns : A Bio::Seq object
Args : accession number (as a string)
Throws : "acc does not exist" exception
seqdbcodeprevnextTop
 Title   : seqdb
Usage : $seqdb = $db->seqdb([$seqdb])
Function: gets/sets the Bio::DB::RandomAccessI database
Returns : a Bio::DB::RandomAccessI database
Args : new sequence database (optional)
Throws : nothing
file_namecodeprevnextTop
 Title   : file_name
Usage : $path = $db->file_name([$file_name])
Function: gets/sets the name of the cache file
Returns : a path
Args : new cache file name (optional)
Throws : nothing
It probably isn't useful to set the cache file name after you've
opened it.
keepcodeprevnextTop
 Title   : keep
Usage : $keep = $db->keep([$flag])
Function: gets/sets the value of the "keep" flag
Returns : current value
Args : new value (optional)
Throws : nothing
The keep flag will cause the index file to be unlinked when the
process exits. Since on some operating systems (Unix, OS/2) the
unlinking occurs during the new() call immediately after opening the
file, it probably isn't safe to change this value.
dbcodeprevnextTop
 Title   : db
Usage : $db->db
Function: returns tied hash to index database
Returns : a Berkeley DB tied hashref
Args : none
Throws : nothing
flushcodeprevnextTop
 Title   : flush
Usage : $db->flush
Function: flushes the cache
Returns : nothing
Args : none
Throws : nothing
get_Seq_by_versioncodeprevnextTop
 Title   : get_Seq_by_version
Usage : $seq = $db->get_Seq_by_version('X77802.1');
Function: Gets a Bio::Seq object by sequence version
Returns : A Bio::Seq object
Args : accession.version (as a string)
Throws : "acc.version does not exist" exception
Methods code
newdescriptionprevnextTop
sub new {
    my ($class,@args) = @_;

    my $self = Bio::Root::Root->new();
    bless $self,$class;

    my ($seqdb,$file_name,$keep) = $self->_rearrange([qw(SEQDB FILE
							 KEEP)],@args);

    if( !defined $seqdb || !ref $seqdb ||
	! $seqdb->isa('Bio::DB::RandomAccessI') ) {
       $self->throw("Must be a randomaccess database not a [$seqdb]");
    }

    $self->seqdb($seqdb);
    $file_name ||= tmpnam();
    $self->file_name($file_name);
    $self->keep($keep);

    $self->_open_database($file_name);
    return $self;
}
get_Seq_by_iddescriptionprevnextTop
sub get_Seq_by_id {
   my ($self,$id) = @_;

   # look in the cache first
my $obj = $self->_get('id' => $id); return $obj if defined $obj; # get object from seqdb
$obj = $self->seqdb->get_Seq_by_id($id); $self->_store('id' => $id, $obj); return $obj;
}
get_Seq_by_accdescriptionprevnextTop
sub get_Seq_by_acc {
   my ($self,$acc) = @_;

   # look in the cache first
my $obj = $self->_get('acc' => $acc); return $obj if defined $obj; # get object from seqdb
$obj = $self->seqdb->get_Seq_by_acc($acc); $self->_store('acc' => $acc, $obj); return $obj;
}
seqdbdescriptionprevnextTop
sub seqdb {
    my ($self, $seqdb) = @_;
    if ($seqdb) {
        $self->{'seqdb'} = $seqdb;
    } else {
        return $self->{'seqdb'};
    }
}
file_namedescriptionprevnextTop
sub file_name {
  my $self = shift;
  my $d = $self->{file_name};
  $self->{file_name} = shift if @_;
  $d;
}
keepdescriptionprevnextTop
sub keep {
  my $self = shift;
  my $d = $self->{keep};
  $self->{keep} = shift if @_;
  $d;
}
dbdescriptionprevnextTop
sub db {
 shift->{db}
}
flushdescriptionprevnextTop
sub flush {
  my $db = shift->db or return;
  %{$db} = ();
}
_getdescriptionprevnextTop
sub _get {
  my $self = shift;
  my ($type,$id) = @_;
  my $serialized = $self->db->{"${type}_${id}"};
  my $obj = thaw($serialized);
  $obj;
}
_storedescriptionprevnextTop
sub _store {
  my $self = shift;
  my ($type,$id,$obj) = @_;
  if( ! defined $obj ) {
      # bug #1628
$self->debug("tried to store an undefined value for $id, skipping"); return; } my $serialized = freeze($obj); $self->db->{"${type}_${id}"} = $serialized;
}
get_Seq_by_versiondescriptionprevnextTop
sub get_Seq_by_version {
   my ($self,@args) = @_;
   $self->throw("Not implemented it");
}
DESTROYdescriptionprevnextTop
sub DESTROY {
  my $self = shift;
  unlink $self->file_name unless $self->keep;
}
_open_databasedescriptionprevnextTop
sub _open_database {
  my $self = shift;
  my $file = shift;
  my $flags = O_CREAT|O_RDWR;
  my %db;
  tie(%db,'DB_File',$file,$flags,0666,$DB_BTREE)
    or $self->throw("Could not open primary index file");
  $self->{db} =\% db;
  unlink $file unless $self->keep;
}
General documentation
CONTACTTop
Lincoln Stein <lstein@cshl.org>
Reporting BugsTop
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:
  http://bugzilla.open-bio.org/
APPENDIXTop
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _