/
JB_json_v1.js
265 lines (232 loc) · 11.7 KB
/
JB_json_v1.js
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
define( [ 'dojo/_base/declare',
'dojo/_base/lang',
'dojo/_base/array',
'dojo/_base/json',
'dojo/request',
'JBrowse/Util',
'JBrowse/Digest/Crc32'
], function(
declare,
lang,
array,
json,
request,
Util,
digest
) {
var dojof = Util.dojof;
return declare('JBrowse.ConfigAdaptor.JB_json_v1',null,
/**
* @lends JBrowse.ConfigAdaptor.JB_json_v1.prototype
*/
{
/**
* Configuration adaptor for JBrowse JSON version 1 configuration
* files (formerly known as trackList.json files).
* @constructs
*/
constructor: function() {},
/**
* Load the configuration file from a URL.
*
* @param args.config.url {String} URL for fetching the config file.
*/
load: function( /**Object*/ args ) {
var that = this;
if( args.config.url ) {
var url = Util.resolveUrl( args.baseUrl || window.location.href, args.config.url );
return request( url, { handleAs: 'text', headers: {'X-Requested-With': null} })
.then( function( o ) {
o = that.parse_conf( o, args ) || {};
o.sourceUrl = url;
o = that.regularize_conf( o, args );
return o;
});
}
else if( args.config.data ) {
return Util.resolved( this.regularize_conf( args.config.data, args ) );
}
},
/**
* In this adaptor, just evals the conf text to parse the JSON, but
* other conf adaptors might want to inherit and override this.
* @param {String} conf_text the configuration text
* @param {Object} load_args the arguments that were passed to <code>load()</code>
* @returns {Object} the parsed JSON
*/
parse_conf: function( conf_text, load_args ) {
try {
return json.fromJson( conf_text );
} catch(e) {
throw e+" when parsing "+( load_args.config.url || 'configuration' )+".";
}
},
/**
* Applies defaults and any other necessary tweaks to the loaded JSON
* configuration. Called by <code>load()</code> on the JSON
* configuration before it calls the <code>onSuccess</code> callback.
* @param {Object} o the object containing the configuration, which it
* modifies in-place
* @param {Object} load_args the arguments that were passed to <code>load()</code>
* @returns the same object it was passed
*/
regularize_conf: function( o, load_args ) {
// if tracks is not an array, convert it to one
if( o.tracks && ! lang.isArray( o.tracks ) ) {
// if it's a single track config, wrap it in an arrayref
if( o.tracks.label ) {
o.tracks = [ o.tracks ];
}
// otherwise, coerce it to an array
else {
var tracks = [];
for( var label in o.tracks ) {
if( ! ( 'label' in o.tracks[label] ) )
o.tracks[label].label = label;
tracks.push( o.tracks[label] );
}
o.tracks = tracks;
}
}
// regularize trackMetadata.sources
var meta = o.trackMetadata;
if( meta && meta.sources ) {
// if it's a single source config, wrap it in an arrayref
if( meta.sources.url || ( typeof meta.sources == 'string' ) ) {
meta.sources = [ meta.sources ];
}
if( ! lang.isArray( meta.sources ) ) {
var sources = [];
for( var name in meta.sources ) {
if( ! ( 'name' in meta.sources ) )
meta.sources[name].name = name;
sources.push( meta.sources[name] );
}
meta.sources = sources;
}
// coerce any string source defs to be URLs, and try to detect their types
array.forEach( meta.sources, function( sourceDef, i ) {
if( typeof sourceDef == 'string' ) {
meta.sources[i] = { url: sourceDef };
var typeMatch = sourceDef.match( /\.(\w+)$/ );
if( typeMatch )
meta.sources[i].type = typeMatch[1].toLowerCase();
}
});
}
o.sourceUrl = o.sourceUrl || load_args.config.url;
o.baseUrl = o.baseUrl || Util.resolveUrl( o.sourceUrl, '.' );
if( o.baseUrl.length && ! /\/$/.test( o.baseUrl ) )
o.baseUrl += "/";
if( o.sourceUrl ) {
// set a default baseUrl in each of the track and store
// confs, and the names conf, if needed
var addBase =
[]
.concat( o.tracks || [] )
.concat( dojof.values(o.stores||{}) ) ;
if( o.names )
addBase.push( o.names );
array.forEach( addBase, function(t) {
if( ! t.baseUrl )
t.baseUrl = o.baseUrl || '/';
},this);
//resolve the refSeqs and nameUrl if present
if( o.refSeqs && typeof o.refSeqs == 'string' )
o.refSeqs = Util.resolveUrl( o.sourceUrl, o.refSeqs );
if( o.nameUrl )
o.nameUrl = Util.resolveUrl( o.sourceUrl, o.nameUrl );
}
o = this.regularizeTrackConfigs( o );
return o;
},
regularizeTrackConfigs: function( conf ) {
conf.stores = conf.stores || {};
array.forEach( conf.tracks || [], function( trackConfig ) {
// if there is a `config` subpart,
// just copy its keys in to the
// top-level config
if( trackConfig.config ) {
var c = trackConfig.config;
delete trackConfig.config;
for( var prop in c ) {
if( !(prop in trackConfig) && c.hasOwnProperty(prop) ) {
trackConfig[prop] = c[prop];
}
}
}
// skip if it's a new-style track def
if( trackConfig.store )
return;
var trackClassName = this._regularizeClass(
'JBrowse/View/Track', {
'FeatureTrack': 'JBrowse/View/Track/HTMLFeatures', 'ImageTrack': 'JBrowse/View/Track/FixedImage',
'ImageTrack.Wiggle': 'JBrowse/View/Track/FixedImage/Wiggle',
'SequenceTrack': 'JBrowse/View/Track/Sequence'
}[ trackConfig.type ]
|| trackConfig.type
);
trackConfig.type = trackClassName;
this._synthesizeTrackStoreConfig( conf, trackConfig );
if( trackConfig.histograms ) {
if( ! trackConfig.histograms.baseUrl )
trackConfig.histograms.baseUrl = trackConfig.baseUrl;
this._synthesizeTrackStoreConfig( conf, trackConfig.histograms );
}
}, this);
return conf;
},
_synthesizeTrackStoreConfig: function( mainconf, trackConfig ) {
// figure out what data store class to use with the track,
// applying some defaults if it is not explicit in the
// configuration
var urlTemplate = trackConfig.urlTemplate;
var storeClass = this._regularizeClass(
'JBrowse/Store',
trackConfig.storeClass ? trackConfig.storeClass :
/\/FixedImage/.test(trackConfig.type) ? 'JBrowse/Store/TiledImage/Fixed' +( trackConfig.backendVersion == 0 ? '_v0' : '' ) :
/\.jsonz?$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/NCList'+( trackConfig.backendVersion == 0 ? '_v0' : '' ) :
/\.bam$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/BAM' :
/\.vcf.gz$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/VCFTabix' :
/\.gff3?.gz$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/GFF3Tabix' :
/\.bed.gz$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/BEDTabix' :
/\.(bw|bigwig)$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/BigWig' :
/\.(bb|bigbed)$/i.test( urlTemplate ) ? 'JBrowse/Store/SeqFeature/BigBed' :
/\.(fa|fasta)$/i.test( urlTemplate ) ? 'JBrowse/Store/Sequence/IndexedFasta' :
/\.2bit$/i.test( urlTemplate ) ? 'JBrowse/Store/Sequence/TwoBit' :
/\/Sequence$/.test(trackConfig.type) ? 'JBrowse/Store/Sequence/StaticChunked' :
null
);
if( ! storeClass ) {
console.warn( "Unable to determine an appropriate data store to use with track '"
+ trackConfig.label + "', please explicitly specify a "
+ "storeClass in the configuration." );
return;
}
// synthesize a separate store conf
var storeConf = lang.mixin( {}, trackConfig );
lang.mixin( storeConf, {
type: storeClass
});
// if this is the first sequence store we see, and we
// have no refseqs store defined explicitly, make this the refseqs store.
if( (storeClass == 'JBrowse/Store/Sequence/StaticChunked' || storeClass == 'JBrowse/Store/Sequence/IndexedFasta' || storeClass == 'JBrowse/Store/Sequence/TwoBit' || trackConfig.useAsRefSeqStore) && !mainconf.stores['refseqs'] )
storeConf.name = 'refseqs';
else
storeConf.name = 'store'+digest.objectFingerprint( storeConf );
// record it
mainconf.stores[storeConf.name] = storeConf;
// connect it to the track conf
trackConfig.store = storeConf.name;
},
_regularizeClass: function( root, class_ ) {
if( ! class_ )
return null;
// prefix the class names with JBrowse/* if they contain no slashes
if( ! /\//.test( class_ ) )
class_ = root+'/'+class_;
class_ = class_.replace(/^\//);
return class_;
}
});
});