lib/TIGR/FASTA/Iterator.pm

filter: Statementstmt 15.7% 37 / 235 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 41.1% 7 / 17 Podpod 100% 6 / 6 totaltotal 10.5% 50 / 473 CC 77 SCAR 56.1
CC
77
Cov
9%
_parse
756.0
new
156.0
next
90.0
CRAP 272.0
« lib/TIGR/FASTA/Reader.pm ↑ Summary lib/TIGR/FASTA/Writer.pm »
1 # $Id: FASTAiterator.pm,v 1.1 2004/04/28 15:03:43 aphillip Exp $
2  
3 package TIGR::FASTA::Iterator;
4 {
5  
6 =head1 NAME
7  
8 TIGR::FASTA::Iterator - TIGR::FASTA::Iterator class for parsing and navigating
9 FASTA format files and streams. An object of this class can parse FASTA
10 records from STDIN and from a pipe.
11  
12 =head1 SYNOPSIS
13  
14 use TIGR::FASTA::Iterator;
15 my $obj_instance = new TIGR::FASTA::Iterator ($foundation_obj_ref,
16 $error_array_ref,
17 $fasta_file_name);
18  
19 =head1 DESCRIPTION
20  
21 This module iterates over a FASTA formatted file stream. It provides
22 data extraction and simple analysis routines. This module utilizes
23 acceptance validation of FASTA formatted files via the TIGR::FASTA::Grammar
24 module.
25  
26 =cut
27  
28 1 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 require 5.006_00;
30 }
31  
32 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶ use TIGR::Foundation;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶ use TIGR::FASTA::Grammar ':public';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶ use TIGR::FASTA::Grammar ':private';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶ use TIGR::FASTA::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 ## internal variables and identifiers
41  
42 1 our $REVISION = (qw$Revision: 1.1 $)[-1];
43 1 our $VERSION = '1.11';
44 1 our $VERSION_STRING = "$VERSION (Build $REVISION)";
45 1 our @DEPEND =
46 (
47 "TIGR::Foundation",
48 "TIGR::FASTA::Grammar",
49 "TIGR::FASTA::Record",
50 );
51  
52 1 my $SYS_ERR = 0; # this flag specifies non-user related error
53 1 my $USR_ERR = 1; # this flag specifies user related error
54  
55 ## external variables
56  
57 1 my $UNBOUND_FASTA_SEPARATOR = $TIGR::FASTA::Grammar::UNBOUND_FASTA_SEPARATOR;
58
59 # debugging scheme
60 #
61 # Debugging via the TIGR Foundation uses increasing log levels based on
62 # nesting. 'MAIN' starts at level 1. Every nest increments the level by
63 # 1.
64 # Subroutines always start nesting at level 2. As debugging levels
65 # increase, logging is more verbose. This makes sense as you log at
66 # greater depth (ie. deeper branching).
67 #
68 # The following definitions help emphasize the debugging in the program.
69 #
70 1 my $DEBUG_LEVEL_1 = 1;
71 1 my $DEBUG_LEVEL_2 = 2;
72 1 my $DEBUG_LEVEL_3 = 3;
73 1 my $DEBUG_LEVEL_4 = 4;
74 1 my $DEBUG_LEVEL_5 = 5;
75 1 my $DEBUG_LEVEL_6 = 6;
76 1 my $DEBUG_LEVEL_7 = 7;
77 1 my $DEBUG_LEVEL_8 = 8;
78 1 my $DEBUG_LEVEL_9 = 9;
79  
80 ## prototypes
81  
82 sub new(;$$$);
83 sub open($);
84 sub close();
85 sub hasNext();
86 sub next();
87 sub get();
88 sub _initialize();
89 sub _parse();
90 sub _nullRecordHandler($);
91 sub _errorHandler($$$);
92  
93  
94 ## implementation
95  
96 =over
97  
98 =item $obj_instance = new TIGR::FASTA::Iterator ($foundation_object,
99 $error_array_ref, $db_file);
100  
101 This method returns a new instance of a TIGR::FASTA::Iterator object. It takes
102 three optional parameters: a TIGR::Foundation object (C<$foundation_object>),
103 a reference to an array for logging user error messages (C<$error_array_ref>),
104 and a FASTA file (C<$db_file>) or stream. The filename "-" describes stdin.
105 The new instance is returned on success. If the file supplied cannot be
106 opened or is invalid, this method returns undefined. This method also returns
107 undefined if the parameters supplied are invalid. Errors in parsing are written
108 to the array C<$error_array_ref>, the error file and the log file.
109  
110 =cut
111  
112  
113 0 ▶ sub new(;$$$) {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
114 0 my $pkg = shift;
115 0 my @method_args = @_;
116  
117 0 my $error_condition = 0;
118 0 my $self = {};
119 0 bless $self, $pkg;
120 0 $self->_initialize(); # set up internal variables;
121  
122 0 ▶ if ( ( scalar (@method_args) > 0 ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @method_args > 0 and ref($method_args[0]) =~ /foundation/i) { } 0 0 0
Condition0%0/3
scalar @method_args > 0 and ref($method_args[0]) =~ /foundation/i
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @method_args > 0 and ref($method_args[0]) =~ /foundation/i
scalar @method_args > 0 ref($method_args[0]) =~ /foundation/i
Truth table0%0/3
A and B
A= scalar @method_args > 0B= ref($method_args[0]) =~ /foundation/i
ABresult
0X0
100
111
123 ( ( ref ($method_args[0]) ) =~ /foundation/i ) ) {
124 0 $self->{foundation} = shift @method_args;
125 0 $self->_errorHandler("Got TIGR::Foundation in new()", $DEBUG_LEVEL_3,
126 $SYS_ERR);
127 }
128 else {
129 0 $self->{foundation} = undef;
130 0 $self->_errorHandler("No TIGR::Foundation in new()", $DEBUG_LEVEL_3,
131 $SYS_ERR);
132 }
133  
134 0 ▶ if ( ( scalar (@method_args) > 0 ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @method_args > 0 and ref($method_args[0]) =~ /array/i) { } 0 0 0
Condition0%0/3
scalar @method_args > 0 and ref($method_args[0]) =~ /array/i
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @method_args > 0 and ref($method_args[0]) =~ /array/i
scalar @method_args > 0 ref($method_args[0]) =~ /array/i
Truth table0%0/3
A and B
A= scalar @method_args > 0B= ref($method_args[0]) =~ /array/i
ABresult
0X0
100
111
135 ( ( ref ($method_args[0]) ) =~ /array/i ) ) {
136 0 $self->{error_ref} = shift @method_args;
137 0 $self->_errorHandler("Got Error ARRAY in new()", $DEBUG_LEVEL_3,
138 $SYS_ERR);
139 }
140 else {
141 0 $self->{error_ref} = undef;
142 0 $self->_errorHandler("No Error ARRAY in new()", $DEBUG_LEVEL_3,
143 $SYS_ERR);
144 }
145  
146 0 ▶ if ( ( scalar (@method_args) > 0 ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @method_args > 0 and not ref $method_args[0]) { } 0 0 0
Condition0%0/3
scalar @method_args > 0 and not ref $method_args[0]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @method_args > 0 and not ref $method_args[0]
scalar @method_args > 0 not ref $method_args[0]
Truth table0%0/3
A and not(B)
A= scalar @method_args > 0B= not ref $method_args[0]
ABresult
0X0
100
111
147 ( ! ref ($method_args[0]) ) ) {
148 0 my $filename = shift @method_args;
149 0 ▶ if(defined($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) { } 0 0 0
150 0 $self->{db_file_name} = $filename ;
151 0 $self->_errorHandler("Got file name in new()", $DEBUG_LEVEL_4,
152 $SYS_ERR);
153 }
154 else {
155 0 $self->_errorHandler("undef passed as filename", $DEBUG_LEVEL_4,
156 $USR_ERR);
157 }
158 }
159 else {
160 0 $self->{db_file_name} = undef;
161 0 $self->_errorHandler("No file name in new()", $DEBUG_LEVEL_3,
162 $SYS_ERR);
163 }
164  
165 # check for invocation errors
166 0 ▶ if ( ( scalar (@method_args) > 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @method_args > 0) { } 0 0 0
elsif (defined $self->{'db_file_name'} and not defined $self->open($self->{'db_file_name'})) { } 0 0 0
167 0 $error_condition = 1;
168 0 $self->_errorHandler("Too many parameters passed to new() method",
169 $DEBUG_LEVEL_3, $SYS_ERR);
170 }
171 ▶ elsif ( defined ( $self->{db_file_name} ) &&
Condition0%0/3
defined $self->{"db_file_name"} and not defined $self->open($self->{"db_file_name"})
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_file_name"} and not defined $self->open($self->{"db_file_name"})
defined $self->{"db_file_name"} not defined $self->open($self->{"db_file_name"})
Truth table0%0/3
A and not(B)
A= defined $self->{"db_file_name"}B= not defined $self->open($self->{"db_file_name"})
ABresult
0X0
100
111
172 ! defined ( $self->open($self->{db_file_name}) ) ) {
173 # the error message is logged via the open() routine
174 0 $self = undef;
175 }
176 0 ▶ return ( $error_condition == 0 ) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$error_condition == 0 ? : 0 0 0
177 }
178  
179  
180 =item $result = $obj_instance->open($file_name);
181  
182 This method opens a FASTA file or pipe for reading. It takes in the filename to
183 be opened. If the file name is "-" the input is taken from stdin. On success,
184 this method returns 1. If the file cannot be opened or parsing fails, this
185 method returns undefined.
186  
187 =cut
188  
189  
190 0 ▶ sub open($) {
Subroutine open: not called CC=8 SCAR=42.8
Pod documented
191 0 my $self = shift;
192 0 my $db_file_name = shift;
193
194 0 my $error_condition = 0;
195  
196 # close a previously open file
197 0 ▶ if ( defined ($self->{db_handle}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"db_handle"}) 0 0 0
198 0 $self->_errorHandler("Closing old handle in open()", $DEBUG_LEVEL_3,
199 $SYS_ERR);
200 0 $self->close();
201 }
202 0 my $name = $self->{db_file_name};
203
204 0 ▶ if (!(
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $db_file_name && ($self->{'db_file_name'} = $db_file_name) && defined $self->{'db_file_name'}) { } 0 0 0
elsif (not defined($self->{'db_handle'} = 'IO::File'->new($self->{'db_file_name'}))) { } 0 0 0
Condition0%0/6
defined $db_file_name && ($self->{'db_file_name'} = $db_file_name) && defined $self->{'db_file_name'}
!ll&&!rl&&r
000
defined $db_file_name and $self->{"db_file_name"} = $db_file_name
!ll&&!rl&&r
000
MC/DC0%0/3
defined $db_file_name && ($self->{'db_file_name'} = $db_file_name) && defined $self->{'db_file_name'}
defined $db_file_name $self->{"db_file_name"} = $db_file_name defined $self->{'db_file_name'}
Truth table0%0/4
A and B && C
A= defined $db_file_nameB= $self->{"db_file_name"} = $db_file_nameC= defined $self->{'db_file_name'}
ABCresult
0XX0
10X0
1100
1111
205 ( defined ( $db_file_name ) ) &&
206 ( $self->{db_file_name} = $db_file_name ) &&
207 ( defined ( $self->{db_file_name} ))
208 ) ) {
209
210 0 $error_condition = 1;
211 0 $self->_errorHandler(
212 "File name does not exist", $DEBUG_LEVEL_3, $USR_ERR);
213 }
214 elsif(!defined ( $self->{db_handle} =
215 new IO::File $self->{db_file_name})) {
216 0 $error_condition = 1;
217 0 $self->_errorHandler(
218 "Cannot open file \'$self->{db_file_name}\'", $DEBUG_LEVEL_3,
219 $USR_ERR);
220 }
221
222
223 0 ▶ if ( $error_condition == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_condition == 1) 0 0 0
224 0 $self->_initialize(); # reset object state
225 }
226
227 0 ▶ return ($error_condition == 1) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$error_condition == 1 ? : 0 0 0
228 }
229  
230  
231 =item $result = $obj_instance->close();
232  
233 This method closes the object file stream and resets all internal data
234 structures. The result of the operation is returned. If the file stream
235 is closed successfully, this object returns true (1), otherwise false
236 (undefined).
237  
238 =cut
239  
240  
241 0 ▶ sub close() {
Subroutine close: not called CC=3 SCAR=24.8
Pod documented
242 0 my $self = shift;
243 0 my $return_val = undef;
244  
245 0 ▶ if ( defined ( $self->{db_handle} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"db_handle"}) 0 0 0
246 0 $return_val = $self->{db_handle}->close();
247 0 ▶ if (!$return_val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($return_val) 0 0 0
248 0 $return_val = undef;
249 0 $self->_errorHandler(
250 "Error closing FASTA file: $self->{db_file_name}",
251 $DEBUG_LEVEL_4, $USR_ERR);
252 }
253 }
254 0 $self->_initialize();
255 0 return $return_val;
256 }
257
258  
259 =item $result = $obj_instance->hasNext();
260  
261 This method returns true (1) if there are more elements beyond the current
262 element in the filestream. If not, this method returns false (undefined).
263  
264 =cut
265  
266 0 ▶ sub hasNext() {
Subroutine hasNext: not called CC=7 SCAR=40.3
Pod documented
267 0 my $self = shift;
268 0 my $next_header = $self->{rec_header};
269 0 $self->_errorHandler(
270 "Checking to see if the header of the next record is set",
271 $DEBUG_LEVEL_2, $SYS_ERR);
272 0 my $result = undef;
273 0 my $newline = undef;
274 0 my $line_number = $self->{line_number};
275 0 ▶ if ($line_number == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_number == 0) 0 0 0
276 0 $self->_errorHandler(
277 "No record has been parsed",
278 $DEBUG_LEVEL_3, $SYS_ERR);
279 my $db_handle = defined ( $self->{db_handle} ) ?
280 0 ▶ $self->{db_handle} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'db_handle'} ? : 0 0 0
281
282 0 ▶ if(defined $db_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db_handle) 0 0 0
283 0 $newline = <$db_handle>;
284 }
285 #reading the first line from a file.
286 0 ▶ if(defined($newline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newline) 0 0 0
287 0 $line_number++;
288 0 $self->{line_number} = $line_number;
289 0 $next_header = $newline;
290 0 $self->_errorHandler(
291 "Assigned the header of the next record",
292 $DEBUG_LEVEL_4, $SYS_ERR);
293 0 $self->{rec_header} = $next_header;
294 0 $result = 1;
295 }
296 }
297
298 0 ▶ if((defined ($next_header)) && (($next_header) ne "")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_header and $next_header ne "") 0 0 0
Condition0%0/3
defined $next_header and $next_header ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $next_header and $next_header ne ""
defined $next_header $next_header ne ""
Truth table0%0/3
A and B
A= defined $next_headerB= $next_header ne ""
ABresult
0X0
100
111
299 0 $result = 1;
300 }
301 0 return $result;
302 }
303  
304  
305 =item $result = $obj_instance->next();
306  
307 This method selects the next record in the file stream for parsing. If the
308 record parses, it is returned, else the method returns undefined. If there is
309 no record in the file stream, the method returns undefined.
310  
311 =cut
312  
313 0 ▶ sub next() {
Subroutine next: not called CC=9 SCAR=45.0
Pod documented
314 0 my $self = shift;
315 0 my $record = undef;
316 0 ▶ if(($self->_parse()) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse == 1) 0 0 0
317 0 $self->_errorHandler(
318 "The record parsed",
319 $DEBUG_LEVEL_3, $SYS_ERR);
320 #obtaining the stored record.
321 0 my $recordarray_ref = $self->{recordinfo};
322
323 0 ▶ if( (defined $recordarray_ref) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recordarray_ref and ref($recordarray_ref) =~ /array/i) 0 0 0
Condition0%0/3
defined $recordarray_ref and ref($recordarray_ref) =~ /array/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $recordarray_ref and ref($recordarray_ref) =~ /array/i
defined $recordarray_ref ref($recordarray_ref) =~ /array/i
Truth table0%0/3
A and B
A= defined $recordarray_refB= ref($recordarray_ref) =~ /array/i
ABresult
0X0
100
111
324 (( ref ($recordarray_ref) ) =~ /array/i) ) {
325
326 0 my @recordarray = @$recordarray_ref;
327 0 my $array_length = 0;
328  
329 0 ▶ if((defined $recordarray_ref) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recordarray_ref and $array_length = @recordarray and $array_length > 0) 0 0 0
Condition0%0/6
defined $recordarray_ref and $array_length = @recordarray and $array_length > 0
!ll&&!rl&&r
000
defined $recordarray_ref and $array_length = @recordarray
!ll&&!rl&&r
000
MC/DC0%0/3
defined $recordarray_ref and $array_length = @recordarray and $array_length > 0
defined $recordarray_ref $array_length = @recordarray $array_length > 0
Truth table0%0/4
A and B and C
A= defined $recordarray_refB= $array_length = @recordarrayC= $array_length > 0
ABCresult
0XX0
10X0
1100
1111
330 ($array_length = @recordarray) &&
331 ($array_length > 0)) {
332
333 0 my $header = shift @recordarray;
334 0 $self->_errorHandler(
335 "Got the record header",
336 $DEBUG_LEVEL_5, $SYS_ERR);
337 0 my $data = undef;
338 0 ▶ if ( scalar(@recordarray) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @recordarray > 0) 0 0 0
339 0 $data = join "", @recordarray;
340 0 $data =~ s/\n//g; # extract new lines from scalar data
341 }
342 $self->_errorHandler(
343 0 "Got the record data",
344 $DEBUG_LEVEL_5, $SYS_ERR);
345 0 $record = new TIGR::FASTA::Record ($header, $data);
346
347 0 ▶ if(defined($record)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $record) 0 0 0
348 0 $self->_errorHandler(
349 "Created new record",
350 $DEBUG_LEVEL_6, $SYS_ERR);
351 }
352 }
353 }
354 }
355 0 return $record;
356 }
357  
358  
359 =item $record_contents = $obj_instance->get();
360  
361 This method returns the current TIGR::FASTA::Record object (active record). If
362 the current object (active record) is undefined, this method returns undefined.
363  
364 =cut
365
366 0 ▶ sub get() {
Subroutine get: not called CC=8 SCAR=42.8
Pod documented
367 0 my $self = shift;
368 0 my $record = undef;
369 #obtaining the stored record information.
370 0 my $recordarray_ref = $self->{recordinfo};
371 0 ▶ if( (defined $recordarray_ref) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recordarray_ref and ref($recordarray_ref) =~ /array/i) 0 0 0
Condition0%0/3
defined $recordarray_ref and ref($recordarray_ref) =~ /array/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $recordarray_ref and ref($recordarray_ref) =~ /array/i
defined $recordarray_ref ref($recordarray_ref) =~ /array/i
Truth table0%0/3
A and B
A= defined $recordarray_refB= ref($recordarray_ref) =~ /array/i
ABresult
0X0
100
111
372 (( ref ($recordarray_ref) ) =~ /array/i) ) {
373 0 my @recordarray = @$recordarray_ref;
374 0 my $array_length = 0;
375  
376 0 ▶ if((defined $recordarray_ref) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recordarray_ref and $array_length = @recordarray and $array_length > 0) 0 0 0
Condition0%0/6
defined $recordarray_ref and $array_length = @recordarray and $array_length > 0
!ll&&!rl&&r
000
defined $recordarray_ref and $array_length = @recordarray
!ll&&!rl&&r
000
MC/DC0%0/3
defined $recordarray_ref and $array_length = @recordarray and $array_length > 0
defined $recordarray_ref $array_length = @recordarray $array_length > 0
Truth table0%0/4
A and B and C
A= defined $recordarray_refB= $array_length = @recordarrayC= $array_length > 0
ABCresult
0XX0
10X0
1100
1111
377 ($array_length = @recordarray) &&
378 ($array_length > 0)) {
379
380 0 my $header = shift @recordarray;
381 0 $self->_errorHandler(
382 "Got the record header",
383 $DEBUG_LEVEL_4, $SYS_ERR);
384 0 my $data = undef;
385 0 ▶ if ( scalar(@recordarray) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @recordarray > 0) 0 0 0
386 0 $data = join "", @recordarray;
387 0 $data =~ s/\n//g; # extract new lines from scalar data
388 }
389 $self->_errorHandler(
390 0 "Got the record data",
391 $DEBUG_LEVEL_4, $SYS_ERR);
392 0 $record = new TIGR::FASTA::Record ($header, $data);
393 0 ▶ if(defined($record)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $record) 0 0 0
394 0 $self->_errorHandler(
395 "Created new record",
396 $DEBUG_LEVEL_5, $SYS_ERR);
397 }
398
399 }
400 }
401 0 return $record;
402 }
403  
404  
405 # $obj_instance->_initialize();
406  
407 #This method resets the object to its initial state. Internal data structures
408 #are reset. This method does not return.
409  
410  
411 0 ▶ sub _initialize() {
Subroutine _initialize: not called CC=1 SCAR=6.9
412 0 my $self = shift;
413 # look up methods for records here
414 0 $self->{error_cnt} = 0; # parse error tabulator
415 0 $self->{db_file_name} = "";
416 0 $self->{db_handle} = undef;
417 0 $self->{rec_header} = undef; # the next record header
418 0 $self->{recordinfo} = undef; # reference to the record contents
419 0 $self->{line_number} = 0; # the line number in the FASTA file.
420 }
421  
422 # $obj_instance->_parse();
423  
424 #This method parses a FASTA record from the file stream.
425 #All the parsing errors for this record are recorded in
426 #the logfile. If the record parses correctly, the method returns 1, else it
427 #returns 0.
428
429
430 0 ▶ sub _parse() {
Subroutine _parse: not called CC=27 SCAR=66.3
431
432 0 my $self = shift;
433 0 my $last_line_length_lt_std_flag = 0;
434 0 my $record_identifier = "<undefined>";
435 0 my $preceding_header_flag = 0;
436 0 my $first_data_line_length = 0;
437 0 my @recarray;
438 0 my $preceding_record_flag = 0;
439  
440 0 my $line_number = $self->{line_number};
441
442 0 $self->{error_cnt} = 0;
443 my $db_handle = defined ( $self->{db_handle} ) ?
444 0 ▶ $self->{db_handle} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'db_handle'} ? : 0 0 0
445
446 # check for FASTA header of next record
447 0 my $header = $self->{rec_header};
448
449 #when in the beginning and end of the filestream.
450 0 ▶ if(!defined ($header)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $header) 0 0 0
451 0 my $newline = <$db_handle>;
452 0 ▶ if((defined($newline)) && ($newline ne "")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newline and $newline ne "") 0 0 0
Condition0%0/3
defined $newline and $newline ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $newline and $newline ne ""
defined $newline $newline ne ""
Truth table0%0/3
A and B
A= defined $newlineB= $newline ne ""
ABresult
0X0
100
111
453 0 $line_number++;
454 0 $header = $newline;
455 }
456 }
457 #parsing the header
458 0 ▶ if ( ( isValidFASTAheader($header) ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&isValidFASTAheader($header) != 0) { } 0 0 0
459 # set up the variables for parsing a new record
460 0 $last_line_length_lt_std_flag = 0;
461 0 $preceding_header_flag = 1;
462 0 $preceding_record_flag = 1;
463 0 $self->{rec_header} = undef;
464  
465 # if it's a valid FASTA header, then don't need to check again
466 # extract the record IDENTIFIER
467 0 $record_identifier = _headerToIdentifier($header);
468 0 push @recarray,$header;
469 }
470 else { #the header is not valid.
471 0 ▶ if((defined $header) && (defined $line_number)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header and defined $line_number) 0 0 0
Condition0%0/3
defined $header and defined $line_number
!ll&&!rl&&r
000
MC/DC0%0/2
defined $header and defined $line_number
defined $header defined $line_number
Truth table0%0/3
A and B
A= defined $headerB= defined $line_number
ABresult
0X0
100
111
472 0 $self->_errorHandler("Expected: record header " .
473 "information in FASTA record header. Got: \'$header\' ".
474 "at line $line_number.", $DEBUG_LEVEL_3, $USR_ERR);
475 }
476 0 $preceding_record_flag = 1;
477 0 $self->{rec_header} = undef;
478 }
479
480 0 ▶ while ( ( defined ( $db_handle ) ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db_handle and defined(my $line = readline $db_handle) and ++$line_number) 0 0 0
Condition0%0/6
defined $db_handle and defined(my $line = readline $db_handle) and ++$line_number
!ll&&!rl&&r
000
defined $db_handle and defined(my $line = readline $db_handle)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $db_handle and defined(my $line = readline $db_handle) and ++$line_number
defined $db_handle defined(my $line = readline $db_handle) ++$line_number
Truth table0%0/4
A and B and C
A= defined $db_handleB= defined(my $line = readline $db_handle)C= ++$line_number
ABCresult
0XX0
10X0
1100
1111
481 ( defined ( my $line = <$db_handle> )) &&
482 (++$line_number)) {
483 0 chomp $line;
484
485 # check FASTA data
486 0 ▶ if ( ( defined ( $record_identifier ) ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $record_identifier and not $record_identifier =~ /<undefined>/ and &isValidFASTAdata($line) != 0) { } 0 0 0
elsif ($line =~ /^>/) { } 0 0 0
Condition0%0/6
defined $record_identifier and not $record_identifier =~ /<undefined>/ and &isValidFASTAdata($line) != 0
!ll&&!rl&&r
000
defined $record_identifier and not $record_identifier =~ /<undefined>/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $record_identifier and not $record_identifier =~ /<undefined>/ and &isValidFASTAdata($line) != 0
defined $record_identifier not $record_identifier =~ /<undefined>/ &isValidFASTAdata($line) != 0
Truth table0%0/4
A and not(B) and C
A= defined $record_identifierB= not $record_identifier =~ /<undefined>/C= &isValidFASTAdata($line) != 0
ABCresult
0XX0
10X0
1100
1111
487 ( $record_identifier !~ /<undefined>/ ) &&
488 ( ( isValidFASTAdata($line) ) != 0 ) ) {
489
490 0 push @recarray,$line;
491 0 ▶ if($preceding_header_flag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preceding_header_flag == 1) 0 0 0
492 0 $first_data_line_length = setValidFASTAlineLength($line);
493 }
494
495 # check $last_line_length_lt_std_flag for an error on
496 # previous line
497 0 ▶ if(defined ($first_data_line_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_data_line_length) 0 0 0
498 0 ▶ if ( $last_line_length_lt_std_flag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_line_length_lt_std_flag == 1) 0 0 0
499 0 $self->{error_cnt}++;
500 0 $self->_errorHandler("Expected: FASTA data ".
501 "definition lines should be ".
502 "$first_data_line_length bases (characters) ".
503 "across. Only the last line of a sequence ".
504 "data definition may be less than " .
505 "$first_data_line_length bases (characters) " .
506 "across, if applicable. See line " .
507 ($line_number - 1) . '.', $DEBUG_LEVEL_6, $USR_ERR);
508 }
509 0 $last_line_length_lt_std_flag = 0;
510
511 # check current line for over-length problem
512 0 ▶ if ( length($line) > $first_data_line_length ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $line > $first_data_line_length) { } 0 0 0
elsif (length $line < $first_data_line_length) { } 0 0 0
513 0 $self->{error_cnt}++;
514 0 $self->_errorHandler("Expected: FASTA data ".
515 "definition lines should be $first_data_line_length ".
516 "bases (characters) across. Only the last line of a ".
517 "sequence data definition may be less than " .
518 "$first_data_line_length bases (characters) ".
519 "across, if applicable. See line " . $line_number .
520 '.',
521 $DEBUG_LEVEL_6,$USR_ERR);
522 }
523
524 #check current line for under-length problem;
525 #report only if not
526 #the last line in the data definition
527 elsif ( length($line) < $first_data_line_length ) {
528 0 $last_line_length_lt_std_flag = 1;
529 }
530 }
531 0 $preceding_header_flag = 0;
532 }
533 elsif($line =~ /^>/) { #the next header
534 0 ▶ if ($preceding_record_flag == 1) { #its the next record.
Branch: 1 branch
BranchTrueFalseTotal
if ($preceding_record_flag == 1) 0 0 0
535 0 $self->{rec_header} = $line;
536 0 $self->_errorHandler(
537 "Assigned the header of the next record",
538 $DEBUG_LEVEL_5, $SYS_ERR);
539 0 last;
540 }
541 }
542 # handle data error types
543 else {
544 0 $self->{error_cnt}++;
545  
546 # line has a separator token in it, so it may be header
547 0 ▶ if ( $line =~ /$UNBOUND_FASTA_SEPARATOR/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /$UNBOUND_FASTA_SEPARATOR/) { } 0 0 0
elsif ($last_line_length_lt_std_flag == 1) { } 0 0 0
elsif (defined $record_identifier and not $record_identifier =~ /<undefined>/) { } 0 0 0
548 0 $self->_errorHandler("Expected: record header " .
549 "information in FASTA record header. Got: \'$line\' ".
550 "at line $line_number.", $DEBUG_LEVEL_6, $USR_ERR);
551 0 $last_line_length_lt_std_flag = 0;
552 }
553 # if last data line was small, expect this to be a header too
554 elsif ( $last_line_length_lt_std_flag == 1 ) {
555 0 $self->_errorHandler("Expected: FASTA record header " .
556 "beginning with \'>\'. Got: \'$line\' at line ".
557 "$line_number.",$DEBUG_LEVEL_6, $USR_ERR);
558 0 $last_line_length_lt_std_flag = 0;
559 }
560 ▶ elsif ( ( defined ( $record_identifier ) ) &&
Condition0%0/3
defined $record_identifier and not $record_identifier =~ /<undefined>/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $record_identifier and not $record_identifier =~ /<undefined>/
defined $record_identifier not $record_identifier =~ /<undefined>/
Truth table0%0/3
A and not(B)
A= defined $record_identifierB= not $record_identifier =~ /<undefined>/
ABresult
0X0
100
111
561 ( $record_identifier !~ /<undefined>/ ) ) {
562 0 $self->_errorHandler("Expected: valid FASTA data " .
563 "definition for record identifier ".
564 "\'$record_identifier\' Check sequence content at line ".
565 "$line_number for invalid bases ".
566 "(data type: invalid data).", $DEBUG_LEVEL_6, $USR_ERR);
567 }
568 else {
569 0 $self->_errorHandler("Expected: FASTA record header " .
570 "followed by definition of sequence. Invalid input at " .
571 "line $line_number.", $DEBUG_LEVEL_6, $USR_ERR);
572 }
573 }
574 } # end while
575  
576 # check terminal case data definition
577 0 ▶ if ( $preceding_header_flag == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preceding_header_flag == 1) 0 0 0
578 0 $self->_nullRecordHandler($line_number);
579 }
580 0 $self->{recordinfo} = \@recarray;
581 0 $self->{line_number} = $line_number;
582 0 ▶ return ( $self->{error_cnt} == 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error_cnt'} == 0 ? : 0 0 0
583 }
584  
585  
586 # $obj_instance->_nullRecordHandler($);
587  
588 #This method handles the case of a null or equivalently empty record
589 #encountered during parsing. It logs the appropriate message to the
590 #TIGR Foundation object. The only argument is the line number.
591  
592  
593 0 ▶ sub _nullRecordHandler($) {
Subroutine _nullRecordHandler: not called CC=3 SCAR=24.8
594 0 my $self = shift;
595 0 my $line_number = shift;
596
597 0 ▶ if ( ! defined ($line_number) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line_number) 0 0 0
598 0 $line_number = "<unknown>";
599 }
600  
601 0 $self->{error_cnt}++;
602 0 ▶ if ( $self->{db_handle}->eof() == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'db_handle'}->eof == 1) { } 0 0 0
603 0 $self->_errorHandler("Expected: FASTA record header " .
604 "followed by definition of sequence. " .
605 "Got end of file after line " .
606 ($line_number) . ".", $DEBUG_LEVEL_5, $USR_ERR);
607 }
608 else {
609 0 $self->_errorHandler("Expected: FASTA record header " .
610 "followed by definition of sequence " .
611 "Got FASTA header at line " .
612 ($line_number-1) . ".", $DEBUG_LEVEL_5, $USR_ERR);
613 }
614 }
615  
616  
617  
618 # $message = $obj_instance->_errorHandler($message, $tf_level,
619 # $internal_log_flag);
620  
621 #This method handles logging to the TIGR::Foundation module and
622 #internal error record reference array. The C<$message> argument is logged
623 #to the appropriate service. The C<$tf_level> parameter specifies the
624 #logging level for TIGR::Foundation, while the C<$internal_log_flag> parameter
625 #specifies if C<$message> should be written to the internal array reference
626 #specified in C<new()>. If a TIGR::Foundation instance does not exist,
627 #no logging to that facility occurs. This method returns C<$message>.
628  
629  
630 0 ▶ sub _errorHandler($$$) {
Subroutine _errorHandler: not called CC=8 SCAR=42.8
631
632 0 my $self = shift;
633  
634 0 my ( $message, $tf_level, $log_facility ) = @_;
635  
636 0 ▶ if ( defined ($message) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and defined $tf_level and defined $log_facility) 0 0 0
Condition0%0/6
defined $message and defined $tf_level and defined $log_facility
!ll&&!rl&&r
000
defined $message and defined $tf_level
!ll&&!rl&&r
000
MC/DC0%0/3
defined $message and defined $tf_level and defined $log_facility
defined $message defined $tf_level defined $log_facility
Truth table0%0/4
A and B and C
A= defined $messageB= defined $tf_levelC= defined $log_facility
ABCresult
0XX0
10X0
1100
1111
637 defined ($tf_level) &&
638 defined ($log_facility) ) {
639  
640 0 ▶ if ( defined ($self->{foundation}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"foundation"}) 0 0 0
641 0 ▶ if ( $log_facility != $USR_ERR ) { # all user errors go to .error
Branch: 1 branch
BranchTrueFalseTotal
if ($log_facility != $USR_ERR) { } 0 0 0
642 0 $self->{foundation}->logLocal($message, $tf_level);
643 }
644 else {
645 0 $self->{foundation}->logError($message);
646 }
647 }
648
649 0 ▶ if ( ( defined ($self->{error_ref}) ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"error_ref"} and $log_facility == $USR_ERR) 0 0 0
Condition0%0/3
defined $self->{"error_ref"} and $log_facility == $USR_ERR
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"error_ref"} and $log_facility == $USR_ERR
defined $self->{"error_ref"} $log_facility == $USR_ERR
Truth table0%0/3
A and B
A= defined $self->{"error_ref"}B= $log_facility == $USR_ERR
ABresult
0X0
100
111
650 ( $log_facility == $USR_ERR ) ) {
651 0 push @{$self->{error_ref}}, $message;
652 }
653 }
654 0 return $message;
655 }
656  
657 =head1 USAGE
658  
659 To use this module, load the C<TIGR::FASTA::Iterator> package via the
660 C<use> function. Then, create a new instance of the object via the
661 C<new()> method, as shown below. There are several invocations possible
662 for this method since all parameters to C<new()> are optional.
663  
664 To access records from the C<TIGR::FASTA::Iterator> instance, the
665 C<TIGR::FASTA::Record> package must be loaded via the C<use> function.
666  
667 An example script using this module follows. The C<TIGR::Foundation>
668 module is included for completeness but does not have to be used.
669  
670 #!/usr/local/bin/perl -w
671  
672 # This script accepts FASTA files with the '-i' option
673 # on the command line and validates every record in the file.
674 # Parse errors for each record are collected to the
675 # '@errors_list' array and written to the .error file.
676 # This program concatenates all of the correct records together to
677 # one output file specified with the '-o' option.
678 # NOTE: The '-i' option must be specified before every input file.
679
680  
681 use strict;
682 use TIGR::FASTA::Iterator;
683 use TIGR::FASTA::Record;
684  
685 MAIN:
686 {
687 my $tf_object = new TIGR::Foundation;
688 my @errors_list = ();
689 my @input_files = ();
690 my $output_file = undef;
691  
692 # Capture the return code from the TIGR::Foundation method
693 my $result = $tf_object->TIGR_GetOptions('i=s' => \@input_files,
694 'o=s' => \$output_file);
695 if ( $result != 1 ) {
696 $tf_object->bail("Invalid command line options.");
697 }
698  
699 # Create a TIGR::FASTA::Iterator instance using TIGR::Foundation and
700 # an error message list.
701 my $fasta_iterator = new TIGR::FASTA::Iterator $tf_object, \@errors_list;
702  
703 if ( !( defined ( $output_file ) &&
704 open OUTFILE, ">$output_file" ) ) {
705 $tf_object->bail("Cannot open output file for writing.");
706 }
707  
708 foreach my $in_file ( @input_files ) {
709 $fasta_iterator->open($in_file) or
710 $tf_object->logLocal("Cannot open or read file $in_file", 2);
711  
712 if ( scalar(@errors_list) > 0 ) { # are there parse errors?
713 while ( @errors_list ) { # get the messages from the list
714 my $message = shift @errors_list;
715 print STDERR $message, "\n";
716 }
717 }
718 #
719 while ( $fasta_iterator->hasNext() ) {
720 # print each record to OUTFILE
721 my $record = $fasta_iterator->next();
722  
723 # print each record to OUTFILE
724 if(defined $record) {
725 print OUTFILE $record->toString();
726 }
727 }
728 }
729 }
730  
731 =cut
732  
733 }
734  
735 1 1;
« lib/TIGR/FASTA/Reader.pm ↑ Summary lib/TIGR/FASTA/Writer.pm »