blib/conf/plugins/ProteinDumper.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 245 CC 23 SCAR 33.6
CC
23
Cov
0%
dump
72.0
mime_type
42.0
gff_dump
30.0
CRAP 28.9
1 # $Id: ProteinDumper.pm,v 1.5 2006-08-30 02:37:07 lstein Exp $
2 #
3 # BioPerl module for Bio::Graphics::Browser2::Plugin::ProteinDumper
4 #
5 # Cared for by Aaron Mackey <amackey@pcbi.upenn.edu>
6 #
7 # You may distribute this module under the same terms as perl itself
8  
9 # POD documentation - main docs before the code
10  
11 =head1 NAME
12  
13 Bio::Graphics::Browser2::Plugin::ProteinDumper - A plugin for dumping translated protein sequences in various formats
14  
15 =head1 SYNOPSIS
16  
17 Give standard usage here
18  
19 =head1 DESCRIPTION
20  
21 This is a plugin to the Generic Model Organism Database browse used by
22 Bio::Graphics::Browser to dump protein translations of genes from an
23 annotated region in the requested flatfile format. Currently the
24 feature formats are
25  
26 =head1 FEEDBACK
27  
28 See the GMOD website for information on bug submission http://www.gmod.org.
29  
30 =head1 AUTHOR - Aaron Mackey
31  
32 Email amackey@pcbi.upenn.edu
33  
34 =head1 CONTRIBUTORS
35  
36 Based on the SequenceDumper plugin written by Jason Stajich
37  
38 =head1 APPENDIX
39  
40 The rest of the documentation details each of the object methods.
41 Internal methods are usually preceded with a _
42  
43 =cut
44  
45  
46 # Let the code begin...
47  
48  
49 package Bio::Graphics::Browser2::Plugin::ProteinDumper;
50 # $Id: ProteinDumper.pm,v 1.5 2006-08-30 02:37:07 lstein Exp $
51 # Protein Dumper plugin
52  
53 use strict;
54 use Bio::Graphics::Browser2::Plugin;
55 use Bio::SeqIO;
56 use Bio::Tools::CodonTable;
57 use CGI qw(:standard *pre);
58 use vars qw($VERSION @ISA);
59 use constant DEBUG => 0;
60  
61 # module label is xml?
62 my @FORMATS = ( 'fasta' => ['Fasta', undef],
63 'genbank' => ['Genbank', undef],
64 'embl' => ['EMBL', undef],
65 'gcg' => ['GCG', undef],
66 'raw' => ['Raw sequence', undef],
67 'game' => ['GAME (XML)', 'xml'],
68 'bsml' => ['BSML (XML)', 'xml'],
69 );
70  
71 # initialize @ORDER using the even-numbered elements of the array
72 # and grepping for those that load successfully (some of the
73 # modules depend on optional XML modules).
74 my @ORDER = grep {
75 my $module = "Bio::SeqIO::$_";
76 warn "trying to load $module\n" if DEBUG;
77 eval "require $module; 1";
78 } grep { ! /gff/i } map { $FORMATS[2*$_] } (0..@FORMATS/2-1);
79  
80 # initialize %FORMATS and %LABELS from @FORMATS
81 my %FORMATS = @FORMATS;
82 my %LABELS = map { $_ => $FORMATS{$_}[0] } keys %FORMATS;
83  
84 $VERSION = '1.00';
85  
86 @ISA = qw(Bio::Graphics::Browser2::Plugin);
87  
88 sub name { "Protein Sequence File" }
89 sub description {
90  
91 p("The protein sequence dumper plugin dumps out translated protein
92 sequences of genes found in the currently displayed genomic segment
93 in the requested format.") .
94  
95 p("This plugin was originally written by Lincoln Stein and Jason
96 Stajich, modified by Aaron Mackey.");
97 }
98  
99 sub dump {
100 my $self = shift;
101 my $segment = shift;
102  
103 unless ($segment) {
104 my $mime_type = $self->mime_type;
105 print start_html($self->name) if $mime_type =~ /html/;
106 print "No sequence specified.\n";
107 print end_html if $mime_type =~ /html/;
108 CORE::exit 0;
109 }
110  
111 my $config = $self->configuration;
112  
113 my $ct = Bio::Tools::CodonTable->new;
114 $ct->id($config->{geneticcode});
115  
116 my @filter = $self->selected_features;
117 $segment->absolute(1);
118  
119 my @seqs;
120  
121 for my $f ($segment->features(-types => \@filter)) {
122 my @cds = $self->_collect_cds($f);
123 next unless @cds;
124  
125 my $cds = join("", map { $self->_get_dna($_) } @cds);
126 if ( (my $phase = $cds[0]->phase) > 0) {
127 # some genefinders will predict incomplete genes, wherein
128 # initial exons may not be in phase 0; in which case, we have to
129 # turn the first incomplete codon into NNN
130 substr($cds, 0, $phase, "NNN");
131 }
132
133 push @seqs, Bio::Seq->new(-display_id => $f->display_id,
134 -descr => $f->location->to_FTstring,
135 -seq => $ct->translate($cds)
136 );
137 }
138  
139 unless (@seqs) {
140 print "# no features with CDS parts found\n";
141 CORE::exit 0;
142 }
143  
144 my $out = new Bio::SeqIO(-format => $config->{fileformat},-fh=>\*STDOUT);
145 my $mime_type = $self->mime_type;
146 if ($mime_type =~ /html/) {
147 print start_html($segment->desc),h1($segment->desc), start_pre;
148 $out->write_seq(@seqs);
149 print end_pre();
150 print end_html;
151 } else {
152 $out->write_seq(@seqs);
153 }
154 undef $out;
155 }
156  
157 sub mime_type {
158 my $self = shift;
159 my $config = $self->configuration;
160  
161 return 'text/plain' if $config->{format} eq 'text';
162 return 'text/xml' if $config->{format} eq 'html' &&
163 $FORMATS{$config->{fileformat}}[1]; # this flag indicates xml
164 return 'text/html' if $config->{format} eq 'html';
165 return wantarray ? ('application/octet-stream','dumped_region')
166 : 'application/octet-stream'
167 if $config->{format} eq 'todisk';
168 return 'text/plain';
169 }
170  
171 sub config_defaults {
172 my $self = shift;
173 my $browser_config = $self->browser_config;
174  
175 # try to get the codon table to use
176 # first priority is the geneticcode or codontabe setting in the plugin config section
177 my $default_code = $browser_config->plugin_setting('geneticcode') || $browser_config->plugin_setting('codontable');
178  
179 # second priority is the setting in any "translation" track.
180 unless (defined $default_code) { # search config file for a translation track
181 for my $label ($browser_config->labels) {
182 next unless $browser_config->setting($label => 'glyph') eq 'translation';
183 $default_code ||= $browser_config->setting($label => 'geneticcode')
184 || $browser_config->setting($label => 'codontable');
185 last if $default_code;
186 }
187 }
188  
189 # last try, set to 1
190 $default_code ||= 1;
191  
192 return { format => 'html',
193 fileformat => 'fasta',
194 geneticcode => $default_code,
195 };
196 }
197  
198 sub reconfigure {
199 my $self = shift;
200 my $current_config = $self->configuration;
201  
202 foreach my $param ( $self->config_param() ) {
203 $current_config->{$param} = $self->config_param($param);
204 }
205 }
206  
207 sub configure_form {
208 my $self = shift;
209 my $current_config = $self->configuration;
210 my @choices = TR({-class => 'searchtitle'},
211 th({-align=>'RIGHT',-width=>'25%'},"Output",
212 td(radio_group(-name => $self->config_name('format'),
213 -values => [qw(text html todisk)],
214 -default => $current_config->{'format'},
215 -labels => {html => 'html/xml',
216 'todisk' => 'Save to Disk',
217 },
218 -override => 1,
219 )
220 )
221 )
222 );
223  
224 push @choices, TR({-class => 'searchtitle'},
225 th({-align=>'RIGHT',-width=>'25%'},"Sequence File Format",
226 td(popup_menu('-name' => $self->config_name('fileformat'),
227 '-values' => \@ORDER,
228 '-labels' => \%LABELS,
229 '-default'=> $current_config->{'fileformat'},
230 )
231 )
232 )
233 );
234  
235 push @choices, TR({-class => 'searchtitle'},
236 th({-align=>'RIGHT',-width=>'25%'},"Genetic Code",
237 td(popup_menu('-name' => $self->config_name('geneticcode'),
238 '-values' => [
239 grep {
240 $Bio::Tools::CodonTable::NAMES[$_-1]
241 } 1..@Bio::Tools::CodonTable::NAMES
242 ],
243 '-labels' => {
244 map {
245 ( $_ => $Bio::Tools::CodonTable::NAMES[$_-1] )
246 } grep {
247 $Bio::Tools::CodonTable::NAMES[$_-1]
248 } 1..@Bio::Tools::CodonTable::NAMES
249 },
250 '-default'=> $current_config->{'geneticcode'},
251 )
252 )
253 )
254 );
255  
256 my $html= table(@choices);
257 $html;
258 }
259  
260 sub gff_dump {
261 my $self = shift;
262 my $segment = shift;
263 my $page_settings = $self->page_settings;
264 my $conf = $self->browser_config;
265 my $date = localtime;
266  
267 my $mime_type = $self->mime_type;
268 my $html = $mime_type =~ /html/;
269 print start_html($segment) if $html;
270
271 print h1($segment),start_pre() if $html;
272 print "##gff-version 2\n";
273 print "##date $date\n";
274 print "##sequence-region ",join(' ',$segment->ref,$segment->start,$segment->end),"\n";
275 print "##source gbrowse SequenceDumper\n";
276 print "##See http://www.sanger.ac.uk/Software/formats/GFF/\n";
277 print "##NOTE: Selected features dumped.\n";
278 my @feature_types = $self->selected_features;
279 $segment->absolute(0);
280 my $iterator = $segment->get_seq_stream(-types => \@feature_types) or return;
281 while (my $f = $iterator->next_seq) {
282 print $f->gff_string,"\n";
283 for my $s ($f->sub_SeqFeature) {
284 print $s->gff_string,"\n";
285 }
286 }
287 print end_pre() if $html;
288 print end_html() if $html;
289 }
290  
291 sub _collect_cds {
292 my $self = shift;
293 my $feature = shift;
294 if ($feature->type =~ /^CDS/i) {return $feature};
295 my @sub = $feature->get_SeqFeatures;
296 return unless @sub;
297 return map {$self->_collect_cds($_)} @sub;
298 }
299  
300 sub _get_dna {
301 my $self = shift;
302 my $f = shift;
303 my $s = $f->seq;
304 return $s unless ref $s;
305 return $s->seq;
306 }
307  
308 1;