[英]Read Turtle file and query the graph using SPARQL
I am in a learning phase of SPARQL so excuse if the question sounds basic.我正处于 SPARQL 的学习阶段,如果这个问题听起来很基础,请原谅。 I am exploring SPARQLWrapper to connect to a SPARQL endpoint to fetch some data.我正在探索 SPARQLWrapper 以连接到 SPARQL 端点以获取一些数据。 Below is the query.下面是查询。
sparql = SPARQLWrapper("http://patho.phenomebrowser.net/sparql/") #endpoint link
sparql.setQuery("""
PREFIX SIO: <http://semanticscience.org/resource/SIO_>
PREFIX RO: <http://purl.obolibrary.org/obo/RO_>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT distinct ?PA_ID ?pathogen ?D_ID ?disease ?disease_pathogen_evidence_code ?P_ID ?phenotype ?disease_phenotype_evidence_code
FROM <http://patho.phenomebrowser.net>
WHERE
{
?D_ID SIO:000255 ?o1 .
?o1 RO:0002558 ?EC1_ID .
?o1 RO:0002200 ?P_ID .
?D_ID SIO:000255 ?o .
?o RO:0002558 ?EC2_ID .
?o RO:0002556 ?PA_ID .
?PA_ID rdfs:label ?pathogen.
?P_ID rdfs:label ?phenotype .
?D_ID rdfs:label ?disease .
?EC1_ID rdfs:label ?disease_phenotype_evidence_code .
?EC2_ID rdfs:label ?disease_pathogen_evidence_code .
} limit 50
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["pathogen"]["value"])
The query above work fine.上面的查询工作正常。 Is there a way to load or read a Turtle file and then use SPARQLWrapper to query the graph?有没有办法加载或读取 Turtle 文件,然后使用 SPARQLWrapper 查询图形?
To be able to query a graph with SPARQLWrapper, you need to load this graph in a SPARQL endpoint.为了能够使用 SPARQLWrapper 查询图,您需要将此图加载到 SPARQL 端点中。 Such an endpoint could be hosted locally, eg:这样的端点可以在本地托管,例如:
sparql = SPARQLWrapper("http://localhost/sparql")
The documentation lists how certain endpoints can be used with SPARQLWrapper: SPARQL Endpoint Implementations文档列出了某些端点如何与 SPARQLWrapper 一起使用: SPARQL 端点实现
Instead of using SPARQLWrapper, you could consider using RDFLib (which gets used by SPARQLWrapper, and comes from the same developers), which allows you to query endpoints (using SERVICE
) as well as files (no need to load them in an endpoint).除了使用 SPARQLWrapper,您可以考虑使用 RDFLib(由 SPARQLWrapper 使用,并且来自相同的开发人员),它允许您查询端点(使用SERVICE
)以及文件(无需在端点中加载它们)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.