-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscipion-workflow.html
503 lines (433 loc) · 21.7 KB
/
scipion-workflow.html
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- jquery -->
<script src="//unpkg.com/jquery/dist/jquery.min.js"></script>
<!-- cytoscape -->
<script src="//unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
<!-- dagre layout -->
<script src="//unpkg.com/dagre/dist/dagre.min.js"></script>
<script src="//unpkg.com/cytoscape-dagre/cytoscape-dagre.js"></script>
<!-- qtip -->
<link rel="stylesheet" type="text/css" href="//unpkg.com/qtip2/dist/jquery.qtip.min.css">
<script src="//unpkg.com/qtip2/dist/jquery.qtip.min.js"></script>
<script src="//unpkg.com/cytoscape-qtip/cytoscape-qtip.js"></script>
<!-- molstar -->
<link rel="stylesheet" type="text/css" href="css/molstar.css">
<script type="text/javascript" src="js/molstar.js"></script>
<title>Scipion workflow</title>
</head>
<body>
</body>
<dom-module id="scipion-workflow">
<style>
#cy {
z-index: 999;
}
.qtip {
max-width: 50%;
}
.qtip-tip {
display: none !important;
}
.qtip-content{
overflow-x: scroll;
overflow-y: scroll;
height: 50vh;
}
.msp-plugin {
background: none;
}
.msp-plugin-content {
margin-top: 20vh;
margin-bottom: 20vh;
margin-right: 20vh;
margin-left: 20vh;
}
.images {
display: none;
margin-top: 10px;
}
.toggle-icon {
cursor: pointer;
font-size: 16px;
user-select: none;
}
</style>
<script>
function getRandomHexColor() {
// Returns random hexadecimal color
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
return `#${randomColor.padStart(6, '0')}`;
}
function getFormattedProtocolStr(dict, viewMode){
// Formats a protocol object to show its data in the qtip as a table
// Returns the table as a html string.
var blackList = ['runName', 'runMode', 'labelColor']; // Params given by scipion json that we dont want to show
var summaryWhiteList = ['object.className', 'summary', 'output']
var formattedStr = '<table><th>Param name</th><th>Param value</th>';
$.each(dict, function(key, value){
if (blackList.indexOf(key)===-1){
if(viewMode!=='summarized' || summaryWhiteList.indexOf(key)!==-1) {
if (key === 'output') { // images
formattedStr += '<tr><td><b>' + 'output' + ':</b></td><td></td></tr>'
$.each(value, function(outputKey, outputValue){
formattedStr += '<tr><td class="toggle-icon" onclick="toggleImages(' + "'" + outputValue['outputName'] + "'" + ', this)"> ▼ -' + outputValue['outputName'] + ':</td><td class="images" id="' + outputValue['outputName'] + '"> ';
var outputItems = outputValue['outputItems']
var nimg = 0
var nvol = 0
$.each(outputItems, function(itemKey, itemValue) {
if ('item_representation' in itemValue) {
var paths = itemValue['item_representation']
if (Array.isArray(paths)){ // is a folder containing images inside
$.each(paths, function(index, path) {
path = path.substring(path.lastIndexOf('/', path.lastIndexOf('/') - 1) + 1);
if (path.indexOf('jpg')!==-1){
if (path.indexOf('slicesX_0000') !== -1) {
volume_name = path.substring(1, path.lastIndexOf("/"))
formattedStr += '<hr><b style="color: #29808A;">' + volume_name + '</b>' + '<br><br>';
formattedStr += '<b>Slices X axis:</b>' + '<br><br>';
}
if (path.indexOf('slicesY_0000') !== -1) {
formattedStr += '<hr>' + '<b>Slices Y axis:</b>' + '<br><br>';
}
if (path.indexOf('slicesZ_0000') !== -1) {
formattedStr += '<hr>' + '<b>Slices Z axis:</b>' + '<br><br>';
}
formattedStr += '<img data-src=' + "images_representation/" + path + ' style="margin: 0px 0px 10px 5px; max-width:100px;" class="zoom ' + outputValue['outputName'] + '">'
nimg = nimg + 1;
if (nimg % 5 == 0) {
formattedStr += '<br>';
}
}
});
}
else {
path = paths.substring(paths.lastIndexOf("/") + 1, paths.length)
if (path.indexOf('jpg')!==-1){ // is a single jpg image
formattedStr += '<img data-src=' + "images_representation/" + path + ' style="margin: 0px 0px 10px 5px; max-width:100px;" class="zoom ' + outputValue['outputName'] + '">'
nimg = nimg + 1;
if (nimg % 5 == 0) {
formattedStr += '<br>';
}
}
else if (path.indexOf('mrc')!==-1 || path.indexOf('map')!==-1){ // open volume with Molstar
if (nvol == 0) {
formattedStr += '<div id-src="app_' + path + '" class="' + outputValue['outputName'] + '"></div>';
color = parseInt(getRandomHexColor().replace('#', '0x'));
formattedStr += '<script type="text/javascript"> molstar.Viewer.create("app_' + path + '", {layoutShowControls: true, viewportShowExpand: false, collapseLeftPanel: true, collapseRightPanel: false,}).then(viewer => {viewer.loadVolumeFromUrl({url:' + '"images_representation/' + path + '",format: "ccp4",isBinary: true}, [{type: "relative",value: 1.5,color: ' + color + '}]);';
nvol = nvol + 1;
}
else {
formattedStr += 'viewer.loadVolumeFromUrl({url:' + '"images_representation/' + path + '",format: "ccp4",isBinary: true}, [{type: "relative",value: 1.5,color: 0x29808A}]);'
}
}
}
}
if (outputValue['outputType'] === 'SetOfClasses3D' ) {
formattedStr += '<br>';
}
});
if (nvol > 0) {
formattedStr += '});'
formattedStr += '<\u002Fscript>';
}
formattedStr += '</td></tr>'
if (outputValue['outputType'] === 'SetOfClasses2D' || outputValue['outputType'] === 'SetOfClasses3D') {
formattedStr += '<tr><td></td><td>' + outputValue['outputSize'] + ' classes';
formattedStr += '</td></tr>';
}
});
}
else {
formattedStr += '<tr><td><b>' + key + ':</b></td><td title="' + value + '">' + value + '</td></tr>'
}
}
}
});
formattedStr += '</table>';
return formattedStr
}
function paintGraph(workflowDiv, nodes, edges){
// Paints the graph inside a given container (workflowDiv).
// Needs nodes and edges as returned by getGraphNodes function
var cy = cytoscape({
container: workflowDiv,
autounselectify: true,
zoom: 1,
minZoom: 1e-50,
maxZoom: 1e50,
wheelSensitivity: 1,
layout: {
name: 'dagre',
padding: 10,
nodeSep: 10
},
style: cytoscape.stylesheet()
.selector('node')
.style({
'shape': 'rectangle',
'width': 'label',
'padding': 5,
'content': 'data(name)',
'text-valign': 'center',
'background-fill': 'linear-gradient',
'background-gradient-stop-colors': 'data(color)',
'color': '#393939'
})
.selector(':selected')
.style({
'border-width': 2,
'border-color': '#333'
})
.selector('edge')
.style({
'opacity': 1,
'width': .5,
'line-color': 'grey',
})
.selector('edge:selected')
.style({
'width': 1,
'border-color': '#333',
'line-color': 'grey'
})
.selector('.faded')
.style({
'opacity': 0.25,
'text-opacity': 0
}),
elements: {
nodes: nodes,
edges: edges
}
});
var currentId;
// just use the regular qtip api but on cy elements
cy.nodes().qtip({
content: {
text: function(){currentId = this.data().id;return this.data().content},
title: {
text: function(){
var title;
cy.nodes().forEach(function(node) {
if (node.data().id === currentId) {
title = node.data().name
}
});
return title
},
button: true
}
},
position: {
my: 'bottom center',
at: 'top center'
},
style: {
classes: 'qtip-bootstrap'
}
});
}
function getWorkflow(jsonSrc, workflowDiv){
// Parses json from jsonSrc and paints workflow in workflowDiv
// (workflowDiv must be an html element)
var viewModeSelector = document.getElementById('view-mode');
var viewModeValue = viewModeSelector.options[viewModeSelector.selectedIndex].value;
if (jsonSrc.startsWith("[")){
getGraphNodes(JSON.parse(jsonSrc), workflowDiv, viewModeValue);
}else{
$.getJSON(jsonSrc, function (data , status){
getGraphNodes(data, workflowDiv, viewModeValue);
});
}
}
function getGraphNodes(data, workflowDiv, viewMode){
// Parses json data as given by getWorkflow function
var nodeList = [{ data: {
id: 'root',
name: 'Project',
color: '#EEEEEE'
}
}
];
var edgeList = [];
var regex = /^(\d+)\./;
const labels = {no_label: '#EEEEEE'};
$.each(data, function(index, protocol){
var color = '#EEEEEE'
if (protocol['labelColor'] != undefined && protocol['labelColor'].length > 0) {
color = protocol['labelColor'][0];
labels[protocol['label'][0]] = color;
}
// if there is no object.label for this protocol, use the object.className instead
var name = protocol['object.label'] && protocol['object.label'].trim() !== '' ? protocol['object.label'] : protocol['object.className'];
nodeList.push({ data: {
id: protocol['object.id'],
name: name,
content: getFormattedProtocolStr(protocol, viewMode),
color: color
}
});
var hasInputs = false;
$.each(protocol, function(key, value){
var protID = protocol['object.id'];
if (Array.isArray(value)){
$.each(value, function(index, arrayValue){
if (typeof(arrayValue) === 'string' && regex.test(arrayValue) && key !== 'pluginVersion' && key !== 'pluginBinaryVersion'){
edgeList.push({ data: {
source: arrayValue.split('.')[0],
target: protID
}
});
hasInputs = true
}
})
}else if (typeof(value) === 'string' && regex.test(value) && key !== 'pluginVersion' && key !== 'pluginBinaryVersion'){
edgeList.push({ data: {
source: value.split('.')[0],
target: protID
}
});
hasInputs = true
}
});
if (!hasInputs){
edgeList.push({ data: {
source: 'root',
target: protocol['object.id']
}
});
}
});
paintGraph(workflowDiv, nodeList, edgeList);
if (Object.keys(labels).length > 1) {
paintLabels(labels);
}
};
function toggleImages(id, element) {
var imgRow = document.getElementById(id);
var icon = element;
var currentHtml = icon.innerHTML;
if (imgRow.style.display === 'none' || imgRow.style.display === '') {
imgRow.style.display = 'table-row';
icon.innerHTML = currentHtml.replace("▼", "▲");
} else {
imgRow.style.display = 'none';
icon.innerHTML = currentHtml.replace("▲", "▼");
}
// actually load jpg images or mrc volumes. Avoids loading all protocol thumbnails at once
if (id.indexOf('3D')!==-1){ // is a 3d volume
const volume = document.getElementsByClassName(id)[0];
volume.id = volume.getAttribute('id-src');
// get element and reload molstar javascript code
var td = document.getElementById(id);
var molstarScriptElement = td.querySelector('script');
var molstarScriptCode = molstarScriptElement.textContent || molstarScriptElement.innerText;
molstarScriptElement.remove();
var newMolstarScriptElement = document.createElement('script');
newMolstarScriptElement.textContent = molstarScriptCode;
td.appendChild(newMolstarScriptElement);
}
else { // is a jpg image
const images = document.getElementsByClassName(id);
for (let i = 0; i < images.length; i++) {
const image = images[i];
image.src = image.getAttribute('data-src');
}
}
};
function paintLabels(labels) {
// Helper function to convert rgb to hex
function rgbToHex(rgb) {
const result = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
if (!result) return null;
const r = parseInt(result[1], 10);
const g = parseInt(result[2], 10);
const b = parseInt(result[3], 10);
const hex = (r << 16) | (g << 8) | b;
return '#' + hex.toString(16).padStart(6, '0').toUpperCase();
}
// Helper function to check if a div with the same background color exists
function colorExists(color) {
const divs = legend.querySelectorAll('.legend_square');
for (const div of divs) {
const divColor = getComputedStyle(div).backgroundColor;
if (rgbToHex(divColor) === color) {
return true;
}
}
return false;
}
const legend = document.getElementById('legend');
const b = document.createElement('b');
b.textContent = 'Labels legend:'
legend.appendChild(b);
Object.entries(labels).forEach(([key, value]) => {
if (!colorExists(value)) {
const li = document.createElement('li');
li.className = 'legend_item_square';
const div = document.createElement('div');
div.className = 'legend_square';
div.style.backgroundColor = value;
const span = document.createElement('span');
span.className = 'legend_label';
span.textContent = key;
li.appendChild(div);
li.appendChild(span);
legend.appendChild(li);
}
});
};
class ScipionWorkflow extends HTMLElement {
static get is() { return "scipion-workflow"; }
// Define attributes to observe
static get observedAttributes() {return ['src']; }
// React to attributes changes
attributeChangedCallback(attr, oldValue, newValue) {
if (attr == 'src' && newValue !== oldValue) {
this.refresh();
}
}
constructor() {
super();
// Create a shadow root
var shadow = this.attachShadow({mode: 'open'});
// Create a div that will contain the workflow graph
var workflowDiv = document.createElement('div');
workflowDiv.id = 'workflowDiv';
this.workflowDiv = workflowDiv;
shadow.appendChild(workflowDiv);
// Paint the graph
this.refreshCalled = false;
this.refresh()
}
refresh() {
if (this.refreshCalled) return; // Skip if already called
this.refreshCalled = true;
// Performs all the steps to paint the graph
// 1. Get attrs from the html tag
var jsonSrc = this.getAttribute('src');
if (jsonSrc == undefined) {
return;
}
// 2. Apply attrs to workflow div
this.workflowDiv.style.width = '100%';
this.workflowDiv.style.height = '100%';
this.workflowDiv.style.position = 'relative';
//3. Parse json data and paint graph
getWorkflow(jsonSrc, this.workflowDiv);
}
}
customElements.define(ScipionWorkflow.is, ScipionWorkflow);
// Prevent styles from being applied via html import, add them explicitly to head
// as specified in https://github.com/TakayoshiKochi/deprecate-style-in-html-imports
var importDoc = document.currentScript.ownerDocument;
var styleLink = importDoc.querySelector('link[rel=stylesheet]');
document.head.appendChild(styleLink);
var style = importDoc.querySelector('style');
document.head.appendChild(style);
</script>
</dom-module>
</html>