Skip to content

Commit

Permalink
move refSeqs.json into the seq/ subdirectory, leaving just trackList.…
Browse files Browse the repository at this point in the history
…json in the top level of the data
  • Loading branch information
rbuels committed Feb 29, 2012
1 parent de19dbd commit cc4b505
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 70 deletions.
5 changes: 3 additions & 2 deletions bin/bam-to-json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ =head1 OPTIONS
$nclChunk *= 4 if $compress;
}

my @refSeqs = @{JSON::from_json( slurp("$outdir/refSeqs.json") || '[]' )}
my $gdb = GenomeDB->new( $outdir );

my @refSeqs = @{ $gdb->refSeqs }
or die "Run prepare-refseqs.pl to define reference sequences before running this script.\n";

my %config = (
Expand All @@ -127,7 +129,6 @@ =head1 OPTIONS
my $hdr = $bam->header;


my $gdb = GenomeDB->new( $outdir );
my $track = $gdb->getTrack( $trackLabel )
|| $gdb->createFeatureTrack( $trackLabel,
\%config,
Expand Down
4 changes: 2 additions & 2 deletions bin/biodb-to-json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ =head2 OPTIONS
$nclChunk *= 4 if $compress;
}

my $gdb = GenomeDB->new( $outdir );

# determine which reference sequences we'll be operating on
my @refSeqs = @{JsonGenerator::readJSON("$outdir/refSeqs.json", [], 1)};
my @refSeqs = @{ $gdb->refSeqs };
if (defined $refid) {
@refSeqs = grep { $_->{id} eq $refid } @refSeqs;
die "Didn't find a refseq with ID $refid (have you run prepare-refseqs.pl to supply information about your reference sequences?)" if $#refSeqs < 0;
Expand All @@ -125,7 +126,6 @@ =head2 OPTIONS
$db->strict_bounds_checking(1) if $db->can('strict_bounds_checking');
$db->absolute(1) if $db->can('absolute');

my $gdb = GenomeDB->new( $outdir );

foreach my $seg (@refSeqs) {
my $segName = $seg->{name};
Expand Down
11 changes: 5 additions & 6 deletions bin/flatfile-to-json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ =head2 BED-SPECIFIC

pod2usage( -verbose => 2 ) if $help;

my %refSeqs =
map {
$_->{name} => $_
} @{JsonGenerator::readJSON("$outdir/refSeqs.json", [], 1)};
my $gdb = GenomeDB->new( $outdir );

my %refSeqs = map { $_->{name} => $_ } @{ $gdb->refSeqs };

die "run prepare-refseqs.pl first to supply information about your reference sequences" unless (scalar keys %refSeqs);
scalar keys %refSeqs
or die "run prepare-refseqs.pl first to supply information about your reference sequences";

pod2usage( "Must provide a --tracklabel parameter." ) unless defined $trackLabel;
pod2usage( "You must supply either a --gff or --bed parameter." )
Expand Down Expand Up @@ -367,7 +367,6 @@ =head2 BED-SPECIFIC

################################

my $gdb = GenomeDB->new( $outdir );
my $track = $gdb->getTrack( $trackLabel )
|| $gdb->createFeatureTrack( $trackLabel,
\%config,
Expand Down
85 changes: 44 additions & 41 deletions bin/generate-names.pl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ =head1 OPTIONS
use lib catdir($Bin, updir(), "lib");

use LazyPatricia;
use JsonGenerator qw/ readJSON writeJSON /;
use GenomeDB;

my %trackHash;
my @tracksWithNames;
Expand All @@ -76,24 +76,12 @@ =head1 OPTIONS
OUTDIR
}

my $gdb = GenomeDB->new( $outDir );
my $nameDir = catdir($outDir, "names");
mkdir($nameDir) unless (-d $nameDir);

sub partitionCallback {
my ($subtreeRoot, $prefix, $thisChunk, $total) = @_;
# output subtree
writeJSON(catfile($nameDir, "lazy-$prefix.json"),
$subtreeRoot,
{pretty => 0});
printf STDERR "subtree for %15s has %10d in chunk, %10d total\n",
$prefix, $thisChunk, $total
if $verbose;
}

my @refSeqs = @{readJSON(catfile($outDir, "refSeqs.json"), [], 1)};
my $trackList = readJSON(catfile($outDir, "trackList.json"),
{"tracks" => []}, 1);
my @tracks = @{$trackList->{tracks}};
my @refSeqs = @{ $gdb->refSeqs };
my @tracks = @{ $gdb->trackList };

# open the root file; we lock this file while we're
# reading the name lists, deleting all the old lazy-*
Expand All @@ -106,59 +94,74 @@ sub partitionCallback {
# read the name list for each track that has one
my %nameHash;
my $trackNum = 0;
my $OLDSEP = $/;
undef $/;
foreach my $ref (@refSeqs) {
foreach my $track (@tracks) {
my $infile = catfile($outDir,
"tracks",
$track->{label},
$ref->{name},
"names.json");
my $infile = catfile( $outDir,
"tracks",
$track->{label},
$ref->{name},
"names.json");
next unless -e $infile;
open JSON, "<$infile"
or die "couldn't open $: $!";
my $names = JSON::from_json(<JSON>);
close JSON
or die "couldn't close $infile: $!";
foreach my $nameinfo (@$names) {
foreach my $alias (@{$nameinfo->[0]}) {

my $names = do {
local $/;
open my $j, '<', $infile or die "$! reading $infile";
JSON::from_json(<$j>);
};

foreach my $nameinfo ( @$names) {
foreach my $alias ( @{$nameinfo->[0]} ) {
my $track = $nameinfo->[1];
if (!defined($trackHash{$track})) {
unless( defined $trackHash{$track} ) {
$trackHash{$track} = $trackNum++;
push @tracksWithNames, $track;
}

push @{$nameHash{lc $alias}}, [$trackHash{$track},
@{$nameinfo}[2..$#{$nameinfo}]];
push @{$nameHash{lc $alias}}, [ $trackHash{$track},
@{$nameinfo}[2..$#{$nameinfo}]];
}
}
}
}
$/ = $OLDSEP;

# clear out old data
$root->seek(0, SEEK_SET);
$root->truncate(0);
my @lazyFiles = glob(catfile($nameDir, "lazy-*"));
if (@lazyFiles) {
if( my @lazyFiles = glob( catfile( $nameDir, "lazy-*" )) ) {
unlink @lazyFiles
or die "couldn't unlink name files: $!";
}

my $trie = LazyPatricia::create(\%nameHash);
my $trie = LazyPatricia::create( \%nameHash );
$trie->[0] = \@tracksWithNames;

my ($total, $thisChunk) =
LazyPatricia::partition($trie, "", $thresh, \&partitionCallback);
LazyPatricia::partition( $trie, "", $thresh, sub {
my ($subtreeRoot, $prefix, $thisChunk, $total) = @_;
# output subtree
writeJSON( catfile($nameDir, "lazy-$prefix.json"),
$subtreeRoot,
{pretty => 0} );
printf STDERR ( "subtree for %15s has %10d in chunk, %10d total\n",
$prefix, $thisChunk, $total )
if $verbose;
});

print STDERR "$total total names, with $thisChunk in the root chunk\n"
if $verbose;

# write the root
$root->print(JSON::to_json($trie, {pretty => 0}));
$root->close()
or die "couldn't close $rootFile: $!";
$root->print( JSON::to_json($trie, {pretty => 0}) );

exit;

############

sub writeJSON {
my ( $file, $contents, $opts ) = @_;
open my $f, '>', $file or die "$! writing $file";
print $f JSON::to_json( $contents, $opts || {} );
}


=head1 AUTHOR
Expand Down
2 changes: 1 addition & 1 deletion bin/prepare-refseqs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ =head1 OPTIONS
exit;
}

JsonGenerator::modifyJsonFile( catfile( $outDir, 'refSeqs.json' ),
JsonGenerator::modifyJsonFile( catfile( $outDir, 'seq', 'refSeqs.json' ),
sub {
#add new ref seqs while keeping the order
#of the existing ref seqs
Expand Down
1 change: 0 additions & 1 deletion bin/ucsc-to-json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ =head1 EXAMPLE
last;
}
}
# next unless defined($refSeqs{$curChrom});
my $jsonRow = $converter->($row, \%fields, $type);
$track->addSorted($jsonRow);
if (defined $nameCol) {
Expand Down
6 changes: 3 additions & 3 deletions bin/wig-to-json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ =head1 OPTIONS
pod2usage( -verbose => 2 ) if $help;
pod2usage( 'Must provide a --wig argument.' ) unless defined $path;

my @refSeqs = @{ JSON::from_json(do{ open my $f, '<', "$outdir/refSeqs.json"; local $/; <$f>}) || [] }
my $gdb = GenomeDB->new( $outdir );

my @refSeqs = @{ $gdb->refSeqs }
or die "Run prepare-refseqs.pl first to supply information about your reference sequences.\n";

$trackLabel = basename( $path ) unless defined $trackLabel;
my $urlTemplate = "tracks/$trackLabel/{refseq}/trackData.json";

my $gdb = GenomeDB->new( $outdir );

my %style = (
"key" => defined($key) ? $key : $trackLabel,
"urlTemplate" => $urlTemplate,
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
var dataRoot = queryParams.data || 'data';
var b = new Browser({
containerID: "GenomeBrowser",
refSeqs: {url: dataRoot + "/refSeqs.json"},
refSeqs: {url: dataRoot + "/seq/refSeqs.json"},
tracklists: [
{url: dataRoot + "/trackList.json"}
],
Expand Down
53 changes: 53 additions & 0 deletions lib/GenomeDB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,59 @@ sub trackDir {
return $result;
}

=head2 refSeqs
Returns a arrayref of hashrefs defining the reference sequences, as:
[ {
name => 'ctgB',
seqDir => 'seq/ctgB',
start => 0
end => 66,
length => 66,
seqChunkSize => 20000,
},
...
]
=cut

sub refSeqs {
shift->{rootStore}->get( 'seq/refSeqs.json', [] );
}


=head2 trackList
Return an arrayref of track definition hashrefs similar to:
[
{
config => {
autocomplete => "all",
compress => 0,
feature => ["remark"],
key => "Example Features",
style => { className => "feature2" },
track => "ExampleFeatures",
urlTemplate => "tracks/ExampleFeatures/{refseq}/trackData.json",
},
key => "Example Features",
label => "ExampleFeatures",
type => "FeatureTrack",
},
...
]
=cut

sub trackList {
shift->{rootStore}->get( 'trackList.json', { tracks => [] } )->{tracks}
}


1;

=head1 AUTHOR
Expand Down
16 changes: 3 additions & 13 deletions lib/ImageTrackRenderer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ sub new {
'datadir' => "data",
'trackdir' => "tracks",
'tiledir' => undef, #< ignored for backcompat
'refseqsfile' => 'refSeqs.json',
'refseqsfile' => undef,
'trackinfofile' => 'trackList.json',
'zooms' =>
[ 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000 ],
Expand Down Expand Up @@ -139,7 +139,7 @@ then adds the track to the data/trackList.json file.

sub render {
my ($self) = @_;
my @refSeqs = $self->refseqs
my @refSeqs = @{ $self->_genomedb->refSeqs }
or die "No reference sequences defined";

foreach my $seqInfo (@refSeqs) {
Expand Down Expand Up @@ -327,23 +327,13 @@ sub tilefilepath {
$self->_file( $self->datadir, $self->trackdir, $self->tilefile( @_ ));
}

sub refseqspath {
my ( $self ) = @_;
$self->_file( $self->datadir, $self->refseqsfile );
}
sub refseqs {
my ($self) = @_;
my $contents = eval { open my $f, '<',$self->refseqspath; local $/; <$f> };
return @{ JSON::from_json( $contents || '[]' ) };
}

######### read-only accessors

sub link { shift->{link} }
sub datadir { shift->{datadir} }
sub tracklabel { shift->{tracklabel} }
sub key { shift->{key} }
sub refseqsfile { shift->{refseqsfile} }
sub refseqsfile { undef } #< only for backcompat
sub trackdir { shift->{trackdir} }
sub tilewidth { shift->{tilewidth} }
sub zooms { shift->{zooms} }
Expand Down

0 comments on commit cc4b505

Please sign in to comment.