Skip to content

Commit

Permalink
adding faceted browsing setup file
Browse files Browse the repository at this point in the history
  • Loading branch information
nsfox committed Jul 4, 2011
1 parent 709b585 commit 7319e90
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 13 deletions.
43 changes: 30 additions & 13 deletions bin/prepare-nested-structure.pl
Expand Up @@ -19,23 +19,13 @@
my @seqTracks;
my %definedTracks;
my @rootChildren = ('Favourites');
my %seq;

my $trackRel = "tracks";
my $trackDir = "$outdir/$trackRel";
mkdir($outdir) unless (-d $outdir);
mkdir($trackDir) unless (-d $trackDir);

my $config = JsonGenerator::readJSON($confFile);

eval "require $config->{db_adaptor}; 1" or die $@;


my $db = eval {$config->{db_adaptor}->new(%{$config->{db_args}})} or warn $@;
die "Could not open database: $@" unless $db;

$db->strict_bounds_checking(1) if $db->can('strict_bounds_checking');
$db->absolute(1) if $db->can('absolute');

my @refSeqs = @{JsonGenerator::readJSON("$outdir/refSeqs.js", [], 1)};
die "run prepare-refseqs.pl first to supply information about your reference sequences" if $#refSeqs < 0;
my @trackInfo = @{JsonGenerator::readJSON("$outdir/trackInfo.js", [], 1)};
Expand All @@ -50,6 +40,7 @@
$categories{'SequenceTrack'} = [$track->{"key"}];
}
$definedTracks{$track->{"key"}} = 2;
$seq{$track->{"key"}} = 1;
}
else {
$definedTracks{$track->{"key"}} = 1;
Expand All @@ -60,9 +51,20 @@
$definedTracks{'General'} = 3;
$definedTracks{'Favourites'} = 3;

if($confFile) {

my @tracks = @{$config->{tracks}};
my $config = JsonGenerator::readJSON($confFile);

eval "require $config->{db_adaptor}; 1" or die $@;


my $db = eval {$config->{db_adaptor}->new(%{$config->{db_args}})} or warn $@;
die "Could not open database: $@" unless $db;

$db->strict_bounds_checking(1) if $db->can('strict_bounds_checking');
$db->absolute(1) if $db->can('absolute');

my @tracks = @{$config->{tracks}};
foreach my $track (@tracks) {
my $group;
if(! $track->{"category"}) {
Expand All @@ -85,7 +87,22 @@
$categories{$group} = [$track->{"track"}];
}
$definedTracks{$track->{"track"}} = 2;
}
}
}
else {
foreach my $track (@trackInfo) {
my $group = "General";
if(!$seq{$track->{"key"}}) {
if($categories{$group}) {
push @{$categories{$group}}, $track->{"key"};
}
else {
$categories{$group} = [$track->{"key"}];
}
}
$definedTracks{$track->{"key"}} = 2;
}
}

foreach my $category (keys %categories) {
my @children_ref = ();
Expand Down
112 changes: 112 additions & 0 deletions bin/setup-faceted-browsing.pl
@@ -0,0 +1,112 @@
#!/usr/bin/env perl

use strict;
use warnings;

use FindBin qw($Bin);
use lib "$Bin/../lib";

use Getopt::Long;
use JsonGenerator;
use Data::Dumper;
use HTML::Template;

my $confFile;
my $outdir = "data";
GetOptions("conf=s" => \$confFile,
"out=s" => $outdir);

my %possible_track_info = ();
my %possible_track_info_options = ();
my @tracks;
my @facetes;
my @track_fields;

mkdir($outdir) unless (-d $outdir);

if($confFile) {

my $config = JsonGenerator::readJSON($confFile);

eval "require $config->{db_adaptor}; 1" or die $@;

@tracks = @{$config->{tracks}};

if($config->{facets}) {
@facetes = @{$config->{facets}};
}
if($config->{extra_track_info}) {
@track_fields = @{$config->{extra_track_info}};
}
}
else {
my @trackInfo = @{JsonGenerator::readJSON("$outdir/trackInfo.js", [], 1)};
die "run biodb-to-json.pl first to supply information about your track sequences" if $#trackInfo < 0;

@tracks = @trackInfo;
}

my @browsing_tracks;
foreach my $track (@tracks) {
foreach my $key (keys %$track) {
if($possible_track_info{$key}) {
$possible_track_info{$key} += 1;
}
else {
$possible_track_info{$key} = 1;
$possible_track_info_options{$key}{$track->{$key}} = 0;
}
$possible_track_info_options{$key}{$track->{$key}} += 1;
}

if($confFile) {
$track->{'label'} = $track->{'key'};
delete $track->{'key'};
}

if($confFile || ($track->{'type'} ne 'TrackGroup' && $track->{'type'} ne 'ROOT')) {
push @browsing_tracks, $track;
}
}
@tracks = @browsing_tracks;

JsonGenerator::writeJSON("$outdir/faceted_browsing.json",
{
'items' => \@tracks,
});

# create faceted_browser.html file for iframe to work

if(!@facetes || !@track_fields) {
my $num_tracks = @tracks;
my $populate_facetes = !@facetes;
my $populate_track_fields = !@track_fields;
foreach my $field (keys %possible_track_info) {
my $num_field_options = $possible_track_info_options{$field};
my $num_field_populated = $possible_track_info{$field};

my $max_times_used = 0;
foreach my $option (keys %{$possible_track_info_options{$field}}) {
my $times_used = $possible_track_info_options{$field}{$option};
if($times_used > $max_times_used) {
$max_times_used = $times_used;
}
}

if($populate_facetes && ($field ne 'type') && ($max_times_used >= 2) && ($num_field_options >= 2)) {
push @facetes, { field => $field , label => $field};
}
if($populate_track_fields && ($field ne 'track') && ($field ne 'type') && ($field ne 'key') && ($field ne 'label') && ($field ne 'url') && ($num_field_populated >= (int $num_tracks*0.5))) {
push @track_fields, { field_label => $field , field_name => $field};
}
}
}

my $template = HTML::Template->new(filename => 'faceted_browsing.tmpl');

$template->param( BROWSING_FACETES => \@facetes);
$template->param( TRACK_INFO_FIELDS => \@track_fields);

open(my $out_file, ">faceted_browsing.html");
print $out_file $template->output;

0 comments on commit 7319e90

Please sign in to comment.