[bug 3148] switch default to "expasy" until we can work out REST service interface
[bioperl-live.git] / Bio / Index / Swissprot.pm
blob251371dbacf6e2c43789234690493e36ad203aae
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 http://bugzilla.open-bio.org/
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("Can't read file: $file");
154 my %done_ids;
155 while (<$SWISSPROT>) {
156 if (/^ID\s+\S+/) {
157 $begin = tell($SWISSPROT) - length( $_ );
159 for my $id (&$id_parser($_)) {
160 next if exists $done_ids{$id};
161 $self->add_record($id, $i, $begin) if $id;
162 $done_ids{$id} = 1;
164 if (m{//}) {
165 %done_ids = ();
168 close $SWISSPROT;
169 return 1;
172 =head2 id_parser
174 Title : id_parser
175 Usage : $index->id_parser( CODE )
176 Function: Stores or returns the code used by record_id to
177 parse the ID for record from a string.
178 Returns \&default_id_parser (see below) if not
179 set. An entry will be added to
180 the index for each string in the list returned.
181 Example : $index->id_parser( \&my_id_parser )
182 Returns : ref to CODE if called without arguments
183 Args : CODE
185 =cut
187 sub id_parser {
188 my( $self, $code ) = @_;
190 if ($code) {
191 $self->{'_id_parser'} = $code;
193 return $self->{'_id_parser'} || \&default_id_parser;
196 =head2 default_id_parser
198 Title : default_id_parser
199 Usage : $id = default_id_parser( $line )
200 Function: The default parser for Swissprot.pm
201 Returns $1 from applying the regexp /^ID\s*(\S+)/
202 or /^AC\s+([A-Z0-9]+)/ to the current line.
203 Returns : ID string
204 Args : a line string
206 =cut
208 sub default_id_parser {
209 my $line = shift;
210 if ($line =~ /^ID\s*(\S+)/) {
211 return $1;
213 elsif ($line =~ /^AC\s+([A-Z0-9]+)/) {
214 return $1;
218 =head2 _file_format
220 Title : _file_format
221 Usage : Internal function for indexing system
222 Function: Provides file format for this database
223 Example :
224 Returns :
225 Args :
228 =cut
230 sub _file_format {
231 return 'swiss';
236 __END__