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
package de.unirostock.sems.masymos.diff;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.unirostock.sems.masymos.database.Manager;
import de.unirostock.sems.masymos.diff.configuration.NodeLabel;
import de.unirostock.sems.masymos.diff.configuration.Property;
import de.unirostock.sems.masymos.diff.configuration.Relation;
import de.unirostock.sems.masymos.diff.exception.ModelAccessException;
import de.unirostock.sems.masymos.diff.traverse.DBModelTraverser;
public abstract class DiffUtils {
protected static Logger log = LoggerFactory.getLogger(DiffUtils.class);
protected static Manager manager = Manager.instance();
protected static GraphDatabaseService graphDB = Manager.instance().getDatabase();
/**
* Return the type of a model
*
* @param modelNode
* @return [SBML | CELLML]
*/
public static String getModelType( Node modelNode ) {
if( modelNode == null )
return null;
if( modelNode.hasLabel(NodeLabel.Types.MODEL) == false )
modelNode = DBModelTraverser.getModelFromDocument(modelNode);
// check again, if result from traversal is null
if( modelNode == null )
return null;
if( modelNode.hasLabel(NodeLabel.Types.CELLML_MODEL) )
return Property.ModelType.CELLML;
else if( modelNode.hasLabel(NodeLabel.Types.SBML_MODEL) )
return Property.ModelType.SBML;
return null;
}
/**
* Downloads a file and returns its content as String
*
* @param documentNode
* @return
* @throws ModelAccessException
*/
public static String downloadDocumentToString( Node documentNode ) throws ModelAccessException {
URL url = null;
try {
url = getUrlFromDocument(documentNode);
return IOUtils.toString(url, "UTF-8");
} catch (IOException e) {
throw new ModelAccessException("Cannot access " + url, e);
}
}
/**
* Returns the origin a a document
*
* @param documentNode
* @return
* @throws ModelAccessException
*/
public static URL getUrlFromDocument( Node documentNode ) throws ModelAccessException {
String url = null;
if( documentNode.hasProperty(Property.General.XMLDOC) )
url = (String) documentNode.getProperty(Property.General.XMLDOC);
else if( documentNode.hasProperty(Property.General.URI) )
url = (String) documentNode.getProperty(Property.General.URI);
try {
return new URL(url);
} catch (MalformedURLException e) {
throw new ModelAccessException("Invalid url stored in document node" + url, e);
}
}
/**
* running through all model part oriented sub-nodes of a model node an stores
* their xml-id in a map, along with the node object.
*
* @param modelNode
* @return
*/
public static Map<String, Node> traverseModelParts( Node modelNode ) {
if( modelNode.hasLabel(NodeLabel.Types.MODEL) == false )
throw new IllegalArgumentException("The entry node is not a modelNode");
Map<String, Node> partList = new HashMap<String, Node>();
try ( Transaction tx = graphDB.beginTx() ) {
// get ID of the model itself
insertIntoPartList(modelNode, partList);
// traverse all CellMl components of this model
if( modelNode.hasLabel(NodeLabel.Types.CELLML_MODEL) ) {
Iterable<Relationship> cellMlComponents = modelNode.getRelationships(Direction.OUTGOING, Relation.CellmlRelTypes.HAS_COMPONENT, Relation.CellmlRelTypes.HAS_VARIABLE, Relation.CellmlRelTypes.HAS_REACTION);
for( Relationship componentRelation : cellMlComponents ) {
Node componentNode = componentRelation.getEndNode();
String componentId = insertIntoPartList(componentNode, partList);
// iterate relations under a Component (Reactions, Variables)
Iterable<Relationship> cellMlSubComponents = componentNode.getRelationships(Direction.OUTGOING, Relation.CellmlRelTypes.HAS_REACTION, Relation.CellmlRelTypes.HAS_VARIABLE);
for( Relationship subComponentRelation : cellMlSubComponents ) {
Node subComponentNode = subComponentRelation.getEndNode();
insertIntoPartList(subComponentNode, componentId, partList);
}
}
}
// traverse all SBML parts
if( modelNode.hasLabel(NodeLabel.Types.SBML_MODEL) ) {
Iterable<Relationship> sbmlParts = modelNode.getRelationships(Direction.OUTGOING, Relation.SbmlRelTypes.HAS_COMPARTMENT,
Relation.SbmlRelTypes.HAS_EVENT, Relation.SbmlRelTypes.HAS_FUNCTION, Relation.SbmlRelTypes.HAS_MODIFIER,
Relation.SbmlRelTypes.HAS_PARAMETER, Relation.SbmlRelTypes.HAS_PRODUCT, Relation.SbmlRelTypes.HAS_REACTANT,
Relation.SbmlRelTypes.HAS_REACTION, Relation.SbmlRelTypes.HAS_RULE, Relation.SbmlRelTypes.HAS_SPECIES );
for( Relationship partRelation : sbmlParts ) {
Node partNode = partRelation.getEndNode();
insertIntoPartList(partNode, partList);
}
}
tx.success();
}
return partList;
}
private static String insertIntoPartList( Node node, Map<String, Node> partList ) {
return insertIntoPartList(node, null, partList);
}
private static String insertIntoPartList( Node node, String prefix, Map<String, Node> partList ) {
String id = null;
if( node.hasProperty(Property.General.ID) )
id = (String) node.getProperty( Property.General.ID );
else if( node.hasProperty(Property.General.NAME) )
id = (String) node.getProperty( Property.General.NAME );
// add prefix
if( prefix != null )
id = prefix + ":" + id;
if( id != null && id.isEmpty() == false ) {
if( partList.containsKey(id) == false )
partList.put( id, node );
else
log.error("Prevented id overwrite in part list for {}", id);
}
return id;
}
public static Node getPatchNodeByBivesId( Node diffNode, String bivesId ) {
if( diffNode.hasLabel(NodeLabel.DiffTypes.DIFF) == false )
throw new IllegalArgumentException("The diff node has not a DIFF lable.");
Node patchNode = null;
try ( Transaction tx = graphDB.beginTx() ) {
// query parameter
Map<String, Object> parameter = new HashMap<String, Object>();
parameter.put( "diffId", diffNode.getId() );
parameter.put( "bivesId", bivesId );
final String query = "MATCH (d:DIFF)-[HAS_DIFF_ENTRY]->(e:DIFF_NODE) WHERE id(d)={diffId} and e.`bives.id`={bivesId} RETURN e LIMIT 1;";
Result result = graphDB.execute(query, parameter);
if( result.hasNext() ) {
patchNode = (Node) result.next().get("e");
}
tx.success();
}
return patchNode;
}
}