我对 OWL 和 RDFlib 相当陌生,所以如果我的术语有问题,我深表歉意。我有一个 CSV,我想将其转换为知识图,其中每一行都成为一种顶级主题,然后具有与其链接的属性。虽然对于问题来说并不重要,但知识图描述了一些生物测定,这就是我的示例“IRIs”的来源。
我的基本结构是:
assay1 a obo:assay;
rdfs:comment "Some comment" ;
BAO:has-endpoint BAO:endpoint .
我想做的是添加:
BAO:endpoint SIO:has-value "5" ;
SIO:has-unit "mg" .
和:
BAO:endpoint CHEMINF:has-uncertainty NCIT:StatisticalDispersion .
NCIT:StatisticalDispersion SIO:has-value "2" ;
SIO:has-unit "mg" .
使得那些额外的三元组仍然与分析1相关。这个想法是每个检测都有它自己的值,但我不太确定如何使用 RDFlib 来实现它。我猜 BNodes 是我的解决方案,但这是否意味着完全放弃像 NCIT:StatisticalDispersion 这样的东西?
我猜
<assay1>
是你的csv文件,BAO:endpoint
等于你的行之一:
from rdflib import Graph, Namespace, Literal, URIRef, BNode, RDF, RDFS
OBO = Namespace("http://your/namespace#")
BAO = Namespace(...)
CHEMINF = Namespace(...)
SIO = Namespace(...)
def main(csv_data):
#initialize your graph with some info about your csv-file
id_file = URIRef("assay1")
g = Graph()
g.add((id_file, RDF.type, OBO.assay))
g.add((id_file, RDFS.comment, Literal("comment")))
#add information for each row
for row in csv_data:
add_information(row, g)
#print using the namespace shortcuts
g.bind("OBO", OBO)
g.bind("BAO", BAO)
g.bind("SIO", SIO)
print(g.serialize())
#These functions extract info from your row
def uri_from(x) -> URIRef:
...
return BAO.endpoint
def extract_value(x) -> (Literal, Literal):
...
return Literal(5), Literal("mg")
def add_information(x, g):
id_array = uri_from(x) # in your example BAO:endpoint
# alternative: id_array = BNode()
val, unit = extract_value(x) # eg (Literal(5), Literal("mg"))
g.add((id_array, SIO["has-value"], val))
g.add((id_array, SIO["has-unit"], unit))
# you use instead of NCIT:StatisticalDispersion a BNode
statistic_info = BNode()
g.add((id_array, CHEMINF.has-uncertainty, statistic_info))
val_unc, unit_unc = extract_uncertainty(x)
g.add((statistic_info, SIO.value, val_unc))
g.add((statistic_info, SIO.unit, unit_unc))