3 # BioPerl module for Bio::Index::Abstract
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Ewan Birney <birney@sanger.ac.uk>
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::Index::GenBank - Interface for indexing one or more GenBank
16 files (i.e. flat file GenBank format).
20 # Complete code for making an index for one or more GenBank files
22 use Bio::Index::GenBank;
24 my $Index_File_Name = shift;
25 my $inx = Bio::Index::GenBank->new(-filename => $Index_File_Name,
26 -write_flag => 'WRITE');
27 $inx->make_index(@ARGV);
29 # Print out sequences present in the index in gcg format
30 use Bio::Index::GenBank;
34 my $Index_File_Name = shift;
35 my $inx = Bio::Index::GenBank->new(-filename => $Index_File_Name);
36 my $seqio = Bio::SeqIO->new(-format => 'gcg');
37 foreach my $id (@ARGV) {
38 my $seq = $inx->fetch($id); # Returns Bio::Seq object
39 $seqio->write_seq($seq);
44 my $seq1 = $inx->get_Seq_by_id($locus);
45 my $seq2 = $inx->get_Seq_by_acc($acc);
49 By default the index that is created uses the LOCUS, ACCESSION, and
50 VERSION identifiers as keys. Inherits functions for managing dbm
51 files from Bio::Index::Abstract.pm, and provides the basic
52 functionality for indexing GenBank files, and retrieving the
53 sequence from them. For best results 'use strict'.
55 You can also set or customize the unique key used to retrieve by
56 writing your own function and calling the id_parser() method.
59 $inx->id_parser(\&get_id);
61 $inx->make_index($file_name);
63 # here is where the retrieval key is specified
66 $line =~ /clone="(\S+)"/;
74 User feedback is an integral part of the evolution of this and other
75 Bioperl modules. Send your comments and suggestions preferably to one
76 of the Bioperl mailing lists. Your participation is much appreciated.
78 bioperl-l@bioperl.org - General discussion
79 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
83 Please direct usage questions or support issues to the mailing list:
85 I<bioperl-l@bioperl.org>
87 rather than to the module maintainer directly. Many experienced and
88 reponsive experts will be able look at the problem and quickly
89 address it. Please include a thorough description of the problem
90 with code and data examples if at all possible.
94 Report bugs to the Bioperl bug tracking system to help us keep track
95 the bugs and their resolution. Bug reports can be submitted via
98 https://github.com/bioperl/bioperl-live/issues
100 =head1 AUTHOR - Ewan Birney
102 Email - birney@ebi.ac.uk
106 The rest of the documentation details each of the object methods.
107 Internal methods are usually preceded with a _
111 # Let's begin the code...
113 package Bio
::Index
::GenBank
;
119 use base
qw(Bio::Index::AbstractSeq);
122 return '__GenBank_FLAT__'; # What kind of index are we?
132 Usage : $index->_index_file($file_name, $i)
133 Function: Specialized function to index GenBank format files.
134 Is provided with a filename and an integer
135 by make_index in its SUPER class.
145 $i # Index-number of file being indexed
150 my $id_parser = $self->id_parser;
152 open my $GENBANK, '<', $file or $self->throw("Could not read file '$file': $!");
156 # In Windows, text files have '\r\n' as line separator, but when reading in
157 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
158 # "length $_" will report 4 although the line is 5 bytes in length.
159 # We assume that all lines have the same line separator and only read current line.
160 my $init_pos = tell($GENBANK);
161 my $curr_line = <$GENBANK>;
162 my $pos_diff = tell($GENBANK) - $init_pos;
163 my $correction = $pos_diff - length $curr_line;
164 seek $GENBANK, $init_pos, 0; # Rewind position to proceed to read the file
168 $begin = tell($GENBANK) - length($_) - $correction;
170 for my $id (&$id_parser($_)) {
171 next if exists $done_ids{$id};
172 $self->add_record($id, $i, $begin) if $id;
186 Usage : $index->id_parser( CODE )
187 Function: Stores or returns the code used by record_id to
188 parse the ID for record from a string.
189 Returns \&default_id_parser (see below) if not
190 set. An entry will be added to
191 the index for each string in the list returned.
192 Example : $index->id_parser( \&my_id_parser )
193 Returns : reference to CODE if called without arguments
199 my ($self,$code) = @_;
202 $self->{'_id_parser'} = $code;
204 return $self->{'_id_parser'} || \
&default_id_parser
;
207 =head2 default_id_parser
209 Title : default_id_parser
210 Usage : $id = default_id_parser($line)
211 Function: The default parser for GenBank.pm
212 Returns : Array of specified ids
219 sub default_id_parser
{
222 if ( $line =~ /^LOCUS\s+(\S+)/ ) {
224 } elsif ( $line =~ /^ACCESSION\s+(.*)/ ) {
225 for my $acc ( split(/\s+/,$1) ) {
228 } elsif ( /^VERSION\s+(.*)/) {
230 for my $acc ( split(/\s+/,$1) ) {
241 Usage : Internal function for indexing system
242 Function: Provides file format for this database
250 my ($self,@args) = @_;