2 # BioPerl module for Bio::DB::IndexedBase
4 # You may distribute this module under the same terms as perl itself
9 Bio::DB::IndexedBase - Base class for modules using indexed sequence files
13 use Bio::DB::XXX; # a made-up class that uses Bio::IndexedBase
15 # 1/ Bio::SeqIO-style access
17 # Index some sequence files
18 my $db = Bio::DB::XXX->new('/path/to/file'); # from a single file
19 my $db = Bio::DB::XXX->new(['file1', 'file2']); # from multiple files
20 my $db = Bio::DB::XXX->new('/path/to/files/'); # from a directory
22 # Get IDs of all the sequences in the database
23 my @ids = $db->get_all_primary_ids;
25 # Get a specific sequence
26 my $seq = $db->get_Seq_by_id('CHROMOSOME_I');
28 # Loop through all sequences
29 my $stream = $db->get_PrimarySeq_stream;
30 while (my $seq = $stream->next_seq) {
35 # 2/ Access via filehandle
36 my $fh = Bio::DB::XXX->newFh('/path/to/file');
37 while (my $seq = <$fh>) {
43 tie %sequences, 'Bio::DB::XXX', '/path/to/file';
44 print $sequences{'CHROMOSOME_I:1,20000'};
48 Bio::DB::IndexedBase provides a base class for modules that want to index
49 and read sequence files and provides persistent, random access to each sequence
50 entry, without bringing the entire file into memory. This module is compliant
51 with the Bio::SeqI interface and both. Bio::DB::Fasta and Bio::DB::Qual both use
54 When you initialize the module, you point it at a single file, several files, or
55 a directory of files. The first time it is run, the module generates an index
56 of the content of the files using the AnyDBM_File module (BerkeleyDB preferred,
57 followed by GDBM_File, NDBM_File, and SDBM_File). Subsequently, it uses the
58 index file to find the sequence file and offset for any requested sequence. If
59 one of the source files is updated, the module reindexes just that one file. You
60 can also force reindexing manually at any time. For improved performance, the
61 module keeps a cache of open filehandles, closing less-recently used ones when
64 Entries may have any line length up to 65,536 characters, and different line
65 lengths are allowed in the same file. However, within a sequence entry, all
66 lines must be the same length except for the last. An error will be thrown if
69 This module was developed for use with the C. elegans and human genomes, and has
70 been tested with sequence segments as large as 20 megabases. Indexing the C.
71 elegans genome (100 megabases of genomic sequence plus 100,000 ESTs) takes ~5
72 minutes on my 300 MHz pentium laptop. On the same system, average access time
73 for any 200-mer within the C. elegans genome was E<lt>0.02s.
75 =head1 DATABASE CREATION AND INDEXING
77 The two constructors for this class are new() and newFh(). The former creates a
78 Bio::DB::IndexedBase object which is accessed via method calls. The latter
79 creates a tied filehandle which can be used Bio::SeqIO style to fetch sequence
80 objects in a stream fashion. There is also a tied hash interface.
84 =item $db = Bio::DB::IndexedBase-E<gt>new($path [,%options])
86 Create a new Bio::DB::IndexedBase object from the files designated by $path
87 $path may be a single file, an arrayref of files, or a directory containing
90 After the database is created, you can use methods like get_all_primary_ids()
91 and get_Seq_by_id() to retrieve sequence objects.
93 =item $fh = Bio::DB::IndexedBase-E<gt>newFh($path [,%options])
95 Create a tied filehandle opened on a Bio::DB::IndexedBase object. Reading
96 from this filehandle with E<lt>E<gt> will return a stream of sequence objects,
97 Bio::SeqIO style. The path and the options should be specified as for new().
99 =item $obj = tie %db,'Bio::DB::IndexedBase', '/path/to/file' [,@args]
101 Create a tied-hash by tieing %db to Bio::DB::IndexedBase using the indicated
102 path to the files. The optional @args list is the same set used by new(). If
103 successful, tie() returns the tied object, undef otherwise.
105 Once tied, you can use the hash to retrieve an individual sequence by
108 my $seq = $db{CHROMOSOME_I};
110 The keys() and values() functions will return the sequence IDs and their
111 sequences, respectively. In addition, each() can be used to iterate over the
114 while (my ($id,$sequence) = each %db) {
115 print "$id => $sequence\n";
119 When dealing with very large sequences, you can avoid bringing them into memory
120 by calling each() in a scalar context. This returns the key only. You can then
121 use tied(%db) to recover the Bio::DB::IndexedBase object and call its methods.
123 while (my $id = each %db) {
124 print "$id: $db{$sequence:1,100}\n";
125 print "$id: ".tied(%db)->length($id)."\n";
128 In addition, you may invoke the FIRSTKEY and NEXTKEY tied hash methods directly
129 to retrieve the first and next ID in the database, respectively. This allows to
130 write the following iterative loop using just the object-oriented interface:
132 my $db = Bio::DB::IndexedBase->new('/path/to/file');
133 for (my $id=$db->FIRSTKEY; $id; $id=$db->NEXTKEY($id)) {
134 # do something with sequence
141 Several attributes of each sequence are stored in the index file. Given a
142 sequence ID, these attributes can be retrieved using the following methods:
148 Get the offset of the indicated sequence from the beginning of the file in which
149 it is located. The offset points to the beginning of the sequence, not the
150 beginning of the header line.
154 Get the number of characters in the sequence string.
158 Get the number of residues of the sequence.
162 Get the length of the line for this sequence. If the sequence is wrapped, then
163 linelen() is likely to be much shorter than strlen().
167 Get the length of the header line for the indicated sequence.
171 Get the offset of the header line for the indicated sequence from the beginning
172 of the file in which it is located. This attribute is not stored. It is
173 calculated from offset() and headerlen().
177 Get the molecular type (alphabet) of the indicated sequence. This method handles
178 residues according to the IUPAC convention.
182 Get the the name of the file in which the indicated sequence can be found.
186 =head1 INTERFACE COMPLIANCE NOTES
188 Bio::DB::IndexedBase is compliant with the Bio::DB::SeqI and hence with the
189 Bio::RandomAccessI interfaces.
191 Database do not necessarily provide any meaningful internal primary ID for the
192 sequences they store. However, Bio::DB::IndexedBase's internal primary IDs are
193 the IDs of the sequences. This means that the same ID passed to get_Seq_by_id()
194 and get_Seq_by_primary_id() will return the same sequence.
196 Since this database index has no notion of sequence version or namespace, the
197 get_Seq_by_id(), get_Seq_by_acc() and get_Seq_by_version() are identical.
201 When a sequence is deleted from one of the files, this deletion is not detected
202 by the module and removed from the index. As a result, a "ghost" entry will
203 remain in the index and will return garbage results if accessed.
205 Also, if you are indexing a directory, it is wise to not add or remove files
208 In case you have changed the files in a directory, or the sequences in a file,
209 you can to rebuild the entire index, either by deleting it manually, or by
210 passing -reindex=E<gt>1 to new() when initializing the module.
222 Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
224 Copyright (c) 2001 Cold Spring Harbor Laboratory.
226 Florent Angly (for the modularization)
228 This library is free software; you can redistribute it and/or modify
229 it under the same terms as Perl itself. See DISCLAIMER.txt for
230 disclaimers of warranty.
234 The rest of the documentation details each of the object
235 methods. Internal methods are usually preceded with a _
240 package Bio
::DB
::IndexedBase
;
243 @AnyDBM_File::ISA
= qw(DB_File GDBM_File NDBM_File SDBM_File)
244 if(!$INC{'AnyDBM_File.pm'});
252 use File
::Basename
qw(basename dirname);
255 use base
qw(Bio::DB::SeqI);
257 # Store offset, strlen, linelen, headerlen, type and fileno
258 use constant STRUCT
=> 'NNNnnCa*'; # 32-bit file offset and seq length
259 use constant STRUCTBIG
=> 'QQQnnCa*'; # 64-bit
261 use constant NA
=> 0;
262 use constant DNA
=> 1;
263 use constant RNA
=> 2;
264 use constant PROTEIN
=> 3;
266 # You can avoid dying if you want but you may get incorrect results
267 use constant DIE_ON_MISSMATCHED_LINES
=> 1;
269 # Remove carriage returns (\r) and newlines (\n) from a string. When
270 # called from subseq, this can take a signficiant portion of time, in
271 # Variant Effect Prediction. Therefore we compile the match portion.
273 eval 'require Inline::C';
274 if ( $INC{'Inline/C.pm'} ) {
275 # C can do _strip_crnl much faster. But this requires the
276 # Inline::C module which we don't require people to have. So we make
277 # this optional by wrapping the C code in an eval. If the eval works,
278 # the Perl strip_crnl() function is overwritten.
282 Strip all newlines
(\n) and carriage returns
(\r) from the string
284 char
* _strip_crnl
(char
* str
) {
287 for (s
= str
; *s
; *s
++) {
288 if (*s
!= '\n' && *s
!= '\r') {
298 # "tr" is much faster than the regex, with "s"
299 *Bio
::DB
::IndexedBase
::_strip_crnl
= sub {
306 return _strip_crnl
(@_);
312 Usage : my $db = Bio::DB::IndexedBase->new($path, -reindex => 1);
313 Function: Initialize a new database object
314 Returns : A Bio::DB::IndexedBase object
315 Args : A single file, or path to dir, or arrayref of files
318 Option Description Default
319 ----------- ----------- -------
320 -glob Glob expression to search for files in directories *
321 -makeid A code subroutine for transforming IDs None
322 -maxopen Maximum size of filehandle cache 32
323 -debug Turn on status messages 0
324 -reindex Force the index to be rebuilt 0
325 -dbmargs Additional arguments to pass to the DBM routine None
326 -index_name Name of the file that will hold the indices
327 -clean Remove the index file when finished 0
329 The -dbmargs option can be used to control the format of the index. For example,
330 you can pass $DB_BTREE to this argument so as to force the IDs to be sorted and
331 retrieved alphabetically. Note that you must use the same arguments every time
334 The -makeid option gives you a chance to modify sequence IDs during indexing.
335 For example, you may wish to extract a portion of the gi|gb|abc|xyz nonsense
336 that GenBank Fasta files use. The original header line can be recovered later.
337 The option value for -makeid should be a code reference that takes a scalar
338 argument (the full header line) and returns a scalar or an array of scalars (the
339 ID or IDs you want to assign). For example:
341 $db = Bio::DB::IndexedBase->new('file.fa', -makeid => \&extract_gi);
344 # Extract GI from GenBank
346 my ($id) = ($header =~ /gi\|(\d+)/m);
350 extract_gi() will be called with the full header line, e.g. a Fasta line would
351 include the "E<gt>", the ID and the description:
353 >gi|352962132|ref|NG_030353.1| Homo sapiens sal-like 3 (Drosophila) (SALL3)
355 In the database, this sequence can now be retrieved by its GI instead of its
358 my $seq = $db->get_Seq_by_id(352962132);
360 The -makeid option is ignored after the index is constructed.
365 my ($class, $path, %opts) = @_;
368 debug
=> $opts{-debug
} || 0,
369 makeid
=> $opts{-makeid
},
370 glob => $opts{-glob} || eval '$'.$class.'::file_glob' || '*',
371 maxopen
=> $opts{-maxopen
} || 32,
372 clean
=> $opts{-clean
} || 0,
373 dbmargs
=> $opts{-dbmargs
} || undef,
380 index_name
=> $opts{-index_name
},
381 obj_class
=> eval '$'.$class.'::obj_class',
382 offset_meth
=> \
&{$class.'::_calculate_offsets'},
387 my ($offsets, $dirname);
388 my $ref = ref $path || '';
389 if ( $ref eq 'ARRAY' ) {
390 $offsets = $self->index_files($path, $opts{-reindex
});
392 $dirname = Cwd
::getcwd
();
394 $self->{index_name
} ||= $self->_default_index_name($path);
396 # because Win32 glob() is broken with respect to long file names
397 # that contain whitespace.
398 $path = Win32
::GetShortPathName
($path)
399 if $^O
=~ /^MSWin/i && eval 'use Win32; 1';
400 $offsets = $self->index_dir($path, $opts{-reindex
});
403 $offsets = $self->index_file($path, $opts{-reindex
});
404 $dirname = dirname
($path);
406 $self->throw( "No file or directory called '$path'");
409 @
{$self}{qw(dirname offsets)} = ($dirname, $offsets);
418 Usage : my $fh = Bio::DB::IndexedBase->newFh('/path/to/files/', %options);
419 Function: Index and get a new Fh for a single file, several files or a directory
420 Returns : Filehandle object
426 my ($class, @args) = @_;
427 my $self = $class->new(@args);
429 my $fh = Symbol
::gensym
;
430 tie
$$fh, 'Bio::DB::Indexed::Stream', $self
431 or $self->throw("Could not tie filehandle: $!");
439 Usage : my @args = $db->dbmargs;
440 Function: Get stored dbm arguments
448 my $args = $self->{dbmargs
} or return;
449 return ref($args) eq 'ARRAY' ? @
$args : $args;
456 Usage : my $glob = $db->glob;
457 Function: Get the expression used to match files in directories
465 return $self->{glob};
472 Usage : $db->index_dir($dir);
473 Function: Index the files that match -glob in the given directory
474 Returns : Hashref of offsets
476 Boolean to force a reindexing the directory
481 my ($self, $dir, $force_reindex) = @_;
482 my @files = glob( File
::Spec
->catfile($dir, $self->{glob}) );
483 return if scalar @files == 0;
484 $self->{index_name
} ||= $self->_default_index_name($dir);
485 my $offsets = $self->_index_files(\
@files, $force_reindex);
490 =head2 get_all_primary_ids
492 Title : get_all_primary_ids, get_all_ids, ids
493 Usage : my @ids = $db->get_all_primary_ids;
494 Function: Get the IDs stored in all indexes. This is a Bio::DB::SeqI method
495 implementation. Note that in this implementation, the internal
496 database primary IDs are also the sequence IDs.
497 Returns : List of ids
502 sub get_all_primary_ids
{
503 return keys %{shift->{offsets
}};
506 *ids
= *get_all_ids
= \
&get_all_primary_ids
;
512 Usage : $db->index_file($filename);
513 Function: Index the given file
514 Returns : Hashref of offsets
516 Boolean to force reindexing the file
521 my ($self, $file, $force_reindex) = @_;
522 $self->{index_name
} ||= $self->_default_index_name($file);
523 my $offsets = $self->_index_files([$file], $force_reindex);
527 sub _default_index_name
{
528 my ($self,$path) = @_;
529 return File
::Spec
->catfile($path,'directory.index') if -d
$path;
530 return "$path.index";
536 Usage : $db->index_files(\@files);
537 Function: Index the given files
538 Returns : Hashref of offsets
539 Args : Arrayref of filenames
540 Boolean to force reindexing the files
545 my ($self, $files, $force_reindex) = @_;
546 my @paths = map { File
::Spec
->rel2abs($_) } @
$files;
548 my $digest = Digest
::MD5
::md5_hex
( join('', sort @paths) );
549 $self->{index_name
} ||= "fileset_$digest.index"; # unique name for the given files
550 my $offsets = $self->_index_files($files, $force_reindex);
558 Usage : my $indexname = $db->index_name($path);
559 Function: Get the full name of the index file
566 return shift->{index_name
};
573 Usage : my $path = $db->path($path);
574 Function: When a single file or a directory of files is indexed, this returns
575 the file directory. When indexing an arbitrary list of files, the
576 return value is the path of the current working directory.
583 return shift->{dirname
};
587 =head2 get_PrimarySeq_stream
589 Title : get_PrimarySeq_stream
590 Usage : my $stream = $db->get_PrimarySeq_stream();
591 Function: Get a SeqIO-like stream of sequence objects. The stream supports a
592 single method, next_seq(). Each call to next_seq() returns a new
593 PrimarySeqI compliant sequence object, until no more sequences remain.
594 This is a Bio::DB::SeqI method implementation.
595 Returns : A Bio::DB::Indexed::Stream object
600 sub get_PrimarySeq_stream
{
602 return Bio
::DB
::Indexed
::Stream
->new($self);
608 Title : get_Seq_by_id, get_Seq_by_acc, get_Seq_by_version, get_Seq_by_primary_id
609 Usage : my $seq = $db->get_Seq_by_id($id);
610 Function: Given an ID, fetch the corresponding sequence from the database.
611 This is a Bio::DB::SeqI and Bio::DB::RandomAccessI method implementation.
612 Returns : A sequence object
618 my ($self, $id) = @_;
619 $self->throw('Need to provide a sequence ID') if not defined $id;
620 return if not exists $self->{offsets
}{$id};
621 return $self->{obj_class
}->new($self, $id);
624 *get_Seq_by_version
= *get_Seq_by_primary_id
= *get_Seq_by_acc
= \
&get_Seq_by_id
;
627 =head2 _calculate_offsets
629 Title : _calculate_offsets
630 Usage : $db->_calculate_offsets($filename, $offsets);
631 Function: This method calculates the sequence offsets in a file based on ID and
632 should be implemented by classes that use Bio::DB::IndexedBase.
633 Returns : Hash of offsets
634 Args : File to process
635 Hashref of file offsets keyed by IDs.
639 sub _calculate_offsets
{
641 $self->throw_not_implemented();
646 # Do the indexing of the given files using the index file on record
647 my ($self, $files, $force_reindex) = @_;
649 $self->_set_pack_method( @
$files );
651 # Get name of index file
652 my $index = $self->index_name;
654 # If caller has requested reindexing, unlink the index file.
655 if ($force_reindex) {
656 # Tied-hash in Strawberry Perl creates "$file.index"
657 unlink $index if -e
$index;
658 # Tied-hash in ActivePerl creates "$file.index.pag" and "$file.index.dir"
659 unlink "$index.dir" if -e
"$index.dir";
660 unlink "$index.pag" if -e
"$index.pag";
663 # Get the modification time of the index
664 my $indextime = (stat $index)[9] || 0;
666 # Register files and find if there has been any update
669 for my $file (@
$files) {
671 $self->_path2fileno(basename
($file));
673 my $m = (stat $file)[9] || 0;
677 if ($m > $indextime) {
678 push @updated, $file;
682 # Get termination length from first file
683 $self->{termination_length
} = $self->_calc_termination_length( $files->[0] );
685 # Reindex contents of changed files if needed
686 my $reindex = $force_reindex || (scalar @updated > 0);
687 $self->{offsets
} = $self->_open_index($index, $reindex) or return;
689 $self->{indexing
} = $index;
690 for my $file (@updated) {
691 my $fileno = $self->_path2fileno(basename
($file));
692 &{$self->{offset_meth
}}($self, $fileno, $file, $self->{offsets
});
694 delete $self->{indexing
};
697 # Closing and reopening might help corrupted index file problem on Windows
698 $self->_close_index($self->{offsets
});
700 return $self->{offsets
} = $self->_open_index($index);
705 # Open index file in read-only or write mode
706 my ($self, $index_file, $write) = @_;
708 my $flags = $write ? O_CREAT
|O_RDWR
: O_RDONLY
;
709 my @dbmargs = $self->dbmargs;
710 tie
%offsets, 'AnyDBM_File', $index_file, $flags, 0644, @dbmargs
711 or $self->throw( "Could not open index file $index_file: $!");
718 my ($self, $index) = @_;
723 # Compiling the below regular expression speeds up _parse_compound_id
724 my $compound_id = qr/^ (.+?) (?:\:([\d_]+)(?:,|-|\.\.)([\d_]+))? (?:\/(.+))?
$/x
;
726 sub _parse_compound_id
{
727 # Handle compound IDs:
729 # $db->seq($id, $start, $stop, $strand)
730 # $db->seq("$id:$start,$stop")
731 # $db->seq("$id:$start..$stop")
732 # $db->seq("$id:$start-$stop")
733 # $db->seq("$id:$start,$stop/$strand")
734 # $db->seq("$id:$start..$stop/$strand")
735 # $db->seq("$id:$start-$stop/$strand")
736 # $db->seq("$id/$strand")
737 my ($self, $id, $start, $stop, $strand) = @_;
739 if ( (not defined $start ) &&
740 (not defined $stop ) &&
741 (not defined $strand) &&
742 ($id =~ m{$compound_id}) ) {
743 # Start, stop and strand not provided and ID looks like a compound ID
744 ($id, $start, $stop, $strand) = ($1, $2, $3, $4);
747 # Start, stop and strand defaults
748 $stop ||= $self->length($id) || 0; # 0 if sequence not found in database
749 $start ||= ($stop > 0) ?
1 : 0;
752 # Convert numbers such as 1_000_000 to 1000000
756 if ($start > $stop) {
758 ($start, $stop) = ($stop, $start);
762 return $id, $start, $stop, $strand;
766 sub _guess_alphabet
{
767 # Determine the molecular type of the given sequence string:
768 # 'dna', 'rna', 'protein' or '' (unknown/empty)
769 my ($self, $string) = @_;
770 # Handle IUPAC residues like PrimarySeq does
771 my $alphabet = Bio
::PrimarySeq
::_guess_alphabet_from_string
($self, $string, 1);
772 return $alphabet eq 'dna' ? DNA
773 : $alphabet eq 'rna' ? RNA
774 : $alphabet eq 'protein' ? PROTEIN
780 # Process the header line by applying any transformation given in -makeid
781 my ($self, $header_line) = @_;
782 return ref($self->{makeid
}) eq 'CODE' ?
$self->{makeid
}->($header_line) : $1;
786 sub _check_linelength
{
787 # Check that the line length is valid. Generate an error otherwise.
788 my ($self, $linelength) = @_;
789 return if not defined $linelength;
791 "Each line of the file must be less than 65,536 characters. Line ".
792 "$. is $linelength chars."
793 ) if $linelength > 65535;
797 sub _calc_termination_length
{
798 # Try the beginning of the file to determine termination length
799 # Account for crlf-terminated Windows and Mac files
800 my ($self, $file) = @_;
801 my $fh = IO
::File
->new($file) or $self->throw( "Could not open $file: $!");
803 # In Windows, text files have '\r\n' as line separator, but when reading in
804 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
805 # "length $_" will report 4 although the line is 5 bytes in length.
806 # We assume that all lines have the same line separator and only read current line.
807 my $init_pos = tell($fh);
808 my $curr_line = <$fh>;
809 my $pos_diff = tell($fh) - $init_pos;
810 my $correction = $pos_diff - length $curr_line;
813 $self->{termination_length
} = ($curr_line =~ /\r\n$/) ?
2 : 1+$correction;
814 return $self->{termination_length
};
819 # Get the offset of the n-th residue of the sequence with the given ID
820 # and termination length (tl)
821 my ($self, $id, $n) = @_;
822 my $tl = $self->{termination_length
};
824 my ($offset, $seqlen, $linelen) = (&{$self->{unpackmeth
}}($self->{offsets
}{$id}))[0,1,3];
826 $n = $seqlen-1 if $n >= $seqlen;
827 return $offset + $linelen * int($n/($linelen-$tl)) + $n % ($linelen-$tl);
832 # Given a sequence ID, return the filehandle on which to find this sequence
833 my ($self, $id) = @_;
834 $self->throw('Need to provide a sequence ID') if not defined $id;
835 my $file = $self->file($id) or return;
836 return $self->_fhcache( File
::Spec
->catfile($self->{dirname
}, $file) ) or
837 $self->throw( "Can't open file $file");
842 my ($self, $path) = @_;
843 if (!$self->{fhcache
}{$path}) {
844 if ($self->{curopen
} >= $self->{maxopen
}) {
845 my @lru = sort {$self->{cacheseq
}{$a} <=> $self->{cacheseq
}{$b};}
846 keys %{$self->{fhcache
}};
847 splice(@lru, $self->{maxopen
} / 3);
848 $self->{curopen
} -= @lru;
850 delete $self->{fhcache
}{$_};
853 $self->{fhcache
}{$path} = IO
::File
->new($path) || return;
854 binmode $self->{fhcache
}{$path};
857 $self->{cacheseq
}{$path}++;
858 return $self->{fhcache
}{$path};
862 #-------------------------------------------------------------
863 # Methods to store and retrieve data from indexed file
869 Usage : my $offset = $db->offset($id);
870 Function: Get the offset of the indicated sequence from the beginning of the
871 file in which it is located. The offset points to the beginning of
872 the sequence, not the beginning of the header line.
874 Args : ID of sequence
879 my ($self, $id) = @_;
880 $self->throw('Need to provide a sequence ID') if not defined $id;
881 my $offset = $self->{offsets
}{$id} or return;
882 return (&{$self->{unpackmeth
}}($offset))[0];
889 Usage : my $length = $db->strlen($id);
890 Function: Get the number of characters in the sequence string.
892 Args : ID of sequence
897 my ($self, $id) = @_;
898 $self->throw('Need to provide a sequence ID') if not defined $id;
899 my $offset = $self->{offsets
}{$id} or return;
900 return (&{$self->{unpackmeth
}}($offset))[1];
907 Usage : my $length = $db->length($id);
908 Function: Get the number of residues of the sequence.
910 Args : ID of sequence
915 my ($self, $id) = @_;
916 $self->throw('Need to provide a sequence ID') if not defined $id;
917 my $offset = $self->{offsets
}{$id} or return;
918 return (&{$self->{unpackmeth
}}($offset))[2];
925 Usage : my $linelen = $db->linelen($id);
926 Function: Get the length of the line for this sequence.
928 Args : ID of sequence
933 my ($self, $id) = @_;
934 $self->throw('Need to provide a sequence ID') if not defined $id;
935 my $offset = $self->{offsets
}{$id} or return;
936 return (&{$self->{unpackmeth
}}($offset))[3];
943 Usage : my $length = $db->headerlen($id);
944 Function: Get the length of the header line for the indicated sequence.
946 Args : ID of sequence
951 my ($self, $id) = @_;
952 $self->throw('Need to provide a sequence ID') if not defined $id;
953 my $offset = $self->{offsets
}{$id} or return;
954 return (&{$self->{unpackmeth
}}($offset))[4];
960 Title : header_offset
961 Usage : my $offset = $db->header_offset($id);
962 Function: Get the offset of the header line for the indicated sequence from
963 the beginning of the file in which it is located.
965 Args : ID of sequence
970 my ($self, $id) = @_;
971 $self->throw('Need to provide a sequence ID') if not defined $id;
972 return if not $self->{offsets
}{$id};
973 return $self->offset($id) - $self->headerlen($id);
980 Usage : my $alphabet = $db->alphabet($id);
981 Function: Get the molecular type of the indicated sequence: dna, rna or protein
983 Args : ID of sequence
988 my ($self, $id) = @_;
989 $self->throw('Need to provide a sequence ID') if not defined $id;
990 my $offset = $self->{offsets
}{$id} or return;
991 my $alphabet = (&{$self->{unpackmeth
}}($offset))[5];
992 return : $alphabet == Bio
::DB
::IndexedBase
::DNA ?
'dna'
993 : $alphabet == Bio
::DB
::IndexedBase
::RNA ?
'rna'
994 : $alphabet == Bio
::DB
::IndexedBase
::PROTEIN ?
'protein'
1002 Usage : my $file = $db->file($id);
1003 Function: Get the the name of the file in which the indicated sequence can be
1006 Args : ID of sequence
1011 my ($self, $id) = @_;
1012 $self->throw('Need to provide a sequence ID') if not defined $id;
1013 my $offset = $self->{offsets
}{$id} or return;
1014 return $self->_fileno2path((&{$self->{unpackmeth
}}($offset))[6]);
1019 my ($self, $fileno) = @_;
1020 return $self->{fileno2path
}->[$fileno];
1025 my ($self, $path) = @_;
1026 if ( not exists $self->{filepath2no
}->{$path} ) {
1027 my $fileno = ($self->{filepath2no
}->{$path} = 0+ $self->{fileno}++);
1028 $self->{fileno2path
}->[$fileno] = $path; # Save path
1030 return $self->{filepath2no
}->{$path};
1036 return pack STRUCT
, @_;
1041 return pack STRUCTBIG
, @_;
1046 return unpack STRUCT
, shift;
1051 return unpack STRUCTBIG
, shift;
1055 sub _set_pack_method
{
1056 # Determine whether to use 32 or 64 bit integers for the given files.
1058 # Find the maximum file size:
1059 my ($maxsize) = sort { $b <=> $a } map { -s
$_ } @_;
1060 my $fourGB = (2 ** 32) - 1;
1062 if ($maxsize > $fourGB) {
1063 # At least one file exceeds 4Gb - we will need to use 64 bit ints
1064 $self->{packmeth
} = \
&_packBig
;
1065 $self->{unpackmeth
} = \
&_unpackBig
;
1067 $self->{packmeth
} = \
&_packSmall
;
1068 $self->{unpackmeth
} = \
&_unpackSmall
;
1074 #-------------------------------------------------------------
1079 return shift->new(@_);
1084 return shift->subseq(@_);
1089 shift->throw("Read-only database");
1094 shift->throw("Read-only database");
1099 shift->throw("Read-only database");
1104 return defined shift->offset(@_);
1109 return tied(%{shift->{offsets
}})->FIRSTKEY(@_);
1114 return tied(%{shift->{offsets
}})->NEXTKEY(@_);
1122 while (my ($file, $fh) = each %{ $self->{fhcache
} }) {
1127 $self->_close_index($self->{offsets
});
1129 if ( $self->{clean
} || $self->{indexing
} ) {
1130 # Indexing aborted or cleaning requested. Delete the index file.
1131 my $index = $self->{index_name
};
1133 # Tied-hash in Strawberry Perl creates "$file.index"
1134 unlink $index if -e
$index;
1135 # Tied-hash in ActivePerl creates "$file.index.pag" and "$file.index.dir"
1136 unlink "$index.dir" if -e
"$index.dir";
1137 unlink "$index.pag" if -e
"$index.pag";
1143 #-------------------------------------------------------------
1144 # stream-based access to the database
1147 package Bio
::DB
::Indexed
::Stream
;
1148 use base
qw(Tie::Handle Bio::DB::SeqI);
1152 my ($class, $db) = @_;
1153 my $key = $db->FIRSTKEY;
1162 my ($key, $db) = @
{$self}{'key', 'db'};
1163 return if not defined $key;
1164 my $value = $db->get_Seq_by_id($key);
1165 $self->{key
} = $db->NEXTKEY($key);
1170 my ($class, $db) = @_;
1171 return $class->new($db);
1176 return $self->next_seq || undef;