lib/TIGR/FASTA/Writer.pm

filter: Statementstmt 27.0% 27 / 100 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 44.4% 4 / 9 Podpod 100% 4 / 4 totaltotal 16.2% 35 / 216 CC 34 SCAR 44.9
CC
34
Cov
15%
new
156.0
open
110.0
_errorHandler
72.0
CRAP 89.0
« lib/TIGR/FASTA/Iterator.pm ↑ Summary lib/TIGR/Foundation.pm »
1 # $Id: FASTAwriter.pm,v 1.1 2004/04/28 15:03:43 aphillip Exp $
2  
3 package TIGR::FASTA::Writer;
4 {
5  
6 =head1 NAME
7  
8 TIGR::FASTA::Writer - TIGR::FASTA::Writer class for writing TIGR::FASTA::Record
9 objects to a file
10  
11 =head1 SYNOPSIS
12  
13 use TIGR::FASTA:Writer;
14 my $obj_instance = new TIGR::FASTA::Writer ($tigr_foundation_obj,
15 $output_file_name);
16  
17 =head1 DESCRIPTION
18  
19 This module provides an object definition for a TIGR::FASTA::Writer.
20 The TIGR::FASTA::Writer object accepts TIGR::FASTA::Record objects for
21 printing to an output file.
22  
23 =cut
24  
25 1 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 require 5.006_00;
27 }
28  
29 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶ use TIGR::FASTA::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34  
35 ## internal variables and identifiers
36  
37 1 our $REVISION = (qw$Revision: 1.1 $)[-1];
38 1 our $VERSION = '1.0';
39 1 our $VERSION_STRING = "$VERSION (Build $REVISION)";
40 1 our @DEPEND = ();
41
42 1 my $SYS_ERR = 0; # this flag specifies non-user related error
43 1 my $USR_ERR = 1; # this flag specifies user related error
44
45 #debugging scheme
46 #
47 # Debugging via the TIGR Foundation uses increasing log levels based on
48 # nesting. 'MAIN' starts at level 1. Every nest increments the level by 1.
49 # Subroutines always start nesting at level 2. As debugging levels
50 # increase, logging is more verbose. This makes sense as you log at
51 # greater depth (ie. deeper branching).
52 #
53 # The following definitions help emphasize the debugging in the program.
54 #
55 1 my $DEBUG_LEVEL_1 = 1;
56 1 my $DEBUG_LEVEL_2 = 2;
57 1 my $DEBUG_LEVEL_3 = 3;
58 1 my $DEBUG_LEVEL_4 = 4;
59 1 my $DEBUG_LEVEL_5 = 5;
60 1 my $DEBUG_LEVEL_6 = 6;
61 1 my $DEBUG_LEVEL_7 = 7;
62 1 my $DEBUG_LEVEL_8 = 8;
63 1 my $DEBUG_LEVEL_9 = 9;
64  
65 ## prototypes
66  
67 sub new(;$$$);
68 sub open($;$);
69 sub close();
70 sub write($);
71 sub _errorHandler($$$);
72  
73 ## implementation
74  
75 =over
76  
77 =item $obj_instance = new TIGR::FASTA::Writer ($foundation_object,
78 $error_array_ref, $output_file);
79  
80 This method returns a new instance of a TIGR::FASTA::Writer object. It takes
81 three optional parameters: a TIGR::Foundation object (C<$foundation_object>),
82 a reference to an array for logging user error messages (C<$error_array_ref>),
83 and an output file name, C<$output_file>, as parameters. A new object instance
84 is returned on success and successful opening of a specified output
85 file.
86 If the file supplied cannot be opened, this method returns undefined.
87 This method also returns undefined if the parameters supplied are invalid.
88 Writing errors are written to the array at C<$error_array_ref> and the
89 log file.
90  
91 =cut
92  
93  
94 0 ▶ sub new(;$$$) {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
95
96 0 my $pkg = shift;
97 0 my @method_args = @_;
98  
99 0 my $error_condition = 0;
100 0 my $self = {};
101 0 bless $self, $pkg;
102
103 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
104 ( ( ref ($method_args[0]) ) =~ /foundation/i ) ) {
105 0 $self->{foundation} = shift @method_args;
106 0 $self->_errorHandler("Got TIGR::Foundation in new()", $DEBUG_LEVEL_3,
107 $SYS_ERR);
108 }
109 else {
110 0 $self->{foundation} = undef;
111 0 $self->_errorHandler("No TIGR::Foundation in new()", $DEBUG_LEVEL_3,
112 $SYS_ERR);
113 }
114
115 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
116 ( ( ref ($method_args[0]) ) =~ /array/i ) ) {
117 0 $self->{error_ref} = shift @method_args;
118
119 0 $self->_errorHandler("Got Error ARRAY in new()", $DEBUG_LEVEL_3,
120 $SYS_ERR);
121 }
122 else {
123 0 $self->{error_ref} = undef;
124 0 $self->_errorHandler("No Error ARRAY in new()", $DEBUG_LEVEL_3,
125 $SYS_ERR);
126 }
127  
128 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
129 ( ! ref ($method_args[0]) ) ) {
130 0 my $filename = shift @method_args;
131 0 ▶ if(defined($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) { } 0 0 0
132 0 $self->{db_file_name} = $filename ;
133 0 $self->_errorHandler("Got file name in new()", $DEBUG_LEVEL_4,
134 $SYS_ERR);
135 }
136 else {
137 0 $self->{db_file_name} = "" ;
138 0 $self->_errorHandler("undef passed as filename", $DEBUG_LEVEL_4,
139 $USR_ERR);
140 }
141 }
142 else {
143 0 $self->{db_file_name} = undef;
144 0 $self->_errorHandler("No file name in new()", $DEBUG_LEVEL_3,
145 $SYS_ERR);
146 }
147  
148 # check for invocation errors
149 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'}, 'w')) { } 0 0 0
150 0 $error_condition = 1;
151 0 $self->_errorHandler("Too many parameters passed to new() method",
152 $DEBUG_LEVEL_3, $SYS_ERR);
153 }
154 ▶ elsif ((defined ( $self->{db_file_name} )) &&
Condition0%0/3
defined $self->{"db_file_name"} and not defined $self->open($self->{"db_file_name"}, "w")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_file_name"} and not defined $self->open($self->{"db_file_name"}, "w")
defined $self->{"db_file_name"} not defined $self->open($self->{"db_file_name"}, "w")
Truth table0%0/3
A and not(B)
A= defined $self->{"db_file_name"}B= not defined $self->open($self->{"db_file_name"}, "w")
ABresult
0X0
100
111
155 (! defined ( $self->open($self->{db_file_name}, "w") ) )) {
156 # the error message is logged via the open() routine
157 0 $self = undef;
158 }
159
160 0 ▶ return ( $error_condition == 0 ) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$error_condition == 0 ? : 0 0 0
161
162 }
163  
164  
165 =item $result = $obj_instance->open($file_name, $flag);
166  
167 This method opens a FASTA file for writing or appending. The file,
168 F<$file_name>, is opened using the C<open()> flags specified by C<$flag>.
169 Supported flags include 'w' and 'a'. On success, this method returns 1.
170 The default C<open()> method is 'w', or truncated open. If the file cannot
171 be opened, this method returns undefined.
172  
173 =cut
174  
175  
176 0 ▶ sub open($;$) {
Subroutine open: not called CC=10 SCAR=47.0
Pod documented
177 0 my $self = shift;
178 0 my $db_file_name = shift;
179 0 my $open_flags = shift;
180  
181 0 my $error_condition = 0;
182  
183 0 ▶ if ( ( ! defined ($open_flags) ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $open_flags and $open_flags =~ /^w$/i || $open_flags =~ /^a$/i) 0 0 0
Condition0%0/6
defined $open_flags and $open_flags =~ /^w$/i || $open_flags =~ /^a$/i
!ll&&!rl&&r
000
$open_flags =~ /^w$/i or $open_flags =~ /^a$/i
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $open_flags and $open_flags =~ /^w$/i || $open_flags =~ /^a$/i
defined $open_flags $open_flags =~ /^w$/i $open_flags =~ /^a$/i
Truth table0%0/4
A and B or C
A= defined $open_flagsB= $open_flags =~ /^w$/iC= $open_flags =~ /^a$/i
ABCresult
0XX0
1000
11X1
1011
184 ( ( $open_flags !~ /^w$/i ) &&
185 ( $open_flags !~ /^a$/i ) ) ) {
186 0 $open_flags = "w";
187 }
188  
189 # close a previously open file
190 0 ▶ if ( defined ($self->{db_handle}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"db_handle"}) 0 0 0
191 0 $self->close();
192 }
193  
194 0 ▶ if (!(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $db_file_name and $self->{"db_file_name"} = $db_file_name and defined $self->{"db_file_name"} and defined($self->{"db_handle"} = "IO::File"->new($self->{"db_file_name"}, $open_flags))) 0 0 0
Condition0%0/9
defined $db_file_name and $self->{"db_file_name"} = $db_file_name and defined $self->{"db_file_name"} and defined($self->{"db_handle"} = "IO::File"->new($self->{"db_file_name"}, $open_flags))
!ll&&!rl&&r
000
defined $db_file_name and $self->{"db_file_name"} = $db_file_name and 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/4
defined $db_file_name and $self->{"db_file_name"} = $db_file_name and defined $self->{"db_file_name"} and defined($self->{"db_handle"} = "IO::File"->new($self->{"db_file_name"}, $open_flags))
defined $db_file_name $self->{"db_file_name"} = $db_file_name defined $self->{"db_file_name"} defined($self->{"db_handle"} = "IO::File"->new($self->{"db_file_name"}, $open_flags))
Truth table0%0/5
A and B and C and D
A= defined $db_file_nameB= $self->{"db_file_name"} = $db_file_nameC= defined $self->{"db_file_name"}D= defined($self->{"db_handle"} = "IO::File"->new($self->{"db_file_name"}, $open_flags))
ABCDresult
0XXX0
10XX0
110X0
11100
11111
195 ( defined ( $db_file_name ) ) &&
196 ( $self->{db_file_name} = $db_file_name ) &&
197 ( defined ( $self->{db_file_name} )) &&
198 ( defined ( $self->{db_handle} =
199 new IO::File $self->{db_file_name}, $open_flags ))
200 ) ) {
201 0 $error_condition = 1;
202 0 $self->_errorHandler(
203 "Cannot open file \'$self->{db_file_name}\'", $DEBUG_LEVEL_3,
204 $USR_ERR);
205 }
206 0 ▶ return ($error_condition == 1) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$error_condition == 1 ? : 0 0 0
207 }
208  
209  
210 =item $result = $obj_instance->close();
211  
212 This method closes the object file stream and resets all internal data
213 structures. The result of the operation is returned. If the file stream
214 is closed successfully, this object returns true (1), otherwise false
215 (undefined).
216  
217 =cut
218  
219  
220 0 ▶ sub close() {
Subroutine close: not called CC=3 SCAR=24.8
Pod documented
221 0 my $self = shift;
222 0 my $return_val = undef;
223  
224 0 ▶ if ( defined ( $self->{db_handle} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"db_handle"}) 0 0 0
225 0 $return_val = $self->{db_handle}->close();
226 0 ▶ if ( ! defined ($return_val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $return_val) 0 0 0
227 0 $return_val = undef;
228 0 $self->_errorHandler(
229 "Error closing FASTA file: $self->{db_file_name}",
230 $DEBUG_LEVEL_4, $USR_ERR);
231 }
232 }
233 0 $self->{db_file_name} = undef;
234 0 $self->{db_handle} = undef;
235 0 return $return_val;
236 }
237
238  
239 =item $result = $obj_instance->write($fasta_obj);
240  
241 This method takes a TIGR::FASTA::Record object, C<$fasta_obj>, and writes it
242 to the file specified in C<new()> or C<open()>. On success, this method
243 returns true (1). On error, this method returns false (undefined) and logs
244 an error message.
245  
246 =cut
247  
248  
249 0 ▶ sub write($) {
Subroutine write: not called CC=5 SCAR=34.0
Pod documented
250 0 my $self = shift;
251 0 my $fasta_obj = shift;
252 0 my $return_val = 1;
253
254 0 ▶ if ( ( defined ($fasta_obj) ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fasta_obj and ref($fasta_obj) =~ /fastarecord/i and defined $self->{'db_handle'}) { } 0 0 0
Condition0%0/6
defined $fasta_obj and ref($fasta_obj) =~ /fastarecord/i and defined $self->{"db_handle"}
!ll&&!rl&&r
000
defined $fasta_obj and ref($fasta_obj) =~ /fastarecord/i
!ll&&!rl&&r
000
MC/DC0%0/3
defined $fasta_obj and ref($fasta_obj) =~ /fastarecord/i and defined $self->{"db_handle"}
defined $fasta_obj ref($fasta_obj) =~ /fastarecord/i defined $self->{"db_handle"}
Truth table0%0/4
A and B and C
A= defined $fasta_objB= ref($fasta_obj) =~ /fastarecord/iC= defined $self->{"db_handle"}
ABCresult
0XX0
10X0
1100
1111
255 ( ( ref($fasta_obj) ) =~ /fastarecord/i ) &&
256 ( defined ($self->{db_handle}) ) ) {
257
258 0 ▶ if ( $self->{db_handle}->print($fasta_obj->toString()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'db_handle'}->print($fasta_obj->toString)) { } 0 0 0
259 0 $return_val = 1;
260 }
261 else {
262
263 0 $return_val = undef;
264 0 $self->_errorHandler(
265 "Error printing to FASTA file: $self->{db_file_name}",
266 $DEBUG_LEVEL_3, $USR_ERR);
267 }
268 }
269 else {
270 0 $return_val = undef;
271 0 $self->_errorHandler(
272 "Invalid method of initialization for " .
273 "TIGR::FASTA::Writer", $DEBUG_LEVEL_3, $USR_ERR);
274 }
275 0 return $return_val;
276 }
277  
278 # $message = $obj_instance->_errorHandler($message, $tf_level,
279 # $internal_log_flag);
280  
281 #This method handles logging to the TIGR::Foundation module and
282 #internal error record reference array. The C<$message> argument is logged
283 #to the appropriate service. The C<$tf_level> parameter specifies the
284 #logging level for TIGR::Foundation, while the C<$internal_log_flag> parameter
285 #specifies if C<$message> should be written to the internal array reference
286 #specified in C<new()>. If a TIGR::Foundation instance does not exist,
287 #no logging to that facility occurs. This method returns C<$message>.
288  
289  
290  
291 0 ▶ sub _errorHandler($$$) {
Subroutine _errorHandler: not called CC=8 SCAR=42.8
292
293 0 my $self = shift;
294  
295 0 my ( $message, $tf_level, $log_facility ) = @_;
296  
297 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
298 defined ($tf_level) &&
299 defined ($log_facility) ) {
300  
301 0 ▶ if ( defined ($self->{foundation}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"foundation"}) 0 0 0
302 0 ▶ if ( $log_facility != $USR_ERR ) { # all user errors go to .error
Branch: 1 branch
BranchTrueFalseTotal
if ($log_facility != $USR_ERR) { } 0 0 0
303 0 $self->{foundation}->logLocal($message, $tf_level);
304 }
305 else {
306 0 $self->{foundation}->logError($message);
307 }
308 }
309  
310 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
311 ( $log_facility == $USR_ERR ) ) {
312 0 push @{$self->{error_ref}}, $message;
313 }
314 }
315 0 return $message;
316 }
317  
318  
319 =back
320  
321 =head1 USAGE
322  
323 To use this module, load the C<TIGR::FASTA::Record> and C<TIGR::FASTA::Writer>
324 modules with the C<use> function. Then, create a new instance of the object
325 via the C<new()> method, as shown below. There are several invocations
326 possible for this method since all parameters to C<new()> are optional.
327 An example script using this module follows. The C<TIGR::Foundation>
328 module is included for completeness but does not have to be used.
329  
330 #!/usr/local/bin/perl -w
331  
332 # This example uses the TIGR::FASTA::Writer object to write
333 # a simple TIGR::FASTA::Record object to a file specified with
334 # the '-o' option to this script.
335 # Writing errors are collected to the '@errors_list' array.
336
337 use strict;
338 use TIGR::Foundation;
339 use TIGR::FASTA::Record;
340 use TIGR::FASTA::Writer;
341  
342 MAIN:
343 {
344 my $tf_object = new TIGR::Foundation;
345 my @errors_list = ();
346 my $output_file = undef;
347  
348 my $getopts_result = undef;
349  
350 $getopts_result = $tf_object->TIGR_GetOptions( "o=s" => \$output_file );
351
352 if ( $getopts_result != 1 ) {
353 $tf_object->bail("Invalid command line option.");
354 }
355  
356 if ( ! defined ( $output_file ) ) {
357 $tf_object->bail("Must specify an output file with the '-o' option");
358 }
359  
360 my $header = "ORF00001";
361 my $data = "ATGC";
362  
363 my $fasta_record = new TIGR::FASTA::Record $header, $data;
364 if ( ! defined ( $fasta_record ) ) {
365 $tf_object->bail("Cannot create TIGR::FASTA::Record object");
366 }
367
368 # Create a TIGR::FASTA::Writer instance using TIGR::Foundation and
369 # an error message list.
370  
371 my $fasta_writer = new TIGR::FASTA::Writer $tf_object, \@errors_list;
372  
373 $fasta_writer->open($output_file) or
374 $tf_object->logLocal("Cannot open output file $output_file",
375 $DEBUG_LEVEL_1);
376  
377 if ( scalar(@errors_list) > 0 ) { # are there parse errors?
378 while ( @errors_list ) { # get the messages from the list
379 my $message = shift @errors_list;
380 print STDERR $message, "\n";
381 }
382 }
383  
384 $fasta_writer->write($fasta_record ) or
385 $tf_object->logLocal("Cannot write FASTA record to $output_file",
386 $DEBUG_LEVEL_1);
387 }
388  
389 =cut
390  
391 }
392  
393 1 1;
« lib/TIGR/FASTA/Iterator.pm ↑ Summary lib/TIGR/Foundation.pm »