changes all issue tracking in preparation for switch to github issues
[bioperl-live.git] / Bio / Index / Swissprot.pm
blob7a5ff78e7e953d662352e4f1913913c5ab4e85d7
2 # BioPerl module for Bio::Index::Swissprot
4 # You may distribute this module under the same terms as perl itself
6 # POD documentation - main docs before the code
8 =head1 NAME
10 Bio::Index::Swissprot - Interface for indexing one or more
11 Swissprot files.
13 =head1 SYNOPSIS
15 # Make an index for one or more Swissprot files:
17 use Bio::Index::Swissprot;
18 use strict;
20 my $index_file_name = shift;
21 my $inx = Bio::Index::Swissprot->new(
22 -filename => $index_file_name,
23 -write_flag => 1);
24 $inx->make_index(@ARGV);
26 # Print out several sequences present in the index in Genbank
27 # format:
29 use Bio::Index::Swissprot;
30 use Bio::SeqIO;
31 use strict;
33 my $out = Bio::SeqIO->new( -format => 'genbank',
34 -fh => \*STDOUT );
35 my $index_file_name = shift;
36 my $inx = Bio::Index::Swissprot->new(-filename => $index_file_name);
38 foreach my $id (@ARGV) {
39 my $seq = $inx->fetch($id); # Returns a Bio::Seq object
40 $out->write_seq($seq);
43 # alternatively
44 my ($id, $acc);
45 my $seq1 = $inx->get_Seq_by_id($id);
46 my $seq2 = $inx->get_Seq_by_acc($acc);
48 =head1 DESCRIPTION
50 By default the index that is created uses the AC and ID identifiers
51 as keys. This module inherits functions for managing dbm files from
52 Bio::Index::Abstract.pm, and provides the basic functionality
53 for indexing Swissprot files and retrieving Sequence objects from
54 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($index_file_name);
64 # here is where the retrieval key is specified
65 sub get_id {
66 my $line = shift;
67 $line =~ /^KW\s+([A-Z]+)/i;
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 I<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 https://github.com/bioperl/bioperl-live/issues
101 =head1 AUTHOR - Ewan Birney
103 Also lorenz@ist.org, bosborne at alum.mit.edu
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::Swissprot;
116 use strict;
118 use Bio::Seq;
120 use base qw(Bio::Index::AbstractSeq);
122 sub _type_stamp {
123 return '__Swissprot_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: Specialist function to index Swissprot 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 # $file is file name, $i is number of file being indexed
145 my( $self, $file, $i ) = @_;
147 # Offset from start of file
148 my $begin = 0;
150 my $id_parser = $self->id_parser;
152 open my $SWISSPROT, '<', $file or $self->throw("Could not read file '$file': $!");
154 my %done_ids;
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($SWISSPROT);
161 my $curr_line = <$SWISSPROT>;
162 my $pos_diff = tell($SWISSPROT) - $init_pos;
163 my $correction = $pos_diff - length $curr_line;
164 seek $SWISSPROT, $init_pos, 0; # Rewind position to proceed to read the file
166 while (<$SWISSPROT>) {
167 if (/^ID\s+\S+/) {
168 $begin = tell($SWISSPROT) - length( $_ ) - $correction;
170 for my $id (&$id_parser($_)) {
171 next if exists $done_ids{$id};
172 $self->add_record($id, $i, $begin) if $id;
173 $done_ids{$id} = 1;
175 if (m{//}) {
176 %done_ids = ();
179 close $SWISSPROT;
180 return 1;
183 =head2 id_parser
185 Title : id_parser
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 : ref to CODE if called without arguments
194 Args : CODE
196 =cut
198 sub id_parser {
199 my( $self, $code ) = @_;
201 if ($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 Swissprot.pm
212 Returns $1 from applying the regexp /^ID\s*(\S+)/
213 or /^AC\s+([A-Z0-9]+)/ to the current line.
214 Returns : ID string
215 Args : a line string
217 =cut
219 sub default_id_parser {
220 my $line = shift;
221 if ($line =~ /^ID\s*(\S+)/) {
222 return $1;
224 elsif ($line =~ /^AC\s+([A-Z0-9]+)/) {
225 return $1;
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 :
239 =cut
241 sub _file_format {
242 return 'swiss';
247 __END__