Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/rdflib/tools/rdf2dot.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac |
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:26e78fe6e8c4 |
|---|---|
| 1 """ | |
| 2 A commandline tool for drawing RDF graphs in Graphviz DOT format | |
| 3 | |
| 4 You can draw the graph of an RDF file directly: | |
| 5 | |
| 6 .. code-block: bash | |
| 7 | |
| 8 rdf2dot my_rdf_file.rdf | dot -Tpng | display | |
| 9 | |
| 10 """ | |
| 11 | |
| 12 import rdflib | |
| 13 import rdflib.extras.cmdlineutils | |
| 14 | |
| 15 import sys | |
| 16 import cgi | |
| 17 import collections | |
| 18 | |
| 19 from rdflib import XSD | |
| 20 | |
| 21 LABEL_PROPERTIES = [rdflib.RDFS.label, | |
| 22 rdflib.URIRef("http://purl.org/dc/elements/1.1/title"), | |
| 23 rdflib.URIRef("http://xmlns.com/foaf/0.1/name"), | |
| 24 rdflib.URIRef("http://www.w3.org/2006/vcard/ns#fn"), | |
| 25 rdflib.URIRef("http://www.w3.org/2006/vcard/ns#org") | |
| 26 ] | |
| 27 | |
| 28 XSDTERMS = [ | |
| 29 XSD[x] for x in ( | |
| 30 "anyURI", "base64Binary", "boolean", "byte", "date", | |
| 31 "dateTime", "decimal", "double", "duration", "float", "gDay", "gMonth", | |
| 32 "gMonthDay", "gYear", "gYearMonth", "hexBinary", "ID", "IDREF", | |
| 33 "IDREFS", "int", "integer", "language", "long", "Name", "NCName", | |
| 34 "negativeInteger", "NMTOKEN", "NMTOKENS", "nonNegativeInteger", | |
| 35 "nonPositiveInteger", "normalizedString", "positiveInteger", "QName", | |
| 36 "short", "string", "time", "token", "unsignedByte", "unsignedInt", | |
| 37 "unsignedLong", "unsignedShort")] | |
| 38 | |
| 39 EDGECOLOR = "blue" | |
| 40 NODECOLOR = "black" | |
| 41 ISACOLOR = "black" | |
| 42 | |
| 43 | |
| 44 def rdf2dot(g, stream, opts={}): | |
| 45 """ | |
| 46 Convert the RDF graph to DOT | |
| 47 writes the dot output to the stream | |
| 48 """ | |
| 49 | |
| 50 fields = collections.defaultdict(set) | |
| 51 nodes = {} | |
| 52 | |
| 53 def node(x): | |
| 54 | |
| 55 if x not in nodes: | |
| 56 nodes[x] = "node%d" % len(nodes) | |
| 57 return nodes[x] | |
| 58 | |
| 59 def label(x, g): | |
| 60 | |
| 61 for labelProp in LABEL_PROPERTIES: | |
| 62 l = g.value(x, labelProp) | |
| 63 if l: | |
| 64 return l | |
| 65 | |
| 66 try: | |
| 67 return g.namespace_manager.compute_qname(x)[2] | |
| 68 except: | |
| 69 return x | |
| 70 | |
| 71 def formatliteral(l, g): | |
| 72 v = cgi.escape(l) | |
| 73 if l.datatype: | |
| 74 return '"%s"^^%s' % (v, qname(l.datatype, g)) | |
| 75 elif l.language: | |
| 76 return '"%s"@%s' % (v, l.language) | |
| 77 return '"%s"' % v | |
| 78 | |
| 79 def qname(x, g): | |
| 80 try: | |
| 81 q = g.compute_qname(x) | |
| 82 return q[0] + ":" + q[2] | |
| 83 except: | |
| 84 return x | |
| 85 | |
| 86 def color(p): | |
| 87 return "BLACK" | |
| 88 | |
| 89 stream.write("digraph { \n node [ fontname=\"DejaVu Sans\" ] ; \n") | |
| 90 | |
| 91 for s, p, o in g: | |
| 92 sn = node(s) | |
| 93 if p == rdflib.RDFS.label: | |
| 94 continue | |
| 95 if isinstance(o, (rdflib.URIRef, rdflib.BNode)): | |
| 96 on = node(o) | |
| 97 opstr = "\t%s -> %s [ color=%s, label=< <font point-size='10' " + \ | |
| 98 "color='#336633'>%s</font> > ] ;\n" | |
| 99 stream.write(opstr % (sn, on, color(p), qname(p, g))) | |
| 100 else: | |
| 101 fields[sn].add((qname(p, g), formatliteral(o, g))) | |
| 102 | |
| 103 for u, n in list(nodes.items()): | |
| 104 stream.write("# %s %s\n" % (u, n)) | |
| 105 f = ["<tr><td align='left'>%s</td><td align='left'>%s</td></tr>" % | |
| 106 x for x in sorted(fields[n])] | |
| 107 opstr = "%s [ shape=none, color=%s label=< <table color='#666666'" + \ | |
| 108 " cellborder='0' cellspacing='0' border='1'><tr>" + \ | |
| 109 "<td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>" + \ | |
| 110 "<td href='%s' bgcolor='#eeeeee' colspan='2'>" + \ | |
| 111 "<font point-size='10' color='#6666ff'>%s</font></td>" + \ | |
| 112 "</tr>%s</table> > ] \n" | |
| 113 stream.write(opstr % (n, NODECOLOR, label(u, g), u, u, "".join(f))) | |
| 114 | |
| 115 stream.write("}\n") | |
| 116 | |
| 117 | |
| 118 def _help(): | |
| 119 sys.stderr.write(""" | |
| 120 rdf2dot.py [-f <format>] files... | |
| 121 Read RDF files given on STDOUT, writes a graph of the RDFS schema in DOT | |
| 122 language to stdout | |
| 123 -f specifies parser to use, if not given, | |
| 124 | |
| 125 """) | |
| 126 | |
| 127 | |
| 128 def main(): | |
| 129 rdflib.extras.cmdlineutils.main(rdf2dot, _help) | |
| 130 | |
| 131 if __name__ == '__main__': | |
| 132 main() |
