blib/lib/BLASTaid.pm

filter: Statementstmt 86.1% 118 / 137 Branchbran 64.0% 41 / 64 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 76.1% 182 / 239 CC 41 SCAR 20.9
CC
41
Cov
77%
return_report
12.3
_build_index
10.4
_load_index_file
9.7
CRAP 8.1
↑ Summary
1 package BLASTaid;
2  
3 # | PACKAGE | BLASTaid
4 # | AUTHOR | Todd Wylie
5 # | EMAIL | perldev@monkeybytes.org
6 # | ID | $Id: BLASTaid.pm 20 2006-03-15 21:28:53Z Todd Wylie $
7  
8 2 ▶use version; $VERSION = qv('0.0.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Seekable;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # --------------------------------------------------------------------------
17 # N E W (class CONSTRUCTOR)
18 # ==========================================================================
19 # USAGE : BLASTaid->new();
20 # PURPOSE : Constructor for class.
21 # RETURNS : Object handle.
22 # PARAMETERS : report => ''
23 # : index => ''
24 # THROWS : croaks if arguments are missing or report file is suspect
25 # COMMENTS : Feed the interface a WU-BLAST report path and also the
26 # : path/name where you want the index file saved. If the index
27 # : file specified already exists, use it.
28 # --------------------------------------------------------------------------
29 2 ▶sub new {
Subroutine new: called CC=6 SCAR=19.7
Pod documented
30 2 my ($class, %arg) = @_;
31  
32 # Do some simple file validation checks.
33 0 ▶ if (!$arg{report} ) { croak "new requires a REPORT value" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"report"}) 0 2 2
34 0 ▶ if (!-f $arg{report}) { croak "report [$arg{report}] does not exist" }
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $arg{"report"}) 0 2 2
35 0 ▶ if (!-T $arg{report}) { croak "report [$arg{report}] is not a text report" }
Branch: 1 branch
BranchTrueFalseTotal
unless (-T $arg{"report"}) 0 2 2
36
37 # Deal with the index file initialization.
38 0 ▶ if (!$arg{index}) { croak "new requires a INDEX value" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"index"}) 0 2 2
39
40 # Class setup.
41 my $self = {
42 _report => $arg{report},
43 _index => $arg{index},
44 2 _ignored => [],
45 };
46 2 bless($self, $class);
47
48 # If the supplied index already exists then use it. Else, build
49 # the index file. Populate the object with the index regardless.
50 2 ▶ if (-f $self->{_index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{'_index'}) { } 1 1 2
51 1 $self->_load_index_file();
52 }
53 else {
54 1 $self->_build_index();
55 }
56
57 2 return($self);
58 }
59  
60  
61 # --------------------------------------------------------------------------
62 # E A C H R E P O R T (accessor method)
63 # ==========================================================================
64 # USAGE : BLASTaid->each_report( ignore => 'yes' );
65 # PURPOSE : Accessor method, iterator for object.
66 # RETURNS : Query names.
67 # PARAMETERS : ignore => ''
68 # THROWS : croaks if no query names are in object
69 # COMMENTS : If ignore = yes, reports with no alignments are skipped.
70 # --------------------------------------------------------------------------
71 1 ▶sub each_report {
Subroutine each_report: called CC=7 SCAR=21.8
Pod documented
72 1 my ($class, %arg) = @_;
73
74 # Must have a valid ignore clause:
75 0 ▶ if (!defined $arg{ignore}) { croak "each_report requires a IGNORE value" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg{"ignore"}) 0 1 1
76  
77 # Iterate through the record index returning associated query
78 # name:
79 1 my @queries;
80 1 foreach my $id ( sort {$a <=> $b} keys %{$class->{_id}} ) {
81 41 ▶ unless ( $class->{_id}->{$id}->{_name} eq "END-OF-FILE" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->{"_id"}{$id}{"_name"} eq "END-OF-FILE") 40 1 41
82 40 ▶ unless ( $arg{ignore} eq "yes" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'ignore'} eq 'yes') { } 40 0 40
83 0 push( @queries, $class->{_id}->{$id}->{_name} );
84 }
85 else {
86 40 ▶ if ($class->{_id}->{$id}->{_alignments} eq "TRUE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->{'_id'}{$id}{'_alignments'} eq 'TRUE') { } 40 0 40
87 40 push( @queries, $class->{_id}->{$id}->{_name} );
88 }
89 else {
90 0 push( @{$class->{_ignored}}, $class->{_id}->{$id}->{_name} );
91 }
92 }
93 }
94 }
95 0 ▶ if (@queries < 1) { croak "no queries found in object" }
Branch: 1 branch
BranchTrueFalseTotal
if (@queries < 1) 0 1 1
96
97 1 return(@queries);
98 }
99  
100  
101 # --------------------------------------------------------------------------
102 # R E T U R N R E P O R T (accessor method)
103 # ==========================================================================
104 # USAGE : BLASTaid->return_report( query => 'contig1.0' );
105 # PURPOSE : Accessor method for returning a report,
106 # RETURNS : String of BLAST report content.
107 # PARAMETERS : query => ''
108 # THROWS : croaks if no query argument is indicated
109 # : croaks if report string is null
110 # : croaks if a record is partial (no EXIT CODE)
111 # --------------------------------------------------------------------------
112 1 ▶sub return_report {
Subroutine return_report: called CC=10 SCAR=25.1
Pod documented
113 1 my ($class, %arg) = @_;
114
115 # Must have a query name to continue.
116 0 ▶ if (!defined $arg{query} || $arg{query} eq "" ) { croak "return_report must have a QUERY argument" }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $arg{"query"} or $arg{"query"} eq "") 0 1 1
Condition33%1/3
not defined $arg{"query"} or $arg{"query"} eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $arg{"query"} or $arg{"query"} eq ""
not defined $arg{"query"} $arg{"query"} eq ""
Truth table33%1/3
not(A) or B
A= not defined $arg{"query"}B= $arg{"query"} eq ""
ABresult
1X1
011
000
117 0 ▶ if (!defined $class->{_queries}->{_name}->{$arg{query}}) { croak "return_report cannot locate $arg{query} in object" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class->{"_queries"}{"_name"}{$arg{"query"}}) 0 1 1
118  
119 # Seek ahead to the entry and grab the report's text.
120 1 my $pass = "false";
121 1 my $REPORT = new IO::File;
122 1 ▶ $REPORT->open( "$class->{_report}" ) or croak "could not open file $class->{_report}";
Branch: 1 branch
BranchTrueFalseTotal
$REPORT->open(qq[$class->{"_report"}]) or croak(qq[could not open file $class->{"_report"}]) 1 0 1
123 1 $REPORT->seek( $class->{_queries}->{_name}->{$arg{query}}->{_start}, 0 );
124 1 my $string;
125 1 my $seeking = <$REPORT>;
126 1 $string = $seeking;
127 SEEKENTRY:
128 1 ▶ while (<$REPORT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $REPORT)) 1024 0 1024
129 1024 ▶ if ($_ =~ /EXIT CODE (\d+)/) { $pass = "true" };
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /EXIT CODE (\d+)/) 1 1023 1024
130 1024 ▶ last SEEKENTRY if ($_ =~ /^BLAST/);
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^BLAST/ 1 1023 1024
131 1023 $string .= $_;
132 }
133 1 $REPORT->close;
134
135 # Error checking.
136 0 ▶ if ($string eq "" ) { croak "return_report has null return for $arg{query}" };
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "") 0 1 1
137 0 ▶ if ($pass ne "true") { croak "$arg{query} is a partial report" };
Branch: 1 branch
BranchTrueFalseTotal
if ($pass ne "true") 0 1 1
138
139 1 return($string);
140 }
141  
142  
143 # --------------------------------------------------------------------------
144 # B U I L D I N D E X (internal method)
145 # ==========================================================================
146 # USAGE : BLASTaid->_build_index();
147 # PURPOSE : Builds & saves the index file for byte positions.
148 # RETURNS : none
149 # PARAMETERS : none
150 # THROWS : croaks if BLAST report cannot be opened
151 # : croaks if object entries are not complete
152 # --------------------------------------------------------------------------
153 1 ▶sub _build_index {
Subroutine _build_index: called CC=10 SCAR=23.4
154 1 my $class = shift;
155
156 # Open the specified BLAST report and index it.
157 1 my $id;
158 1 my $REPORT = new IO::File;
159 1 ▶ $REPORT->open( "$class->{_report}" ) or croak "could not open file $class->{_report}";
Branch: 1 branch
BranchTrueFalseTotal
$REPORT->open(qq[$class->{"_report"}]) or croak(qq[could not open file $class->{"_report"}]) 1 0 1
160 1 ▶ while (<$REPORT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $REPORT)) 79945 1 79946
161 79945 ▶ if (/^(BLAST\S+)\s+/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^(BLAST\S+)\s+/) { } 40 79905 79945
elsif (/^Query\=\s+(\S+)/) { } 40 79865 79905
elsif (/\s*.+NONE.+\s*/) { } 0 79865 79865
elsif ($REPORT->eof) { } 1 79864 79865
162 40 $id++;
163 40 $class->{_id}->{$id}->{_start} = $REPORT->tell - length($_);
164 40 $class->{_id}->{$id}->{_alignments} = "TRUE";
165 40 $class->{_id}->{$id}->{_type} = $1;
166 }
167 elsif (/^Query\=\s+(\S+)/) {
168 40 $class->{_id}->{$id}->{_name} = $1;
169 }
170 elsif (/\s*.+NONE.+\s*/) {
171 # No alignments:
172 0 $class->{_id}->{$id}->{_alignments} = "FALSE";
173 }
174 elsif ($REPORT->eof) {
175 1 $id++;
176 1 $class->{_id}->{$id}->{_start} = $REPORT->tell - length($_);
177 1 $class->{_id}->{$id}->{_name} = "END-OF-FILE";
178 1 $class->{_id}->{$id}->{_alignments} = "FALSE";
179 1 $class->{_id}->{$id}->{_type} = "N/A";
180 }
181 }
182 1 $REPORT->close;
183
184 # Make sure that all entries have needed values. Revise the object
185 # to support the query names as unique keys.
186 1 foreach my $report (sort {$a <=> $b} keys %{$class->{_id}}) {
187 41 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $class->{'_id'}{$report}{'_name'} && defined $class->{'_id'}{$report}{'_start'}) { } 0 41 41
Condition33%1/3
defined $class->{'_id'}{$report}{'_name'} && defined $class->{'_id'}{$report}{'_start'}
!ll&&!rl&&r
0041
MC/DC0%0/2
defined $class->{'_id'}{$report}{'_name'} && defined $class->{'_id'}{$report}{'_start'}
defined $class->{'_id'}{$report}{'_name'} defined $class->{'_id'}{$report}{'_start'}
Truth table33%1/3
A && B
A= defined $class->{'_id'}{$report}{'_name'}B= defined $class->{'_id'}{$report}{'_start'}
ABresult
0X0
100
111
188 !defined $class->{_id}->{$report}->{_name} ||
189 !defined $class->{_id}->{$report}->{_start}
190 ) {
191 0 croak "missing QUERY NAME or START for entry $report";
192 }
193 else {
194 $class->{_queries}->{_name}->{ $class->{_id}->{$report}->{_name} } = {
195 _start => $class->{_id}->{$report}->{_start},
196 _alignments => $class->{_id}->{$report}->{_alignments},
197 _type => $class->{_id}->{$report}->{_type},
198 41 };
199 }
200 }
201
202 # Save the object to an index file.
203 1 $class->_save_index_file();
204
205 1 return($class);
206 }
207  
208  
209 # --------------------------------------------------------------------------
210 # S A V E I N D E X F I L E (internal method)
211 # ==========================================================================
212 # USAGE : BLASTaid->_save_index_file();
213 # PURPOSE : Saves the object to an index byte file.
214 # RETURNS : none
215 # PARAMETERS : none
216 # THROWS : croaks if index file cannot be saved
217 # --------------------------------------------------------------------------
218 1 ▶sub _save_index_file {
Subroutine _save_index_file: called CC=3 SCAR=11.0
219 1 my $class = shift;
220
221 # Save the object to a file.
222 1 my $OUT = new IO::File;
223 1 ▶ $OUT->open( ">$class->{_index}" ) or croak "could not write file $class->{_index}";
Branch: 1 branch
BranchTrueFalseTotal
$OUT->open(qq[>$class->{"_index"}]) or croak(qq[could not write file $class->{"_index"}]) 1 0 1
224 1 foreach my $report (sort {$a <=> $b} keys %{$class->{_id}}) {
225 41 my $line = sprintf "%-15s %-15s %-15s %-15s $class->{_id}->{$report}->{_name}", $report, $class->{_id}->{$report}->{_start}, $class->{_id}->{$report}->{_alignments}, $class->{_id}->{$report}->{_type};
226 41 $OUT->print("$line\n");
227 }
228 1 $OUT->close;
229
230 1 return($class);
231 }
232  
233  
234 # --------------------------------------------------------------------------
235 # L O A D I N D E X F I L E (internal method)
236 # ==========================================================================
237 # USAGE : BLASTaid->_load_index_file();
238 # PURPOSE : Populates an object from a saved index file.
239 # RETURNS : none
240 # PARAMETERS : none
241 # THROWS : croaks if index file cannot be opened
242 # : croaks if object entries are not complete
243 # : croaks if the first entry is not byte position 0
244 # --------------------------------------------------------------------------
245 1 ▶sub _load_index_file {
Subroutine _load_index_file: called CC=8 SCAR=22.8
246 1 my $class = shift;
247
248 # Load object from a file.
249 1 my $IN = new IO::File;
250 1 ▶ $IN->open( "$class->{_index}" ) or croak "could not open file $class->{_index}";
Branch: 1 branch
BranchTrueFalseTotal
$IN->open(qq[$class->{"_index"}]) or croak(qq[could not open file $class->{"_index"}]) 1 0 1
251 1 ▶ while(<$IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $IN)) 41 1 42
252 41 chomp;
253 41 my ($report, $start, $alignments, $type, $name) = split(/\s+/, $_);
254 41 $class->{_id}->{$report} = {
255 _start => $start,
256 _name => $name,
257 _type => $type,
258 _alignments => $alignments,
259 };
260 }
261
262 # Make sure that all entries have needed values. Revise the object
263 # to support the query names as unique keys.
264 0 ▶ if ($class->{_id}->{1}->{_start} != 0) { croak "check incoming INDEX format" }
Branch: 1 branch
BranchTrueFalseTotal
if ($class->{"_id"}{1}{"_start"} != 0) 0 1 1
265 1 foreach my $report (sort {$a <=> $b} keys %{$class->{_id}}) {
266 41 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $class->{'_id'}{$report}{'_name'} && defined $class->{'_id'}{$report}{'_start'} && defined $class->{'_id'}{$report}{'_type'}) { } 0 41 41
Condition33%2/6
defined $class->{'_id'}{$report}{'_name'} && defined $class->{'_id'}{$report}{'_start'} && defined $class->{'_id'}{$report}{'_type'}
!ll&&!rl&&r
0041
defined $class->{"_id"}{$report}{"_name"} and defined $class->{"_id"}{$report}{"_start"}
!ll&&!rl&&r
0041
MC/DC0%0/3
defined $class->{'_id'}{$report}{'_name'} && defined $class->{'_id'}{$report}{'_start'} && defined $class->{'_id'}{$report}{'_type'}
defined $class->{"_id"}{$report}{"_name"} defined $class->{"_id"}{$report}{"_start"} defined $class->{'_id'}{$report}{'_type'}
Truth table25%1/4
A and B && C
A= defined $class->{"_id"}{$report}{"_name"}B= defined $class->{"_id"}{$report}{"_start"}C= defined $class->{'_id'}{$report}{'_type'}
ABCresult
0XX0
10X0
1100
1111
267 !defined $class->{_id}->{$report}->{_name} ||
268 !defined $class->{_id}->{$report}->{_start} ||
269 !defined $class->{_id}->{$report}->{_type}
270 ) {
271 0 croak "missing QUERY NAME or START or TYPE for entry $report";
272 }
273 else {
274 $class->{_queries}->{_name}->{ $class->{_id}->{$report}->{_name} } = {
275 _start => $class->{_id}->{$report}->{_start},
276 _alignments => $class->{_id}->{$report}->{_alignments},
277 _type => $class->{_id}->{$report}->{_type},
278 41 };
279 }
280 }
281
282 1 return($class);
283 }
284  
285  
286 # --------------------------------------------------------------------------
287 # T Y P E (accessor method)
288 # ==========================================================================
289 # USAGE : BLASTaid->type( report => '' )
290 # PURPOSE : Returns the BLAST report type.
291 # RETURNS : Scalar: BLAST type name.
292 # PARAMETERS : report => ''
293 # THROWS : croaks if report attribute is missing
294 # : croaks if type is null in the index onject
295 # --------------------------------------------------------------------------
296 1 ▶sub type {
Subroutine type: called CC=3 SCAR=12.7
Pod documented
297 1 my ( $class, %arg ) = @_;
298
299 # Do some simple file validation checks.
300 0 ▶ if ( !$arg{report} ) { croak "new requires a REPORT value" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"report"}) 0 1 1
301  
302 # Validation & return:
303 1 ▶ if (defined $class->{_queries}->{_name}->{$arg{report}}->{_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class->{'_queries'}{'_name'}{$arg{'report'}}{'_type'}) { } 1 0 1
304 1 return( $class->{_queries}->{_name}->{$arg{report}}->{_type} );
305 }
306 else {
307 0 croak "type is null for report $arg{report}";
308 }
309
310 }
311  
312  
313 # --------------------------------------------------------------------------
314 # U N D E F (accessor method)
315 # ==========================================================================
316 # USAGE : BLASTaid->undef();
317 # PURPOSE : Deletes the object.
318 # RETURNS : Scalar: BLAST type name.
319 # PARAMETERS : none
320 # THROWS : none
321 # --------------------------------------------------------------------------
322 1 ▶sub undef {
Subroutine undef: called CC=1 SCAR=0.0
Pod documented
323 1 my $class = shift;
324
325 # Delete content from the object.
326 1 undef(%{$class});
327
328 1 return($class);
329 }
330  
331 2 1; # End of module.
332  
333 __END__
↑ Summary