lib/Bio/CUA/SeqIO.pm

filter: Statementstmt 100% 51 / 51 Branchbran 70.8% 17 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 87.6% 78 / 89 SCAR 26.6
CC
14
Cov
88%
next_seq
11.3
_initialize
3.0
new
2.0
CRAP 14.3
« lib/Bio/CUA.pm ↑ Summary script/calculate_CUB.pl »
1 package Bio::CUA::SeqIO;
2  
3 =head1 NAME
4  
5 Bio::CUA::SeqIO - a package to parse sequence file if module
6 L<Bio::SeqIO> 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<Bio::SeqIO> is not installed in the system. At present, this module
25 can only process fasta-formatted sequence file.
26  
27 =cut
28  
29 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use parent qw/Bio::CUA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Bio::CUA::Seq; # sequence object package
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 5 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 ▶ my ($caller, @args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
52  
53 8 my $self = $caller->SUPER::new(@args);
54  
55 8 ▶ $self->_initialize(@args) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_initialize(@args) or return undef 0 8 8
56  
57 8 return $self;
58 }
59  
60 # open the file and prepare to read the sequence
61 sub _initialize
62 {
63 8 ▶ my ($self, @args) = @_;
Subroutine _initialize: called CC=3 SCAR=11.1
64  
65 8 my $hashRef = $self->_array_to_hash(\@args);
66 8 my $file = $hashRef->{'file'};
67 8 ▶ $self->throw("option '-file' is needed for creating object of",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 8 8
68 ref($self)) unless(defined $file);
69  
70 8 $file =~ s/^[\s><]+//;
71 8 ▶ my $fh = $self->_open_file($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->_open_file($file) or return 0 8 8
72  
73 8 $self->{'_fh'} = $fh;
74 8 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<Bio::CUA::Seq> or undef if no more
83 sequence
84 Args : none
85  
86 =cut
87  
88 sub next_seq
89 {
90 112 ▶ my $self = shift;
Subroutine next_seq: called CC=11 SCAR=24.2
Pod documented
91  
92 112 my $fh = $self->{'_fh'};
93  
94 112 ▶ $self->throw("No open filehandle stored in the object of $self")
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 112 112
95 unless($fh);
96 112 ▶ return undef if(eof($fh)); # no more sequences
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 8 104 112
97
98 104 ▶ $self->throw("Errors on filehandle $fh") if(tell($fh) < 0);
Branch: 1 branch
BranchTrueFalseTotal
if tell $fh < 0 0 104 104
99  
100 104 my $moreData = 0; # indicates whether more data rather than
101 # comments or empty lines
102 104 my $seqStr = '';
103 104 my $defLine;
104 # the defline read by the program when reading the last sequence
105 $defLine = $self->{'_last_defline'} if(exists
106 104 ▶ $self->{'_last_defline'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_last_defline"} 96 8 104
107 104 ▶ while(<$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 3696 8 3704
108 {
109 3696 ▶ next if /^\s*$/ or /^#/; # empty or comment lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ or /^#/ 0 3696 3696
Condition33%1/3
/^\s*$/ or /^#/
l!l&&r!l&&!r
003696
MC/DC0%0/2
/^\s*$/ or /^#/
/^\s*$/ /^#/
Truth table33%1/3
A or B
A= /^\s*$/B= /^#/
ABresult
1X1
011
000
110 3696 chomp;
111 3696 $moreData = 1;
112 3696 ▶ if(/^>/) # a new sequence
Branch: 1 branch
BranchTrueFalseTotal
if (/^>/) 104 3592 3696
113 {
114 104 ▶ if($defLine) # reach next sequence
Branch: 1 branch
BranchTrueFalseTotal
if ($defLine) { } 96 8 104
115 {
116 # store this line for next time's use
117 96 $self->{'_last_defline'} = $_;
118 96 last;
119 }else # this is what to read, only for the 1st seq
120 {
121 8 $defLine = $_;
122 8 $seqStr = '';
123 8 next;
124 }
125 }
126  
127 3592 $seqStr .= $_;
128 }
129  
130 104 ▶ return undef unless($moreData);
Branch: 1 branch
BranchTrueFalseTotal
unless $moreData 0 104 104
131  
132 104 my ($id, $desc) = $defLine =~ /^>(\S+)(.*)$/;
133 104 $desc =~ s/^\s+//;
134  
135 104 my $seqObj = $seq_pkg->new(
136 -seq => $seqStr,
137 -id => $id,
138 -desc => $desc );
139  
140 104 return $seqObj;
141 }
142  
143 =head1 AUTHOR
144  
145 Zhenguo Zhang, C<< <zhangz.sci at gmail.com> >>
146  
147 =head1 BUGS
148  
149 Please report any bugs or feature requests to C<bug-bio-cua at rt.cpan.org>, or through
150 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-CUA>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-CUA>
168  
169 =item * AnnoCPAN: Annotated CPAN documentation
170  
171 L<http://annocpan.org/dist/Bio-CUA>
172  
173 =item * CPAN Ratings
174  
175 L<http://cpanratings.perl.org/d/Bio-CUA>
176  
177 =item * Search CPAN
178  
179 L<http://search.cpan.org/dist/Bio-CUA/>
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<http://www.gnu.org/licenses/>.
203  
204  
205 =cut
206  
207 5 1; # End of Bio::CUA::SeqIo
« lib/Bio/CUA.pm ↑ Summary script/calculate_CUB.pl »