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/.
59 Note that by default the key for the sequence will be the first continuous
60 string after the 'E<gt>' in the fasta header. If you want to use a specific
61 substring of the fasta header you must use the id_parser() method.
63 You can also set or customize the unique key used to retrieve by
64 writing your own function and calling the id_parser() method.
67 $inx->id_parser(\&get_id);
69 $inx->make_index($file_name);
71 # here is where the retrieval key is specified
74 $line =~ /^>.+gi\|(\d+)/;
83 User feedback is an integral part of the evolution of this and other
84 Bioperl modules. Send your comments and suggestions preferably to one
85 of the Bioperl mailing lists. Your participation is much appreciated.
87 bioperl-l@bioperl.org - General discussion
88 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
92 Please direct usage questions or support issues to the mailing list:
94 I<bioperl-l@bioperl.org>
96 rather than to the module maintainer directly. Many experienced and
97 reponsive experts will be able look at the problem and quickly
98 address it. Please include a thorough description of the problem
99 with code and data examples if at all possible.
101 =head2 Reporting Bugs
103 Report bugs to the Bioperl bug tracking system to help us keep track
104 the bugs and their resolution. Bug reports can be submitted via the
107 https://github.com/bioperl/bioperl-live/issues
109 =head1 AUTHOR - James Gilbert
111 Email - jgrg@sanger.ac.uk
115 The rest of the documentation details each of the object
116 methods. Internal methods are usually preceded with a _
121 # Let the code begin...
124 package Bio
::Index
::Fasta
;
131 use base
qw(Bio::Index::AbstractSeq);
134 # Suggested fix by Michael G Schwern <schwern@pobox.com> to
135 # get around a clash with CPAN shell...
145 Function: The file format for this package, which is needed
146 by the SeqIO system when reading the sequence.
158 Usage : $index->_index_file( $file_name, $i )
159 Function: Specialist function to index FASTA format files.
160 Is provided with a filename and an integer
161 by make_index in its SUPER class.
171 $i, # Index-number of file being indexed
174 my( $begin, # Offset from start of file of the start
175 # of the last found record.
178 my $id_parser = $self->id_parser;
180 open my $FASTA, '<', $file or $self->throw("Could not read file '$file': $!");
182 # In Windows, text files have '\r\n' as line separator, but when reading in
183 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
184 # "length $_" will report 4 although the line is 5 bytes in length.
185 # We assume that all lines have the same line separator and only read current line.
186 my $init_pos = tell($FASTA);
187 my $curr_line = <$FASTA>;
188 my $pos_diff = tell($FASTA) - $init_pos;
189 my $correction = $pos_diff - length $curr_line;
190 seek $FASTA, $init_pos, 0; # Rewind position to proceed to read the file
196 # the following was fixed to allow validation - cjfields
198 # $begin is the position of the first character after the '>'
199 $begin = tell($FASTA) - length( $_ ) - $correction;
201 foreach my $id (&$id_parser($_)) {
202 $self->add_record($id, $i, $begin);
213 Usage : $index->id_parser( CODE )
214 Function: Stores or returns the code used by record_id to
215 parse the ID for record from a string. Useful
216 for (for instance) specifying a different
217 parser for different flavours of FASTA file.
218 Returns \&default_id_parser (see below) if not
219 set. If you supply your own id_parser
220 subroutine, then it should expect a fasta
221 description line. An entry will be added to
222 the index for each string in the list returned.
223 Example : $index->id_parser( \&my_id_parser )
224 Returns : ref to CODE if called without arguments
230 my( $self, $code ) = @_;
233 $self->{'_id_parser'} = $code;
235 return $self->{'_id_parser'} || \
&default_id_parser
;
238 =head2 default_id_parser
240 Title : default_id_parser
241 Usage : $id = default_id_parser( $header )
242 Function: The default Fasta ID parser for Fasta.pm
243 Returns $1 from applying the regexp /^>\s*(\S+)/
246 Args : a fasta header line string
250 sub default_id_parser
{
251 if ($_[0] =~ /^>\s*(\S+)/) {