« blib/conf/MobyServices/GbrowseServices.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/OptionPick.pm »
| 1 | package Bio::Graphics::Browser2::Plugin::BatchDumper; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Bio::Graphics::Browser2::Plugin; | ||
| 5 | use Bio::Seq::RichSeq; | ||
| 6 | use Bio::SeqIO; | ||
| 7 | use Bio::Seq; | ||
| 8 | use Bio::Graphics::Browser2::TrackDumper::RichSeqMaker; | ||
| 9 | use CGI qw(:standard *pre); | ||
| 10 | use POSIX; | ||
| 11 | use vars qw($VERSION @ISA); | ||
| 12 | use constant DEBUG => 0; | ||
| 13 | $VERSION = 1.0; | ||
| 14 | |||
| 15 | # module label is xml? | ||
| 16 | my @FORMATS = ( 'fasta' => ['Fasta', undef], | ||
| 17 | 'genbank' => ['Genbank', undef], | ||
| 18 | 'embl' => ['EMBL', undef], | ||
| 19 | 'gcg' => ['GCG', undef], | ||
| 20 | 'raw' => ['Raw sequence', undef], | ||
| 21 | 'bsml' => ['BSML (XML)', 'xml'], | ||
| 22 | 'gff' => ['GFF', undef], | ||
| 23 | 'gff3' => ['GFF3', undef], | ||
| 24 | ); | ||
| 25 | |||
| 26 | # initialize @ORDER using the even-numbered elements of the array | ||
| 27 | # and grepping for those that load successfully (some of the | ||
| 28 | # modules depend on optional XML modules). | ||
| 29 | my @ORDER = grep { | ||
| 30 | my $module = "Bio::SeqIO::$_"; | ||
| 31 | warn "trying to load $module\n" if DEBUG; | ||
| 32 | eval "require $module; 1"; | ||
| 33 | } | ||
| 34 | grep { ! /gff/i } map { $FORMATS[2*$_] } (0..@FORMATS/2-1); | ||
| 35 | |||
| 36 | unshift @ORDER,'gff','gff3'; | ||
| 37 | @ORDER = sort @ORDER; | ||
| 38 | |||
| 39 | # initialize %FORMATS and %LABELS from @FORMATS | ||
| 40 | my %FORMATS = @FORMATS; | ||
| 41 | my %LABELS = map { $_ => $FORMATS{$_}[0] } keys %FORMATS; | ||
| 42 | |||
| 43 | $VERSION = '0.20'; | ||
| 44 | |||
| 45 | @ISA = qw(Bio::Graphics::Browser2::Plugin); | ||
| 46 | |||
| 47 | sub name { "Sequence File" } | ||
| 48 | sub description { | ||
| 49 | p("The Sequence file plugin dumps out the currently displayed genomic segment", | ||
| 50 | "or the segments corresponding to the given accessions, in the requested format."). | ||
| 51 | p("This plugin was written by Lincoln Stein and Jason Stajich."); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub dump { | ||
| 55 | my $self = shift; | ||
| 56 | my $segment = shift; | ||
| 57 | my @more_feature_sets = @_; | ||
| 58 | |||
| 59 | my $browser = $self->browser_config; | ||
| 60 | my $config = $self->configuration; | ||
| 61 | my $wantsorted = $config->{'wantsorted'}; | ||
| 62 | |||
| 63 | my @segments = map { | ||
| 64 | ( $self->renderer->name2segments($_,$self->database) ) | ||
| 65 | } split /\s+/m, $config->{sequence_IDs}||''; | ||
| 66 | |||
| 67 | # take the original segment if no segments were found/entered via the sequence_IDs textarea field | ||
| 68 | @segments = $segment if $segment && !@segments; | ||
| 69 | |||
| 70 | my $mime_type = $self->mime_type; | ||
| 71 | |||
| 72 | unless (@segments) { | ||
| 73 | print start_html($self->name) if $mime_type =~ /html/; | ||
| 74 | print "No sequence specified.\n"; | ||
| 75 | print end_html if $mime_type =~ /html/; | ||
| 76 | exit 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | my @filter = $self->selected_features; | ||
| 80 | |||
| 81 | # special case for GFF dumping | ||
| 82 | if ($config->{fileformat} =~ /gff(3?)/) { | ||
| 83 | $self->gff_dump($1,@segments,@more_feature_sets); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | foreach my $segment ( @segments ) { | ||
| 88 | my $iterator = $segment->get_seq_stream(-types => \@filter); | ||
| 89 | my $seq = Bio::Graphics::Browser2::TrackDumper::RichSeqMaker->stream_to_rich_seq($segment,$iterator); | ||
| 90 | $seq->desc($segment->as_string); | ||
| 91 | $segment = $seq; | ||
| 92 | } | ||
| 93 | |||
| 94 | # for the external viewer (like VNTI) the best import format is genbank (?) | ||
| 95 | $config->{'fileformat'} = 'Genbank' if ($config->{'format'} eq 'external_viewer'); | ||
| 96 | |||
| 97 | my $flip = $config->{fileformat} =~ /^(fasta|raw)$/ && | ||
| 98 | (defined $config->{flip} ? $config->{flip} | ||
| 99 | : $self->page_settings->{flip}); | ||
| 100 | if ($flip) { | ||
| 101 | foreach (@segments) { | ||
| 102 | $_ = $_->revcom; | ||
| 103 | $_->desc($_->desc . " (reverse complemented)"); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | my $out = new Bio::SeqIO(-format => $config->{'fileformat'},-fh=>\*STDOUT); | ||
| 108 | if ($mime_type =~ /html/) { | ||
| 109 | print start_html('Batch Sequence'); | ||
| 110 | foreach my $segment (@segments) { | ||
| 111 | my $label = $segment->desc; | ||
| 112 | print h1($label),"\n", | ||
| 113 | start_pre(); | ||
| 114 | $out->write_seq($segment); | ||
| 115 | print end_pre(); | ||
| 116 | } | ||
| 117 | print end_html; | ||
| 118 | } else { | ||
| 119 | $out->write_seq($_) for @segments; | ||
| 120 | } | ||
| 121 | undef $out; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub mime_type { | ||
| 125 | my $self = shift; | ||
| 126 | my $config = $self->configuration; | ||
| 127 | return 'text/plain' if $config->{format} eq 'text'; | ||
| 128 | return 'text/xml' if $config->{format} eq 'html' && $FORMATS{$config->{fileformat}}[1]; # this flag indicates xml | ||
| 129 | return 'text/html' if $config->{format} eq 'html'; | ||
| 130 | return 'application/chemical-na' if $config->{format} eq 'external_viewer'; | ||
| 131 | return wantarray ? ('application/octet-stream','dumped_region') : 'application/octet-stream' | ||
| 132 | if $config->{format} eq 'todisk'; | ||
| 133 | return 'text/plain'; # default | ||
| 134 | } | ||
| 135 | |||
| 136 | sub config_defaults { | ||
| 137 | my $self = shift; | ||
| 138 | return { format => 'html', | ||
| 139 | fileformat => 'fasta', | ||
| 140 | wantsorted => 0, | ||
| 141 | }; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub reconfigure { | ||
| 145 | my $self = shift; | ||
| 146 | my $current_config = $self->configuration; | ||
| 147 | |||
| 148 | $current_config->{flip} = ''; | ||
| 149 | foreach my $p ( $self->config_param() ) { | ||
| 150 | $current_config->{$p} = $self->config_param($p); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | sub configure_form { | ||
| 155 | my $self = shift; | ||
| 156 | my $current_config = $self->configuration; | ||
| 157 | my @choices = TR({-class => 'searchtitle'}, | ||
| 158 | th({-align=>'RIGHT',-width=>'25%'},"Output", | ||
| 159 | td(radio_group('-name' => $self->config_name('format'), | ||
| 160 | '-values' => [qw(text html external_viewer todisk)], | ||
| 161 | '-default'=> $current_config->{'format'}, | ||
| 162 | -labels => {'html' => 'html/xml', | ||
| 163 | 'external_viewer' => 'GenBank Helper Application', | ||
| 164 | 'todisk' => 'Save to Disk', | ||
| 165 | }, | ||
| 166 | '-override' => 1)))); | ||
| 167 | my $browser = $self->browser_config(); | ||
| 168 | # this to be fixed as more general | ||
| 169 | |||
| 170 | push @choices, TR({-class => 'searchtitle'}, | ||
| 171 | th({-align=>'RIGHT',-width=>'25%'},"Sequence File Format", | ||
| 172 | td(popup_menu('-name' => $self->config_name('fileformat'), | ||
| 173 | '-values' => \@ORDER, | ||
| 174 | '-labels' => \%LABELS, | ||
| 175 | '-default'=> $current_config->{'fileformat'} )))); | ||
| 176 | |||
| 177 | push @choices,TR({-class=>'searchtitle'}, | ||
| 178 | th({-align=>'RIGHT',-width=>'25%'},"Orientation", | ||
| 179 | td(checkbox(-name => $self->config_name('flip'), | ||
| 180 | -label => 'Flip (Fasta and raw sequence only)', | ||
| 181 | -checked => $self->page_settings->{flip}, | ||
| 182 | -override => 1)))); | ||
| 183 | |||
| 184 | |||
| 185 | push @choices, TR({-class => 'searchtitle'}, | ||
| 186 | th({-align=>'RIGHT',-width=>'25%'}, | ||
| 187 | "Sorted SubLocations (for VectorNTI input of GenBank)", | ||
| 188 | td(popup_menu('-name' => $self->config_name('wantsorted'), | ||
| 189 | '-values' => [qw(0 1)], | ||
| 190 | '-labels' => { '0' => 'No', | ||
| 191 | '1' => 'Yes'}, | ||
| 192 | '-default'=> $current_config->{'wantsorted'} )))); | ||
| 193 | |||
| 194 | push @choices, TR({-class=>'searchtitle'}, | ||
| 195 | th({-align=>'RIGHT',-width=>'25%'},'Sequence IDs','<p><i>(Entry overrides chosen segment)</i></p>', | ||
| 196 | td(textarea(-name=>$self->config_name('sequence_IDs'), | ||
| 197 | -rows=>20, | ||
| 198 | -columns=>20, | ||
| 199 | )))); | ||
| 200 | |||
| 201 | my $html= table(@choices); | ||
| 202 | $html; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub gff_dump { | ||
| 206 | my $self = shift; | ||
| 207 | my ($gff3_flag,$segment,@extra) = @_; | ||
| 208 | my $page_settings = $self->page_settings; | ||
| 209 | my $conf = $self->browser_config; | ||
| 210 | my $date = localtime; | ||
| 211 | |||
| 212 | my $mime_type = $self->mime_type; | ||
| 213 | my $html = $mime_type =~ /html/; | ||
| 214 | print start_html($segment) if $html; | ||
| 215 | my @feature_types = $self->selected_features; | ||
| 216 | |||
| 217 | print h1($segment),start_pre() if $html; | ||
| 218 | print "##gff-version ",$gff3_flag || 2,"\n"; | ||
| 219 | print "##date $date\n"; | ||
| 220 | print "##sequence-region ",join(' ',$segment->ref,$segment->start,$segment->stop),"\n"; | ||
| 221 | print "##source gbrowse BatchDumper\n"; | ||
| 222 | print $gff3_flag ? "##See http://song.sourceforge.net/gff3.shtml\n" | ||
| 223 | : "##See http://www.sanger.ac.uk/Software/formats/GFF/\n"; | ||
| 224 | print "##NOTE: Selected features dumped.\n"; | ||
| 225 | my $iterator = $segment->get_seq_stream(-types=>\@feature_types) or return; | ||
| 226 | do_dump($gff3_flag,$iterator); | ||
| 227 | for my $set (@extra) { | ||
| 228 | do_dump($gff3_flag,$set->get_seq_stream) if $set->can('get_seq_stream'); | ||
| 229 | } | ||
| 230 | print end_pre() if $html; | ||
| 231 | print end_html() if $html; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub do_dump { | ||
| 235 | my $gff3 = shift; | ||
| 236 | my $iterator = shift; | ||
| 237 | while (my $f = $iterator->next_seq) { | ||
| 238 | eval {$f->version($gff3 || 2)}; | ||
| 239 | my $s = $f->gff_string(1); | ||
| 240 | chomp $s; | ||
| 241 | print "$s\n"; | ||
| 242 | for my $ss ($f->sub_SeqFeature) { | ||
| 243 | my $string = $ss->gff_string; | ||
| 244 | chomp $string; | ||
| 245 | print "$string\n"; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | 1; | ||
| 252 |