-
Notifications
You must be signed in to change notification settings - Fork 8
/
nodeSBOL.js
240 lines (215 loc) · 7.98 KB
/
nodeSBOL.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
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 Node-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
*/
function makeStackFromStateGraph(stateGraph, stateStack, id){
let endIds = []; //prevent redundant ends
let queue = [];
queue.push(id);
while (queue.length !== 0){
let id = queue.shift();
let node = stateGraph.get(id);
if (node.visited) {
if (node.type !== constants.ATOM
&& !endIds.includes(id)) {
stateStack.push({end: id});
endIds.push(id);
}
continue;
}
if (node.type === constants.ACCEPT) {
continue;
}
if (node.type === constants.ATOM) {
if (node.orientation === constants.INLINE) {
stateStack.push({[constants.ATOM]: node.text, orientation: constants.INLINE_URI});
} else {
stateStack.push({[constants.ATOM]: node.text, orientation: constants.REV_COMP_URI});
}
}
// handle immediate cycles first (node 1 to node 2 and back)
let immediateCycles = stateGraph.getEdges(id).filter(e => stateGraph.get(e).visited);
for (let edgeID of immediateCycles) {
if (!endIds.includes(edgeID)) {
stateStack.push({end: edgeID});
endIds.push(edgeID);
}
}
let lastEdge = handleOperations(stateGraph, stateStack, id, endIds);
node.visited = true;
if (lastEdge){ //from OR
id = lastEdge;
handleOperations(stateGraph, stateStack, id, endIds);
}
queue.push(...stateGraph.getEdges(id));
}
}
function handleOperations(stateGraph, stack, id, endIds){
let lastEdge;
for (let operation of stateGraph.getOperators(id)){
if (operation === constants.OR){
lastEdge = traverseOr(stateGraph, stack, id, endIds);
} else {
stack.push({[operation]: id});
}
}
return lastEdge;
}
/**
* The OR operator must be done depth first because you need to get to the end of the path
* @param stateGraph Node-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
* @returns {*}
*/
function traverseOr(stateGraph, stateStack, id, endIds){
let orStack = [];
let lastEdge;
stateStack.push({[constants.OR]: orStack});
stateGraph.get(id).visited = true;
stateGraph.getEdges(id).forEach(function (edge){
let orSubStack = [];
lastEdge = traverseOrEdges(stateGraph, stateStack, orSubStack, edge, endIds);
if (orSubStack.length !== 0) {
orStack.push(orSubStack);
}
});
return lastEdge;
}
function traverseOrEdges(stateGraph, stack, subStack, id, endIds, moreStartId){
let node = stateGraph.get(id);
let ops = stateGraph.getOperators(id);
if (node.visited && endIds.includes(id)){
return null;
}
if (node.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)
|| ops.includes(constants.ZERO_ONE)){
stack.push({end: id});
endIds.push(id);
}
return null;
}
if (node.type === constants.ACCEPT){
return null;
}
if (node.type === constants.ATOM){
if (node.orientation === constants.INLINE) {
subStack.push({[constants.ATOM]: node.text, orientation: constants.INLINE_URI});
} else {
subStack.push({[constants.ATOM]: node.text, orientation: constants.REV_COMP_URI});
}
}
if (ops.length === 1 &&
(ops[0] === constants.ONE_MORE ||
ops[0] === constants.ZERO_MORE ||
ops[0] === constants.ZERO_ONE)){
moreStartId = id;
}
let lastEdge;
for (let operation of ops){
if (node.type === constants.EPSILON
&& operation === constants.THEN){
node.visited = false; //reset
return id; //a 'then' that's not on the atom is not part of the OR
}
if (operation === constants.OR){
lastEdge = traverseOr(stateGraph, subStack, id, endIds);
} else {
subStack.push({[operation]: id});
}
}
node.visited = true;
if (lastEdge){ //from OR
id = lastEdge;
handleOperations(stateGraph, subStack, id, endIds);
}
for (let edge of stateGraph.getEdges(id)){
lastEdge = traverseOrEdges(stateGraph, stack, subStack, edge, endIds, moreStartId);
}
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 Node-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){
let currentNode = stateGraph.get(id);
currentNode.visited = false;
let atomType = currentNode.type;
let atomText = currentNode.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);
for (let role in categories[atomText]) {
// if no IDs in a role, create member for the role instead (abstract)
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
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 stateStack = [];
makeStackFromStateGraph(stateGraph, stateStack, rootId);
//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;