sync w/ main trunk
[bioperl-live.git] / Bio / Index / GenBank.pm
blobba721c49ae28713d2cd571d305d1a8440dea33e1
2 # $Id$
4 # BioPerl module for Bio::Index::Abstract
6 # Please direct questions and support issues to <bioperl-l@bioperl.org>
8 # Cared for by Ewan Birney <birney@sanger.ac.uk>
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::Index::GenBank - Interface for indexing one or more GenBank
17 files (i.e. flat file GenBank format).
19 =head1 SYNOPSIS
21 # Complete code for making an index for one or more GenBank files
22 use strict;
23 use Bio::Index::GenBank;
25 my $Index_File_Name = shift;
26 my $inx = Bio::Index::GenBank->new(-filename => $Index_File_Name,
27 -write_flag => 'WRITE');
28 $inx->make_index(@ARGV);
30 # Print out sequences present in the index in gcg format
31 use Bio::Index::GenBank;
32 use Bio::SeqIO;
33 use strict;
35 my $Index_File_Name = shift;
36 my $inx = Bio::Index::GenBank->new(-filename => $Index_File_Name);
37 my $seqio = Bio::SeqIO->new(-format => 'gcg');
38 foreach my $id (@ARGV) {
39 my $seq = $inx->fetch($id); # Returns Bio::Seq object
40 $seqio->write_seq($seq);
43 # alternatively
44 my ($locus, $acc);
45 my $seq1 = $inx->get_Seq_by_id($locus);
46 my $seq2 = $inx->get_Seq_by_acc($acc);
48 =head1 DESCRIPTION
50 By default the index that is created uses the LOCUS, ACCESSION, and
51 VERSION identifiers as keys. Inherits functions for managing dbm
52 files from Bio::Index::Abstract.pm, and provides the basic
53 functionality for indexing GenBank files, and retrieving the
54 sequence from them. For best results 'use strict'.
56 You can also set or customize the unique key used to retrieve by
57 writing your own function and calling the id_parser() method.
58 For example:
60 $inx->id_parser(\&get_id);
61 # make the index
62 $inx->make_index($file_name);
64 # here is where the retrieval key is specified
65 sub get_id {
66 my $line = shift;
67 $line =~ /clone="(\S+)"/;
68 $1;
71 =head1 FEED_BACK
73 =head2 Mailing Lists
75 User feedback is an integral part of the evolution of this and other
76 Bioperl modules. Send your comments and suggestions preferably to one
77 of the Bioperl mailing lists. Your participation is much appreciated.
79 bioperl-l@bioperl.org - General discussion
80 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
82 =head2 Support
84 Please direct usage questions or support issues to the mailing list:
86 L<bioperl-l@bioperl.org>
88 rather than to the module maintainer directly. Many experienced and
89 reponsive experts will be able look at the problem and quickly
90 address it. Please include a thorough description of the problem
91 with code and data examples if at all possible.
93 =head2 Reporting Bugs
95 Report bugs to the Bioperl bug tracking system to help us keep track
96 the bugs and their resolution. Bug reports can be submitted via
97 the web:
99 http://bugzilla.open-bio.org/
101 =head1 AUTHOR - Ewan Birney
103 Email - birney@ebi.ac.uk
105 =head1 APPENDIX
107 The rest of the documentation details each of the object methods.
108 Internal methods are usually preceded with a _
110 =cut
112 # Let's begin the code...
114 package Bio::Index::GenBank;
116 use strict;
118 use Bio::Seq;
120 use base qw(Bio::Index::AbstractSeq);
122 sub _type_stamp {
123 return '__GenBank_FLAT__'; # What kind of index are we?
126 sub _version {
127 return 0.1;
130 =head2 _index_file
132 Title : _index_file
133 Usage : $index->_index_file($file_name, $i)
134 Function: Specialized function to index GenBank format files.
135 Is provided with a filename and an integer
136 by make_index in its SUPER class.
137 Example :
138 Returns :
139 Args :
141 =cut
143 sub _index_file {
144 my( $self,
145 $file, # File name
146 $i # Index-number of file being indexed
147 ) = @_;
149 my $begin = 0;
151 my $id_parser = $self->id_parser;
153 open my $GENBANK, '<', $file or
154 $self->throw("Can't open file for read : $file");
156 my %done_ids;
157 while (<$GENBANK>) {
158 if (/^LOCUS/) {
159 $begin = tell($GENBANK) - length($_);
161 for my $id (&$id_parser($_)) {
162 next if exists $done_ids{$id};
163 $self->add_record($id, $i, $begin) if $id;
164 $done_ids{$id} = 1;
166 if (m{//}) {
167 %done_ids = ();
170 close $GENBANK;
171 return 1;
174 =head2 id_parser
176 Title : id_parser
177 Usage : $index->id_parser( CODE )
178 Function: Stores or returns the code used by record_id to
179 parse the ID for record from a string.
180 Returns \&default_id_parser (see below) if not
181 set. An entry will be added to
182 the index for each string in the list returned.
183 Example : $index->id_parser( \&my_id_parser )
184 Returns : reference to CODE if called without arguments
185 Args : CODE
187 =cut
189 sub id_parser {
190 my ($self,$code) = @_;
192 if ($code) {
193 $self->{'_id_parser'} = $code;
195 return $self->{'_id_parser'} || \&default_id_parser;
198 =head2 default_id_parser
200 Title : default_id_parser
201 Usage : $id = default_id_parser($line)
202 Function: The default parser for GenBank.pm
203 Returns : Array of specified ids
204 Args : a line string
206 =cut
210 sub default_id_parser {
211 my $line = shift;
212 my %accs;
213 if ( $line =~ /^LOCUS\s+(\S+)/ ) {
214 $accs{$1}++;
215 } elsif ( $line =~ /^ACCESSION\s+(.*)/ ) {
216 for my $acc ( split(/\s+/,$1) ) {
217 $accs{$acc}++;
219 } elsif ( /^VERSION\s+(.*)/) {
220 my $x = $1;
221 for my $acc ( split(/\s+/,$1) ) {
222 $acc=~ s/GI\://;
223 $accs{$acc}++;
226 keys %accs;
229 =head2 _file_format
231 Title : _file_format
232 Usage : Internal function for indexing system
233 Function: Provides file format for this database
234 Example :
235 Returns :
236 Args :
238 =cut
240 sub _file_format{
241 my ($self,@args) = @_;
242 return 'GenBank';
247 __END__