File Coverage

blib/lib/Bio/CUA/SeqIO.pm
Criterion Covered Total %
statement 49 49 100.0
branch 15 22 68.1
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 74 83 89.1


line stmt bran cond sub pod time code
1             package Bio::CUA::SeqIO;
2              
3             =head1 NAME
4              
5             Bio::CUA::SeqIO - a package to parse sequence file if module
6             L is unavailable in the system.
7              
8             =head1 SYNOPSIS
9              
10             use Bio::CUA::SeqIO;
11              
12             # create an IO to a sequence file in FASTA format
13             my $io = Bio::CUA::SeqIO->new("seq_file.fa")
14              
15             # read each sequence as a Bio::CUA::Seq object from this io
16             while(my $seq = $io->next_seq)
17             {
18             printf("%s: %d\n", $seq->id, $seq->length);
19             }
20              
21             =head1 DESCRIPTION
22              
23             This is an auxiliary module to process sequences in case the module
24             L is not installed in the system. At present, this module
25             can only process fasta-formatted sequence file.
26              
27             =cut
28              
29 5     5   36475 use 5.006;
  5         17  
30 5     5   26 use strict;
  5         8  
  5         136  
31 5     5   1075 use parent qw/Bio::CUA/;
  5         608  
  5         30  
32 5     5   2340 use Bio::CUA::Seq; # sequence object package
  5         10  
  5         2417  
33              
34             my $seq_pkg = 'Bio::CUA::Seq';
35              
36             =head1 METHODS
37              
38             =head2 new
39              
40             Title : new
41             Usage : $io = Bio::CUA::SeqIO->new(-file => "seq_file.fa")
42             Function: create an IO to read sequences from a file
43             Returns : an object of this class
44             Args : a hash in the format of
45             -file => "seq_file.fa"
46              
47             =cut
48              
49             sub new
50             {
51 8     8 1 51 my ($caller, @args) = @_;
52              
53 8         54 my $self = $caller->SUPER::new(@args);
54              
55 8 50       33 $self->_initialize(@args) or return undef;
56              
57 8         24 return $self;
58             }
59              
60             # open the file and prepare to read the sequence
61             sub _initialize
62             {
63 8     8   21 my ($self, @args) = @_;
64              
65 8         27 my $hashRef = $self->_array_to_hash(\@args);
66 8         19 my $file = $hashRef->{'file'};
67 8 50       23 $self->throw("option '-file' is needed for creating object of",
68             ref($self)) unless(defined $file);
69              
70 8         17 $file =~ s/^[\s><]+//;
71 8 50       41 my $fh = $self->_open_file($file) or return;
72              
73 8         38 $self->{'_fh'} = $fh;
74 8         37 return 1;
75             }
76              
77             =head2 next_seq
78              
79             Title : next_seq
80             Usage : my $seq = $self->next_seq;
81             Function: read next sequence in the IO stream
82             Returns : an object of L or undef if no more
83             sequence
84             Args : none
85              
86             =cut
87              
88             sub next_seq
89             {
90 112     112 1 167 my $self = shift;
91              
92 112         165 my $fh = $self->{'_fh'};
93              
94 112 50       238 $self->throw("No open filehandle stored in the object of $self")
95             unless($fh);
96 112 100       542 return undef if(eof($fh)); # no more sequences
97            
98 104 50       250 $self->throw("Errors on filehandle $fh") if(tell($fh) < 0);
99              
100 104         109 my $moreData = 0; # indicates whether more data rather than
101             # comments or empty lines
102 104         124 my $seqStr = '';
103 104         99 my $defLine;
104             # the defline read by the program when reading the last sequence
105             $defLine = $self->{'_last_defline'} if(exists
106 104 100       329 $self->{'_last_defline'});
107 104         775 while(<$fh>)
108             {
109 3696 50 33     13772 next if /^\s*$/ or /^#/; # empty or comment lines
110 3696         4182 chomp;
111 3696         3301 $moreData = 1;
112 3696 100       5981 if(/^>/) # a new sequence
113             {
114 104 100       259 if($defLine) # reach next sequence
115             {
116             # store this line for next time's use
117 96         211 $self->{'_last_defline'} = $_;
118 96         214 last;
119             }else # this is what to read, only for the 1st seq
120             {
121 8         24 $defLine = $_;
122 8         16 $seqStr = '';
123 8         62 next;
124             }
125             }
126              
127 3592         9965 $seqStr .= $_;
128             }
129              
130 104 50       215 return undef unless($moreData);
131              
132 104         492 my ($id, $desc) = $defLine =~ /^>(\S+)(.*)$/;
133 104         188 $desc =~ s/^\s+//;
134              
135 104         747 my $seqObj = $seq_pkg->new(
136             -seq => $seqStr,
137             -id => $id,
138             -desc => $desc );
139              
140 104         443 return $seqObj;
141             }
142              
143             =head1 AUTHOR
144              
145             Zhenguo Zhang, C<< >>
146              
147             =head1 BUGS
148              
149             Please report any bugs or feature requests to C, or through
150             the web interface at L. I will be notified, and then you'll
151             automatically be notified of progress on your bug as I make changes.
152              
153              
154             =head1 SUPPORT
155              
156             You can find documentation for this module with the perldoc command.
157              
158             perldoc Bio::CUA::SeqIO
159              
160              
161             You can also look for information at:
162              
163             =over 4
164              
165             =item * RT: CPAN's request tracker (report bugs here)
166              
167             L
168              
169             =item * AnnoCPAN: Annotated CPAN documentation
170              
171             L
172              
173             =item * CPAN Ratings
174              
175             L
176              
177             =item * Search CPAN
178              
179             L
180              
181             =back
182              
183              
184             =head1 ACKNOWLEDGEMENTS
185              
186              
187             =head1 LICENSE AND COPYRIGHT
188              
189             Copyright 2015 Zhenguo Zhang.
190              
191             This program is free software: you can redistribute it and/or modify
192             it under the terms of the GNU General Public License as published by
193             the Free Software Foundation, either version 3 of the License, or
194             (at your option) any later version.
195              
196             This program is distributed in the hope that it will be useful,
197             but WITHOUT ANY WARRANTY; without even the implied warranty of
198             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
199             GNU General Public License for more details.
200              
201             You should have received a copy of the GNU General Public License
202             along with this program. If not, see L.
203              
204              
205             =cut
206              
207             1; # End of Bio::CUA::SeqIo