In [41]:
import networkx as nx
import requests
import json

query_all = """
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                PREFIX dct: <http://purl.org/dc/terms/>
                PREFIX prov: <http://www.w3.org/ns/prov#>
                PREFIX qb: <http://purl.org/linked-data/cube#>
                
    SELECT ?dataset ?structure ?component ?dimension ?codelist ?code ?dimension2 ?codelist2 ?code2 WHERE {
        ?dataset qb:structure ?structure .
        ?structure qb:component ?component .
        ?component qb:dimension ?dimension .
        ?dimension qb:codeList ?codelist .
        {?codelist skos:member ?code . } UNION {?code skos:inScheme ?codelist .}
        OPTIONAL {?dimension rdfs:subPropertyOf ?dimension2 }
        OPTIONAL {?codelist prov:wasDerivedFrom ?codelist2 }
        OPTIONAL {?code skos:exactMatch ?code2 }
    }

"""

query = """
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                PREFIX dct: <http://purl.org/dc/terms/>
                PREFIX prov: <http://www.w3.org/ns/prov#>
                PREFIX qb: <http://purl.org/linked-data/cube#>
                
    SELECT ?dataset ?structure ?component ?dimension ?codelist ?code ?dimension2 ?codelist2 ?code2 WHERE {
        ?dataset qb:structure ?structure .
        ?structure qb:component ?component .
        ?component qb:dimension ?dimension .
        ?dimension qb:codeList ?codelist .
        {?codelist skos:member ?code . } UNION {?code skos:inScheme ?codelist .}
        { ?dimension rdfs:subPropertyOf ?dimension2 . } UNION { ?dimension2 rdfs:subPropertyOf ?dimension .}
        { ?codelist prov:wasDerivedFrom ?codelist2 .} UNION { ?codelist2 prov:wasDerivedFrom ?codelist .}
        { ?code skos:exactMatch ?code2 . } UNION { ?code2 skos:exactMatch ?code . } 
    }

"""

### This is old style, but leaving for backwards compatibility with earlier versions of Stardog
QUERY_HEADERS = {
                    'Accept': 'application/sparql-results+json'
                }

result = requests.get('http://localhost:5830/qber/query',params={'query':query}, headers=QUERY_HEADERS)

results = json.loads(result.content)['results']['bindings']

In [58]:
g = nx.DiGraph()


for r in results:
    
    for k,v in r.items():
        if v['value'] == 'tag:stardog:api:':
            continue
        elif k in ['dataset','structure','component','dimension','codelist','code']:
            g.add_node(v['value'],{'name': v['value'], 'type': k, 'origin': r['dataset']['value']})
        elif not g.has_node(v['value']) :
            g.add_node(v['value'],{'name': v['value'], 'type': k, 'origin': 'external'})
        
    g.add_edge(r['dataset']['value'],r['structure']['value'])
    g.add_edge(r['structure']['value'],r['component']['value'])
    g.add_edge(r['component']['value'],r['dimension']['value'])
    g.add_edge(r['dimension']['value'],r['codelist']['value'])
    g.add_edge(r['codelist']['value'],r['code']['value'])
    
    if 'dimension2' in r:
        g.add_edge(r['dimension']['value'],r['dimension2']['value'])
        
    if 'codelist2' in r:
        g.add_edge(r['codelist']['value'],r['codelist2']['value'])
        
    if 'code2' in r and r['code2']['value'] != 'tag:stardog:api:':
        g.add_edge(r['code']['value'],r['code2']['value'])

In [59]:
from networkx.readwrite import json_graph

data = json_graph.node_link_data(g)

with open('graph.json','w') as f:
    json.dump(data,f)

In [ ]: