Fetching the disease ontology level

Hi kind people of the OT community!
I wanted to know if it is possible, using the API, to fetch the level of a disease in the disease ontology?
So for example, in the image - colitis is positioned 5th from the root. Is it possible to get this number using the API?

Hi Aner,

We don’t provide the level in the API. It is relatively complicated to get, as you can:

  • Have different levels of depth depending on which Therapeutic Area you use as root (diseases can have more than one).
  • Have multiple paths with different lengths from the disease to the Therapeutic Area .

In the next release (which is coming very soon, stay tuned!), we’ve changed the disease object to include a resolvedAncestors field which contains the list of all ancestors up to therapeutic areas as resolvable objects. With that you will be able to query like:

{
  disease(efoId: "MONDO_0011913") {
    id
    parents {
      id
    }
    resolvedAncestors {
      id
      parents {
        id
      }
    }
  }
}

And you would be able to get the level for every Therapeutic Area relevant to the disease by computing a DFS for each and storing the longest path. Here is an example in Python:

import requests

def get_node_levels(data):
    disease = data['data']['disease']
    roots = {n['id']: 1 for n in disease['resolvedAncestors'] if not n['parents']}
    nodes = {disease['id']: disease, **{n['id']: n for n in disease['resolvedAncestors']}}

    def max_path_to_root(current_id, root_id, visited=None):
        if visited is None:
            visited = set()
        if current_id == root_id:
            return 1
        if current_id in visited:
            return -1
        visited.add(current_id)
        paths = [max_path_to_root(p['id'], root_id, visited.copy()) for p in nodes[current_id]['parents']]
        valid_paths = [p + 1 for p in paths if p != -1]
        return max(valid_paths) if valid_paths else -1

    for root_id in roots:
        max_path = max_path_to_root(disease['id'], root_id)
        if max_path != -1:
            roots[root_id] = max_path
    return roots

query = lambda d: requests.post(
    "https://platform.opentargets.org/api/v4/graphql",
    json={
        "query": """
        {
            disease(efoId: "%s") {
                id
                parents {
                    id
                }
                resolvedAncestors {
                    id
                    parents {
                        id
                    }
                }
            }
        }
        """ % d
    }
).json()

print(get_node_levels(query("MONDO_0011913")))
❯ python levels.py
{'MONDO_0002025': 8, 'OTAR_0000018': 7, 'EFO_0000618': 10}

I’ve picked up a disease with a relatively complex graph, but there might be some that are even worse.

1 Like

Thank you. I think that the “resolvedAncestors” object would work great. Looking forward for that.