« blib/lib/Bio/Graphics/Browser2/DataLoader/bigwig.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/DataLoader.pm »
| 1 | # $Id: SequenceDumper.pm,v 1.18 2009-01-02 20:57:37 lstein Exp $ | ||
| 2 | # | ||
| 3 | # BioPerl module for Bio::Graphics::Browser2::Plugin::SequenceDumper | ||
| 4 | # | ||
| 5 | # Cared for by Jason Stajich <jason@bioperl.org> | ||
| 6 | # | ||
| 7 | # Copyright Jason Stajich and Cold Spring Harbor Laboratories 2002 | ||
| 8 | # | ||
| 9 | # You may distribute this module under the same terms as perl itself | ||
| 10 | |||
| 11 | # POD documentation - main docs before the code | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Bio::Graphics::Browser2::Plugin::SequenceDumper - A plugin for dumping sequences in various formats | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | Give standard usage here | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This is a plugin to the Generic Model Organism Database browse used by | ||
| 24 | Bio::Graphics::Browser to dump out an annotated region in a requested | ||
| 25 | flatfile format. Currently the feature formats are | ||
| 26 | |||
| 27 | =head1 FEEDBACK | ||
| 28 | |||
| 29 | See the GMOD website for information on bug submission http://www.gmod.org. | ||
| 30 | |||
| 31 | =head1 AUTHOR - Jason Stajich | ||
| 32 | |||
| 33 | Email jason@bioperl.org | ||
| 34 | |||
| 35 | Describe contact details here | ||
| 36 | |||
| 37 | =head1 CONTRIBUTORS | ||
| 38 | |||
| 39 | Additional contributors names and emails here | ||
| 40 | |||
| 41 | =head1 APPENDIX | ||
| 42 | |||
| 43 | The rest of the documentation details each of the object methods. | ||
| 44 | Internal methods are usually preceded with a _ | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | |||
| 49 | # Let the code begin... | ||
| 50 | |||
| 51 | package Bio::Graphics::Browser2::Plugin::SequenceDumper; | ||
| 52 | # $Id: SequenceDumper.pm,v 1.18 2009-01-02 20:57:37 lstein Exp $ | ||
| 53 | # Sequence Dumper plugin | ||
| 54 | |||
| 55 | use strict; | ||
| 56 | use Bio::Graphics::Browser2::Plugin; | ||
| 57 | use Bio::SeqFeature::Generic; | ||
| 58 | use Bio::SeqIO; | ||
| 59 | use Bio::Seq::RichSeq; | ||
| 60 | use Bio::Graphics::Browser2::TrackDumper::RichSeqMaker; | ||
| 61 | |||
| 62 | use CGI qw(:standard *pre); | ||
| 63 | use POSIX qw(strftime); | ||
| 64 | |||
| 65 | use vars qw($VERSION @ISA); | ||
| 66 | use constant DEBUG => 0; | ||
| 67 | |||
| 68 | # module label is xml? | ||
| 69 | my @FORMATS = ( 'fasta' => ['Fasta', undef], | ||
| 70 | 'genbank' => ['Genbank', undef], | ||
| 71 | 'embl' => ['EMBL', undef], | ||
| 72 | 'gcg' => ['GCG', undef], | ||
| 73 | 'raw' => ['Raw sequence', undef], | ||
| 74 | 'bsml' => ['BSML (XML)', 'xml'], | ||
| 75 | 'featurefasta'=>['Feature Fasta', undef], | ||
| 76 | 'gff' => ['GFF3', undef], | ||
| 77 | ); | ||
| 78 | |||
| 79 | # initialize @ORDER using the even-numbered elements of the array | ||
| 80 | # and grepping for those that load successfully (some of the | ||
| 81 | # modules depend on optional XML modules). | ||
| 82 | my @ORDER = grep { | ||
| 83 | my $module = "Bio::SeqIO::$_"; | ||
| 84 | warn "trying to load $module\n" if DEBUG; | ||
| 85 | eval "require $module; 1"; | ||
| 86 | } | ||
| 87 | grep { ! /gff/i } map { $FORMATS[2*$_] } (0..@FORMATS/2-1); | ||
| 88 | |||
| 89 | unshift @ORDER,'featurefasta'; | ||
| 90 | unshift @ORDER,'gff'; | ||
| 91 | |||
| 92 | # initialize %FORMATS and %LABELS from @FORMATS | ||
| 93 | my %FORMATS = @FORMATS; | ||
| 94 | my %LABELS = map { $_ => $FORMATS{$_}[0] } keys %FORMATS; | ||
| 95 | |||
| 96 | $VERSION = '0.14'; | ||
| 97 | |||
| 98 | @ISA = qw(Bio::Graphics::Browser2::Plugin); | ||
| 99 | |||
| 100 | sub name { "Sequence File" } | ||
| 101 | sub description { | ||
| 102 | p("The Sequence dumper plugin dumps out the currently displayed genomic segment", | ||
| 103 | "in the requested format."). | ||
| 104 | p("This plugin was written by Lincoln Stein and Jason Stajich."); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub dump { | ||
| 108 | my $self = shift; | ||
| 109 | my $segment = shift; | ||
| 110 | |||
| 111 | my $config = $self->configuration; | ||
| 112 | my $wantsorted = $config->{'wantsorted'} || 0; | ||
| 113 | my $browser = $self->browser_config(); | ||
| 114 | my @markup; | ||
| 115 | my %markuptype; | ||
| 116 | |||
| 117 | # special case for GFF dumping | ||
| 118 | if ($config->{'fileformat'} eq 'gff') { | ||
| 119 | $self->gff_dump($segment); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | # special case for feature fasta | ||
| 123 | if ($config->{'fileformat'} eq 'featurefasta') { | ||
| 124 | $self->feature_fasta_dump($segment); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | my @filter = $self->selected_features; | ||
| 128 | my $iterator = $segment->get_seq_stream(-types => \@filter); | ||
| 129 | my $seq = Bio::Graphics::Browser2::TrackDumper::RichSeqMaker->stream_to_rich_seq($segment,$iterator); | ||
| 130 | |||
| 131 | my $out = new Bio::SeqIO(-format => $config->{fileformat},-fh=>\*STDOUT); | ||
| 132 | my $mime_type = $self->mime_type; | ||
| 133 | if ($mime_type =~ /html/) { | ||
| 134 | print start_html($segment->desc),h1($segment->desc), start_pre; | ||
| 135 | $out->write_seq($seq); | ||
| 136 | print end_pre(); | ||
| 137 | print end_html; | ||
| 138 | } else { | ||
| 139 | $out->write_seq($seq); | ||
| 140 | } | ||
| 141 | undef $out; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub mime_type { | ||
| 145 | my $self = shift; | ||
| 146 | my $config = $self->configuration; | ||
| 147 | return 'text/plain' if $config->{format} eq 'text'; | ||
| 148 | return 'text/xml' if $config->{format} eq 'html' && $FORMATS{$config->{fileformat}}[1]; # this flag indicates xml | ||
| 149 | return 'text/html' if $config->{format} eq 'html'; | ||
| 150 | return wantarray ? ('application/octet-stream','dumped_region') : 'application/octet-stream' | ||
| 151 | if $config->{format} eq 'todisk'; | ||
| 152 | return 'text/plain'; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub config_defaults { | ||
| 156 | my $self = shift; | ||
| 157 | return { format => 'html', | ||
| 158 | fileformat => 'fasta', | ||
| 159 | wantsorted => 0, | ||
| 160 | }; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub reconfigure { | ||
| 164 | my $self = shift; | ||
| 165 | my $current_config = $self->configuration; | ||
| 166 | |||
| 167 | foreach my $param ( $self->config_param() ) { | ||
| 168 | $current_config->{$param} = $self->config_param($param); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | sub configure_form { | ||
| 173 | my $self = shift; | ||
| 174 | my $current_config = $self->configuration; | ||
| 175 | my @choices = TR({-class => 'searchtitle'}, | ||
| 176 | th({-align=>'RIGHT',-width=>'25%'},"Output", | ||
| 177 | td(radio_group(-name => $self->config_name('format'), | ||
| 178 | -values => [qw(text html todisk)], | ||
| 179 | -default => $current_config->{'format'}, | ||
| 180 | -labels => {html => 'html/xml', | ||
| 181 | 'todisk' => 'Save to Disk', | ||
| 182 | }, | ||
| 183 | -override => 1)))); | ||
| 184 | my $browser = $self->browser_config(); | ||
| 185 | # this to be fixed as more general | ||
| 186 | |||
| 187 | push @choices, TR({-class => 'searchtitle'}, | ||
| 188 | th({-align=>'RIGHT',-width=>'25%'},"Sequence File Format", | ||
| 189 | td(popup_menu('-name' => $self->config_name('fileformat'), | ||
| 190 | '-values' => \@ORDER, | ||
| 191 | '-labels' => \%LABELS, | ||
| 192 | '-default'=> $current_config->{'fileformat'} )))); | ||
| 193 | push @choices, TR({-class => 'searchtitle'}, | ||
| 194 | th({-align=>'RIGHT',-width=>'25%'}, | ||
| 195 | "Sorted SubLocations (for VectorNTI input of GenBank)", | ||
| 196 | td(popup_menu('-name' => $self->config_name('wantsorted'), | ||
| 197 | '-values' => [qw(0 1)], | ||
| 198 | '-labels' => { '0' => 'No', | ||
| 199 | '1' => 'Yes'}, | ||
| 200 | '-default'=> $current_config->{'wantsorted'} )))); | ||
| 201 | |||
| 202 | my $html= table(@choices); | ||
| 203 | $html; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub gff_dump { | ||
| 207 | my $self = shift; | ||
| 208 | my $segment = shift; | ||
| 209 | my $page_settings = $self->page_settings; | ||
| 210 | my $conf = $self->browser_config; | ||
| 211 | my $date = localtime; | ||
| 212 | |||
| 213 | my $mime_type = $self->mime_type; | ||
| 214 | my $html = $mime_type =~ /html/; | ||
| 215 | print start_html($segment) if $html; | ||
| 216 | |||
| 217 | print h1($segment),start_pre() if $html; | ||
| 218 | print "##gff-version 3\n"; | ||
| 219 | print "##date $date\n"; | ||
| 220 | print "##sequence-region ",join(' ',$segment->ref,$segment->start,$segment->stop),"\n"; | ||
| 221 | print "##source gbrowse SequenceDumper\n"; | ||
| 222 | print "##See http://www.sanger.ac.uk/Software/formats/GFF/\n"; | ||
| 223 | print "##NOTE: Selected features dumped.\n"; | ||
| 224 | my @feature_types = $self->selected_features; | ||
| 225 | $segment->absolute(0); | ||
| 226 | my $iterator = $segment->get_seq_stream(-types => \@feature_types) or return; | ||
| 227 | while (my $f = $iterator->next_seq) { | ||
| 228 | eval{$f->version(3)}; | ||
| 229 | my $s =$f->gff_string(1); | ||
| 230 | chomp($s); | ||
| 231 | print $s,"\n"; | ||
| 232 | } | ||
| 233 | |||
| 234 | print end_pre() if $html; | ||
| 235 | print end_html() if $html; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub feature_fasta_dump { | ||
| 239 | my $self = shift; | ||
| 240 | my $segment = shift; | ||
| 241 | my $page_settings = $self->page_settings; | ||
| 242 | my $conf = $self->browser_config; | ||
| 243 | my $date = localtime; | ||
| 244 | |||
| 245 | my $mime_type = $self->mime_type; | ||
| 246 | my $html = $mime_type =~ /html/; | ||
| 247 | print start_html($segment) if $html; | ||
| 248 | |||
| 249 | print h1($segment),start_pre() if $html; | ||
| 250 | print "##date $date\n"; | ||
| 251 | print "##source gbrowse SequenceDumper\n"; | ||
| 252 | print "##$segment\n"; | ||
| 253 | print "##NOTE: Selected features dumped.\n"; | ||
| 254 | my @feature_types = $self->selected_features; | ||
| 255 | $segment->absolute(0); | ||
| 256 | my $iterator = $segment->get_seq_stream(-types => \@feature_types) or return; | ||
| 257 | while (my $f = $iterator->next_seq) { | ||
| 258 | my $out = new Bio::SeqIO(-format =>'fasta',-fh=>\*STDOUT); | ||
| 259 | $out->write_seq($f->seq); | ||
| 260 | } | ||
| 261 | |||
| 262 | print end_pre() if $html; | ||
| 263 | print end_html() if $html; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | 1; |