Skip to content

Commit d32ed2b

Browse files
committed
Fix OBO parsing to resolve parent-child relationships after all terms are parsed
Added a two-pass parsing approach in `Ontology.parseStanza()` to handle cases where parent terms appear after child terms in OBO files. Unresolved is_a links are now collected during the first pass and properly resolved once all terms are loaded.
1 parent 42c7b78 commit d32ed2b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/app/obo/Ontology.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export class Ontology {
146146
private parseStanza(lines: string[]) {
147147
// Regular expression to match stanza headers
148148
const stanzaRegex = /^\[(\w+)\]/;
149+
// Array to hold unresolved parent-child relationships
150+
const unresolvedLinks: { childId: string; parentId: string }[] = [];
149151
// Object mapping term properties to their setter functions
150152
const termProperties: {
151153
[key: string]: { setter: (value: string) => void };
@@ -161,17 +163,15 @@ export class Ontology {
161163
},
162164
is_a: {
163165
setter: (parentID: string) => {
164-
const parent = this.stanzas.find((term) => term.id === parentID);
165-
if (parent) {
166-
parent.addChild(ontologyTerm);
167-
ontologyTerm.addParent(parent);
168-
}
166+
// Record the relationship for later resolution
167+
unresolvedLinks.push({ childId: ontologyTerm.id, parentId: parentID });
169168
},
170169
},
171170
};
172171

173172
let ontologyTerm: TermStanza = new TermStanza();
174173
let line;
174+
// First pass: parse terms and their properties
175175
while ((line = lines.shift()) !== undefined) {
176176
let matched = false;
177177
// Split the line into tag, value, and comment
@@ -195,6 +195,16 @@ export class Ontology {
195195
}
196196
// Add the last term to the ontology
197197
this.addTerm(ontologyTerm);
198+
199+
// Second pass: resolve parent-child relationships
200+
unresolvedLinks.forEach(({ childId, parentId }) => {
201+
const child = this.findTermById(childId);
202+
const parent = this.findTermById(parentId);
203+
if (child && parent) {
204+
parent.addChild(child);
205+
child.addParent(parent);
206+
}
207+
});
198208
}
199209

200210
private kebabToCamel(s: string): string {

0 commit comments

Comments
 (0)