sync w/ main trunk
[bioperl-live.git] / Bio / Tools / Geneid.pm
blob43b238cc71911bfb96da97b33d07a3c4d4ddf50f
1 # $Id$
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 # Cared for by Keith James
7 # Copyright Genome Research Ltd.
9 # You may distribute this module under the same terms as Perl itself
11 # POD documentation - main docs before the code
13 =head1 NAME
15 Bio::Tools::Geneid - Results of one geneid run
17 =head1 SYNOPSIS
19 use Bio::Tools::Geneid;
20 my $gid = Bio::Tools::Geneid(-file => "geneid.out");
22 while (my $gene = $gid->next_prediction)
24 my @transcripts = $gene->transcripts;
25 foreach my $t (@transcripts)
27 my @exons = $t->exons;
28 foreach my $e (@exons)
30 printf("Exon %d..%d\n", $e->start, $e->end);
35 =head1 DESCRIPTION
37 This is the parser for the output of geneid by Enrique Blanco and
38 Roderic Guigó (IMIM-UPF). See http://www1.imim.es/software/geneid. It
39 relies on native geneid output format internally and will work with
40 geneid versions 1.0 and 1.1. Currently this module supports only the
41 default mode of operation which is to predict exons and assemble an
42 optimal gene prediction.
44 It takes either a file handle or a file name and returns a
45 Bio::SeqFeature::Gene::GeneStructure object.
47 =head1 FEEDBACK
49 =head2 Mailing Lists
51 User feedback is an integral part of the evolution of this and other
52 Bioperl modules. Send your comments and suggestions preferably to one
53 of the Bioperl mailing lists. Your participation is much appreciated.
55 bioperl-l@bioperl.org - General discussion
56 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
58 =head2 Support
60 Please direct usage questions or support issues to the mailing list:
62 L<bioperl-l@bioperl.org>
64 rather than to the module maintainer directly. Many experienced and
65 reponsive experts will be able look at the problem and quickly
66 address it. Please include a thorough description of the problem
67 with code and data examples if at all possible.
69 =head2 Reporting Bugs
71 Report bugs to the Bioperl bug tracking system to help us keep track
72 the bugs and their resolution. Bug reports can be submitted via the
73 web:
75 http://bugzilla.open-bio.org/
77 =head1 AUTHOR - Keith James
79 Email: kdj@sanger.ac.uk
81 =head1 APPENDIX
83 The rest of the documentation details each of the object methods.
84 Internal methods are usually preceded with a _
86 =cut
88 package Bio::Tools::Geneid;
90 use vars qw($SOURCE_TAG);
91 use strict;
93 use Bio::Tools::AnalysisResult;
94 use Bio::SeqFeature::Generic;
95 use Bio::SeqFeature::Gene::Exon;
96 use Bio::SeqFeature::Gene::Transcript;
97 use Bio::SeqFeature::Gene::GeneStructure;
99 use base qw(Bio::Root::Root Bio::Root::IO);
100 $SOURCE_TAG = 'geneid';
102 =head2 new
104 Title : new
105 Usage : $obj->new(-file = "<geneid.out");
106 $obj->new(-fh => \*GI);
107 Function: Constructor for geneid wrapper. Takes either a file
108 : or filehandle
109 Returns : L<Bio::Tools::Geneid>
111 =cut
113 sub new
115 my($class, @args) = @_;
116 my $self = $class->SUPER::new(@args);
117 $self->_initialize_io(@args);
118 return $self;
121 =head2 next_prediction
123 Title : next_prediction
124 Usage : while($gene = $geneid->next_prediction)
126 # do something
128 Function: Returns the gene structure prediction of the geneid result
129 file. Call this method repeatedly until FALSE is returned.
130 Returns : A Bio::SeqFeature::Gene::GeneStructure object
131 Args : None
133 =cut
135 sub next_prediction
137 my ($self) = @_;
139 my ($gene, $transcript, $current_gene_id);
140 my $transcript_score = 0;
142 my ($gene_id, $exon_type, $exon_start, $exon_end, $exon_score,
143 $exon_strand, $start_phase, $end_phase, $start_sig_score,
144 $end_sig_score, $coding_pot_score, $homol_score);
146 while (defined($_ = $self->_readline))
148 $self->debug($_);
150 s/^\s+//;
151 s/\s+$//;
153 # We have a choice of geneid, gff or XML formats. The native
154 # geneid format has more information than gff. However, we
155 # then need to perform the hack of extracting the sequence ID
156 # from the header of the embedded Fasta file which comes after
157 # the exon data, as it is not stored elsewhere. Ack.
158 # the new version of geneID includes the sequence ID in a slightly
159 # different format and a new "or" statement was added below
160 # also removed "unless defined $self->_target_id;" inorder to continue
161 # generating new sequence IDs.
163 if (/^>(\S+)\|GeneId/ or /^# Sequence (\S+)/)
165 my $target_id = $1;
166 $self->_target_id($target_id);
167 next;
170 next unless (/(Single|First|Internal|Terminal)/);
172 my @fields = split(/\s+/, $_);
174 # Grab gene_id from eol first as there are issues with
175 # inconsistent whitespace in the AA coords field
176 $gene_id = pop @fields;
178 ($exon_type, $exon_start, $exon_end, $exon_score,
179 $exon_strand, $start_phase, $end_phase, $start_sig_score,
180 $end_sig_score, $coding_pot_score, $homol_score) = @fields[0..10];
182 if (! defined $current_gene_id)
184 # Starting the requested prediction
185 $current_gene_id = $gene_id;
186 $transcript_score = $exon_score;
188 $gene = Bio::SeqFeature::Gene::GeneStructure->new(-source =>
189 $SOURCE_TAG);
190 $transcript = Bio::SeqFeature::Gene::Transcript->new(-source =>
191 $SOURCE_TAG);
193 $self->_add_exon($gene, $transcript, $exon_type, $exon_start, $exon_end, $exon_score,
194 $exon_strand, $start_phase, $end_phase, $start_sig_score,
195 $end_sig_score, $coding_pot_score, $homol_score);
197 elsif ($gene_id eq $current_gene_id)
199 # Still in requested prediction
200 $transcript_score += $exon_score;
202 $self->_add_exon($gene, $transcript, $exon_type, $exon_start, $exon_end, $exon_score,
203 $exon_strand, $start_phase, $end_phase, $start_sig_score,
204 $end_sig_score, $coding_pot_score, $homol_score);
206 else
208 # Found following prediction
209 $self->_pushback($_);
210 last;
214 if (defined $gene)
216 $transcript->seq_id($self->_target_id);
217 $transcript->score($transcript_score);
218 $gene->add_transcript($transcript);
219 $gene->seq_id($self->_target_id);
221 foreach my $exon ($gene->exons)
223 $exon->seq_id($self->_target_id);
226 $self->_set_strand($gene);
229 return $gene;
232 =head2 _add_exon
234 Title : _add_exon
235 Usage : $obj->_add_exon($gene, $transcript, ... exon data ...)
236 Function: Adds a new exon to both gene and transcript from the data
237 : supplied as args
238 Example :
239 Returns : Nothing
241 =cut
243 sub _add_exon
245 my ($self, $gene, $transcript, $exon_type, $exon_start, $exon_end,
246 $exon_score, $exon_strand, $start_phase, $end_phase, $start_sig_score,
247 $end_sig_score, $coding_pot_score, $homol_score) = @_;
249 $exon_type =~ s/First/Initial/;
251 my $strand = $exon_strand eq '+' ? 1 : -1;
253 my $exon = Bio::SeqFeature::Gene::Exon->new(-source => $SOURCE_TAG,
254 -start => $exon_start,
255 -end => $exon_end,
256 -strand => $strand,
257 -score => $exon_score);
258 $exon->is_coding(1);
259 $exon->add_tag_value("Type", $exon_type);
260 $exon->add_tag_value('phase', $start_phase);
261 $exon->add_tag_value('end_phase', $end_phase);
262 $exon->add_tag_value('start_signal_score', $start_sig_score);
263 $exon->add_tag_value('end_signal_score', $end_sig_score);
264 $exon->add_tag_value('coding_potential_score', $coding_pot_score);
265 $exon->add_tag_value('homology_score', $homol_score);
267 $transcript->strand($strand) unless $transcript->strand != 0;
268 $transcript->add_exon($exon, $exon_type);
271 =head2 _set_strand
273 Title : _set_strand
274 Usage : $obj->_set_strand($gene)
275 Function: Sets the overall gene strand to the same strand as all
276 : the exons if they are all on the same strand, or to strand 0
277 : if the exons are on different strands.
278 Example :
279 Returns : Nothing
281 =cut
283 sub _set_strand
285 my ($self, $gene) = @_;
287 my $fwd = 0;
288 my $rev = 0;
290 my @exons = $gene->exons;
291 foreach my $exon (@exons)
293 my $strand = $exon->strand;
295 if ($strand == 1)
297 $fwd++;
299 elsif ($strand == -1)
301 $rev++;
305 if ($#exons == $fwd)
307 $gene->strand(1);
309 elsif ($#exons == $rev)
311 $gene->strand(-1);
313 else
315 $gene->strand(0);
318 return $gene;
321 =head2 _target_id
323 Title : _target_id
324 Usage : $obj->_target_id
325 Function: get/set for genomic sequence id
326 Example :
327 Returns : A target ID
329 =cut
331 sub _target_id
333 my ($self,$val) = @_;
334 if ($val)
336 $self->{'_target_id'} = $val;
339 return $self->{'_target_id'};