« blib/lib/Legacy/Graphics/Browser/Synteny.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/Render/HTML/TrackListing.pm »
| 1 | package Bio::Graphics::Browser2::Render::HTML::TrackListing::Categories; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'Bio::Graphics::Browser2::Render::HTML::TrackListing'; | ||
| 6 | |||
| 7 | use Bio::Graphics::Browser2::Shellwords; | ||
| 8 | use CGI qw(:standard); | ||
| 9 | use Carp 'croak'; | ||
| 10 | use constant DEBUG => 0; | ||
| 11 | |||
| 12 | sub render_track_listing { | ||
| 13 | my $self = shift; | ||
| 14 | |||
| 15 | my $settings = $self->settings; | ||
| 16 | my $source = $self->source; | ||
| 17 | my $render = $self->render; | ||
| 18 | |||
| 19 | # read category table information | ||
| 20 | my $category_table_labels = $self->category_table(); | ||
| 21 | my @labels = $render->potential_tracks; | ||
| 22 | |||
| 23 | warn "favorites = {$settings->{show_favorites}} " if DEBUG; | ||
| 24 | warn "active = {$settings->{active_only}} " if DEBUG; | ||
| 25 | |||
| 26 | if( $settings->{show_favorites}){ | ||
| 27 | warn "favorites = @labels = $settings->{show_favorites}" if DEBUG; | ||
| 28 | @labels= grep {$settings->{favorites}{$_}} @labels; | ||
| 29 | } | ||
| 30 | |||
| 31 | if ($settings->{active_only}) { | ||
| 32 | @labels = grep {$settings->{features}{$_}{visible}} @labels; | ||
| 33 | } | ||
| 34 | |||
| 35 | warn "label = @labels" if DEBUG; | ||
| 36 | |||
| 37 | my ($filter_active,@hilite); | ||
| 38 | if (my $filter = $render->track_filter_plugin) { | ||
| 39 | my $tracks = @labels; | ||
| 40 | eval {@labels = $filter->filter_tracks(\@labels,$source)}; | ||
| 41 | warn $@ if $@; | ||
| 42 | eval {@hilite = $filter->hilite_terms}; | ||
| 43 | warn $@ if $@; | ||
| 44 | $filter_active = @labels<$tracks; # mark filter active if the filter has changed the track count | ||
| 45 | } | ||
| 46 | |||
| 47 | $filter_active++ if $settings->{active_only} || $settings->{show_favorites}; | ||
| 48 | |||
| 49 | # for unknown reasons, replacing the below loop with "map" | ||
| 50 | # causes lots of undefined variable warnings. | ||
| 51 | my (%labels,%label_sort); | ||
| 52 | for my $l (@labels) { | ||
| 53 | $labels{$l} = $self->render_one_track($l,\@hilite); | ||
| 54 | $label_sort{$l} = $render->label2key($l); | ||
| 55 | } | ||
| 56 | |||
| 57 | my @defaults = grep {$settings->{features}{$_}{visible} } @labels; | ||
| 58 | |||
| 59 | # Sort the tracks into categories: | ||
| 60 | # Overview tracks | ||
| 61 | # Region tracks | ||
| 62 | # Regular tracks (which may be further categorized by user) | ||
| 63 | # Plugin tracks | ||
| 64 | # External tracks | ||
| 65 | my %track_groups; | ||
| 66 | foreach (@labels) { | ||
| 67 | my $category = $self->categorize_track($_); | ||
| 68 | push @{$track_groups{$category}},$_; | ||
| 69 | } | ||
| 70 | |||
| 71 | autoEscape(0); | ||
| 72 | |||
| 73 | # Get the list of all the categories needed. | ||
| 74 | my %exclude = map {$_=>1} map {$render->translate($_)} qw(OVERVIEW REGION ANALYSIS EXTERNAL); | ||
| 75 | |||
| 76 | (my $usertrack_cat = $render->translate('UPLOADED_TRACKS_CATEGORY')||'') =~ s/:.+$//; | ||
| 77 | $usertrack_cat ||= ''; | ||
| 78 | my @user_tracks = grep {/^$usertrack_cat/i} keys %track_groups; | ||
| 79 | $exclude{$_}++ foreach @user_tracks; | ||
| 80 | |||
| 81 | my @user_keys = grep {!$exclude{$_}} sort keys %track_groups; | ||
| 82 | |||
| 83 | my $all_on = $render->translate('ALL_ON'); | ||
| 84 | my $all_off = $render->translate('ALL_OFF'); | ||
| 85 | |||
| 86 | my (%seenit,%section_contents); | ||
| 87 | |||
| 88 | my @categories = (@user_keys, | ||
| 89 | $render->translate('OVERVIEW'), | ||
| 90 | $render->translate('REGION'), | ||
| 91 | $render->translate('ANALYSIS'), | ||
| 92 | ); | ||
| 93 | unshift @categories,@user_tracks if @user_tracks; | ||
| 94 | |||
| 95 | my $c_default = $source->category_default; | ||
| 96 | |||
| 97 | my @titles; # for sorting | ||
| 98 | |||
| 99 | # For each category, create the appropriately-nested node. "My Tracks" node positions comes from the track's config file. | ||
| 100 | my $usertracks = $render->user_tracks; | ||
| 101 | foreach my $category (@categories) { | ||
| 102 | next if $seenit{$category}++; | ||
| 103 | my $id = "${category}_section"; | ||
| 104 | my $category_title = (split m/(?<!\\):/,$category)[-1]; | ||
| 105 | $category_title =~ s!($_)!<span style="background-color:yellow">$1</span>!gi foreach @hilite; | ||
| 106 | |||
| 107 | my $file_id; | ||
| 108 | |||
| 109 | if ($category eq $render->translate('REGION') && !$render->setting('region segment')) { | ||
| 110 | next; | ||
| 111 | } | ||
| 112 | |||
| 113 | elsif (exists $track_groups{$category}) { | ||
| 114 | my @track_labels = @{$track_groups{$category}}; | ||
| 115 | |||
| 116 | $settings->{sk} ||= 'sorted'; # get rid of annoying warning | ||
| 117 | |||
| 118 | # if these tracks are in a grid, then don't sort them | ||
| 119 | BLOCK: { | ||
| 120 | no warnings; # kill annoying uninit warnings under modperl | ||
| 121 | @track_labels = sort {lc ($label_sort{$a}) cmp lc ($label_sort{$b})} @track_labels | ||
| 122 | if $settings->{sk} eq 'sorted' && !defined $category_table_labels->{$category}; | ||
| 123 | } | ||
| 124 | |||
| 125 | my $visible = $filter_active ? 1 | ||
| 126 | : exists $settings->{section_visible}{$id} ? $settings->{section_visible}{$id} | ||
| 127 | : $c_default; | ||
| 128 | |||
| 129 | my @entries = map {$labels{$_}} @track_labels; | ||
| 130 | my $table = $self->tableize(\@entries,$category,\@track_labels); | ||
| 131 | |||
| 132 | # Get the content for this track. | ||
| 133 | my ($control,$section)=$render->toggle_section({on=>$visible,nodiv => 1}, | ||
| 134 | $id, | ||
| 135 | b(ucfirst $category_title), | ||
| 136 | div({-style=>'padding-left:1em'}, | ||
| 137 | span({-id=>$id},$table))); | ||
| 138 | $control .= ' '.i({-class=>'nojs'}, | ||
| 139 | checkbox(-id=>"${id}_a",-name=>"${id}_a", | ||
| 140 | -label=>$all_on,-onClick=>"gbCheck(this,1);"), | ||
| 141 | checkbox(-id=>"${id}_n",-name=>"${id}_n", | ||
| 142 | -label=>$all_off,-onClick=>"gbCheck(this,0);") | ||
| 143 | )." ".span({-class => "list", | ||
| 144 | -id => "${id}_list", | ||
| 145 | -style => "display: " . ($visible? "none" : "inline") . ";"},"") | ||
| 146 | .br(); | ||
| 147 | $section_contents{$category} = div({-class=>'track_section'},$control.$section); | ||
| 148 | |||
| 149 | } | ||
| 150 | else { | ||
| 151 | next; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | autoEscape(1); | ||
| 156 | my $slice_and_dice = $self->indent_categories(\%section_contents,\@categories,$filter_active); | ||
| 157 | |||
| 158 | my $expand_all = ' ' .img({-class => 'clickable expand_all range_expand', | ||
| 159 | -src => $source->globals->button_url .'/open_open.png', | ||
| 160 | -onClick => "gbExpandAll(this,'range',event)"}); | ||
| 161 | return join( "\n", | ||
| 162 | start_form(-name=>'trackform', | ||
| 163 | -id=>'trackform'), | ||
| 164 | div({-class=>'searchbody',-id=> 'range', -style=>'padding-left:1em'},$expand_all,br(),$slice_and_dice), | ||
| 165 | end_form); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub tableize { | ||
| 169 | my $self = shift; | ||
| 170 | my ($keys,$category,$labels) = @_; | ||
| 171 | |||
| 172 | my $categorytable = $self->category_table(); | ||
| 173 | my (@row_labels,@column_labels); | ||
| 174 | if (defined $category and exists $categorytable->{$category} ) { | ||
| 175 | @row_labels = @{$categorytable->{$category}{col_labels}}; | ||
| 176 | @column_labels = @{$categorytable->{$category}{row_labels}}; | ||
| 177 | return $self->render->tableize($keys,undef,$labels,\@row_labels,\@column_labels); | ||
| 178 | } else { | ||
| 179 | return $self->render->tableize($keys,undef,$labels); | ||
| 180 | } | ||
| 181 | |||
| 182 | } | ||
| 183 | |||
| 184 | sub categorize_track { | ||
| 185 | my $self = shift; | ||
| 186 | my $label = shift; | ||
| 187 | |||
| 188 | my $render = $self->render; | ||
| 189 | my $user_labels = $render->get_usertrack_labels; | ||
| 190 | |||
| 191 | return $render->translate('OVERVIEW') if $label =~ /:overview$/; | ||
| 192 | return $render->translate('REGION') if $label =~ /:region$/; | ||
| 193 | return $render->translate('EXTERNAL') if $label =~ /^(http|ftp|file):/; | ||
| 194 | return $render->translate('ANALYSIS') if $label =~ /^plugin:/; | ||
| 195 | |||
| 196 | if ($user_labels->{$label}) { | ||
| 197 | my $cat = $render->user_tracks->is_mine($user_labels->{$label}) | ||
| 198 | ? $render->translate('UPLOADED_TRACKS_CATEGORY') | ||
| 199 | : $render->translate('SHARED_WITH_ME_CATEGORY'); | ||
| 200 | return "$cat:".$render->user_tracks->title($user_labels->{$label}); | ||
| 201 | } | ||
| 202 | |||
| 203 | my $category; | ||
| 204 | for my $l ($render->language->language) { | ||
| 205 | $category ||= $render->setting($label=>"category:$l"); | ||
| 206 | } | ||
| 207 | $category ||= $render->setting($label => 'category'); | ||
| 208 | $category ||= ''; # prevent uninit variable warnings | ||
| 209 | $category =~ s/^["']//; # get rid of leading quotes | ||
| 210 | $category =~ s/["']$//; # get rid of trailing quotes | ||
| 211 | return $category ||= $render->translate('GENERAL'); | ||
| 212 | } | ||
| 213 | |||
| 214 | # Category Table - This returns the hash of the category table. | ||
| 215 | sub category_table { | ||
| 216 | my $self = shift; | ||
| 217 | my $source = $self->source; | ||
| 218 | |||
| 219 | my $tabledata = $source->setting('category tables'); | ||
| 220 | my @tabledata = shellwords($tabledata||''); | ||
| 221 | my %categorytable=(); | ||
| 222 | while (@tabledata) { | ||
| 223 | my $category = shift(@tabledata); | ||
| 224 | my $rows = shift(@tabledata); | ||
| 225 | my @rows = split(/\s+/,$rows); | ||
| 226 | my $cols = shift(@tabledata); | ||
| 227 | my @cols = split(/\s+/,$cols); | ||
| 228 | $categorytable{$category}{row_labels}=\@rows; | ||
| 229 | $categorytable{$category}{col_labels}=\@cols; | ||
| 230 | } | ||
| 231 | |||
| 232 | return \%categorytable; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub indent_categories { | ||
| 236 | my $self = shift; | ||
| 237 | my ($contents,$categories,$force_open) = @_; | ||
| 238 | |||
| 239 | my $category_hash = {}; | ||
| 240 | my %sort_order; | ||
| 241 | my $sort_index = 0; | ||
| 242 | |||
| 243 | for my $category (@$categories) { | ||
| 244 | my $cont = $contents->{$category} || ''; | ||
| 245 | |||
| 246 | my @parts = map {s/\\//g; $_} split m/(?<!\\):/,$category; | ||
| 247 | $sort_order{$_} = $sort_index++ foreach @parts; | ||
| 248 | |||
| 249 | my $i = $category_hash; | ||
| 250 | |||
| 251 | # we need to add phony __next__ and __contents__ keys to avoid | ||
| 252 | # the case in which the track sections are placed at different | ||
| 253 | # levels of the tree, for instance | ||
| 254 | # "category=level1:level2" and "category=level1" | ||
| 255 | for my $index (0..$#parts) { | ||
| 256 | $i = $i->{__next__}{$parts[$index]} ||= {}; | ||
| 257 | $i->{__contents__} = $cont if $index == $#parts; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | my $i = 1; | ||
| 261 | my $nested_sections = $self->nest_toggles($category_hash,\%sort_order,$force_open); | ||
| 262 | } | ||
| 263 | |||
| 264 | # Nest Toggles - This turns the nested category/subcategory hashes into a prettily-indented tracks table. | ||
| 265 | sub nest_toggles { | ||
| 266 | my $self = shift; | ||
| 267 | my ($hash,$sort,$force_open) = @_; | ||
| 268 | |||
| 269 | my $settings = $self->settings; | ||
| 270 | my $source = $self->source; | ||
| 271 | my $render = $self->render; | ||
| 272 | |||
| 273 | my $result = ''; | ||
| 274 | my $default = $source->category_default; | ||
| 275 | |||
| 276 | for my $key (sort { | ||
| 277 | ($sort->{$a}||0)<=>($sort->{$b}||0) || $a cmp $b | ||
| 278 | } keys %$hash) { | ||
| 279 | if ($key eq '__contents__') { | ||
| 280 | $result .= $hash->{$key}."\n"; | ||
| 281 | } elsif ($key eq '__next__') { | ||
| 282 | $result .= $self->nest_toggles($hash->{$key},$sort,$force_open); | ||
| 283 | } elsif ($hash->{$key}{__next__}) { | ||
| 284 | my $id = "${key}_section"; | ||
| 285 | my $ea = ' ' . img({-class => "clickable expand_all ${id}_expand", | ||
| 286 | -src => $source->globals->button_url .'/open_open.png', | ||
| 287 | -onClick => "gbExpandAll(this,'$id',event)"}); | ||
| 288 | $settings->{section_visible}{$id} = $default unless exists $settings->{section_visible}{$id}; | ||
| 289 | $result .= $render->toggle_section({on=>$force_open||$settings->{section_visible}{$id}}, | ||
| 290 | $id, | ||
| 291 | b($key). | ||
| 292 | $ea. | ||
| 293 | span({-class => "list", | ||
| 294 | -id => "${id}_list"},''), | ||
| 295 | div({-style=>'margin-left:1.5em;margin-right:1em'}, | ||
| 296 | $self->nest_toggles($hash->{$key},$sort,$force_open))); | ||
| 297 | } else { | ||
| 298 | $result .= $self->nest_toggles($hash->{$key},$sort,$force_open); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | return $result; | ||
| 302 | } | ||
| 303 | |||
| 304 | 1; |