Bio/Index/SwissPfam.pm

filter: Statementstmt 22.0% 11 / 50 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 100% 1 / 1 totaltotal 20.8% 15 / 72 CC 8 SCAR 33.8
CC
8
Cov
17%
_index_file
56.0
fetch
6.0
_type_stamp
2.0
CRAP 29.4
« Bio/AlignIO/emboss.pm ↑ Summary Bio/DB/InMemoryCache.pm »
1 #
2 # BioPerl module for Bio::Index::SwissPfam
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6  
7 # You may distribute this module under the same terms as perl itself
8  
9 # POD documentation - main docs before the code
10  
11 =head1 NAME
12  
13 Bio::Index::SwissPfam - Interface for indexing swisspfam files
14  
15 =head1 SYNOPSIS
16  
17 use Bio::Index::SwissPfam;
18 use strict;
19  
20 my $Index_File_Name = shift;
21 my $inx = Bio::Index::SwissPfam->new('-filename' => $Index_File_Name,
22 '-write_flag' => 'WRITE');
23 $inx->make_index(@ARGV);
24  
25 use Bio::Index::SwissPfam;
26 use strict;
27  
28 my $Index_File_Name = shift;
29 my $inx = Bio::Index::SwissPfam->new('-filename' => $Index_File_Name);
30  
31 foreach my $id (@ARGV) {
32 my $seq = $inx->fetch($id); # Returns stream
33 while( <$seq> ) {
34 if(/^>/) {
35 print;
36 last;
37 }
38 }
39 }
40  
41  
42 =head1 DESCRIPTION
43  
44 SwissPfam is one of the flat files released with Pfam. This modules
45 provides a way of indexing this module.
46  
47 Inherits functions for managing dbm files from Bio::Index::Abstract.pm, and
48 provides the basic funtionality for indexing SwissPfam files. Only
49 retrieves FileStreams at the moment. Once we have something better
50 (ie, an object!), will use that. Heavily snaffled from Index::Fasta system of
51 James Gilbert. Note: for best results 'use strict'.
52  
53 =head1 FEED_BACK
54  
55 =head2 Mailing Lists
56  
57 User feedback is an integral part of the evolution of this and other
58 Bioperl modules. Send your comments and suggestions preferably to one
59 of the Bioperl mailing lists. Your participation is much appreciated.
60  
61 bioperl-l@bioperl.org - General discussion
62 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
63  
64 =head2 Support
65  
66 Please direct usage questions or support issues to the mailing list:
67  
68 I<bioperl-l@bioperl.org>
69  
70 rather than to the module maintainer directly. Many experienced and
71 reponsive experts will be able look at the problem and quickly
72 address it. Please include a thorough description of the problem
73 with code and data examples if at all possible.
74  
75 =head2 Reporting Bugs
76  
77 Report bugs to the Bioperl bug tracking system to help us keep track
78 the bugs and their resolution. Bug reports can be submitted via the
79 web:
80  
81 https://github.com/bioperl/bioperl-live/issues
82  
83 =head1 AUTHOR - Ewan Birney
84  
85 =head1 APPENDIX
86  
87 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
88  
89 =cut
90  
91 # Let's begin the code...
92  
93 package Bio::Index::SwissPfam;
94 1 $Bio::Index::SwissPfam::VERSION = '1.7.8';
95 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use Bio::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 1 ▶use base qw(Bio::Index::Abstract);
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 0 ▶sub _type_stamp {
Subroutine _type_stamp: not called CC=1 SCAR=6.9
101 0 return '__SWISSPFAM_FLAT__'; # What kind of index are we?
102 }
103  
104 0 ▶sub _version {
Subroutine _version: not called CC=1 SCAR=6.9
105 0 return 0.1;
106 }
107  
108 =head2 _index_file
109  
110 Title : _index_file
111 Usage : $index->_index_file( $file_name, $i )
112 Function: Specialist function to index swisspfam format files.
113 Is provided with a filename and an integer
114 by make_index in its SUPER class.
115 Example :
116 Returns :
117 Args :
118  
119 =cut
120  
121 0 ▶sub _index_file {
Subroutine _index_file: not called CC=7 SCAR=40.3
122 0 my( $self,
123 $file, # File name
124 $i # Index-number of file being indexed
125 ) = @_;
126  
127 0 my( $begin, # Offset from start of file of the start
128 # of the last found record.
129 $end, # Offset from start of file of the end
130 # of the last found record.
131 $id, # ID of last found record.
132 $acc, # accession of last record. Also put into the index
133 $nid, $nacc, # new ids for the record just found
134 );
135  
136 0 $begin = 0;
137 0 $end = 0;
138  
139 0 ▶ open my $SP, '<', $file or $self->throw("Could not read file '$file': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $SP, "<", $file or $self->throw("Could not read file '${file}': $!") 0 0 0
140  
141 # In Windows, text files have '\r\n' as line separator, but when reading in
142 # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n",
143 # "length $_" will report 4 although the line is 5 bytes in length.
144 # We assume that all lines have the same line separator and only read current line.
145 0 my $init_pos = tell($SP);
146 0 my $curr_line = <$SP>;
147 0 my $pos_diff = tell($SP) - $init_pos;
148 0 my $correction = $pos_diff - length $curr_line;
149 0 seek $SP, $init_pos, 0; # Rewind position to proceed to read the file
150  
151 # Main indexing loop
152 0 ▶ while (<$SP>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $SP)) 0 0 0
153 0 ▶ if (/^>(\S+)\s+\|=*\|\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^>(\S+)\s+\|=*\|\s+(\S+)/) 0 0 0
154 0 $nid = $1;
155 0 $nacc = $2;
156 0 my $new_begin = tell($SP) - length( $_ ) - $correction;
157 0 $end = $new_begin - 1;
158  
159 0 ▶ if( $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
160 0 $self->add_record($id, $i, $begin, $end);
161 0 ▶ if( $acc ne $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acc ne $id) 0 0 0
162 0 $self->add_record($acc, $i, $begin, $end);
163 }
164 }
165 0 $begin = $new_begin;
166 0 $id = $nid;
167 0 $acc = $nacc;
168 }
169 }
170 # Don't forget to add the last record
171 0 $end = tell($SP);
172 0 ▶ $self->add_record($id, $i, $begin, $end) if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
173  
174 0 close $SP;
175 0 return 1;
176 }
177  
178  
179 =head2 fetch
180  
181 Title : fetch
182 Usage : $index->fetch( $id )
183 Function: Returns a Bio::Seq object from the index
184 Example : $seq = $index->fetch( 'dJ67B12' )
185 Returns : Bio::Seq object
186 Args : ID
187  
188 =cut
189  
190 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
191 0 my( $self, $id ) = @_;
192 0 my $desc;
193 0 my $db = $self->db();
194 0 ▶ if (my $rec = $db->{ $id }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $db->{$id}) { } 0 0 0
195 0 my( @record );
196 0 my ($file, $begin, $end) = $self->unpack_record( $rec );
197 # Get the (possibly cached) filehandle
198 0 my $fh = $self->_file_handle( $file );
199  
200 # move to start
201 0 seek($fh, $begin, 0);
202  
203 0 return $fh;
204 } else {
205 0 $self->throw("Unable to find a record for $id in SwissPfam flat file index");
206 }
207 }
208  
209 1 1;
« Bio/AlignIO/emboss.pm ↑ Summary Bio/DB/InMemoryCache.pm »