forked from lisestork/SFB-Annotator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdf2svg.py
executable file
·70 lines (64 loc) · 2.33 KB
/
rdf2svg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import argparse as argp
import os
import matplotlib.pyplot as plt
import networkx as nx
import rdflib
from rdflib.namespace import (RDF, RDFS, FOAF, DCTERMS)
# CLI
parser = argp.ArgumentParser(description='Plot RDF graph in SVG.')
parser.add_argument('-i',
'--input',
help='Input in RDF/Turtle.',
type=str,
required=True)
parser.add_argument('-o',
'--output',
help='Output in SVG.',
type=str)
args = parser.parse_args()
infile = args.input
basename = os.path.splitext(infile)[0]
outfile = args.output if args.output else basename + '.svg'
title = os.path.basename(infile)
print(outfile)
# set namespace prefixes
ANNOT = rdflib.Namespace("http://localhost:8080/rdf/nc/annotation/")
DCMITYPE = rdflib.Namespace("http://purl.org/dc/dcmitype/")
DSW = rdflib.Namespace("http://purl.org/dsw/")
DWC = rdflib.Namespace("http://rs.tdwg.org/dwc/terms/")
DWCIRI = rdflib.Namespace("http://rs.tdwg.org/dwc/iri/")
GN = rdflib.Namespace("http://sws.geonames.org/")
GBIF = rdflib.Namespace("http://www.gbif.org/species/")
IMG = rdflib.Namespace("http://localhost:8080/semanticAnnotator/files/")
OA = rdflib.Namespace("http://www.w3.org/ns/oa#")
OBO = rdflib.Namespace("http://purl.obolibrary.org/obo/")
# populate RDF graph
g = rdflib.Graph()
g.parse(infile, format='ttl')
g.bind("annot", ANNOT)
g.bind("dcterms", DCTERMS)
g.bind("dcmitype", DCMITYPE)
g.bind("dsw", DSW), FOAF
g.bind("img", IMG)
g.bind("oa", OA)
g.bind("obo", OBO)
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
# plot the graph
G = nx.DiGraph()
for (s, p, o) in g:
G.add_node(s.n3(g.namespace_manager), group=s.n3(g.namespace_manager))
G.add_node(o.n3(g.namespace_manager), group=o.n3(g.namespace_manager))
G.add_edge(s.n3(g.namespace_manager), o.n3(g.namespace_manager),
group=p.n3(g.namespace_manager))
pos = nx.drawing.layout.spring_layout(G, iterations=50)
plt.figure()
nx.draw(G, pos, with_labels=True, node_shape='o', node_size=10,
node_color='lightblue', edge_color="gray", width=0.2, font_size=4)
nx.draw_networkx_edge_labels(G, pos,
edge_labels=nx.get_edge_attributes(G, 'group'),
font_color='red', font_size=4)
plt.title(title)
plt.axis('off')
plt.savefig(outfile)