Bio::DB::Universal: move into its own distribution
[bioperl-live.git] / Bio / Tools / Pseudowise.pm
bloba63eba2195caea0a4774592e0aa891c5759378fa
1 # BioPerl module for Bio::Tools::Pseudowise
3 #
4 # Copyright Jason Stajich, Fugu Team
6 # You may distribute this module under the same terms as perl itself
8 # POD documentation - main docs before the code
10 =head1 NAME
12 Bio::Tools::Pseudowise - Results of one Pseudowise run
14 =head1 SYNOPSIS
16 use Bio::Tools::Pseudowise;
18 my $parser = Bio::Tools::Pseudowise->new(-file=>"pw.out");
19 while(my $feat = $parser->next_result){
20 push @feat, $feat;
23 =head1 DESCRIPTION
25 Pseudowise is a pseudogene prediction program written by Ewan Birney
26 as part of the Wise Package. This module is the parser for the output
27 of the program.
29 http://www.sanger.ac.uk/software/wise2
31 =head1 FEEDBACK
33 =head2 Mailing Lists
35 User feedback is an integral part of the evolution of this and other
36 Bioperl modules. Send your comments and suggestions preferably to one
37 of the Bioperl mailing lists. Your participation is much appreciated.
39 bioperl-l@bioperl.org - General discussion
40 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
42 =head2 Support
44 Please direct usage questions or support issues to the mailing list:
46 I<bioperl-l@bioperl.org>
48 rather than to the module maintainer directly. Many experienced and
49 reponsive experts will be able look at the problem and quickly
50 address it. Please include a thorough description of the problem
51 with code and data examples if at all possible.
53 =head2 Reporting Bugs
55 Report bugs to the Bioperl bug tracking system to help us keep track
56 the bugs and their resolution. Bug reports can be submitted via the
57 web:
59 https://github.com/bioperl/bioperl-live/issues
61 =head1 AUTHOR - Jason Stajich
63 Previous committed by the Fugu Team
65 Re-written by Jason Stajich jason-at-bioperl-dot-org
67 =head1 APPENDIX
69 The rest of the documentation details each of the object methods.
70 Internal methods are usually preceded with a _
72 =cut
75 # Let the code begin...
78 package Bio::Tools::Pseudowise;
79 use strict;
80 use Symbol;
82 use Bio::Root::Root;
83 use Bio::SeqFeature::Generic;
84 use Bio::SeqFeature::Gene::Exon;
85 use Bio::SeqFeature::FeaturePair;
86 use Bio::SeqFeature::Gene::Transcript;
87 use Bio::SeqFeature::Gene::GeneStructure;
89 use base qw(Bio::Tools::AnalysisResult);
91 sub _initialize_state {
92 my ($self,@args) = @_;
94 # first call the inherited method!
95 $self->SUPER::_initialize_state(@args);
97 # our private state variables
98 $self->{'_preds_parsed'} = 0;
99 $self->{'_has_cds'} = 0;
100 # array of pre-parsed predictions
101 $self->{'_preds'} = [];
102 # seq stack
103 $self->{'_seqstack'} = [];
106 =head2 analysis_method
108 Usage : $pseudowise->analysis_method();
109 Purpose : Inherited method. Overridden to ensure that the name matches
110 /pseudowise/i.
111 Returns : String
112 Argument : n/a
114 =cut
116 #-------------
117 sub analysis_method {
118 #-------------
119 my ($self, $method) = @_;
120 if($method && ($method !~ /pseudowise/i)) {
121 $self->throw("method $method not supported in " . ref($self));
123 return $self->SUPER::analysis_method($method);
126 =head2 next_feature
128 Title : next_feature
129 Usage : $seqfeature = $obj->next_feature();
130 Function: Returns the next feature available in the analysis result, or
131 undef if there are no more features.
132 Example :
133 Returns : A Bio::SeqFeatureI implementing object, or undef if there are no
134 more features.
135 Args : none
137 See Also L<Bio::SeqFeatureI>
139 =cut
141 sub next_feature {
142 return shift->next_prediction(@_);
146 =head2 next_prediction
148 Title : next_prediction
149 Usage : while($gene = $pseudowise->next_prediction()) {
150 # do something
152 Function: Returns the gene of the Pseudowise result
153 file. Call this method repeatedly until FALSE is returned.
155 Example :
156 Returns : a Bio::SeqFeature::Generic
157 Args : none
159 See Also L<Bio::SeqFeature::Generic>
161 =cut
163 sub next_prediction {
164 my ($self) = @_;
165 # if the prediction section hasn't been parsed yet, we do this now
166 $self->_parse_predictions unless $self->_predictions_parsed;
168 # get next gene structure
169 return $self->_prediction();
172 =head2 _parse_predictions
174 Title : _parse_predictions()
175 Usage : $obj->_parse_predictions()
176 Function: Parses the prediction section. Automatically called by
177 next_prediction() if not yet done.
178 Example :
179 Returns :
181 =cut
183 sub _parse_predictions {
184 my ($self) = @_;
185 my $gene;
186 my @genes;
188 local $/= "\n";
189 local($_);
190 my %tags;
191 while (defined( $_ = $self->_readline)){
192 if( /^(Total codons|\S+)\s+:\s+(\S+)/ ) {
193 $tags{$1} = $2;
194 } elsif(m!^//! ) {
195 if( $gene ) {
196 $gene = undef;
197 %tags = ();
199 } elsif (/Gene\s+(\d+)\s*$/i) {
200 $gene = Bio::SeqFeature::Generic->new
201 ( -primary => 'pseudogene',
202 -source => 'pseudowise',
203 -tag => \%tags);
204 push @genes, $gene;
205 } elsif( /Gene\s+(\d+)\s+(\d+)/i ) {
206 if( $1 < $2 ) {
207 $gene->start($1);
208 $gene->end($2);
209 $gene->strand(1);
210 } else {
211 $gene->start($2);
212 $gene->end($1);
213 $gene->strand(-1);
215 } elsif (/Exon\s+(\d+)\s+(\d+)\s+phase\s+(\S+)/i) {
216 my ($s,$e,$st) = ($1,$2,1);
217 if( $s > $e) {
218 ($s,$e,$st)=($e,$s,-1);
220 my $exon = Bio::SeqFeature::Generic->new
221 ( -start => $s,
222 -end => $e,
223 -strand => $st,
224 -primary => 'exon',
225 -source => 'pseudowise',
226 -tag => {'frame' => $3});
227 $gene->add_sub_SeqFeature($exon);
230 $self->_add_prediction(\@genes);
231 $self->_predictions_parsed(1);
234 =head1 _prediction
236 Title : _prediction()
237 Usage : $gene = $obj->_prediction()
238 Function: internal
239 Example :
240 Returns :
242 =cut
244 sub _prediction {
245 my ($self) = @_;
246 return shift(@{$self->{'_preds'} || []});
249 =head2 _add_prediction
251 Title : _add_prediction()
252 Usage : $obj->_add_prediction($gene)
253 Function: internal
254 Example :
255 Returns :
257 =cut
259 sub _add_prediction {
260 my ($self, $gene) = @_;
261 $self->{'_preds'} ||= [];
263 if( ref($gene) =~ /ARRAY/ ) {
264 push(@{$self->{'_preds'}}, @$gene);
265 } else {
266 push(@{$self->{'_preds'}}, $gene);
270 =head2 _predictions_parsed
272 Title : _predictions_parsed
273 Usage : $obj->_predictions_parsed
274 Function: internal
275 Example :
276 Returns : TRUE or FALSE
278 =cut
280 sub _predictions_parsed {
281 my ($self, $val) = @_;
283 $self->{'_preds_parsed'} = $val if $val;
284 if(! exists($self->{'_preds_parsed'})) {
285 $self->{'_preds_parsed'} = 0;
287 return $self->{'_preds_parsed'};