forked from binfalse/BiVeS-Core
/
GraphTranslatorGraphML.java
477 lines (423 loc) · 13.6 KB
/
GraphTranslatorGraphML.java
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
/**
*
*/
package de.unirostock.sems.bives.ds.graph;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import de.binfalse.bflog.LOGGER;
import de.unirostock.sems.bives.ds.GraphEntity;
import de.unirostock.sems.bives.ds.hn.HierarchyNetwork;
import de.unirostock.sems.bives.ds.hn.HierarchyNetworkComponent;
import de.unirostock.sems.bives.ds.hn.HierarchyNetworkVariable;
import de.unirostock.sems.bives.ds.ontology.SBOTerm;
import de.unirostock.sems.bives.ds.rn.ReactionNetwork;
import de.unirostock.sems.bives.ds.rn.ReactionNetworkCompartment;
import de.unirostock.sems.bives.ds.rn.ReactionNetworkReaction;
import de.unirostock.sems.bives.ds.rn.ReactionNetworkSubstance;
import de.unirostock.sems.bives.ds.rn.ReactionNetworkSubstanceRef;
import de.unirostock.sems.xmlutils.tools.XmlTools;
/**
* The Class GraphTranslatorGraphML to translate internal graph structures into
* GraphML.
*
* The resulting graph will look like:
*
* <pre>
* <graphml>
* <key attr.name="name" attr.type="string" for="node" id="name"/>
* <key attr.name="node set" attr.type="string" for="node" id="ns">
* <default>species</default>
* </key>
* <key attr.name="version" attr.type="int" for="all" id="vers">
* <default>0</default>
* </key>
* <key attr.name="modifier" attr.type="string" for="edge" id="mod">
* <default>none</default>
* </key>
* <graph edgedefault="directed" id="G">
* <node id="c1">
* <data key="vers">0</data>
* <data key="name">compartment</data>
* <graph edgedefault="directed" id="G1">
* <node id="s2">
* <data key="ns">species</data>
* <data key="vers">0</data>
* <data key="name">sigb</data>
* </node>
* <node id="s1">
* <data key="ns">species</data>
* <data key="vers">0</data>
* <data key="name">lacz</data>
* </node>
* <node id="r3">
* <data key="ns">reaction</data>
* <data key="vers">2</data>
* <data key="name">sigb degr</data>
* </node>
* <!-- more nodes -->
* </graph>
* </node>
* <edge source="s2" target="r3">
* <data key="mod">none</data>
* <data key="vers">0</data>
* </edge>
* <edge source="s4" target="r3">
* <data key="mod">stimulator</data>
* <data key="vers">-1</data>
* </edge>
* <!-- more edges -->
* </graph>
* </graphml>
* </pre>
*
* for more information see <a href=
* "https://sems.uni-rostock.de/trac/bives-core/wiki/GraphmlFormatDescription"
* >GraphmlFormatDescription</a>
*
* @author Martin Scharm
*/
public class GraphTranslatorGraphML
extends GraphTranslator
{
/** The root node of the graph document. */
private Element graphRoot;
/** The graph document. */
private Document graphDocument;
/** The graph id. */
private int graphid;
/*
* (non-Javadoc)
*
* @see
* de.unirostock.sems.bives.ds.graph.GraphTranslator#translate(de.unirostock
* .sems.bives.ds.graph.HierarchyNetwork)
*/
@Override
public String translate (HierarchyNetwork hn)
{
if (hn == null)
return null;
/*graphDocument = DocumentBuilderFactory.newInstance ().newDocumentBuilder ()
.newDocument ();*/
Element graphML = new Element ("graphml");
graphRoot = addGraphMLPreamble (graphML);
graphDocument = new Document (graphML);
graphid = 1;
Collection<HierarchyNetworkComponent> components = hn.getComponents ();
for (HierarchyNetworkComponent comp : components)
{
LOGGER.info ("creating comp: ", comp.getId ());
Element node = createGraphMLNode (graphRoot, comp.getId (), null,
comp.getLabel (), comp.getModification () + "");
Element subtree = createGraphRoot (true);
node.addContent (subtree);
List<HierarchyNetworkVariable> vars = comp.getVariables ();
for (HierarchyNetworkVariable var : vars)
{
LOGGER.info ("creating var: ", var.getId ());
//Element vNode =
createGraphMLNode (subtree, var.getId (), null,
var.getLabel (), var.getModification () + "");
//subtree.addContent (vNode);
}
}
for (HierarchyNetworkComponent comp : components)
{
HierarchyNetworkComponent parA = comp.getParentA (), parB = comp
.getParentB ();
if (parA != null || parB != null)
{
if (parA == parB)
{
// connect w/o mod
createEdge (graphRoot, parA.getId (), comp.getId (), null, null);
}
else
{
if (parA != null)
{
// connect delete
createEdge (graphRoot, parA.getId (), comp.getId (), GraphEntity.DELETE
+ "", null);
}
if (parB != null)
{
// connect insert
createEdge (graphRoot, parB.getId (), comp.getId (), GraphEntity.INSERT
+ "", null);
}
}
}
List<HierarchyNetworkVariable> vars = comp.getVariables ();
for (HierarchyNetworkVariable var : vars)
{
HashMap<HierarchyNetworkVariable, HierarchyNetworkVariable.VarConnection> cons = var
.getConnections ();
for (HierarchyNetworkVariable con : cons.keySet ())
{
LOGGER.info ("connecting var: ", var.getId (), " -> ",
con.getId ());
createEdge (graphRoot, con.getId (), var.getId (), "" + cons.get (con)
.getModification (), null);
}
}
}
return XmlTools.prettyPrintDocument (graphDocument);
}
/*
* (non-Javadoc)
*
* @see
* de.unirostock.sems.bives.ds.graph.GraphTranslator#translate(de.unirostock
* .sems.bives.ds.graph.rn.ReactionNetwork)
*/
@Override
public String translate (ReactionNetwork rn)
{
if (rn == null)
return null;
/*graphDocument = DocumentBuilderFactory.newInstance ().newDocumentBuilder ()
.newDocument ();*/
Element graphML = new Element ("graphml");
graphRoot = addGraphMLPreamble (graphML);
graphDocument = new Document (graphML);
HashMap<ReactionNetworkCompartment, Element> compartments = new HashMap<ReactionNetworkCompartment, Element> ();
graphid = 1;
for (ReactionNetworkCompartment c : rn.getCompartments ())
{
Element node = createGraphMLNode (graphRoot, c.getId (), null,
c.getLabel (), c.getModification () + "");
Element compartment = createGraphRoot (true);
node.addContent (compartment);
compartments.put (c, compartment);
}
for (ReactionNetworkSubstance s : rn.getSubstances ())
{
ReactionNetworkCompartment compartment = s.getCompartment ();
if (compartment != null)
createGraphMLNode (compartments.get (compartment), s.getId (),
"species", s.getLabel (), s.getModification () + "");
else
createGraphMLNode (graphRoot, s.getId (), "species", s.getLabel (),
s.getModification () + "");
}
for (ReactionNetworkReaction r : rn.getReactions ())
{
ReactionNetworkCompartment compartment = r.getCompartment ();
if (compartment != null)
createGraphMLNode (compartments.get (compartment), r.getId (),
"reaction", r.getLabel (), r.getModification () + "");
else
createGraphMLNode (graphRoot, r.getId (), "reaction", r.getLabel (),
r.getModification () + "");
for (ReactionNetworkSubstanceRef s : r.getInputs ())
createEdge (graphRoot, s.getSubstance ().getId (), r.getId (),
s.getModification () + "", SBOTerm.MOD_NONE);
for (ReactionNetworkSubstanceRef s : r.getOutputs ())
createEdge (graphRoot, r.getId (), s.getSubstance ().getId (),
s.getModification () + "", SBOTerm.MOD_NONE);
for (ReactionNetworkSubstanceRef s : r.getModifiers ())
{
/*if (s.getModification () == ReactionNetwork.MODIFIED)
{
createEdge (graphRoot, s.getSubstance ().getId (), r.getId (),
ReactionNetwork.DELETE + "", s.getModTermA ());
createEdge (graphRoot, s.getSubstance ().getId (), r.getId (),
ReactionNetwork.INSERT + "", s.getModTermB ());
}
else*/
createEdge (graphRoot, s.getSubstance ().getId (), r.getId (),
s.getModification () + "", s.getModTerm ());
}
}
return XmlTools.prettyPrintDocument (graphDocument);
}
/**
* Gets the graphml document.
*
* @return the graphml document
*/
public Document getGraphmlDocument ()
{
return graphDocument;
}
/**
* Produces the preamble of the graph.
*
* <p>
* Creates some nodes in the document defining the graph and its properties.
* Creates also the <graph> node to insert the graph itself. This graph-node
* will be returned afterwards. Example of a typical preamble:
* </p>
*
* <pre>
* <graphml>
* <key attr.name="name" attr.type="string" for="node" id="name"/>
* <key attr.name="node set" attr.type="string" for="node" id="ns">
* <default>species</default>
* </key>
* <key attr.name="version" attr.type="int" for="all" id="vers">
* <default>0</default>
* </key>
* <key attr.name="modifier" attr.type="string" for="edge" id="mod">
* <default>none</default>
* </key>
* <graph edgedefault="directed" id="G">
* <!-- this node will be returned -->
* </graph>
* </graphml>
* </pre>
*
* @param graphML
* the document
* @return the graph node (root for the graph)
*/
private Element addGraphMLPreamble (Element graphML)
{
// key for node name
Element keyEl = new Element ("key");
keyEl.setAttribute ("id", "name");
keyEl.setAttribute ("for", "node");
keyEl.setAttribute ("attr.name", "name");
keyEl.setAttribute ("attr.type", "string");
graphML.addContent (keyEl);
// key for node set (e.g. species or reaction)
keyEl = new Element ("key");
keyEl.setAttribute ("id", "ns");
keyEl.setAttribute ("for", "node");
keyEl.setAttribute ("attr.name", "node set");
keyEl.setAttribute ("attr.type", "string");
Element defEl = new Element ("default");
defEl.setText ("species");
keyEl.addContent (defEl);
graphML.addContent (keyEl);
// key for version flag
keyEl = new Element ("key");
keyEl.setAttribute ("id", "vers");
keyEl.setAttribute ("for", "all");
keyEl.setAttribute ("attr.name", "version");
keyEl.setAttribute ("attr.type", "int");
defEl = new Element ("default");
defEl.setText ("0");
keyEl.addContent (defEl);
graphML.addContent (keyEl);
// key for modifier flag
keyEl = new Element ("key");
keyEl.setAttribute ("id", "mod");
keyEl.setAttribute ("for", "edge");
keyEl.setAttribute ("attr.name", "modifier");
keyEl.setAttribute ("attr.type", "string");
defEl = new Element ("default");
defEl.setText (SBOTerm.MOD_NONE);
keyEl.addContent (defEl);
graphML.addContent (keyEl);
// <graph>
keyEl = createGraphRoot (true);
graphML.addContent (keyEl);
return keyEl;
}
/**
* Creates the root of a graph.
*
* @param directed
* the directed flag, if true this (sub)graph will be directed
* @return the root node
*/
private Element createGraphRoot (boolean directed)
{
Element keyEl = new Element ("graph");
keyEl.setAttribute ("id", "G" + graphid++);
if (directed)
keyEl.setAttribute ("edgedefault", "directed");
else
keyEl.setAttribute ("edgedefault", "undirected");
return keyEl;
}
/**
* Inserts a new node to the graph.
* This node will automatically appended a node to the {@code parent}s
* children.
*
* @param parent
* the parent node
* @param id
* the ID of the node
* @param ns
* the node set
* @param name
* the name of the node
* @param version
* the version flag: [-1,0,1,2]
* @return the new node
*/
private Element createGraphMLNode (Element parent, String id, String ns,
String name, String version)
{
LOGGER.debug ("create gml node: ", id, " mod: ", version);
Element element = new Element ("node");
element.setAttribute ("id", id);
if (ns != null)
{
Element nsElement = new Element ("data");
nsElement.setAttribute ("key", "ns");
nsElement.setText (ns);
element.addContent (nsElement);
}
if (version != null)
{
Element srcElement = new Element ("data");
srcElement.setAttribute ("key", "vers");
srcElement.setText (version);
element.addContent (srcElement);
}
Element nameElement = new Element ("data");
nameElement.setAttribute ("key", "name");
nameElement.setText (name);
element.addContent (nameElement);
if (parent != null)
parent.addContent (element);
return element;
}
/**
* Inserts a new edge to the graph.
* This edge gets automatically appended to the {@code parent}s children.
*
* @param parent
* the parent
* @param source
* the source
* @param target
* the target
* @param version
* the version flag: [-1,0,1,2]
* @param modifier
* the modifier property (SBOTerm.*)
*/
private void createEdge (Element parent, String source, String target,
String version, String modifier)
{
LOGGER.debug ("create gml edge: ", source, " -> ", target, " mod: ",
version);
Element element = new Element ("edge");
element.setAttribute ("source", source);
element.setAttribute ("target", target);
if (modifier != null)
{
Element nsElement = new Element ("data");
nsElement.setAttribute ("key", "mod");
nsElement.setText (modifier);
element.addContent (nsElement);
}
if (version != null)
{
Element srcElement = new Element ("data");
srcElement.setAttribute ("key", "vers");
srcElement.setText (version);
element.addContent (srcElement);
}
parent.addContent (element);
}
}