3 # BioPerl module for Bio::Index::Fasta
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by James Gilbert <jgrg@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::Fasta - Interface for indexing (multiple) fasta files
19 # Make an index for one or more fasta files
20 use Bio::Index::Fasta;
23 my $Index_File_Name = shift;
24 my $inx = Bio::Index::Fasta->new(-filename => $Index_File_Name,
26 $inx->make_index(@ARGV);
29 # Once the index is made it can accessed, either in the
30 # same script or a different one
31 use Bio::Index::Fasta;
34 my $Index_File_Name = shift;
35 my $inx = Bio::Index::Fasta->new(-filename => $Index_File_Name);
36 my $out = Bio::SeqIO->new(-format => 'Fasta',
39 foreach my $id (@ARGV) {
40 my $seq = $inx->fetch($id); # Returns Bio::Seq object
41 $out->write_seq($seq);
46 my $seq = $inx->get_Seq_by_id($id); # identical to fetch()
50 Inherits functions for managing dbm files from Bio::Index::Abstract.pm,
51 and provides the basic funtionallity for indexing fasta files, and
52 retrieving the sequence from them. For best results 'use strict'.
54 Bio::Index::Fasta supports the Bio::DB::BioSeqI interface, meaning
55 it can be used as a Sequence database for other parts of bioperl
57 Additional example code is available in scripts/index/*PLS and in
58 the Bioperl Tutorial (L<http://www.bioperl.org/wiki/Bptutorial.pl>)
60 Note that by default the key for the sequence will be the first continuous
61 string after the 'E<gt>' in the fasta header. If you want to use a specific
62 substring of the fasta header you must use the id_parser() method.
64 You can also set or customize the unique key used to retrieve by
65 writing your own function and calling the id_parser() method.
68 $inx->id_parser(\&get_id);
70 $inx->make_index($file_name);
72 # here is where the retrieval key is specified
75 $line =~ /^>.+gi\|(\d+)/;
84 User feedback is an integral part of the evolution of this and other
85 Bioperl modules. Send your comments and suggestions preferably to one
86 of the Bioperl mailing lists. Your participation is much appreciated.
88 bioperl-l@bioperl.org - General discussion
89 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
93 Please direct usage questions or support issues to the mailing list:
95 I<bioperl-l@bioperl.org>
97 rather than to the module maintainer directly. Many experienced and
98 reponsive experts will be able look at the problem and quickly
99 address it. Please include a thorough description of the problem
100 with code and data examples if at all possible.
102 =head2 Reporting Bugs
104 Report bugs to the Bioperl bug tracking system to help us keep track
105 the bugs and their resolution. Bug reports can be submitted via the
108 https://github.com/bioperl/bioperl-live/issues
110 =head1 AUTHOR - James Gilbert
112 Email - jgrg@sanger.ac.uk
116 The rest of the documentation details each of the object
117 methods. Internal methods are usually preceded with a _
122 # Let the code begin...
125 package Bio
::Index
::Fasta
;
132 use base
qw(Bio::Index::AbstractSeq);
135 # Suggested fix by Michael G Schwern <schwern@pobox.com> to
136 # get around a clash with CPAN shell...
146 Function: The file format for this package, which is needed
147 by the SeqIO system when reading the sequence.
159 Usage : $index->_index_file( $file_name, $i )
160 Function: Specialist function to index FASTA format files.
161 Is provided with a filename and an integer
162 by make_index in its SUPER class.
172 $i, # Index-number of file being indexed
175 my( $begin, # Offset from start of file of the start
176 # of the last found record.
179 my $id_parser = $self->id_parser;
181 open my $FASTA, '<', $file or $self->throw("Could not read file '$file': $!");
183 # In Windows, text files have '\r\n' as line separator, but when reading in
184 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
185 # "length $_" will report 4 although the line is 5 bytes in length.
186 # We assume that all lines have the same line separator and only read current line.
187 my $init_pos = tell($FASTA);
188 my $curr_line = <$FASTA>;
189 my $pos_diff = tell($FASTA) - $init_pos;
190 my $correction = $pos_diff - length $curr_line;
191 seek $FASTA, $init_pos, 0; # Rewind position to proceed to read the file
197 # the following was fixed to allow validation - cjfields
199 # $begin is the position of the first character after the '>'
200 $begin = tell($FASTA) - length( $_ ) - $correction;
202 foreach my $id (&$id_parser($_)) {
203 $self->add_record($id, $i, $begin);
214 Usage : $index->id_parser( CODE )
215 Function: Stores or returns the code used by record_id to
216 parse the ID for record from a string. Useful
217 for (for instance) specifying a different
218 parser for different flavours of FASTA file.
219 Returns \&default_id_parser (see below) if not
220 set. If you supply your own id_parser
221 subroutine, then it should expect a fasta
222 description line. An entry will be added to
223 the index for each string in the list returned.
224 Example : $index->id_parser( \&my_id_parser )
225 Returns : ref to CODE if called without arguments
231 my( $self, $code ) = @_;
234 $self->{'_id_parser'} = $code;
236 return $self->{'_id_parser'} || \
&default_id_parser
;
239 =head2 default_id_parser
241 Title : default_id_parser
242 Usage : $id = default_id_parser( $header )
243 Function: The default Fasta ID parser for Fasta.pm
244 Returns $1 from applying the regexp /^>\s*(\S+)/
247 Args : a fasta header line string
251 sub default_id_parser
{
252 if ($_[0] =~ /^>\s*(\S+)/) {