Bio::D...edBase(3pm)
π Bio::DB::IndexedBase π
okay bestie, sit down, we're doing Perl manpage time β¨ This is the User Contributed Perl Documentation page for Bio::DB::IndexedBase (that's the (3pm) situation, iykyk). It's long so it's coming to you in parts β this is part 1, more is on the way, don't go anywhere. π«Ά
β¨ NAME β¨
Bio::DB::IndexedBase β it's the base class for modules that vibe with indexed sequence files. It's the blueprint, the blueprint-iest blueprint. π
β¨ SYNOPSIS β¨
use Bio::DB::XXX; # a made-up class that uses Bio::IndexedBase
# 1/ Bio::SeqIO-style access
# Index some sequence files
my $db = Bio::DB::XXX->new('/path/to/file'); # from a single file
my $db = Bio::DB::XXX->new(['file1', 'file2']); # from multiple files
my $db = Bio::DB::XXX->new('/path/to/files/'); # from a directory
# Get IDs of all the sequences in the database
my @ids = $db->get_all_primary_ids;
# Get a specific sequence
my $seq = $db->get_Seq_by_id('CHROMOSOME_I');
# Loop through all sequences
my $stream = $db->get_PrimarySeq_stream;
while (my $seq = $stream->next_seq) {
# Do something...
}
# 2/ Access via filehandle
my $fh = Bio::DB::XXX->newFh('/path/to/file');
while (my $seq = <$fh>) {
# Do something...
}
# 3/ Tied-hash access
tie %sequences, 'Bio::DB::XXX', '/path/to/file';
print $sequences{'CHROMOSOME_I:1,20000'};
πΈ DESCRIPTION πΈ
Bio::DB::IndexedBase is the base class for modules that wanna index and read sequence files and give you persistent, random access to every single sequence entry without dragging the entire file into memory. Like, the file can stay on disk, it's fine, it's thriving. π¦
This module is compliant with the Bio::SeqI interface and both. (Yes that's what it says. We love a mysterious sentence. π) Both Bio::DB::Fasta and Bio::DB::Qual use Bio::DB::IndexedBase. Slay.
When you initialize it, you point it at a single file, several files, or a directory of files. The first time it runs, it generates an index of the file contents using the AnyDBM_File module β and the preference order is BerkeleyDB first, then GDBM_File, then NDBM_File, then SDBM_File. Ranking matters, bestie. π
After that, it uses the index file to find the sequence file and the offset for whatever sequence you asked for. If one of the source files gets updated, it reindexes just that one file β not the whole situation, just the one. Efficient. π You can also force reindexing manually whenever you want.
For improved performance, it keeps a cache of open filehandles and closes the less-recently-used ones when the cache is full. Old filehandles get gently shown the door. πͺ
Entries may have any line length up to 65,536 characters, and different line lengths are allowed in the same file. BUT β within a sequence entry, all lines must be the same length except for the last one. If that's not the case, an error will be thrown. No cap, it's not playing. π
This module was developed for use with the C. elegans and human genomes, and has been tested with sequence segments as large as 20 megabases. Indexing the C. elegans genome (100 megabases of genomic sequence plus 100,000 ESTs) takes ~5 minutes on a 300 MHz pentium laptop. On that same system, average access time for any 200-mer within the C. elegans genome was <0.02s. Ate. π₯
π DATABASE CREATION AND INDEXING π
The two constructors for this class are new() and newFh(). The first one makes a Bio::DB::IndexedBase object that you talk to via method calls. The second one makes a tied filehandle you can use Bio::SeqIO style to fetch sequence objects in a stream. And there's also a tied hash interface, because options are iconic. π
$db = Bio::DB::IndexedBase->new($path [,%options])β Make a freshBio::DB::IndexedBaseobject from the files designated by $path. That $path can be a single file, an arrayref of files, or a directory containing such files. Once the database exists, you can use methods likeget_all_primary_ids()andget_Seq_by_id()to pull sequence objects. π«Ά$fh = Bio::DB::IndexedBase->newFh($path [,%options])β Make a tied filehandle opened on aBio::DB::IndexedBaseobject. Reading from this filehandle with<>gives you a stream of sequence objects,Bio::SeqIOstyle. The path and the options get specified the same way as fornew(). β¨$obj = tie %db,'Bio::DB::IndexedBase', '/path/to/file' [,@args]β Make a tied hash by tieing%dbtoBio::DB::IndexedBaseusing that indicated path to the files. The optional @args list is the same set thatnew()uses. If it works,tie()returns the tied object; otherwiseundef. It's giving all-or-nothing. π
Once it's tied, you can use the hash to grab an individual sequence by its ID, like this:
my $seq = $db{CHROMOSOME_I};
The keys() and values() functions will return the sequence IDs and their sequences, respectively. And each() can be used to iterate over the entire data set:
while (my ($id,$sequence) = each %db) {
print "$id => $sequence\n";
}
When you're dealing with very large sequences, you can avoid bringing them into memory by calling each() in a scalar context. That returns the key only. Then you can use tied(%db) to recover the Bio::DB::IndexedBase object and call its methods. Main character energy, honestly. π
while (my $id = each %db) {
print "$id: $db{$sequence:1,100}\n";
print "$id: ".tied(%db)->length($id)."\n";
}
On top of that, you can invoke the FIRSTKEY and NEXTKEY tied hash methods directly to retrieve the first and next ID in the database, respectively. Which means you can write this iterative loop using just the object-oriented interface:
my $db = Bio::DB::IndexedBase->new('/path/to/file');
for (my $id=$db->FIRSTKEY; $id; $id=$db->NEXTKEY($id)) {
# do something with sequence
}
π¦ INDEX CONTENT π¦
Several attributes of each sequence are stored in the index file. Give it a sequence ID and you can pull those attributes back with these methods:
offset($id)β Get the offset of the indicated sequence from the beginning of the file it lives in. And listen β the offset points to the beginning of the sequence, not the beginning of the header line. Important distinction, bestie. πstrlen($id)β Get the number of characters in the sequence string.length($id)β Get the number of residues of the sequence.linelen($id)β Get the length of the line for this sequence. If the sequence is wrapped, thenlinelen()is likely to be much shorter thanstrlen(). πheaderlen($id)β Get the length of the header line for the indicated sequence.header_offsetβ Get the offset of the header line for the indicated sequence from the beginning of the file it lives in. This attribute is not stored β it gets calculated fromoffset()andheaderlen(). Math is doing the heavy lifting. β¨alphabet($id)β Get the molecular type (alphabet) of the indicated sequence. This method handles residues according to the IUPAC convention. π§¬file($id)β Get the name of the file where the indicated sequence can be found.
π INTERFACE COMPLIANCE NOTES π
Bio::DB::IndexedBase is compliant with the Bio::DB::SeqI interface and hence with the Bio::RandomAccessI interfaces. β¨
Databases do not necessarily provide any meaningful internal primary ID for the sequences they store. However, Bio::DB::IndexedBase's internal primary IDs are the IDs of the sequences. Which means the same ID passed to get_Seq_by_id() and get_Seq_by_primary_id() will return the same sequence. Convenient? We love that for us. π«Ά
Since this database index has no notion of sequence version or namespace, the get_Seq_by_id(), get_Seq_by_acc() and get_Seq_by_version() are identical. Three names, one behavior. Periodt.
π BUGS π
When a sequence is deleted from one of the files, this deletion is not detected by the module and not removed from the index. As a result, a "ghost" entry will remain in the index and will return garbage results if accessed. Haunted behavior, honestly. π»
Also, if you are indexing a directory, it is wise to not add or remove files from it.
In case you have changed the files in a directory, or the sequences in a file, you can rebuild the entire index β either by deleting it manually, or by passing -reindex=>1 to new() when initializing the module. Fresh start, glow up. β¨
π SEE ALSO π
DB_FileBio::DB::FastaBio::DB::Qual
π AUTHOR π
Lincoln Stein <lstein@cshl.org>.
Copyright (c) 2001 Cold Spring Harbor Laboratory.
Florent Angly (for the modularization) π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See DISCLAIMER.txt for disclaimers of warranty. π
and that's part 1, bestie β more is coming, stay tuned π
β¨ APPENDIX β¨
okay bestie, the rest of this doc is where we go method by method through the object. Internal methods are usually preceded with a _ (you know, the little underscore marking the private ones π).
new
- Title : new
- Usage :
my $db = Bio::DB::IndexedBase->new($path, -reindex => 1); - Function : Initialize a new database object
- Returns : A Bio::DB::IndexedBase object
- Args : A single file, or path to dir, or arrayref of files
Optional arguments, and bestie I need you to read ALL of these:
-globβ Glob expression to search for files in directories. Default:*-makeidβ A code subroutine for transforming IDs. Default:None-maxopenβ Maximum size of filehandle cache. Default:32-debugβ Turn on status messages. Default:0-reindexβ Force the index to be rebuilt. Default:0-dbmargsβ Additional arguments to pass to the DBM routine. Default:None-index_nameβ Name of the file that will hold the indices-cleanβ Remove the index file when finished. Default:0
The -dbmargs option can be used to control the format of the index. Like, for example, you can pass $DB_BTREE to this argument so as to force the IDs to be sorted and retrieved alphabetically. But heads up β you MUST use the same arguments every time you open the index! No switching up on it, fr fr.
The -makeid option gives you a chance to modify sequence IDs during indexing. Say what?? Yes. For example, you may wish to extract a portion of the gi|gb|abc|xyz nonsense that GenBank Fasta files use. The original header line can be recovered later. The option value for -makeid should be a code reference that takes a scalar argument (the full header line) and returns a scalar or an array of scalars (the ID or IDs you want to assign). For example:
$db = Bio::DB::IndexedBase->new('file.fa', -makeid => \&extract_gi);
sub extract_gi {
# Extract GI from GenBank
my $header = shift;
my ($id) = ($header =~ /gi\|(\d+)/m);
return $id || '';
}
extract_gi() will be called with the full header line, e.g. a Fasta line would include the >, the ID and the description:
>gi|352962132|ref|NG_030353.1| Homo sapiens sal-like 3 (Drosophila) (SALL3)
So in the database, this sequence can now be retrieved by its GI instead of its complete ID:
my $seq = $db->get_Seq_by_id(352962132);
the -makeid option is ignored after the index is constructed. It's a one-time thing, it's giving set-and-forget π
newFh
- Title : newFh
- Usage :
my $fh = Bio::DB::IndexedBase->newFh('/path/to/files/', %options); - Function : Index and get a new Fh for a single file, several files or a directory
- Returns : Filehandle object
- Args : Same as
new()
dbmargs
- Title : dbmargs
- Usage :
my @args = $db->dbmargs; - Function : Get stored dbm arguments
- Returns : Array
- Args : None
glob
- Title : glob
- Usage :
my $glob = $db->glob; - Function : Get the expression used to match files in directories
- Returns : String
- Args : None
index_dir
- Title : index_dir
- Usage :
$db->index_dir($dir); - Function : Index the files that match
-globin the given directory - Returns : Hashref of offsets
- Args : Dirname
- Boolean to force a reindexing the directory
get_all_primary_ids
- Title : get_all_primary_ids, get_all_ids, ids
- Usage :
my @ids = $db->get_all_primary_ids; - Function : Get the IDs stored in all indexes. This is a Bio::DB::SeqI method implementation. Note that in this implementation, the internal database primary IDs are also the sequence IDs.
- Returns : List of ids
- Args : None
index_file
- Title : index_file
- Usage :
$db->index_file($filename); - Function : Index the given file
- Returns : Hashref of offsets
- Args : Filename
- Boolean to force reindexing the file
index_files
- Title : index_files
- Usage :
$db->index_files(\@files); - Function : Index the given files
- Returns : Hashref of offsets
- Args : Arrayref of filenames
- Boolean to force reindexing the files
index_name
- Title : index_name
- Usage :
my $indexname = $db->index_name($path); - Function : Get the full name of the index file
- Returns : String
- Args : None
path
- Title : path
- Usage :
my $path = $db->path($path); - Function : When a single file or a directory of files is indexed, this returns the file directory. When indexing an arbitrary list of files, the return value is the path of the current working directory.
- Returns : String
- Args : None
get_PrimarySeq_stream
- Title : get_PrimarySeq_stream
- Usage :
my $stream = $db->get_PrimarySeq_stream(); - Function : Get a SeqIO-like stream of sequence objects. The stream supports a single method,
next_seq(). Each call tonext_seq()returns a new PrimarySeqI compliant sequence object, until no more sequences remain. This is a Bio::DB::SeqI method implementation. - Returns : A Bio::DB::Indexed::Stream object
- Args : None
get_Seq_by_id
- Title : get_Seq_by_id, get_Seq_by_acc, get_Seq_by_version, get_Seq_by_primary_id
- Usage :
my $seq = $db->get_Seq_by_id($id); - Function : Given an ID, fetch the corresponding sequence from the database. This is a Bio::DB::SeqI and Bio::DB::RandomAccessI method implementation.
- Returns : A sequence object
- Args : ID
_calculate_offsets
okay this one has the little underscore so you already know it's internal business π
- Title : _calculate_offsets
- Usage :
$db->_calculate_offsets($filename, $offsets); - Function : This method calculates the sequence offsets in a file based on ID and should be implemented by classes that use Bio::DB::IndexedBase.
- Returns : Hash of offsets
- Args : File to process
- Hashref of file offsets keyed by IDs.
offset
- Title : offset
- Usage :
my $offset = $db->offset($id); - Function : Get the offset of the indicated sequence from the beginning of the file in which it is located. The offset points to the beginning of the sequence, not the beginning of the header line. (important distinction bestie, don't get it twisted π€)
- Returns : String
- Args : ID of sequence
strlen
- Title : strlen
- Usage :
my $length = $db->strlen($id); - Function : Get the number of characters in the sequence string.
- Returns : Integer
- Args : ID of sequence
length
- Title : length
- Usage :
my $length = $db->length($id); - Function : Get the number of residues of the sequence.
- Returns : Integer
- Args : ID of sequence
linelen
- Title : linelen
- Usage :
my $linelen = $db->linelen($id); - Function : Get the length of the line for this sequence.
- Returns : Integer
- Args : ID of sequence
headerlen
- Title : headerlen
- Usage :
my $length = $db->headerlen($id); - Function : Get the length of the header line for the indicated sequence.
- Returns : Integer
- Args : ID of sequence
header_offset
- Title : header_offset
- Usage :
my $offset = $db->header_offset($id); - Function : Get the offset of the header line for the indicated sequence from the beginning of the file in which it is located.
- Returns : String
- Args : ID of sequence
alphabet
- Title : alphabet
- Usage :
my $alphabet = $db->alphabet($id); - Function : Get the molecular type of the indicated sequence:
dna,rnaorprotein - Returns : String
- Args : ID of sequence
file
- Title : file
- Usage :
my $file = $db->file($id); - Function : Get the the name of the file in which the indicated sequence can be found. (yes bestie, "the the" is in the original, we're not fixing it, we're respecting it π)
- Returns : String
- Args : ID of sequence
and that's the whole page, you ate it π β¨
this was Bio::DB::IndexedBase, part of the perl v5.32.1 docs, dated 2021-08-15 β go forth and index your lil heart out bestie, ily π«Άππ¦
π¦ where this page comes from
- debian bookworm: libbio-perl-perl 1.7.8-1 Β· Perl
- debian trixie: libbio-perl-perl 1.7.8-1 Β· Perl
- ubuntu jammy: libbio-perl-perl 1.7.8-1 Β· Perl
- ubuntu noble: libbio-perl-perl 1.7.8-1 Β· Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms