| 1 | package Bio::Graphics::Browser2::Plugin::TrackDumper; | ||
| 2 | # $Id: TrackDumper.pm,v 1.3 2009-01-30 22:06:19 lstein Exp $ | ||
| 3 | # test plugin | ||
| 4 | use strict; | ||
| 5 | use Bio::Graphics::Browser2::Plugin; | ||
| 6 | use Bio::Graphics::Browser2::TrackDumper; | ||
| 7 | use CGI qw(:standard *sup); | ||
| 8 | |||
| 9 | use vars '$VERSION','@ISA'; | ||
| 10 | $VERSION = '1.00'; | ||
| 11 | |||
| 12 | @ISA = qw/ Bio::Graphics::Browser2::Plugin /; | ||
| 13 | |||
| 14 | sub name { "Track Data" } | ||
| 15 | sub description { | ||
| 16 | p("The Track dumper plugin dumps out the currently selected tracks and their configuration in", | ||
| 17 | a({-href=>'http://www.sequenceontology.org/gff3.shtml'},'GFF Version 3 format.'), | ||
| 18 | "The information can be edited and then uploaded to this, or another GBrowse instance to create new tracks.", | ||
| 19 | "This plugin was written by Lincoln Stein & Sheldon McKay."); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub config_defaults { | ||
| 23 | my $self = shift; | ||
| 24 | return { | ||
| 25 | version => 3, | ||
| 26 | mode => 'selected', | ||
| 27 | disposition => 'save', | ||
| 28 | coords => 'absolute', | ||
| 29 | region => 'selected', | ||
| 30 | embed => 0, | ||
| 31 | print_config=> 1, | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub reconfigure { | ||
| 36 | my $self = shift; | ||
| 37 | my $current_config = $self->configuration; | ||
| 38 | my @keys = keys %{$self->config_defaults}; | ||
| 39 | foreach my $p ( @keys ) { | ||
| 40 | $current_config->{$p} = $self->config_param($p); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | sub configure_form { | ||
| 45 | my $self = shift; | ||
| 46 | my $current_config = $self->configuration; | ||
| 47 | my $html = p('Dump', | ||
| 48 | popup_menu(-name => $self->config_name('mode'), | ||
| 49 | -values => ['selected','all'], | ||
| 50 | -default => $current_config->{mode}, | ||
| 51 | -override => 1, | ||
| 52 | ), | ||
| 53 | ' features using GFF version', | ||
| 54 | popup_menu(-name => $self->config_name('version'), | ||
| 55 | -values => [2,2.5,3], | ||
| 56 | -labels => { 2 => '2', | ||
| 57 | 2.5 => '2.5*', | ||
| 58 | 3 => '3'}, | ||
| 59 | -default => $current_config->{version}, | ||
| 60 | -override => 1), | ||
| 61 | |||
| 62 | popup_menu(-name=>$self->config_name('region'), | ||
| 63 | -default=>$current_config->{region}, | ||
| 64 | -override=>1, | ||
| 65 | -values => ['selected','all'], | ||
| 66 | -labels=>{all => 'Across entire genome', | ||
| 67 | selected => 'Across currently visible region'}) | ||
| 68 | ); | ||
| 69 | |||
| 70 | autoEscape(0); | ||
| 71 | $html .= p( | ||
| 72 | radio_group(-name=>$self->config_name('disposition'), | ||
| 73 | -values => ['view','save','edit'], | ||
| 74 | -labels => {view => 'View', | ||
| 75 | save => 'Save to File', | ||
| 76 | edit => 'Edit'.sup('**'), | ||
| 77 | } | ||
| 78 | )); | ||
| 79 | $html .= p( | ||
| 80 | checkbox(-name=>$self->config_name('embed'), | ||
| 81 | -checked=>$current_config->{embed}, | ||
| 82 | -override=>1, | ||
| 83 | -label=>'Embed DNA sequence in the file') | ||
| 84 | ); | ||
| 85 | $html .= p( | ||
| 86 | checkbox(-name=>$self->config_name('print_config'), | ||
| 87 | -checked=>$current_config->{print_config}, | ||
| 88 | -override=>1, | ||
| 89 | -label=>'Include track configuration data') | ||
| 90 | ); | ||
| 91 | autoEscape(1); | ||
| 92 | |||
| 93 | my $href = a( {-href => 'javascript:void(0)', -onclick => "alert('" . | ||
| 94 | "\\'Target\\' syntax in the group field:\\n" . | ||
| 95 | "GFF2: Target class:name start stop\\n" . | ||
| 96 | "GFF2.5: Target class:name ; tstart start ; tstop stop\\n')"}, | ||
| 97 | "similarity target" ); | ||
| 98 | |||
| 99 | $html .= p(sup('*'), | ||
| 100 | "GFF2.5 is GFF2 with a special syntax for $href" | ||
| 101 | ) . | ||
| 102 | p(sup('**'), | ||
| 103 | "To edit, install a helper application for MIME type", | ||
| 104 | cite('application/x-gff2'),'or', | ||
| 105 | cite('application/x-gff3') | ||
| 106 | ); | ||
| 107 | $html; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub mime_type { | ||
| 111 | my $self = shift; | ||
| 112 | my $config = $self->configuration; | ||
| 113 | my $ps = $self->page_settings; | ||
| 114 | my $base = join '_',@{$ps}{qw(ref start stop)}; | ||
| 115 | my $gff = $config->{version} < 3 ? 'gff2' : 'gff3'; | ||
| 116 | return $config->{disposition} eq 'view' ? 'text/plain' | ||
| 117 | :$config->{disposition} eq 'save' ? ('application/octet-stream',"$base.$gff") | ||
| 118 | :$config->{disposition} eq 'edit' ? "application/x-${gff}" | ||
| 119 | :'text/plain'; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub dump { | ||
| 123 | my $self = shift; | ||
| 124 | my ($segment, @more_feature_sets) = @_; | ||
| 125 | my $conf = $self->browser_config; | ||
| 126 | my $page_settings = $self->page_settings; | ||
| 127 | my $config = $self->configuration; | ||
| 128 | my $version = $config->{version} || 3; | ||
| 129 | my $mode = $config->{mode} || 'selected'; | ||
| 130 | my $entire_genome = $config->{region} && $config->{region} eq 'all'; | ||
| 131 | my $db = $self->database; | ||
| 132 | my $whole_segment = $db->segment(Accession => $segment->seq_id) || | ||
| 133 | $db->segment($segment->seq_id); | ||
| 134 | my $coords = $config->{coords}; | ||
| 135 | my $embed = $config->{embed}; | ||
| 136 | |||
| 137 | my $thing_to_dump = $entire_genome ? $segment->db : $segment; | ||
| 138 | |||
| 139 | # safest thing to do is to use embedded logic | ||
| 140 | if ($version == 3 && $config->{print_config}) { | ||
| 141 | my $dumper = Bio::Graphics::Browser2::TrackDumper->new( | ||
| 142 | -data_source => $conf, | ||
| 143 | -id => $page_settings->{userid}, | ||
| 144 | -segment => $segment->seq_id.':'.$segment->start.'..'.$segment->end, | ||
| 145 | -labels => $mode eq 'selected' | ||
| 146 | ? [$self->selected_tracks] | ||
| 147 | : [] | ||
| 148 | ) or return; | ||
| 149 | $dumper->print_datafile(); | ||
| 150 | } | ||
| 151 | |||
| 152 | elsif ($config->{print_config}) { | ||
| 153 | Bio::Graphics::Browser2::TrackDumper->print_configuration | ||
| 154 | ($self->browser_config, | ||
| 155 | $mode eq 'selected' ? [$self->selected_tracks] : () | ||
| 156 | ); | ||
| 157 | $self->print_gff($thing_to_dump,@more_feature_sets); | ||
| 158 | } | ||
| 159 | |||
| 160 | else { | ||
| 161 | $self->print_gff($thing_to_dump,@more_feature_sets); | ||
| 162 | } | ||
| 163 | |||
| 164 | if ( $embed && !$entire_genome) { | ||
| 165 | my $dna = $segment->dna; | ||
| 166 | $dna =~ s/(\S{60})/$1\n/g; | ||
| 167 | print ">$segment\n$dna\n" if $dna; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | sub print_gff { | ||
| 172 | my $self = shift; | ||
| 173 | my ($segment, @more_feature_sets) = @_; | ||
| 174 | my $config = $self->configuration; | ||
| 175 | my $version = $config->{version} || 3; | ||
| 176 | my $mode = $config->{mode} || 'selected'; | ||
| 177 | |||
| 178 | my $date = localtime; | ||
| 179 | print "##gff-version $version\n"; | ||
| 180 | print "##date $date\n"; | ||
| 181 | eval {print "##sequence-region ",join(' ',$segment->ref,$segment->start,$segment->stop),"\n"}; | ||
| 182 | print "##source gbrowse GFFDumper plugin\n"; | ||
| 183 | print $mode eq 'selected' ? "##NOTE: Selected features dumped.\n" | ||
| 184 | : "##NOTE: All features dumped.\n"; | ||
| 185 | |||
| 186 | my @args; | ||
| 187 | if ($mode eq 'selected') { | ||
| 188 | my @feature_types = $self->selected_features; | ||
| 189 | @args = (-types => \@feature_types); | ||
| 190 | } | ||
| 191 | |||
| 192 | my @feats = (); | ||
| 193 | |||
| 194 | my $iterator = $segment->get_seq_stream(@args); | ||
| 195 | while ( my $f = $iterator->next_seq ) { | ||
| 196 | $self->print_feature($f,$version); | ||
| 197 | } | ||
| 198 | |||
| 199 | for my $set (@more_feature_sets) { | ||
| 200 | if ( $set->can('get_seq_stream') ) { | ||
| 201 | my @feats = (); | ||
| 202 | my $iterator = $set->get_seq_stream; | ||
| 203 | while ( my $f = $iterator->next_seq ) { | ||
| 204 | $self->print_feature($f); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | sub print_feature { | ||
| 211 | my $self = shift; | ||
| 212 | my ($f,$version) = @_; | ||
| 213 | $version ||= 3; | ||
| 214 | eval{$f->version($version)}; | ||
| 215 | my $s = $f->gff_string(1); # the flag is for GFF3 subfeature recursion | ||
| 216 | chomp $s; | ||
| 217 | print $s,"\n"; | ||
| 218 | return if $version >= 3; # gff3 recurses automatically | ||
| 219 | for my $ss ($f->sub_SeqFeature) { | ||
| 220 | # next if $ss eq $f; | ||
| 221 | my $s = $ss->gff_string; | ||
| 222 | print $s,"\n"; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | 1; |