related-sciences / nxontology

NetworkX-based Python library for representing ontologies
Apache License 2.0
81 stars 8 forks source link

`imports.from_file` fails on Allen Mouse Brain Structural Ontology (StructureGraph) #12

Closed THinnerichs closed 2 years ago

THinnerichs commented 3 years ago

Hey there, thank you very much for already fixing the isolated roots and leaves issue for ontologies given as owl files, which took me some time on my own.

However, I have another issue with your JSON parser. Please run imports.from_file on the Allen Mouse Brain Structural Ontology, throwing an error in the newest nxontology version. Parsed it myself already, but couldn't find a fix to your one.

Best

dhimmel commented 3 years ago

Here's the head of the file at http://api.brain-map.org/api/v2/structure_graph_download/1.json:

{
  "success": true,
  "id": 0,
  "start_row": 0,
  "num_rows": 1,
  "total_rows": 1,
  "msg": [
    {
     "id": 997,
     "atlas_id": -1,
     "ontology_id": 1,
     "acronym": "root",
     "name": "root",
     "color_hex_triplet": "FFFFFF",
     "graph_order": 0,
     "st_level": 0,
     "hemisphere_id": 3,
     "parent_structure_id": null,
     "children": [
      {
       "id": 8,
       "atlas_id": 0,
       "ontology_id": 1,
       "acronym": "grey",
       "name": "Basic cell groups and regions",
       "color_hex_triplet": "BFDAE3",
       "graph_order": 1,
       "st_level": 1,
       "hemisphere_id": 3,
       "parent_structure_id": 997,
       "children": [
        {
         "id": 567,
         "atlas_id": 70,
         "ontology_id": 1,
         "acronym": "CH",
         "name": "Cerebrum",
         "color_hex_triplet": "B0F0FF",
         "graph_order": 2,
         "st_level": 2,
         "hemisphere_id": 3,
         "parent_structure_id": 8,
         "children": [
          {

This doesn't look like OBO Graphs JSON which is the only JSON format from_file supports.

From Allen's page downloading an Ontology's StructureGraph, it appears that they are calling this format a "StructureGraph". I don't think this is a standard graph or ontology encoding format.

Parsed it myself already

Sounds like you've already written a parser for this format. You can always use your parser to import to nxontology or networkx:

from nxontology import NXOntology
nxo = NXOntology
# add nodes
for node in nodes:
    nxo.add_node(node)
# add edges
for parent, child in edges:
    nxo.add_edge(parent, child)

If you want a more permissive constructor with greater chance for bugs, you can do:

import networkx as nx
graph = nx.DiGraph()
# add nodes
for node in nodes:
    graph.add_node(node)
# add edges
for parent, child in edges:
    graph.add_edge(parent, child)
nxo = NXOntology(graph)