Bio::DB::Universal: move into its own distribution
[bioperl-live.git] / Bio / Index / Fasta.pm
blob25470621e7fca67daa5fa3740375f74bfa308f9b
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
13 =head1 NAME
15 Bio::Index::Fasta - Interface for indexing (multiple) fasta files
17 =head1 SYNOPSIS
19 # Make an index for one or more fasta files
20 use Bio::Index::Fasta;
21 use strict;
23 my $Index_File_Name = shift;
24 my $inx = Bio::Index::Fasta->new(-filename => $Index_File_Name,
25 -write_flag => 1);
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;
32 use strict;
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',
37 -fh => \*STDOUT);
39 foreach my $id (@ARGV) {
40 my $seq = $inx->fetch($id); # Returns Bio::Seq object
41 $out->write_seq($seq);
44 # or, alternatively
45 my $id;
46 my $seq = $inx->get_Seq_by_id($id); # identical to fetch()
48 =head1 DESCRIPTION
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.
65 For example:
67 $inx->id_parser(\&get_id);
68 # make the index
69 $inx->make_index($file_name);
71 # here is where the retrieval key is specified
72 sub get_id {
73 my $line = shift;
74 $line =~ /^>.+gi\|(\d+)/;
75 $1;
79 =head1 FEED_BACK
81 =head2 Mailing Lists
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
90 =head2 Support
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
105 web:
107 https://github.com/bioperl/bioperl-live/issues
109 =head1 AUTHOR - James Gilbert
111 Email - jgrg@sanger.ac.uk
113 =head1 APPENDIX
115 The rest of the documentation details each of the object
116 methods. Internal methods are usually preceded with a _
118 =cut
121 # Let the code begin...
124 package Bio::Index::Fasta;
126 use strict;
127 use warnings;
129 use Bio::Seq;
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...
138 sub _version {
139 return 0.2;
142 =head2 _file_format
144 Title : _file_format
145 Function: The file format for this package, which is needed
146 by the SeqIO system when reading the sequence.
147 Returns : 'Fasta'
149 =cut
151 sub _file_format {
152 return 'Fasta';
155 =head2 _index_file
157 Title : _index_file
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.
162 Example :
163 Returns :
164 Args :
166 =cut
168 sub _index_file {
169 my( $self,
170 $file, # File name
171 $i, # Index-number of file being indexed
172 ) = @_;
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
192 # Main indexing loop
193 while (<$FASTA>) {
194 if (/^>/) {
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);
206 close $FASTA;
207 return 1;
210 =head2 id_parser
212 Title : id_parser
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
225 Args : CODE
227 =cut
229 sub id_parser {
230 my( $self, $code ) = @_;
232 if ($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+)/
244 to $header.
245 Returns : ID string
246 Args : a fasta header line string
248 =cut
250 sub default_id_parser {
251 if ($_[0] =~ /^>\s*(\S+)/) {
252 return $1;
253 } else {
254 return;