~ubuntu-branches/ubuntu/trusty/bioperl/trusty-proposed

« back to all changes in this revision

Viewing changes to Bio/Index/Stockholm.pm

  • Committer: Bazaar Package Importer
  • Author(s): Charles Plessy
  • Date: 2009-03-10 07:19:11 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090310071911-fukqzw54pyb1f0bd
Tags: 1.6.0-2
* Removed patch system (not used):
  - removed instuctions in debian/rules;
  - removed quilt from Build-Depends in debian/control.
* Re-enabled tests:
  - uncommented test command in debian/rules;
  - uncommented previously missing build-dependencies in debian/control.
  - Re-enabled tests and uncommented build-dependencies accordingly.
* Removed libmodule-build-perl and libtest-harness-perl from
  Build-Depends-Indep (provided by perl-modules).
* Better cleaning of empty directories using find -type d -empty -delete
  instead of rmdir in debian/rules (LP: #324001).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: Stockholm.pm 14708 2008-06-10 00:08:17Z heikki $
 
2
#
 
3
# BioPerl module for Bio::Index::Stockholm
 
4
#
 
5
# Cared for by Chris Fields <cjfields@uiuc.edu>
 
6
#
 
7
# Copyright Chris Fields
 
8
#
 
9
# You may distribute this module under the same terms as perl itself
 
10
 
 
11
# POD documentation - main docs before the code
 
12
 
 
13
=head1 NAME
 
14
 
 
15
Bio::Index::Stockholm - Indexes Stockholm format alignments (such as those from
 
16
Pfam and Rfam.  Retrieves raw stream data using the ID or a Bio::SimpleAlign
 
17
(via Bio::AlignIO)
 
18
 
 
19
=head1 SYNOPSIS
 
20
 
 
21
    use strict;
 
22
    use Bio::Index::Stockholm;
 
23
    my ($indexfile,$file1,$file2,$query);
 
24
    my $index = Bio::Index::Stockholm->new(-filename => $indexfile,
 
25
                                                          -write_flag => 1);
 
26
    $index->make_index($file1,$file2);
 
27
 
 
28
    # get raw data stream starting at alignment position
 
29
    my $fh = $index->get_stream($query);
 
30
 
 
31
    # fetch individual alignment
 
32
    my $align = $index->fetch_aln($query); # alias for fetch_report
 
33
    my $align = $index->fetch_report($query); # same as above
 
34
    print "query is ", $align->display_id, "\n";
 
35
 
 
36
=head1 DESCRIPTION
 
37
 
 
38
This object allows one to build an index for any file (or files)
 
39
containing Stockholm alignment format (such as Rfam and Pfam) and provides
 
40
quick access to the alignment based on the alignment ID.
 
41
 
 
42
This also allows for ID parsing using a callback:
 
43
 
 
44
   $inx->id_parser(\&get_id);
 
45
   # make the index
 
46
   $inx->make_index($file_name);
 
47
 
 
48
   # here is where the retrieval key is specified
 
49
   sub get_id {
 
50
      my $line = shift;
 
51
      $line =~ /^>.+gi\|(\d+)/;
 
52
      $1;
 
53
   }
 
54
 
 
55
The indexer is capable of indexing based on multiple IDs passed back from the
 
56
callback; this is assuming of course all IDs are unique.  The default is to use
 
57
the alignment ID provided for Rfam/Pfam output.
 
58
 
 
59
Note: for best results 'use strict'.
 
60
 
 
61
=head1 TODO
 
62
 
 
63
- allow using an alternative regex for indexing (for instance, the ID instead of AC)
 
64
 
 
65
=head1 FEEDBACK
 
66
 
 
67
=head2 Mailing Lists
 
68
 
 
69
User feedback is an integral part of the evolution of this and other
 
70
Bioperl modules. Send your comments and suggestions preferably to
 
71
the Bioperl mailing list.  Your participation is much appreciated.
 
72
 
 
73
  bioperl-l@bioperl.org                  - General discussion
 
74
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
 
75
 
 
76
=head2 Reporting Bugs
 
77
 
 
78
Report bugs to the Bioperl bug tracking system to help us keep track
 
79
of the bugs and their resolution. Bug reports can be submitted via the
 
80
web:
 
81
 
 
82
  http://bugzilla.open-bio.org/
 
83
 
 
84
=head1 AUTHOR - Chris Fields
 
85
 
 
86
Email cjfields-at-bioperl-dot-org
 
87
 
 
88
=head1 APPENDIX
 
89
 
 
90
The rest of the documentation details each of the object methods.
 
91
Internal methods are usually preceded with a _
 
92
 
 
93
=cut
 
94
 
 
95
# Let the code begin...
 
96
 
 
97
package Bio::Index::Stockholm;
 
98
use strict;
 
99
use Bio::AlignIO;
 
100
 
 
101
use base qw(Bio::Index::Abstract Bio::Root::Root);
 
102
 
 
103
sub _version {
 
104
        return ${Bio::Root::Version::VERSION};
 
105
}
 
106
 
 
107
=head2 new
 
108
 
 
109
  Usage   : $index = Bio::Index::Abstract->new(
 
110
                -filename    => $dbm_file,
 
111
                -write_flag  => 0,
 
112
                -dbm_package => 'DB_File',
 
113
                -verbose     => 0);
 
114
 
 
115
  Function: Returns a new index object.  If filename is
 
116
            specified, then open_dbm() is immediately called. 
 
117
            Bio::Index::Abstract->new() will usually be called
 
118
            directly only when opening an existing index.
 
119
  Returns : A new index object
 
120
  Args    : -filename    The name of the dbm index file.
 
121
            -write_flag  TRUE if write access to the dbm file is
 
122
                         needed.
 
123
            -dbm_package The Perl dbm module to use for the
 
124
                         index.
 
125
            -verbose     Print debugging output to STDERR if
 
126
                         TRUE.
 
127
 
 
128
=cut
 
129
 
 
130
sub new {
 
131
 
 
132
  my($class,@args) = @_;
 
133
 
 
134
  my $self = $class->SUPER::new(@args);
 
135
 
 
136
}
 
137
 
 
138
=head2 Bio::Index::Stockholm implemented methods
 
139
 
 
140
=cut
 
141
 
 
142
=head2 fetch_report
 
143
 
 
144
 Title   : fetch_report
 
145
 Usage   : my $align = $idx->fetch_report($id);
 
146
 Function: Returns a Bio::SimpleAlign object 
 
147
           for a specific alignment
 
148
 Returns : Bio::SimpleAlign
 
149
 Args    : valid id
 
150
 
 
151
=cut
 
152
 
 
153
sub fetch_report{
 
154
        my ($self,$id) = @_;
 
155
        my $fh = $self->get_stream($id);
 
156
        my $report = Bio::AlignIO->new(-noclose => 1,
 
157
                                                                        -format => 'stockholm',
 
158
                                                            -fh => $fh);
 
159
        return $report->next_aln;
 
160
}
 
161
 
 
162
=head2 fetch_report
 
163
 
 
164
 Title   : fetch_report
 
165
 Usage   : my $align = $idx->fetch_report($id);
 
166
 Function: Returns a Bio::SimpleAlign object 
 
167
           for a specific alignment
 
168
 Returns : Bio::SimpleAlign
 
169
 Args    : valid id
 
170
 Note    : alias for fetch_report
 
171
 
 
172
=cut
 
173
 
 
174
*fetch_aln = \&fetch_report;
 
175
 
 
176
=head2 Require methods from Bio::Index::Abstract
 
177
 
 
178
=cut
 
179
 
 
180
=head2 _index_file
 
181
 
 
182
  Title   : _index_file
 
183
  Usage   : $index->_index_file( $file_name, $i )
 
184
  Function: Specialist function to index report file(s).
 
185
            Is provided with a filename and an integer
 
186
            by make_index in its SUPER class.
 
187
  Example : 
 
188
  Returns : 
 
189
  Args    : 
 
190
 
 
191
=cut
 
192
 
 
193
sub _index_file {
 
194
        my( $self,
 
195
                 $file, # File name
 
196
                 $i,    # Index-number of file being indexed
 
197
          ) = @_;
 
198
 
 
199
        my( $begin,  # Offset from start of file of the start
 
200
                          # of the last found record.
 
201
          );
 
202
    local $/ ="\n";
 
203
        open(my $BLAST, '<', $file) or $self->throw("cannot open file $file\n");
 
204
        my $indexpoint = 0;
 
205
        my $lastline = 0;
 
206
        while( <$BLAST> ) {
 
207
                if(m{^#\sSTOCKHOLM} ) {
 
208
            $indexpoint = tell($BLAST)-length $_;
 
209
            $self->debug("Index:$indexpoint\n")
 
210
                }
 
211
        if(m{^#=GF\s+AC\s+(\S[^\n]+)}) {
 
212
            foreach my $id ($self->id_parser()->($1)) {
 
213
                                $self->debug("id is $id, begin is $indexpoint\n");
 
214
                                #$self->add_record($id, $i, $indexpoint);
 
215
                        }
 
216
        }
 
217
        }
 
218
}
 
219
 
 
220
# shamelessly stolen from Bio::Index::Fasta
 
221
 
 
222
=head2 id_parser
 
223
 
 
224
  Title   : id_parser
 
225
  Usage   : $index->id_parser( CODE )
 
226
  Function: Stores or returns the code used by record_id to
 
227
            parse the ID for record from a string.  Useful
 
228
            for (for instance) specifying a different
 
229
            parser for different flavours of IDs (for instance,
 
230
            custom stockholm-formated files). 
 
231
            Returns \&default_id_parser (see below) if not
 
232
            set. If you supply your own id_parser
 
233
            subroutine, then it should expect a fasta
 
234
            description line.  An entry will be added to
 
235
            the index for each string in the list returned.
 
236
  Example : $index->id_parser( \&my_id_parser )
 
237
  Returns : ref to CODE if called without arguments
 
238
  Args    : CODE
 
239
 
 
240
=cut
 
241
 
 
242
sub id_parser {
 
243
        my( $self, $code ) =@_;
 
244
 
 
245
        if ($code) {
 
246
                $self->{'_id_parser'} = $code;
 
247
        }
 
248
        return $self->{'_id_parser'} || \&default_id_parser;
 
249
}
 
250
 
 
251
=head2 default_id_parser
 
252
 
 
253
  Title   : default_id_parser
 
254
  Usage   : $id = default_id_parser( $header )
 
255
  Function: The default Blast Query ID parser for Bio::Index::Blast.pm
 
256
            Returns $1 from applying the regexp /^>\s*(\S+)/
 
257
            to $header.
 
258
  Returns : ID string
 
259
  Args    : a header line string
 
260
 
 
261
=cut
 
262
 
 
263
sub default_id_parser
 
264
{
 
265
        if ($_[0] =~ /^\s*(\S+)/) {
 
266
                return $1;
 
267
        } else {
 
268
                return;
 
269
        }
 
270
}
 
271
 
 
272
=head2 Bio::Index::Abstract methods
 
273
 
 
274
=cut
 
275
 
 
276
=head2 filename
 
277
 
 
278
 Title   : filename
 
279
 Usage   : $value = $self->filename();
 
280
           $self->filename($value);
 
281
 Function: Gets or sets the name of the dbm index file.
 
282
 Returns : The current value of filename
 
283
 Args    : Value of filename if setting, or none if
 
284
           getting the value.
 
285
 
 
286
=head2 write_flag
 
287
 
 
288
 Title   : write_flag
 
289
 Usage   : $value = $self->write_flag();
 
290
           $self->write_flag($value);
 
291
 Function: Gets or sets the value of write_flag, which
 
292
           is wether the dbm file should be opened with
 
293
           write access.
 
294
 Returns : The current value of write_flag (default 0)
 
295
 Args    : Value of write_flag if setting, or none if
 
296
           getting the value.
 
297
 
 
298
=head2 dbm_package
 
299
 
 
300
 Usage   : $value = $self->dbm_package();
 
301
           $self->dbm_package($value);
 
302
 
 
303
 Function: Gets or sets the name of the Perl dbm module used. 
 
304
           If the value is unset, then it returns the value of
 
305
           the package variable $USE_DBM_TYPE or if that is
 
306
           unset, then it chooses the best available dbm type,
 
307
           choosing 'DB_File' in preference to 'SDBM_File'. 
 
308
           Bio::Abstract::Index may work with other dbm file
 
309
           types.
 
310
 
 
311
 Returns : The current value of dbm_package
 
312
 Args    : Value of dbm_package if setting, or none if
 
313
           getting the value.
 
314
 
 
315
 
 
316
=head2 get_stream
 
317
 
 
318
 Title   : get_stream
 
319
 Usage   : $stream = $index->get_stream( $id );
 
320
 Function: Returns a file handle with the file pointer
 
321
           at the approprite place
 
322
 
 
323
           This provides for a way to get the actual
 
324
           file contents and not an object 
 
325
 
 
326
           WARNING: you must parse the record deliminter
 
327
           *yourself*. Abstract wont do this for you 
 
328
           So this code
 
329
 
 
330
           $fh = $index->get_stream($myid);
 
331
           while( <$fh> ) {
 
332
              # do something
 
333
           }
 
334
           will parse the entire file if you do not put in
 
335
           a last statement in, like
 
336
 
 
337
           while( <$fh> ) {
 
338
              /^\/\// && last; # end of record
 
339
              # do something
 
340
           }
 
341
 
 
342
 Returns : A filehandle object
 
343
 Args    : string represents the accession number
 
344
 Notes   : This method should not be used without forethought 
 
345
 
 
346
 
 
347
=head2 open_dbm
 
348
 
 
349
  Usage   : $index->open_dbm()
 
350
  Function: Opens the dbm file associated with the index
 
351
            object.  Write access is only given if explicitly
 
352
            asked for by calling new(-write => 1) or having set
 
353
            the write_flag(1) on the index object.  The type of
 
354
            dbm file opened is that returned by dbm_package(). 
 
355
            The name of the file to be is opened is obtained by
 
356
            calling the filename() method.
 
357
 
 
358
  Example : $index->_open_dbm()
 
359
  Returns : 1 on success
 
360
 
 
361
 
 
362
=head2 _version
 
363
 
 
364
  Title   : _version
 
365
  Usage   : $type = $index->_version()
 
366
  Function: Returns a string which identifes the version of an
 
367
            index module.  Used to permanently identify an index
 
368
            file as having been created by a particular version
 
369
            of the index module.  Must be provided by the sub class
 
370
  Example : 
 
371
  Returns : 
 
372
  Args    : none
 
373
 
 
374
=head2 _filename
 
375
 
 
376
  Title   : _filename
 
377
  Usage   : $index->_filename( FILE INT )
 
378
  Function: Indexes the file
 
379
  Example : 
 
380
  Returns : 
 
381
  Args    : 
 
382
 
 
383
=head2 _file_handle
 
384
 
 
385
  Title   : _file_handle
 
386
  Usage   : $fh = $index->_file_handle( INT )
 
387
  Function: Returns an open filehandle for the file
 
388
            index INT.  On opening a new filehandle it
 
389
            caches it in the @{$index->_filehandle} array.
 
390
            If the requested filehandle is already open,
 
391
            it simply returns it from the array.
 
392
  Example : $fist_file_indexed = $index->_file_handle( 0 );
 
393
  Returns : ref to a filehandle
 
394
  Args    : INT
 
395
 
 
396
=head2 _file_count
 
397
 
 
398
  Title   : _file_count
 
399
  Usage   : $index->_file_count( INT )
 
400
  Function: Used by the index building sub in a sub class to
 
401
            track the number of files indexed.  Sets or gets
 
402
            the number of files indexed when called with or
 
403
            without an argument.
 
404
  Example : 
 
405
  Returns : INT
 
406
  Args    : INT
 
407
 
 
408
 
 
409
=head2 add_record
 
410
 
 
411
  Title   : add_record
 
412
  Usage   : $index->add_record( $id, @stuff );
 
413
  Function: Calls pack_record on @stuff, and adds the result
 
414
            of pack_record to the index database under key $id.
 
415
            If $id is a reference to an array, then a new entry
 
416
            is added under a key corresponding to each element
 
417
            of the array.
 
418
  Example : $index->add_record( $id, $fileNumber, $begin, $end )
 
419
  Returns : TRUE on success or FALSE on failure
 
420
  Args    : ID LIST
 
421
 
 
422
=head2 pack_record
 
423
 
 
424
  Title   : pack_record
 
425
  Usage   : $packed_string = $index->pack_record( LIST )
 
426
  Function: Packs an array of scalars into a single string
 
427
            joined by ASCII 034 (which is unlikely to be used
 
428
            in any of the strings), and returns it. 
 
429
  Example : $packed_string = $index->pack_record( $fileNumber, $begin, $end )
 
430
  Returns : STRING or undef
 
431
  Args    : LIST
 
432
 
 
433
=head2 unpack_record
 
434
 
 
435
  Title   : unpack_record
 
436
  Usage   : $index->unpack_record( STRING )
 
437
  Function: Splits the sting provided into an array,
 
438
            splitting on ASCII 034.
 
439
  Example : ( $fileNumber, $begin, $end ) = $index->unpack_record( $self->db->{$id} )
 
440
  Returns : A 3 element ARRAY
 
441
  Args    : STRING containing ASCII 034
 
442
 
 
443
=head2 DESTROY
 
444
 
 
445
 Title   : DESTROY
 
446
 Usage   : Called automatically when index goes out of scope
 
447
 Function: Closes connection to database and handles to
 
448
           sequence files
 
449
 Returns : NEVER
 
450
 Args    : NONE
 
451
 
 
452
 
 
453
=cut
 
454
 
 
455
1;