lvaudor / glitter

an R package which writes SPARQL queries
https://lvaudor.github.io/glitter
44 stars 5 forks source link

Optional graph pattern with several triples #102

Closed nleguillarme closed 12 months ago

nleguillarme commented 1 year ago

Hi

I need to write a query with an optional graph pattern that contains several triples, for instance:

spq_add("?value obo:IAO_0000039 ?trait_unit . ?trait_unit rdfs:label ?unit .", .required=FALSE)

But it seems that glitter cannot handle this kind of pattern, as it results in the following:

OPTIONAL {?value obo:IAO_0000039 ?trait_unit.}

I also tried using a blank node:

spq_add("?value obo:IAO_0000039 [rdfs:label ?unit]", .required=FALSE)

which results in the following error:

Can't find prefix(es) [rdfs. Please use spq_prefix().
lvaudor commented 1 year ago

Hi,

Yes, I guess we would write something like:

spq_add("?value obo:IAO_0000039 ?trait_unit", .required=FALSE) %>%
spq_add("?trait_unit rdfs:label ?unit", .required=FALSE)

We haven't allowed for the user to use blank nodes for now. I'm not sure we will either (but I'll make an issue to make sure we properly ask ourselves ;-) ).

maelle commented 1 year ago

related to #202

maelle commented 1 year ago

@nleguillarme for this particular pattern, where you're labelling a variable, after #205 you'd run something like

library("glitter")
spq_init() %>%     
  spq_prefix(prefixes = c(obo = "http://purl.obolibrary.org/obo/")) %>%
  spq_add("?value obo:IAO_0000039 ?trait_unit", .required = FALSE) %>%
  spq_label(trait_unit)
#> PREFIX obo: <http://purl.obolibrary.org/obo/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?trait_unit (COALESCE(?trait_unit_labell,'') AS ?trait_unit_label)
#> ?value
#> WHERE {
#> 
#> OPTIONAL {
#> ?value obo:IAO_0000039 ?trait_unit.
#> 
#> OPTIONAL {
#> ?trait_unit rdfs:label ?trait_unit_labell.
#> FILTER(lang(?trait_unit_labell) IN ('en'))
#> }
#> 
#> }
#> 
#> }

Created on 2023-10-19 with reprex v2.0.2

maelle commented 1 year ago

Or

library("glitter")
spq_init() %>%     
  spq_prefix(prefixes = c(obo = "http://purl.obolibrary.org/obo/")) %>%
  spq_add("?value obo:IAO_0000039 ?trait", .required = FALSE) %>%
  spq_label(trait, .overwrite = TRUE)
#> PREFIX obo: <http://purl.obolibrary.org/obo/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?value (COALESCE(?trait_labell,'') AS ?trait)
#> WHERE {
#> 
#> OPTIONAL {
#> ?value obo:IAO_0000039 ?trait0.
#> 
#> OPTIONAL {
#> ?trait0 rdfs:label ?trait_labell.
#> FILTER(lang(?trait_labell) IN ('en'))
#> }
#> 
#> }
#> 
#> }

Created on 2023-10-19 with reprex v2.0.2

maelle commented 1 year ago

And a tad more cumbersome:

library("glitter")
spq_init() %>%     
  spq_prefix(prefixes = c(obo = "http://purl.obolibrary.org/obo/")) %>%
  spq_add("?value obo:IAO_0000039 ?trait_unit", .required = FALSE) %>%
  spq_add("?trait_unit rdfs:label ?unit", .sibling_triple_pattern = "?value obo:IAO_0000039 ?trait_unit")
#> PREFIX obo: <http://purl.obolibrary.org/obo/>
#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
#> SELECT ?trait_unit ?unit ?value
#> WHERE {
#> 
#> OPTIONAL {
#> ?value obo:IAO_0000039 ?trait_unit.
#> 
#> ?trait_unit rdfs:label ?unit.
#> }
#> 
#> }

Created on 2023-10-19 with reprex v2.0.2