-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathipyigv.js
327 lines (274 loc) · 10.3 KB
/
ipyigv.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
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
const widgets = require('@jupyter-widgets/base');
var _ = require('lodash');
var igv = require('./igv.js')
// Import the CSS
import '../css/widget.css'
import { MODULE_NAME, MODULE_VERSION } from './version'
export class TrackModel extends widgets.WidgetModel {
defaults () {
return _.extend(super.defaults(), {
_model_name : 'TrackModel',
_view_name : 'TrackView',
_model_module : MODULE_NAME,
_view_module : MODULE_NAME,
_model_module_version : MODULE_VERSION,
_view_module_version : MODULE_VERSION,
});
};
// returns a dictionary representing a track which igv.js can take in its configuration
to_dict(include_empty = false) {
let keys = Object.keys(this.attributes).filter(k => !k.startsWith("_"))
// removing empty values unless specified otherwise
.filter(k=> (include_empty || !([null, undefined, ""].includes(this.get(k)))));
var dict = keys.reduce((result, key)=> ({...result, [key]:this.get(key)}), {});
if (keys.includes('roi')) {
dict['roi'] = dict['roi'].map(track => track.to_dict(include_empty));
};
console.log ("Finished generating dictionary from TrackModel", dict);
return dict ;
};
};
export class ReferenceGenomeModel extends widgets.WidgetModel {
defaults () {
return _.extend(super.defaults(), {
_model_name : 'ReferenceGenomeModel',
_view_name : 'ReferenceGenomeView',
_model_module : MODULE_NAME,
_view_module : MODULE_NAME,
_model_module_version : MODULE_VERSION,
_view_module_version : MODULE_VERSION,
});
};
// returns a dictionary representing a genome which igv.js can take in its configuration
to_dict(include_empty = false) {
let keys = Object.keys(this.attributes).filter(k => !k.startsWith("_"))
// removing empty values unless specified otherwise
.filter(k=> (include_empty || !([null, undefined, ""].includes(this.get(k)))));
var dict = keys.reduce((result, key)=> ({...result, [key]:this.get(key)}), {});
// special treatment for tracks
if (keys.includes('tracks')) {
let tracks = dict['tracks'];
dict['tracks'] = tracks.map(track=> track.to_dict(include_empty));
};
console.log ("Finished generating dictionary from ReferenceGenomeView", dict);
return dict ;
};
};
ReferenceGenomeModel.serializers = _.extend({
tracks: { deserialize: widgets.unpack_models }
},
widgets.WidgetModel.serializers
)
export class IgvModel extends widgets.DOMWidgetModel {
defaults () {
return _.extend(super.defaults(), {
_model_name : 'IgvModel',
_view_name : 'IgvBrowser',
_model_module : MODULE_NAME,
_view_module : MODULE_NAME,
_model_module_version : MODULE_VERSION,
_view_module_version : MODULE_VERSION,
});
};
initialize(attributes, options) {
super.initialize(attributes, options);
this.on("msg:custom", this.custom_message_handler);
};
custom_message_handler(msg) {
console.log('custom_message_handler in model', msg);
if (msg.type === 'dump_json') {
this.trigger('return_json');
}
else if (msg.type === 'search') {
var symbol = msg.symbol
this.trigger('search', symbol);
}
};
// returns a dictionary representing a genome which igv.js can take in its configuration
to_dict(include_empty = false) {
var keys = Object.keys(this.attributes).filter(k => !k.startsWith("_"))
// removing the layout attribute -- not related to igv
.filter(k=> (k!='layout'))
// removing empty values unless specified otherwise
.filter(k=> (include_empty || !([null, undefined, ""].includes(this.get(k)))));
var dict = keys.reduce((result, key)=> ({...result, [key]:this.get(key)}), {});
// special treatment for tracks
if (keys.includes('tracks')) {
let tracks = dict['tracks'];
dict['tracks'] = tracks.map(track=> track.to_dict(include_empty));
};
// special treatment for regions of interest
if (keys.includes('roi')) {
let roi = dict['roi'];
dict['roi'] = roi.map(roi=> roi.to_dict(include_empty));
};
// special treatment for genome
if (keys.includes('genome')) { // should always be the case
let genome = dict['genome'];
dict['genome'] = genome.to_dict(include_empty);
};
console.log ("Finished generating dictionary from IgvModel", dict);
return dict ;
};
};
IgvModel.serializers = _.extend({
genome: { deserialize: widgets.unpack_models },
tracks: { deserialize: widgets.unpack_models },
roi: { deserialize: widgets.unpack_models },
},
widgets.DOMWidgetModel.serializers
)
export class ReferenceGenomeView extends widgets.WidgetView {
render () {
super.render();
console.log("rendering ReferenceGenomeView");
}
}
export class TrackView extends widgets.WidgetView {
render () {
super.render();
console.log("rendering TrackView");
}
}
export class IgvBrowser extends widgets.DOMWidgetView {
initialize(options) {
super.initialize(options);
this.tracks_initialized = true;
this.browser = null;
this.track_views = new widgets.ViewList(this.add_track_view, this.remove_track_view, this);
console.log("configuring track_views");
this.track_views.update(this.model.get('tracks'));
this.tracks_initialized = true;
console.log("Done configuring track_views");
this.roi_views = new widgets.ViewList(this.add_roi_view, this.remove_roi_view, this);
console.log("configuring roi_views");
this.roi_views.update(this.model.get('roi'));
console.log("Done configuring roi_views")
}
render() {
super.render();
this.igvDiv = document.createElement("div");
var options = this.model.to_dict()
console.log("rendering browser", options);
this.browser = igv.createBrowser(this.igvDiv, options)
.then((browser) => {
console.log("Created IGV browser with options ", options);
browser.on('trackremoved', this.track_removed);
browser.on('trackdragend', this.track_dragged);
browser.on('locuschange', this.locus_changed);
browser.on('trackclick', this.track_clicked);
return browser;
});
this.el.appendChild(this.igvDiv);
this.listenTo(this.model, 'change:genome', this.update_genome);
this.listenTo(this.model, 'change:tracks', this.update_tracks);
this.listenTo(this.model, 'change:roi', this.update_roi);
this.listenTo(this.model, "return_json", this._return_json);
this.listenTo(this.model, "search", this._search);
}
update_genome () {
var genome = this.model.get('genome');
console.log('Updating browser reference with ', genome);
this.browser.then((b) => {
b.loadGenome(genome);
});
}
update_tracks () {
console.log("update_tracks")
if (this.tracks_initialized) {
var tracks = this.model.get('tracks');
console.log('Updating tracks_views with ', tracks);
this.track_views.update(tracks);
}
else {
console.log ("Tracks not yet initialized - skipping");
}
}
add_track_view (child_model) {
return this.create_child_view(child_model, {}).then(view => {
console.log('add_track_view with child :', child_model);
if (this.tracks_initialized) {
return this.browser.then((browser) => {
return browser.loadTrack(child_model.attributes).then((newTrack) => {
console.log("new track loaded in browser: " , newTrack);
view.igvTrack = newTrack
return view;
});
});
} else {
console.log("track_view not yet initialized, skipping");
return view;
}
});
}
remove_track_view (child_view) {
console.log('removing Track from genome', child_view.igvTrack);
if (!this.tracks_initialized) {
console.log("track_view not yet initialized, skipping");
return;
}
var child = child_view
this.browser.then(b => {
//b.removeTrackByName(child_view.model.get("name"));
if (child.igvTrack){
b.removeTrack(child.igvTrack);
}
else {
b.removeTrackByName(child.model.get("name"));
}
});
}
update_roi () {
console.log("update_roi")
var roi = this.model.get('roi');
console.log('Updating roi_views with ', roi);
// the browser lets us only add ROI (one or several), or delete all ROI. (no unitary delete)
if (roi == []) {
console.log('removing Regions of Interest');
this.browser.then(b => { b.clearROIs(); });
}
else {
this.roi_views.update(roi);
}
}
add_roi_view (child_model) {
return this.create_child_view(child_model, {}).then(view => {
console.log('add_roi_view with child view :', view);
return this.browser.then((browser) => {
return browser.loadROI(view.model.attributes).then((newROI) => {
console.log("new roi loaded in browser: " , newROI);
return view;
});
});
});
}
remove_all_roi_view (child_view) {
console.log('Oops - removing one Region of Interest not supported - Ignoring');
}
track_removed (tracks) {
console.log('track removed', tracks);
}
track_dragged (arg) {
console.log('track dragged', arg);
}
locus_changed (referenceFrame, label) {
console.log('locus changed', referenceFrame, label);
}
track_clicked (track, popoverData) {
console.log('track clicked', track, popoverData);
}
_return_json(event) {
console.log('view._return_json', event);
this.browser.then((browser)=> {
var json = browser.toJSON()
this.send({ event: 'return_json', json:json});
});
}
_search(symbol) {
console.log('view._search', symbol);
var symbol = symbol
this.browser.then((browser) => {
browser.search(symbol);
});
}
}