-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
261 lines (231 loc) · 7.13 KB
/
main.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
var nodesFormat = '[';
let reactionArray = [];
var cyFormat = '';
var nodesFormat = "";
var edgesFormat = "";
let edgesArray = [];
var testcyto =
[
{ data: { id: 'reactant: 69507' } },
{ data: { id: 'product: 439427' } },
{ data: { id: 'reactant: 122357' } },
{ data: { id: 'reactant: 638308' } },
{ data: { id: 'reactant: 962' } },
{ data: { id: 'product: 1061' } },
{ data: { id: 'reactant: 160647' } },
{ data: { id: 'product: 1061' } },
{ data: { id: '1', source: 'reactant: 69507' , target: 'product: 439427' } },
{ data: { id: '2.0', source: 'reactant: 122357' , target: 'product: 1061' } },
{ data: { id: '2.1', source: 'reactant: 638308' , target: 'product: 1061' } },
{ data: { id: '2.3', source: 'reactant: 962' , target: 'product: 1061' } },
{ data: { id: '3', source: 'reactant: 160647' , target: 'product: 1061' } },
]
var testcyFormat =[{ data: { id: 'reactant: 69507' } },
{ data: { id: 'product: 439427' } },
{ data: { id: 'reactant: 122357' } },
{ data: { id: 'reactant: 638308' } },
{ data: { id: 'reactant: 962' } },
{ data: { id: 'product: 1061' } },
{ data: { id: 'product: 160647' } },
{ data: { id: 'reactant: 160647' } },
{ data: { id: 'product: 1061' } },
{ data: { id: 'product: 5460271' } },
{ data: { id: '0', source: 'reactant: 69507', target: 'product: 439427' } },
{ data: { id: '1', source: 'reactant: 122357', target: 'product: 1061' } },
{ data: { id: '1', source: 'reactant: 122357', target: 'product: 160647' } },
{ data: { id: '1', source: 'reactant: 638308', target: 'product: 1061' } },
{ data: { id: '1', source: 'reactant: 638308', target: 'product: 160647' } },
{ data: { id: '1', source: 'reactant: 962', target: 'product: 1061' } },
{ data: { id: '1', source: 'reactant: 962', target: 'product: 160647' } },
{ data: { id: '2', source: 'reactant: 160647', target: 'product: 1061' } },
{ data: { id: '2', source: 'reactant: 160647', target: 'product: 5460271' } },
]
function processData(data)
{
DataArray = createReationArray(data);
createNodes(reactionArray);
createEdges(reactionArray);
return cyFormat;
}
function createReationArray(data)
{
const table = data.split('\n').slice(1, -1); // Separate the lines and remove the header
table.forEach(element => {
const row = element.split(','); // Separate the elements of each line
const cidsreactant = row[14]; // Isolating the reactants
const cidsproduct = row[15]; // Isolating the products
const reactArray = [];
const reactant = cidsreactant.split('|'); // Separating the reactants
reactant.forEach(function (obj) {
reactArray.push(parseInt(obj));
});
const productArray = [];
const product = cidsproduct.split('|'); // Separating the products
product.forEach(function (obj) {
productArray.push(parseInt(obj));
});
const oneReactionArray = [reactArray, productArray];
reactionArray.push(oneReactionArray);
});
//console.log(reactionArray);
return reactionArray;
}
function createNodes(reactionArray)
{
for (let i = 0; i < reactionArray.length; i++)
{
for (let j = 0; j < reactionArray[i].length; j++)
{
if ((j%2)==0)
{
for (let k = 0; k < reactionArray[i][j].length; k++)
{
nodesFormat += '{ data: { id: \'reactant: ' + reactionArray[i][j][k] + '\' } }, \n';
}
}
else
{
for (let k = 0; k < reactionArray[i][j].length; k++)
{
nodesFormat += '{ data: { id: \'product: ' + reactionArray[i][j][k] + '\' } }, \n';
}
}
}
}
cyFormat += nodesFormat
return cyFormat;
}
function createEdges(reactionArray)
{
///// Creation of the edges
for (let i = 0; i < reactionArray.length; i++)
{
for (let k = 0; k < reactionArray[i][0].length; k++)
{
let reactFormat = '{ data: { id: \'' + i + '\', source: \'reactant: ' + reactionArray[i][0][k] + '\',';
if (reactionArray[i][1].length==1)
{
for (let k = 0; k < reactionArray[i][1].length; k++)
{
let prodFormat = ' target: \'product: ' + reactionArray[i][1][k] + '\' } }, \n';
cyFormat += reactFormat + prodFormat;
}
}
if (reactionArray[i][1].length>1)
{
for (let k = 0; k < reactionArray[i][1].length; k++)
{
let prodFormat = ' target: \'product: ' + reactionArray[i][1][k] + '\' } }, \n';
cyFormat += reactFormat + prodFormat;
}
}
}
}
return cyFormat;
}
function fetchDataAndProcessData()
{
const file = new XMLHttpRequest();
file.open('GET', 'test.csv', false);
file.send(null);
if (file.status === 200)
{
const data = file.responseText;
const result = processData(data);
return result;
}
else
{
console.error('Failed to fetch data');
}
}
cyFormat = fetchDataAndProcessData() + ']';
console.log(cyFormat);
//var parsedcyFormat= JSON.parse(cyFormat);
//////////////////////////////////////////////
///// VISUALISATION
var cy = cytoscape({
container: document.getElementById('cy'),
elements: testcyto,
style: [
{
selector: 'node',
css: {
width: 50,
height: 50,
'background-color':'#61bffc',
content: 'data(id)'
}
}
{
selector: 'node.highlight',
style: {
'border-color': 'yellow',
'background-color':'yellow',
'border-width': '2px'
}
},
{
selector: 'node.semitransp',
style:{ 'opacity': '0.5' }
},
{
selector: 'edge.highlight',
style: { 'mid-target-arrow-color': 'red' }
},
{
selector: 'edge.semitransp',
style:{ 'opacity': '0.2' }
}
],
layout: {
name: 'breadthfirst',
directed: true,
padding: 10,
/* color: "#ffff00",*/
fit: true
}
});
//////////////////////////////////////////////
//EXPORT FORMAT .png
var png64 = cy.png();
async function downloadImage(
imageSrc,
nameOfDownload = 'cy.png',
) {
const response = await fetch(imageSrc);
const blobImage = await response.blob();
const href = URL.createObjectURL(blobImage);
const anchorElement = document.createElement('a');
anchorElement.href = href;
anchorElement.download = nameOfDownload;
document.body.appendChild(anchorElement);
anchorElement.click();
document.body.removeChild(anchorElement);
window.URL.revokeObjectURL(href);
}
btn.addEventListener('click', () => {
downloadImage(
png64,
'cy.png',
)
.then(() => {
console.log('The image has been downloaded');
})
.catch(err => {
console.log('Error downloading image: ', err);
});
});
var neighbor;
function selection(){
cy.on('tap', 'node', function(e) {
var node = e.cyTarget;
var directlyConnected = node.neighborhood();
neighbor=directlyConnected.nodes().addClass('connectednodes');
neighbor.addClass('highlight');
return neighbor;
});
}
select.addEventListener('click',()=>{
selection();
});