Bio/Tools/Genscan.pm

filter: Statementstmt 95.6% 154 / 161 Branchbran 84.7% 61 / 72 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 90.4% 246 / 272 CC 41 SCAR 23.0
CC
41
Cov
91%
_parse_predictions
20.0
next_prediction
9.3
_read_fasta_seq
5.0
CRAP 10.0
« Bio/Annotation/Collection.pm ↑ Summary Bio/SeqFeature/Gene/Transcript.pm »
1 #
2 # BioPerl module for Bio::Tools::Genscan
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Hilmar Lapp <hlapp@gmx.net>
7 #
8 # Copyright Hilmar Lapp
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::Tools::Genscan - Results of one Genscan run
17  
18 =head1 SYNOPSIS
19  
20 use Bio::Tools::Genscan;
21  
22 $genscan = Bio::Tools::Genscan->new(-file => 'result.genscan');
23 # filehandle:
24 $genscan = Bio::Tools::Genscan->new( -fh => \*INPUT );
25  
26 # parse the results
27 # note: this class is-a Bio::Tools::AnalysisResult which implements
28 # Bio::SeqAnalysisParserI, i.e., $genscan->next_feature() is the same
29 while($gene = $genscan->next_prediction()) {
30 # $gene is an instance of Bio::Tools::Prediction::Gene, which inherits
31 # off Bio::SeqFeature::Gene::Transcript.
32 #
33 # $gene->exons() returns an array of
34 # Bio::Tools::Prediction::Exon objects
35 # all exons:
36 @exon_arr = $gene->exons();
37  
38 # initial exons only
39 @init_exons = $gene->exons('Initial');
40 # internal exons only
41 @intrl_exons = $gene->exons('Internal');
42 # terminal exons only
43 @term_exons = $gene->exons('Terminal');
44 # singleton exons:
45 ($single_exon) = $gene->exons();
46 }
47  
48 # essential if you gave a filename at initialization (otherwise the file
49 # will stay open)
50 $genscan->close();
51  
52 =head1 DESCRIPTION
53  
54 The Genscan module provides a parser for Genscan gene structure prediction
55 output. It parses one gene prediction into a Bio::SeqFeature::Gene::Transcript-
56 derived object.
57  
58 This module also implements the Bio::SeqAnalysisParserI interface, and thus
59 can be used wherever such an object fits. See L<Bio::SeqAnalysisParserI>.
60  
61 =head1 FEEDBACK
62  
63 =head2 Mailing Lists
64  
65 User feedback is an integral part of the evolution of this and other
66 Bioperl modules. Send your comments and suggestions preferably to one
67 of the Bioperl mailing lists. Your participation is much appreciated.
68  
69 bioperl-l@bioperl.org - General discussion
70 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
71  
72 =head2 Support
73  
74 Please direct usage questions or support issues to the mailing list:
75  
76 I<bioperl-l@bioperl.org>
77  
78 rather than to the module maintainer directly. Many experienced and
79 reponsive experts will be able look at the problem and quickly
80 address it. Please include a thorough description of the problem
81 with code and data examples if at all possible.
82  
83 =head2 Reporting Bugs
84  
85 Report bugs to the Bioperl bug tracking system to help us keep track
86 the bugs and their resolution. Bug reports can be submitted via the
87 web:
88  
89 https://github.com/bioperl/bioperl-live/issues
90  
91 =head1 AUTHOR - Hilmar Lapp
92  
93 Email hlapp@gmx.net
94  
95 =head1 APPENDIX
96  
97 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
98  
99 =cut
100  
101  
102 # Let the code begin...
103  
104  
105 package Bio::Tools::Genscan;
106 3 $Bio::Tools::Genscan::VERSION = '1.7.8';
107 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 3 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 3 ▶use Bio::Root::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 3 ▶use Bio::Tools::Prediction::Gene;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 3 ▶use Bio::Tools::Prediction::Exon;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 3 ▶use base qw(Bio::Tools::AnalysisResult);
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 3 my %ExonTags = ('Init' => 'Initial',
117 'Intr' => 'Internal',
118 'Term' => 'Terminal',
119 'Sngl' => '');
120
121 3 ▶sub _initialize_state {
Subroutine _initialize_state: called CC=1 SCAR=0.0
122 3 my ($self,@args) = @_;
123
124 # first call the inherited method!
125 3 $self->SUPER::_initialize_state(@args);
126  
127 # our private state variables
128 3 $self->{'_preds_parsed'} = 0;
129 3 $self->{'_has_cds'} = 0;
130 # array of pre-parsed predictions
131 3 $self->{'_preds'} = [];
132 # seq stack
133 3 $self->{'_seqstack'} = [];
134 }
135  
136 =head2 analysis_method
137  
138 Usage : $genscan->analysis_method();
139 Purpose : Inherited method. Overridden to ensure that the name matches
140 /genscan/i.
141 Returns : String
142 Argument : n/a
143  
144 =cut
145  
146 #-------------
147 3 ▶sub analysis_method {
Subroutine analysis_method: called CC=3 SCAR=13.3
Pod documented
148 #-------------
149 3 my ($self, $method) = @_;
150 3 ▶ if($method && ($method !~ /genscan/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method and not $method =~ /genscan/i) 0 3 3
Condition33%1/3
$method and not $method =~ /genscan/i
!ll&&!rl&&r
030
MC/DC0%0/2
$method and not $method =~ /genscan/i
$method not $method =~ /genscan/i
Truth table33%1/3
A and not(B)
A= $methodB= not $method =~ /genscan/i
ABresult
0X0
100
111
151 0 $self->throw("method $method not supported in " . ref($self));
152 }
153 3 return $self->SUPER::analysis_method($method);
154 }
155  
156 =head2 next_feature
157  
158 Title : next_feature
159 Usage : while($gene = $genscan->next_feature()) {
160 # do something
161 }
162 Function: Returns the next gene structure prediction of the Genscan result
163 file. Call this method repeatedly until FALSE is returned.
164  
165 The returned object is actually a SeqFeatureI implementing object.
166 This method is required for classes implementing the
167 SeqAnalysisParserI interface, and is merely an alias for
168 next_prediction() at present.
169  
170 Example :
171 Returns : A Bio::Tools::Prediction::Gene object.
172 Args :
173  
174 =cut
175  
176 4 ▶sub next_feature {
Subroutine next_feature: called CC=1 SCAR=0.0
Pod documented
177 4 my ($self,@args) = @_;
178 # even though next_prediction doesn't expect any args (and this method
179 # does neither), we pass on args in order to be prepared if this changes
180 # ever
181 4 return $self->next_prediction(@args);
182 }
183  
184 =head2 next_prediction
185  
186 Title : next_prediction
187 Usage : while($gene = $genscan->next_prediction()) {
188 # do something
189 }
190 Function: Returns the next gene structure prediction of the Genscan result
191 file. Call this method repeatedly until FALSE is returned.
192  
193 Example :
194 Returns : A Bio::Tools::Prediction::Gene object.
195 Args :
196  
197 =cut
198  
199 9 ▶sub next_prediction {
Subroutine next_prediction: called CC=9 SCAR=22.3
Pod documented
200 9 my ($self) = @_;
201 9 my $gene;
202  
203 # if the prediction section hasn't been parsed yet, we do this now
204 9 ▶ $self->_parse_predictions() unless $self->_predictions_parsed();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_predictions_parsed 3 6 9
205  
206 # get next gene structure
207 9 $gene = $self->_prediction();
208  
209 9 ▶ if($gene) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gene) 7 2 9
210 # fill in predicted protein, and if available the predicted CDS
211 #
212 7 my ($id, $seq);
213 # use the seq stack if there's a seq on it
214 7 my $seqobj = pop(@{$self->{'_seqstack'}});
215 7 ▶ if(! $seqobj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seqobj) 7 0 7
216 # otherwise read from input stream
217 7 ($id, $seq) = $self->_read_fasta_seq();
218 # there may be no sequence at all, or none any more
219 7 ▶ if($id && $seq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $seq) 6 1 7
Condition66%2/3
$id and $seq
!ll&&!rl&&r
106
MC/DC50%1/2
$id and $seq
$id $seq
Truth table66%2/3
A and B
A= $idB= $seq
ABresult
0X0
100
111
220 6 $seqobj = Bio::PrimarySeq->new('-seq' => $seq,
221 '-display_id' => $id,
222 '-alphabet' => "protein");
223 }
224 }
225 7 ▶ if($seqobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqobj) 6 1 7
226 # check that prediction number matches the prediction number
227 # indicated in the sequence id (there may be incomplete gene
228 # predictions that contain only signals with no associated protein
229 # and CDS, like promoters, poly-A sites etc)
230 6 $gene->primary_tag() =~ /[^0-9]([0-9]+)$/;
231 6 my $prednr = $1;
232 6 ▶ if($seqobj->display_id() !~ /_predicted_\w+_$prednr\|/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $seqobj->display_id =~ /_predicted_\w+_$prednr\|/) { } 0 6 6
233 # this is not our sequence, so push back for next prediction
234 0 push(@{$self->{'_seqstack'}}, $seqobj);
235 } else {
236 6 $gene->predicted_protein($seqobj);
237 # CDS prediction, too?
238 6 ▶ if($self->_has_cds()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_cds) 6 0 6
239 6 ($id, $seq) = $self->_read_fasta_seq();
240 6 $seqobj = Bio::PrimarySeq->new('-seq' => $seq,
241 '-display_id' => $id,
242 '-alphabet' => "dna");
243 6 $gene->predicted_cds($seqobj);
244 }
245 }
246 }
247 }
248  
249 9 return $gene;
250 }
251  
252 =head2 _parse_predictions
253  
254 Title : _parse_predictions()
255 Usage : $obj->_parse_predictions()
256 Function: Parses the prediction section. Automatically called by
257 next_prediction() if not yet done.
258 Example :
259 Returns :
260  
261 =cut
262  
263 3 ▶sub _parse_predictions {
Subroutine _parse_predictions: called CC=20 SCAR=30.0
264 3 my ($self) = @_;
265 3 my $gene;
266 my $seqname;
267  
268 3 ▶ while(defined($_ = $self->_readline())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 144 1 145
269 144 ▶ if(/^\s*(\d+)\.(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(\d+)\.(\d+)/) 85 59 144
270 # exon or signal
271 85 my $prednr = $1;
272 85 my $signalnr = $2; # not used presently
273 85 ▶ if(! defined($gene)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gene) 7 78 85
274 7 $gene = Bio::Tools::Prediction::Gene->new(
275 '-primary' => "GenePrediction$prednr",
276 '-source' => 'Genscan');
277 }
278 # split into fields
279 85 chomp();
280 85 my @flds = split(' ', $_);
281 # create the feature object depending on the type of signal
282 85 my $predobj;
283 85 my $is_exon = grep {$_ eq $flds[1];} (keys(%ExonTags));
284 85 ▶ if($is_exon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_exon) { } 74 11 85
285 74 $predobj = Bio::Tools::Prediction::Exon->new();
286 } else {
287 # PolyA site, or Promoter
288 11 $predobj = Bio::SeqFeature::Generic->new();
289 }
290 # set common fields
291 85 $predobj->source_tag('Genscan');
292 85 $predobj->score($flds[$#flds]);
293 85 ▶ $predobj->strand((($flds[2] eq '+') ? 1 : -1));
Branch: 1 branch
BranchTrueFalseTotal
$flds[2] eq '+' ? : 10 75 85
294 85 my ($start, $end) = @flds[(3,4)];
295 85 ▶ if($predobj->strand() == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predobj->strand == 1) { } 10 75 85
296 10 $predobj->start($start);
297 10 $predobj->end($end);
298 } else {
299 75 $predobj->end($start);
300 75 $predobj->start($end);
301 }
302 # add to gene structure (should be done only when start and end
303 # are set, in order to allow for proper expansion of the range)
304 85 ▶ if($is_exon) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_exon) { } 74 11 85
elsif ($flds[1] eq 'PlyA') { } 6 5 11
elsif ($flds[1] eq 'Prom') { } 5 0 5
305 # first, set fields unique to exons
306 74 $predobj->start_signal_score($flds[8]);
307 74 $predobj->end_signal_score($flds[9]);
308 74 $predobj->coding_signal_score($flds[10]);
309 74 $predobj->significance($flds[11]);
310 74 $predobj->primary_tag($ExonTags{$flds[1]} . 'Exon');
311 74 $predobj->is_coding(1);
312 # Figure out the frame of this exon. This is NOT the frame
313 # given by Genscan, which is the absolute frame of the base
314 # starting the first predicted complete codon. By comparing
315 # to the absolute frame of the first base we can compute the
316 # offset of the first complete codon to the first base of the
317 # exon, which determines the frame of the exon.
318 74 my $cod_offset;
319 74 ▶ if($predobj->strand() == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predobj->strand == 1) { } 6 68 74
320 6 $cod_offset = $flds[6] - (($predobj->start()-1) % 3);
321 # Possible values are -2, -1, 0, 1, 2. -1 and -2 correspond
322 # to offsets 2 and 1, resp. Offset 3 is the same as 0.
323 6 ▶ $cod_offset += 3 if($cod_offset < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $cod_offset < 1 6 0 6
324 } else {
325 # On the reverse strand the Genscan frame also refers to
326 # the first base of the first complete codon, but viewed
327 # from forward, which is the third base viewed from
328 # reverse.
329 68 $cod_offset = $flds[6] - (($predobj->end()-3) % 3);
330 # Possible values are -2, -1, 0, 1, 2. Due to the reverse
331 # situation, {2,-1} and {1,-2} correspond to offsets
332 # 1 and 2, resp. Offset 3 is the same as 0.
333 68 ▶ $cod_offset -= 3 if($cod_offset >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $cod_offset >= 0 56 12 68
334 68 $cod_offset = -$cod_offset;
335 }
336 # Offsets 2 and 1 correspond to frame 1 and 2 (frame of exon
337 # is the frame of the first base relative to the exon, or the
338 # number of bases the first codon is missing).
339 74 $predobj->frame(3 - $cod_offset);
340 # then add to gene structure object
341 74 $gene->add_exon($predobj, $ExonTags{$flds[1]});
342 } elsif($flds[1] eq 'PlyA') {
343 6 $predobj->primary_tag("PolyAsite");
344 6 $gene->poly_A_site($predobj);
345 } elsif($flds[1] eq 'Prom') {
346 5 $predobj->primary_tag("Promoter");
347 5 $gene->add_promoter($predobj);
348 }
349 85 next;
350 }
351 59 ▶ if(/^\s*$/ && defined($gene)) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/ and defined $gene) 7 52 59
Condition100%3/3
/^\s*$/ and defined $gene
!ll&&!rl&&r
27257
MC/DC100%2/2
/^\s*$/ and defined $gene
/^\s*$/ defined $gene
Truth table100%3/3
A and B
A= /^\s*$/B= defined $gene
ABresult
0X0
100
111
352 # current gene is completed
353 7 $gene->seq_id($seqname);
354 7 $self->_add_prediction($gene);
355 7 $gene = undef;
356 7 next;
357 }
358 52 ▶ if(/^(GENSCAN)\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(GENSCAN)\s+(\S+)/) 3 49 52
359 3 $self->analysis_method($1);
360 3 $self->analysis_method_version($2);
361 3 next;
362 }
363 49 ▶ if(/^Sequence\s+(\S+)\s*:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Sequence\s+(\S+)\s*:/) 3 46 49
364 3 $seqname = $1;
365 3 next;
366 }
367
368 46 ▶ if(/^Parameter matrix:\s+(\S+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Parameter matrix:\s+(\S+)/i) 3 43 46
369 3 $self->analysis_subject($1);
370 3 next;
371 }
372
373 43 ▶ if(/^Predicted coding/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Predicted coding/) 3 40 43
374 3 $self->_has_cds(1);
375 3 next;
376 }
377 40 ▶ /^>/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^>/ and do { $self->_pushback($_); last } 2 38 40
378 # section of predicted sequences
379 2 $self->_pushback($_);
380 2 last;
381 };
382 }
383 3 $self->_predictions_parsed(1);
384 }
385  
386 =head2 _prediction
387  
388 Title : _prediction()
389 Usage : $gene = $obj->_prediction()
390 Function: internal
391 Example :
392 Returns :
393  
394 =cut
395  
396 9 ▶sub _prediction {
Subroutine _prediction: called CC=3 SCAR=11.0
397 9 my ($self) = @_;
398  
399 9 ▶ return unless(exists($self->{'_preds'}) && @{$self->{'_preds'}});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_preds"} and @{$self->{"_preds"};} 2 7 9
Condition66%2/3
exists $self->{"_preds"} and @{$self->{"_preds"};}
!ll&&!rl&&r
027
MC/DC50%1/2
exists $self->{"_preds"} and @{$self->{"_preds"};}
exists $self->{"_preds"} @{$self->{"_preds"};}
Truth table66%2/3
A and B
A= exists $self->{"_preds"}B= @{$self->{"_preds"};}
ABresult
0X0
100
111
400 7 return shift(@{$self->{'_preds'}});
401 }
402  
403 =head2 _add_prediction
404  
405 Title : _add_prediction()
406 Usage : $obj->_add_prediction($gene)
407 Function: internal
408 Example :
409 Returns :
410  
411 =cut
412  
413 7 ▶sub _add_prediction {
Subroutine _add_prediction: called CC=2 SCAR=7.4
414 7 my ($self, $gene) = @_;
415  
416 7 ▶ if(! exists($self->{'_preds'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_preds"}) 0 7 7
417 0 $self->{'_preds'} = [];
418 }
419 7 push(@{$self->{'_preds'}}, $gene);
420 }
421  
422 =head2 _predictions_parsed
423  
424 Title : _predictions_parsed
425 Usage : $obj->_predictions_parsed
426 Function: internal
427 Example :
428 Returns : TRUE or FALSE
429  
430 =cut
431  
432 12 ▶sub _predictions_parsed {
Subroutine _predictions_parsed: called CC=3 SCAR=11.3
433 12 my ($self, $val) = @_;
434  
435 12 ▶ $self->{'_preds_parsed'} = $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 3 9 12
436 12 ▶ if(! exists($self->{'_preds_parsed'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_preds_parsed"}) 0 12 12
437 0 $self->{'_preds_parsed'} = 0;
438 }
439 12 return $self->{'_preds_parsed'};
440 }
441  
442 =head2 _has_cds
443  
444 Title : _has_cds()
445 Usage : $obj->_has_cds()
446 Function: Whether or not the result contains the predicted CDSs, too.
447 Example :
448 Returns : TRUE or FALSE
449  
450 =cut
451  
452 9 ▶sub _has_cds {
Subroutine _has_cds: called CC=3 SCAR=11.3
453 9 my ($self, $val) = @_;
454  
455 9 ▶ $self->{'_has_cds'} = $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 3 6 9
456 9 ▶ if(! exists($self->{'_has_cds'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_has_cds"}) 0 9 9
457 0 $self->{'_has_cds'} = 0;
458 }
459 9 return $self->{'_has_cds'};
460 }
461  
462 =head2 _read_fasta_seq
463  
464 Title : _read_fasta_seq()
465 Usage : ($id,$seqstr) = $obj->_read_fasta_seq();
466 Function: Simple but specialised FASTA format sequence reader. Uses
467 $self->_readline() to retrieve input, and is able to strip off
468 the traling description lines.
469 Example :
470 Returns : An array of two elements.
471  
472 =cut
473  
474 13 ▶sub _read_fasta_seq {
Subroutine _read_fasta_seq: called CC=5 SCAR=16.2
475 13 my ($self) = @_;
476 13 my ($id, $seq);
477 13 local $/ = ">";
478
479 13 my $entry = $self->_readline();
480 13 ▶ if($entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) 12 1 13
481 12 $entry =~ s/^>//;
482 # complete the entry if the first line came from a pushback buffer
483 12 ▶ while($entry !~ />$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry =~ />$/) 2 12 14
484 2 ▶ last unless $_ = $self->_readline();
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = $self->_readline 0 2 2
485 2 $entry .= $_;
486 }
487 # delete everything onwards from an intervening empty line (at the
488 # end there might be statistics stuff)
489 12 $entry =~ s/\n\n.*$//s;
490 # id and sequence
491 12 ▶ if($entry =~ /^(\S+)\n([^>]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /^(\S+)\n([^>]+)/) { } 12 0 12
492 12 $id = $1;
493 12 $seq = $2;
494 } else {
495 0 $self->throw("Can't parse Genscan predicted sequence entry");
496 }
497 12 $seq =~ s/\s//g; # Remove whitespace
498 }
499 13 return ($id, $seq);
500 }
501  
502 3 1;
« Bio/Annotation/Collection.pm ↑ Summary Bio/SeqFeature/Gene/Transcript.pm »