/
xgmml.js
160 lines (144 loc) · 6.02 KB
/
xgmml.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
/* DO NOT MODIFY CODE BELOW THIS LINE */
//DROPBOX and parse
$(loadDropbox = function(_elements, id_obj) {
if (id_obj) {
$("#" + id_obj).cytoscape({
layout: options,
showOverlay: false,
zoom: 1,
style: cerebral_style,
elements: _elements,
ready: function() {
cy = this;
cerebral_ready(cy);
}
});
}
});
function dragenter(e) {
e.stopPropagation();
e.preventDefault();
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
var dt = e.dataTransfer;
var files = dt.files;
loadFiles(files, e.currentTarget.id);
}
$(document).ready(function() {
$(".cerebral-dropbox").each(function(index) {
$(this)[0].addEventListener("dragenter", dragenter, false);
$(this)[0].addEventListener("dragover", dragover, false);
$(this)[0].addEventListener("drop", drop, false);
});
});
function loadFiles(files, id_obj) {
if (files.length != 1) {
alert("Only one file at a time please");
} else if (!files[0]['name'].endsWith(".xgmml") && !files[0]['name'].endsWith(".xml")) {
alert("Your file doesn't look like an XGMML file")
} else {
parseXGMMLandLoad(files[0], id_obj);
}
}
function parseXGMMLandLoad(xgmml_file, id_obj) {
var reader = new FileReader();
xgmml = '';
elements = [];
nodes = {};
identifier_type = '';
reader.onload = function() {
xgmml = this.result;
var xml = $.parseXML(xgmml);
var graph = xml.children[0];
for (e in graph.children) {
element = graph.children[e];
if (element.nodeName == "node") {
var localization = null;
var identifier = '';
for (i in element.children) {
att = element.children[i];
if (att.nodeName == 'att' && att.getAttribute('name').toLowerCase() == 'localization') {
localization = att.getAttribute('value');
break;
} else if (att.nodeName == 'att' && att.getAttribute('name').toLowerCase() == 'identifier type') {
identifier_type = att.getAttribute('value');
} else if (att.nodeName == 'att' && att.getAttribute('name').toLowerCase() == 'identifier') {
identifier = att.getAttribute('value');
if (identifier_type.length > 0)
break;
} else if (att.nodeName == 'att' && att.getAttribute('name').toLowerCase().indexOf('id')!=-1){
aux = att.getAttribute('value').split(':');
if (aux.length == 2){
if ((aux[0].toLowerCase().indexOf('uniprot') !=-1) ||
(aux[0].toLowerCase().indexOf('innatedb') !=-1) ||
(aux[0].toLowerCase().indexOf('ensembl') !=-1) ||
(aux[0].toLowerCase().indexOf('entrez') !=-1)) {
identifier_type = aux[0];
identifier = aux[1];
break;
}
}
}
}
if (localization == null) {
nodes[identifier] = {"id": element.id, "name": element.getAttribute('label')}
} else {
elements.push({group: "nodes", data: {"id": element.id, "name": element.getAttribute('label'), 'localization': localization, 'color': defaultNodeColor}});
}
} else if (element.nodeName == "edge") {
for (i in element.children) {
att = element.children[i];
if (att.nodeName == 'att'){
if ((att.getAttribute('name').toLowerCase().indexOf('type') != -1) || (att.getAttribute('name').toLowerCase() == 'interaction')) {
interaction_type = att.getAttribute('value');
break;
}
}
}
elements.push({group: "edges", data: {"id": element.getAttribute('source') + '#' + element.getAttribute('target'), "name": interaction_type, "source": element.getAttribute('source'), "target": element.getAttribute('target'), "idgroup": element.getAttribute('source') + '#' + element.getAttribute('target')}});
}
}
// We need to query the WS to get localisations
if (Object.keys(nodes).length > 0) {
if (identifier_type.toLowerCase().indexOf("uniprot") - 1) {
identifier_type = "UniProt";
}
var ids = JSON.stringify(Object.keys(nodes)).replace('\[', '').replace('\]', '').replaceAll('"', '');
$.ajax({
dataType: "json",
async: false,
type: "GET",
url: "http://www.innatedb.com/cerebralLocalizationWS.do",
data: 'xref=' + identifier_type + '&ids=' + ids,
success: function(data)
{
for (d in data) {
var obj = {
data: {
id: nodes[d]['id'],
name: nodes[d]['name'],
localization: data[d],
color: defaultNodeColor
},
group: "nodes"
};
elements.push(obj)
//console.log(JSON.stringify(obj));
}
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(errorThrown);
}
});
}
loadDropbox(elements, id_obj);
}
reader.readAsText(xgmml_file);
}