Skip to content

Commit

Permalink
Add utility for loading as a graph
Browse files Browse the repository at this point in the history
References #35
  • Loading branch information
cthoyt committed Dec 29, 2020
1 parent 46aa7f9 commit c6843a2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
layout: page
title: Graph
permalink: /graph/
---
This chart shows various aspects of the positive mappings graph. Larger components means that more extensive curation
has been done. Higher density components means that they have been checked multiple times.

<img src="https://raw.githubusercontent.com/biomappings/biomappings/main/docs/img/components.png" alt="Comparison"/>
Binary file added docs/img/components.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ keywords =

[options]
install_requires =
networkx
requests
click
pyyaml
Expand Down
72 changes: 72 additions & 0 deletions src/biomappings/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-

"""Load Biomappings as a graph."""

import os

import matplotlib.pyplot as plt
import networkx as nx
import seaborn as sns

from biomappings.resources import load_mappings
from biomappings.utils import IMG, MiriamValidator


def get_positive_graph() -> nx.Graph:
"""Get a graph of the positive mappings."""
v = MiriamValidator()
graph = nx.Graph()
for mapping in load_mappings():
source_curie = v.get_curie(mapping['source prefix'], mapping['source identifier'])
graph.add_node(
source_curie,
prefix=mapping['source prefix'],
identifier=mapping['source identifier'],
name=mapping['source name'],
)
target_curie = v.get_curie(mapping['target prefix'], mapping['target identifier'])
graph.add_node(
target_curie,
prefix=mapping['target prefix'],
identifier=mapping['target identifier'],
name=mapping['target name'],
)
graph.add_edge(
source_curie,
target_curie,
provenance=mapping['source'],
type=mapping['type'],
)
return graph


def _main():
fig, axes = plt.subplots(1, 3, figsize=(10, 3.5))
graph = get_positive_graph()

sizes = [len(c) for c in nx.connected_components(graph)]
sns.histplot(sizes, ax=axes[0])
axes[0].set_yscale('log')
axes[0].set_title('Component Node Sizes')

sizes = [graph.subgraph(c).number_of_edges() for c in nx.connected_components(graph)]
sns.histplot(sizes, ax=axes[1])
axes[1].set_yscale('log')
axes[1].set_title('Component Edge Sizes')
axes[1].set_ylabel('')

densities = [nx.density(graph.subgraph(c)) for c in nx.connected_components(graph)]
sns.histplot(densities, ax=axes[2], kde=True)
axes[2].set_xlim([0.0, 1.0])
axes[2].set_title('Component Densities')
axes[2].set_ylabel('')

path = os.path.join(IMG, 'components.png')
print('saving to', path)
plt.tight_layout()
plt.savefig(path, dpi=300)
plt.close(fig)


if __name__ == '__main__':
_main()
3 changes: 1 addition & 2 deletions src/biomappings/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import os
from typing import Dict, Iterable, List, Mapping, Sequence, Tuple

from biomappings.utils import get_canonical_tuple
from biomappings.utils import RESOURCE_PATH, get_canonical_tuple

RESOURCE_PATH = os.path.dirname(os.path.abspath(__file__))
MAPPINGS_HEADER = [
'source prefix',
'source identifier',
Expand Down
4 changes: 4 additions & 0 deletions src/biomappings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import requests

HERE = os.path.dirname(os.path.abspath(__file__))
RESOURCE_PATH = os.path.abspath(os.path.join(HERE, 'resources'))
IMG = os.path.abspath(os.path.join(HERE, os.pardir, os.pardir, 'docs', 'img'))


def get_git_hash() -> Optional[str]:
"""Get the git hash.
Expand Down

0 comments on commit c6843a2

Please sign in to comment.