forked from wurmlab/afra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenomeDB.pm
376 lines (281 loc) · 10.1 KB
/
GenomeDB.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
=head1 NAME
GenomeDB - central "handle" for a directory tree of JBrowse JSON data
=head1 SYNOPSIS
my $gdb = GenomeDB->new( '/path/to/data/dir' );
my $track = $gdb->getTrack($tableName, $trackConfig, $track->{shortLabel} );
#returns an object for the track, e.g. a FeatureTrack
unless( defined $track ) {
$track = $gdb->createFeatureTrack( $trackLabel,
$trackConfig,
$track->{shortLabel} );
}
=head1 DESCRIPTION
Central "handle" on a directory tree of JBrowse JSON data, containing
accessors for accessing the tracks and (soon) reference sequences it
contains.
=head1 METHODS
=cut
package GenomeDB;
use strict;
use warnings;
use File::Spec;
use IO::File;
use Storable 'dclone';
use Hash::Merge ();
use JsonFileStorage;
use Bio::JBrowse::ConfigurationFile;
my $defaultTracklist = {
formatVersion => 1,
tracks => []
};
my $trackListPath = "trackList.json";
my @trackDirHeirarchy = ("tracks", "{tracklabel}", "{refseq}");
=head2 new( '/path/to/data/dir' )
Create a new handle for a data dir.
=cut
sub new {
my ($class, $dataDir) = @_;
my $self = {
dataDir => $dataDir,
rootStore => JsonFileStorage->new($dataDir, 0, {pretty => 1}),
trackDirTempl => File::Spec->join($dataDir, @trackDirHeirarchy),
trackUrlTempl => join("/", @trackDirHeirarchy)
};
bless $self, $class;
# drop a .htaccess file in the root of the data dir to apply CORS
# requests
{
my $f = File::Spec->catfile($dataDir,'.htaccess');
open my $ht, '>', $f
or die "$! writing $f";
$ht->print( $self->CORS_htaccess );
}
return $self;
}
=head2 writeTrackEntry( $track_object )
Record an entry for a new track in the data dir.
=cut
sub writeTrackEntry {
my ($self, $track) = @_;
my $setTrackEntry = sub {
my ($trackData) = @_;
unless (defined($trackData)) {
$trackData = $defaultTracklist;
}
# we want to add this track entry to the "tracks" list,
# replacing any existing entry with the same label,
# and preserving the original ordering
my $trackIndex;
my $trackList = $trackData->{tracks};
foreach my $index (0..$#{$trackList}) {
$trackIndex = $index
if ($trackList->[$index]->{label} eq $track->label);
}
$trackIndex = ($#{$trackList} + 1) unless defined($trackIndex);
$trackList->[$trackIndex] = {
%{ $track->config || {} },
type => $track->config->{trackType} || $track->type,
label => $track->label,
key => $track->key,
};
return $trackData;
};
$self->modifyTrackList( $setTrackEntry );
}
=head2 modifyTrackList( sub {} )
Modify the trackList.json file with the given subroutine.
=cut
sub modifyTrackList {
my ( $self, $sub ) = @_;
$self->{rootStore}->touch( 'tracks.conf' );
$self->{rootStore}->modify($trackListPath, $sub);
}
=head2 createFeatureTrack( $label, \%config, $key, $jsclass )
Create a new FeatureTrack object in this data dir with the given
label, config, key, and (JavaScript) class.
$jsclass is optional, and defaults to C<FeatureTrack>.
=cut
sub createFeatureTrack {
my $self = shift;
push( @_, 'FeatureTrack' ) if @_ < 4;
$self->_create_track( FeatureTrack => @_ );
}
=head2 createImageTrack( $label, \%config, $key, $jsclass )
Create a new ImageTrack object in this data dir with the given
label, config, key, and (JavaScript) class.
$jsclass is optional, and defaults to C<ImageTrack>.
=cut
sub createImageTrack {
my $self = shift;
push( @_, 'ImageTrack' ) if @_ < 4;
$self->_create_track( ImageTrack => @_ );
}
sub _create_track {
my ($self, $class, $trackLabel, $config, $key, $jsclass) = @_;
eval "require $class"; die $@ if $@;
(my $baseUrl = $self->{trackUrlTempl}) =~ s/\{tracklabel\}/$trackLabel/g;
return $class->new( $self->trackDir($trackLabel), $baseUrl,
$trackLabel, $config, $key, $jsclass );
}
=head2 getTrack( $trackLabel, $config, $key, $jsclass )
Get a track object (FeatureTrack or otherwise) from the GenomeDB. If
$config, $key, and/or $jsclass are provided, they are merged into and
override the existing settings for that track.
=cut
sub getTrack {
my ($self, $trackLabel, $config, $key, $jsclass ) = @_;
my $trackList = $self->{rootStore}->get($trackListPath,
$defaultTracklist);
my ( $trackDesc ) = my @selected =
grep { $_->{label} eq $trackLabel } @{$trackList->{tracks}};
return unless @selected;
# this should never happen
die "multiple tracks labeled $trackLabel" if @selected > 1;
# merge the $config into the trackdesc
if( $config ) {
$trackDesc = {
%$trackDesc,
%$config,
style => { %{$trackDesc->{style}||{}}, %{$config->{style}||{}} },
};
}
# merge the $key into the trackdesc
$trackDesc->{key} = $key if defined $key;
# merge the jsclass into the trackdesc
$trackDesc->{type} = $jsclass if defined $jsclass;
my $type = $trackDesc->{type};
$type =~ s/\./::/g;
$type =~ s/[^\w:]//g;
# make a list of perl packages to try, finding the most specific
# perl track class that matches the type in the JSON file. For
# example, ImageTrack.Wiggle.Frobnicated will try first to require
# ImageTrack::Wiggle::Frobnicated, then ImageTrack::Wiggle, then
# finally ImageTrack.
my @packages_to_try = ( $type );
while( $type =~ s/::[^:]+$// ) {
push @packages_to_try, $type;
}
for( @packages_to_try ) {
eval "require $_";
last unless $@;
}
die $@ if $@;
(my $baseUrl = $self->{trackUrlTempl}) =~ s/\{tracklabel\}/$trackLabel/g;
return $type->new( $self->trackDir($trackLabel),
$baseUrl,
$trackDesc->{label},
$trackDesc,
$trackDesc->{key},
);
}
# private method
# Get the data subdirectory for a given track, using its label.
sub trackDir {
my ($self, $trackLabel) = @_;
(my $result = $self->{trackDirTempl}) =~ s/\{tracklabel\}/$trackLabel/g;
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:
[
{
compress => 0,
feature => ["remark"],
style => { className => "feature2" },
track => "ExampleFeatures",
urlTemplate => "tracks/ExampleFeatures/{refseq}/trackData.json",
key => "Example Features",
label => "ExampleFeatures",
type => "FeatureTrack",
},
...
]
=cut
sub trackList {
my ( $self ) = @_;
my $json_tracks = $self->{rootStore}->get( 'trackList.json', { tracks => [] } )->{tracks};
my $conf_tracks = $self->_read_text_conf( 'tracks.conf' )->{tracks} || [];
return [ @$json_tracks, @$conf_tracks ];
}
sub _read_text_conf {
my ( $self, $path ) = @_;
$path = File::Spec->catfile( $self->{dataDir}, $path );
return Bio::JBrowse::ConfigurationFile->new( path => $path )->to_hashref;
}
=head2 CORS_htaccess
Static method to return a string to write into a .htaccess file that
will instruct Apache (if AllowOverride is on) to set the proper
"Access-Control-Allow-Origin *" headers on data files to enable
cross-origin data sharing.
=cut
sub CORS_htaccess {
my ( $self ) = @_;
my $class = ref $self || $self;
return <<EOA;
# This Apache .htaccess file is generated by JBrowse ($class) for
# allowing cross-origin requests as defined by the Cross-Origin
# Resource Sharing working draft from the W3C
# (http://www.w3.org/TR/cors/). In order for Apache to pay attention
# to this, it must have mod_headers enabled, and its AllowOverride
# configuration directive must allow FileInfo overrides.
<IfModule mod_headers.c>
Header onsuccess set Access-Control-Allow-Origin *
Header onsuccess set Access-Control-Allow-Headers X-Requested-With,Range
</IfModule>
EOA
}
=head2 precompression_htaccess( @precompressed_extensions )
Static method to return a string to write into a .htaccess file that
will instruct Apache (if AllowOverride is on) to set the proper
"Content-Encoding gzip" headers on precompressed files (.jsonz and
.txtz).
=cut
sub precompression_htaccess {
my ( $self, @extensions ) = @_;
my $re = '('.join('|',@extensions).')$';
$re =~ s/\./\\./g;
my $package = ref $self || $self;
return <<EOA;
# This Apache .htaccess file is generated by JBrowse ($package) for
# serving precompressed files (@extensions) with the proper
# Content-Encoding HTTP headers. In order for Apache to pay attention
# to this, its AllowOverride configuration directive for this
# filesystem location must allow FileInfo overrides.
<IfModule mod_gzip.c>
mod_gzip_item_exclude "$re"
</IfModule>
<IfModule setenvif.c>
SetEnvIf Request_URI "$re" no-gzip dont-vary
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "$re">
Header onsuccess set Content-Encoding gzip
</FilesMatch>
</IfModule>
EOA
}
1;
=head1 AUTHOR
Mitchell Skinner E<lt>[email protected]<gt>
Copyright (c) 2007-2011 The Evolutionary Software Foundation
This package and its accompanying libraries are free software; you can
redistribute it and/or modify it under the terms of the LGPL (either
version 2.1, or at your option, any later version) or the Artistic
License 2.0. Refer to LICENSE for the full license text.
=cut