File Coverage

blib/lib/TIGR/FASTA/Writer.pm
Criterion Covered Total %
statement 10 83 12.0
branch 0 38 0.0
condition 0 42 0.0
subroutine 4 9 44.4
pod 4 4 100.0
total 18 176 10.2


line stmt bran cond sub pod time code
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             BEGIN {
26 1     1   1508 require 5.006_00;
27             }
28              
29 1     1   4 use strict;
  1         2  
  1         19  
30 1     1   4 use IO::File;
  1         1  
  1         142  
31 1     1   9 use TIGR::FASTA::Record;
  1         3  
  1         844  
32              
33              
34              
35             ## internal variables and identifiers
36              
37             our $REVISION = (qw$Revision: 1.1 $)[-1];
38             our $VERSION = '1.0';
39             our $VERSION_STRING = "$VERSION (Build $REVISION)";
40             our @DEPEND = ();
41            
42             my $SYS_ERR = 0; # this flag specifies non-user related error
43             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             my $DEBUG_LEVEL_1 = 1;
56             my $DEBUG_LEVEL_2 = 2;
57             my $DEBUG_LEVEL_3 = 3;
58             my $DEBUG_LEVEL_4 = 4;
59             my $DEBUG_LEVEL_5 = 5;
60             my $DEBUG_LEVEL_6 = 6;
61             my $DEBUG_LEVEL_7 = 7;
62             my $DEBUG_LEVEL_8 = 8;
63             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             sub new(;$$$) {
95            
96 0     0 1   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 0 0       if ( ( scalar (@method_args) > 0 ) &&
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 0 0       if ( ( scalar (@method_args) > 0 ) &&
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 0 0       if ( ( scalar (@method_args) > 0 ) &&
129             ( ! ref ($method_args[0]) ) ) {
130 0           my $filename = shift @method_args;
131 0 0         if(defined($filename)) {
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 0 0       if ( ( scalar (@method_args) > 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} )) &&
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 0         return ( $error_condition == 0 ) ? $self : undef;
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 flags specified by C<$flag>.
169             Supported flags include 'w' and 'a'. On success, this method returns 1.
170             The default C method is 'w', or truncated open. If the file cannot
171             be opened, this method returns undefined.
172              
173             =cut
174              
175              
176             sub open($;$) {
177 0     0 1   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 0 0       if ( ( ! defined ($open_flags) ) ||
      0        
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 0         if ( defined ($self->{db_handle}) ) {
191 0           $self->close();
192             }
193              
194 0 0 0       if (!(
      0        
      0        
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 0         return ($error_condition == 1) ? undef : 1;
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             sub close() {
221 0     0 1   my $self = shift;
222 0           my $return_val = undef;
223              
224 0 0         if ( defined ( $self->{db_handle} ) ) {
225 0           $return_val = $self->{db_handle}->close();
226 0 0         if ( ! defined ($return_val) ) {
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 or C. 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             sub write($) {
250 0     0 1   my $self = shift;
251 0           my $fasta_obj = shift;
252 0           my $return_val = 1;
253            
254 0 0 0       if ( ( defined ($fasta_obj) ) &&
      0        
255             ( ( ref($fasta_obj) ) =~ /fastarecord/i ) &&
256             ( defined ($self->{db_handle}) ) ) {
257            
258 0 0         if ( $self->{db_handle}->print($fasta_obj->toString()) ) {
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. If a TIGR::Foundation instance does not exist,
287             #no logging to that facility occurs. This method returns C<$message>.
288              
289              
290              
291             sub _errorHandler($$$) {
292            
293 0     0     my $self = shift;
294              
295 0           my ( $message, $tf_level, $log_facility ) = @_;
296              
297 0 0 0       if ( defined ($message) &&
      0        
298             defined ($tf_level) &&
299             defined ($log_facility) ) {
300              
301 0 0         if ( defined ($self->{foundation}) ) {
302 0 0         if ( $log_facility != $USR_ERR ) { # all user errors go to .error
303 0           $self->{foundation}->logLocal($message, $tf_level);
304             }
305             else {
306 0           $self->{foundation}->logError($message);
307             }
308             }
309              
310 0 0 0       if ( ( defined ($self->{error_ref}) ) &&
311             ( $log_facility == $USR_ERR ) ) {
312 0           push @{$self->{error_ref}}, $message;
  0            
313             }
314             }
315 0           return $message;
316             }
317              
318              
319             =back
320              
321             =head1 USAGE
322              
323             To use this module, load the C and C
324             modules with the C function. Then, create a new instance of the object
325             via the C method, as shown below. There are several invocations
326             possible for this method since all parameters to C are optional.
327             An example script using this module follows. The C
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;