Skip to content

Commit 03bf4cd

Browse files
committed
refactor _extract_class_hierarchy
1 parent a95415b commit 03bf4cd

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

chebai/preprocessing/datasets/chebi.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -758,27 +758,18 @@ def _extract_class_hierarchy(self, chebi_path: str) -> nx.DiGraph:
758758
"""
759759
Extracts a subset of ChEBI based on subclasses of the top class ID.
760760
761+
This method calls the superclass method to extract the full class hierarchy,
762+
then extracts the subgraph containing only the descendants of the top class ID, including itself.
763+
761764
Args:
762765
chebi_path (str): The file path to the ChEBI ontology file.
763766
764767
Returns:
765-
nx.DiGraph: The extracted class hierarchy as a directed graph.
768+
nx.DiGraph: The extracted class hierarchy as a directed graph, limited to the
769+
descendants of the top class ID.
766770
"""
767-
with open(chebi_path, encoding="utf-8") as chebi:
768-
chebi = "\n".join(l for l in chebi if not l.startswith("xref:"))
769-
elements = [
770-
term_callback(clause)
771-
for clause in fastobo.loads(chebi)
772-
if clause and ":" in str(clause.id)
773-
]
774-
g = nx.DiGraph()
775-
for n in elements:
776-
g.add_node(n["id"], **n)
777-
g.add_edges_from([(p, q["id"]) for q in elements for p in q["parents"]])
778-
779-
g = nx.transitive_closure_dag(g)
780-
g = g.subgraph(list(nx.descendants(g, self.top_class_id)) + [self.top_class_id])
781-
print("Compute transitive closure")
771+
g = super()._extract_class_hierarchy(chebi_path)
772+
g = g.subgraph(list(g.successors(self.top_class_id)) + [self.top_class_id])
782773
return g
783774

784775

0 commit comments

Comments
 (0)