-
Notifications
You must be signed in to change notification settings - Fork 8
/
edgeSBOL.js
360 lines (335 loc) · 12.6 KB
/
edgeSBOL.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
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
const constants = require('./constants');
SbolGeneration = require('./sbol');
/**
* Breadth width search of the state graph to create a structure that is better for SBOL generation
* @param stateGraph Edge-based graph representation of design space
* @param stateStack Equivalent representation of the stateGraph to help with SBOL generation
* @param id The id to start the traverse from
* @param edgeUsed the edge used to get to stateGraph[id] (edgeUsed.dest should be id)
*/
function makeStackFromStateGraph(stateGraph, stateStack, id, edgeUsed){
let pMap = stateGraph.pMap;
let endIds = []; //prevent redundant ends
let queue = [];
let edgeQueue = [];
queue.push(id);
edgeQueue.push(edgeUsed);
while (queue.length !== 0){
let id = queue.shift();
let node = stateGraph.get(id);
let edgeUsed = edgeQueue.shift();
// if node has been visited and we got here using a non-Atom, push to endIds
if (node.visited){
let ops = stateGraph.getOperators(id);
if (edgeUsed.type === constants.ATOM) {
if (edgeUsed.orientation === constants.INLINE) {
stateStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.INLINE_URI});
} else {
stateStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.REV_COMP_URI});
}
}
if (ops.includes(constants.ZERO_MORE) || ops.includes(constants.ONE_MORE)) {
if (!endIds.includes(id)) {
stateStack.push({end: id});
endIds.push(id);
} else if (!enoughEnds(id, endIds, ops, [constants.ONE_MORE, constants.ZERO_MORE])) {
stateStack.push({end: id});
endIds.push(id);
}
}
// check for zero-or-one end (need to check parent's operators)
if (id in pMap) {
let ZOParent = parentWithOp(stateGraph, Array.from(pMap[id]), constants.ZERO_ONE);
if (ZOParent !== null) {
if (!enoughEnds(ZOParent, endIds, stateGraph.getOperators(ZOParent), [constants.ZERO_ONE])) {
// if (!endIds.includes(ZOParent)) {
stateStack.push({end: ZOParent});
endIds.push(ZOParent);
}
}
}
continue;
}
// if we used an atom to get to stateGraph[id], push atom to stack
if (edgeUsed.type === constants.ATOM){
if (edgeUsed.orientation === constants.INLINE) {
stateStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.INLINE_URI});
} else {
stateStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.REV_COMP_URI});
}
}
// handle immediate cycles first (node 1 to node 2 and back)
let immediateCycles = stateGraph.getEdges(id).filter(e => e.dest === edgeUsed.src);
if (immediateCycles.length > 0) {
for (let edge of immediateCycles) {
if (edge.type === constants.ATOM) {
if (edge.orientation === constants.INLINE) {
stateStack.push({[constants.ATOM]: edge.text, orientation: constants.INLINE_URI});
} else {
stateStack.push({[constants.ATOM]: edge.text, orientation: constants.REV_COMP_URI});
}
}
if (stateStack[stateStack.length - 1].end !== edge.dest) {
stateStack.push({end: edge.dest});
endIds.push(edge.dest);
}
}
}
// handle the operations at this node (OR is only non-null case for lastEdge)
let lastEdge = handleOperations(stateGraph, stateStack, id, endIds, edgeUsed, pMap);
node.visited = true;
if (lastEdge){ //from OR
id = lastEdge;
handleOperations(stateGraph, stateStack, id, endIds, edgeUsed, pMap);
stateGraph.get(id).visited = true;
}
// push destinations of stateGraph[id]'s edges
let nonCycles = stateGraph.getEdges(id).filter(e => e.dest !== edgeUsed.src);
let dests = [];
let nextEdges = [];
for (let edge of nonCycles) {
dests.push(edge.dest);
nextEdges.push(edge);
}
queue.push(...dests);
edgeQueue.push(...nextEdges); // push edges
}
}
/**
* Checks to make sure that there are enough 'end' entries for the number of cycle operators on a node
* @param id
* @param endIds
* @param ops
* @param opSearchArr
* @return {boolean}
*/
function enoughEnds(id, endIds, ops, opSearchArr) {
let numEnds = ops.filter(op => opSearchArr.includes(op)).length;
let numFound = endIds.filter(i => i === id).length;
return numEnds === numFound;
}
/**
* Iterate through operators and add to stack (unless Or)
* @param stateGraph
* @param stack
* @param id
* @param endIds
* @param edgeUsed
* @param pMap
* @return {*}
*/
function handleOperations(stateGraph, stack, id, endIds, edgeUsed, pMap) {
let lastEdge;
let ops = stateGraph.getOperators(id);
// handle THENs first
if (ops.includes(constants.THEN)) {
stack.push({[constants.THEN]: id});
}
// then handle anything except for ORs
for (let operation of ops) {
if (operation !== constants.OR && operation !== constants.THEN) {
stack.push({[operation]: id});
}
}
// then handle ORs
for (let operation of ops) {
if (operation === constants.OR) {
let temp = traverseOr(stateGraph, stack, id, endIds, edgeUsed, pMap);
if (temp !== undefined && temp !== null) {
lastEdge = temp;
}
}
}
return lastEdge;
}
/**
* Checks parent nodes to see if any of them have the operator you're looking for
* (used ofr zero-or-one)
* @param stateGraph
* @param parentArr
* @param op
* @return {null}
*/
function parentWithOp(stateGraph, parentArr, op) {
for (let p of parentArr) {
let parentOps = stateGraph.getOperators(p);
if (parentOps.includes(op)) {
return p;
}
}
return null;
}
/**
* The OR operator must be done depth first because you need to get to the end of the path
* @param stateGraph Edge-based graph representation of design space
* @param stateStack Equivalent representation of the stateGraph to help with SBOL generation
* @param id The id to start the traverse from
* @param endIds List of IDs that mark the end of a path
* @param edgeUsed the edge used to get to stateGraph[id] (edgeUsed.dest should be id)
* @returns {*}
*/
function traverseOr(stateGraph, stateStack, id, endIds, edgeUsed, pMap) {
let orStack = [];
let lastEdge;
stateStack.push({[constants.OR]: orStack});
stateGraph.get(id).visited = true;
let nonCycles = stateGraph.getEdges(id).filter(e => e.dest !== edgeUsed.src);
nonCycles.forEach(function (edge) {
let orSubStack = [];
let temp = traverseOrEdges(stateGraph, stateStack, orSubStack, edge.dest, edge, endIds, edgeUsed, pMap);
if (temp !== undefined && temp !== null) {
lastEdge = temp;
}
if (orSubStack.length !== 0) {
orStack.push(orSubStack);
}
});
return lastEdge;
}
function traverseOrEdges(stateGraph, stack, subStack, id, edgeUsed, endIds, prevEdgeUsed, pMap, moreStartId, ZOStart, ZOEnd) {
let ops = stateGraph.getOperators(id);
if (stateGraph.get(id).visited) {
// if the MORE started within the OR, then end it on this stack
if (id === moreStartId) {
subStack.push({end: id});
endIds.push(id);
} // else end it on the original stack
else if (ops.includes(constants.ONE_MORE)
|| ops.includes(constants.ZERO_MORE)) {
if (stack[stack.length - 1].end !== id) {
stack.push({end: id});
if (!endIds.includes(id)) {
endIds.push(id);
}
}
} else if (edgeUsed.type === constants.ATOM) { // in case of zero-or-one
subStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.orientation});
}
return null;
}
if (edgeUsed.type === constants.ATOM) {
if (edgeUsed.orientation === constants.INLINE) {
subStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.INLINE_URI});
} else {
subStack.push({[constants.ATOM]: edgeUsed.text, orientation: constants.REV_COMP_URI});
}
}
if (id === ZOEnd) {
subStack.push({end: ZOStart});
endIds.push(ZOStart);
}
if (ops.length === 1 &&
(ops[0] === constants.ONE_MORE ||
ops[0] === constants.ZERO_MORE)) {
moreStartId = id;
}
if (ops.includes(constants.ZERO_ONE)) {
let epsEdges = stateGraph.getEdges(id).filter(e => e.component === constants.EPSILON);
if (epsEdges.length === 1) {
ZOEnd = epsEdges[0].dest;
ZOStart = id;
}
}
if (edgeUsed.type === constants.EPSILON && ops.includes(constants.THEN)) {
stateGraph.get(id).visited = false; //reset
return id; //a 'then' that's not on the atom is not part of the OR
}
let lastEdge;
for (let operation of ops) {
if (operation === constants.OR){
lastEdge = traverseOr(stateGraph, subStack, id, endIds, prevEdgeUsed);
} else {
subStack.push({[operation]: id});
}
}
stateGraph.get(id).visited = true;
if (lastEdge) { //from OR
id = lastEdge;
handleOperations(stateGraph, subStack, id, endIds, prevEdgeUsed, pMap);
}
for (let edge of stateGraph.getEdges(id)) {
lastEdge = traverseOrEdges(stateGraph, stack, subStack, edge.dest, edge, endIds, prevEdgeUsed, pMap, moreStartId, ZOStart, ZOEnd);
}
return lastEdge; //the last node of the OR chain
}
/**
* Generates SBOL 2.2 representation of a design space using the combinatorial derivation extension
* @param stateGraph Edge-based graph representation of design space
* @param categories Categories of biological parts provided by the user
* @param designName Name of the design space
* @param numDesigns Maximum number of designs to be enumerated
* @param maxCycles Max number of iterations through cycles
* @returns {string} SBOL XML document
*/
function generateCombinatorialSBOL(stateGraph, categories, designName, numDesigns, maxCycles){
let sbolDoc = new SbolGeneration(designName);
// traverse stateGraph and mark all as not visited
//create ComponentDefinition for every atom and add to atomMap
stateGraph.nodes.forEach(function(id){
stateGraph.get(id).visited = false;
for (let edge of stateGraph.getEdges(id)) {
let atomType = edge.type;
let atomText = edge.text;
if (atomType === constants.ATOM && !(atomText in sbolDoc.atomMap)){
// add atom to atomMap
if (!sbolDoc.atomMap[atomText]) {
sbolDoc.atomMap[atomText] = {};
}
/** @type {Collection} */
let collection = sbolDoc.makeCollection(atomText);
// if no IDs in a role, create member for the role instead
for (let role in categories[atomText]) {
// if abstract part
if (categories[atomText][role].length === 0) {
let abstractCD = sbolDoc.makeAtomComponentDefinition(role, [role], true);
collection.addMember(abstractCD);
if (!sbolDoc.atomMap[atomText].collections){
sbolDoc.atomMap[atomText].collections = [];
}
sbolDoc.atomMap[atomText].abstract = true;
// for abstract parts, store the CD in the atom map to reference later
sbolDoc.atomMap[atomText].compDef = abstractCD;
continue;
}
// if not abstract part
for (let id of categories[atomText][role]) {
collection.addMember(sbolDoc.makeAtomComponentDefinition(id, [role]));
if (!sbolDoc.atomMap[atomText].collections){
sbolDoc.atomMap[atomText].collections = [];
}
}
sbolDoc.atomMap[atomText].abstract = false;
}
sbolDoc.atomMap[atomText].collections.push(collection);
}
}
});
//create a stateStack from the stateGraph
const rootId = stateGraph.root;
let dummyEdge = {'src': 'dummy',
'dest': rootId,
'component': constants.EPSILON,
'type': constants.EPSILON,
'text': constants.EPSILON
};
let stateStack = [];
makeStackFromStateGraph(stateGraph, stateStack, rootId, dummyEdge);
//use stateStack to generate SBOL
let componentDefStack = [];
sbolDoc.makeSBOLFromStack(stateStack, componentDefStack);
// Make root CD and CV
const rootTemplate = sbolDoc.makeComponentDefinition(designName, [], false, true);
const rootCV = sbolDoc.addComponentsAndRestraintsToTemplate(rootTemplate, componentDefStack, null, designName, true);
// add custom attributes to root CV
// Github README should explain the custom attributes
rootCV.addStringAnnotation(constants.CONSTELLATION_GIT + constants.DELIMITER + "numDesigns", numDesigns);
rootCV.addStringAnnotation(constants.CONSTELLATION_GIT + constants.DELIMITER + "maxCycles", maxCycles);
//clean up
sbolDoc.sequenceConstraintCount = 1;
sbolDoc.orRegionCount = 1;
sbolDoc.cycleRegionCount = 1;
for (let atom in sbolDoc.atomMap) delete sbolDoc.atomMap[atom];
return sbolDoc.doc.serializeXML();
}
let sbol = generateCombinatorialSBOL;
module.exports = sbol;