problem with subquery and optional part

1 view
Skip to first unread message

Bram

unread,
Feb 24, 2015, 11:01:16 AM2/24/15
to sta...@clarkparsia.com
Hi,

I'm building a fairly complex SPARQL query but I'm stuck on some part.

We have a medical ontology that has some information regarding infections and white bloodcell count (wbc). The general idea is to select a respiratory infection for which there is a wbc value in the last 24 hours that is below 75% of the highest value that was found within the infection period and 48 hours before that.

I have this query that works fine. It selects the peak value for the required infection.

PREFIX infection:    <http://www.uzalerting.intec.ugent.be/infection#>
PREFIX general:    <http://www.uzalerting.intec.ugent.be/general#>
PREFIX medication:    <http://www.uzalerting.intec.ugent.be/medication#>
PREFIX fevertype:    <http://www.uzalerting.intec.ugent.be/feverType#>
PREFIX bodytemperature:    <http://www.uzalerting.intec.ugent.be/bodyTemperature#>
PREFIX pao2fio2:    <http://www.uzalerting.intec.ugent.be/pao2fio2#>
PREFIX sputum:    <http://www.uzalerting.intec.ugent.be/sputum#>
PREFIX leucocytes:    <http://www.uzalerting.intec.ugent.be/leucocytes#>
PREFIX temporal:    <http://swrl.stanford.edu/ontologies/built-ins/3.3/temporal.owl#>
select *
where { 
    ?inf a infection:RespiratoryInfection .
    ?inf infection:hasInfectionEnd ?infend .
    ?inf infection:hasInfectionId ?infid .
    ?inf infection:isInfectionOf ?adm .
    ?adm a general:PatientAdmission .
    {
      select ?infid (count(?wbcmaxval) as ?peakwbcc) (max(?wbcmaxval) as ?peakwbc)
      where {
        ?inf a infection:RespiratoryInfection .
        ?inf infection:hasInfectionStart ?infstart .
        ?inf infection:hasInfectionEnd ?infend .
        ?inf infection:hasInfectionId ?infid .
        ?inf infection:isInfectionOf ?adm .
        ?adm a general:PatientAdmission .
        OPTIONAL {
          ?wbcmax leucocytes:isLeucocytesOf ?adm .
          ?wbcmax a leucocytes:Leucocytes .
          ?wbcmax temporal:hasTime ?wbcmaxtime .
          ?wbcmax general:hasDoubleValue ?wbcmaxval .
          FILTER(
            (
              ?infend <= "2013-02-01T00:00:00.0"^^xsd:dateTime &&
              ?wbcmaxtime <= ?infend &&
              ?wbcmaxtime >= (?infstart - "P2DT0H0M0S"^^xsd:dayTimeDuration)
            ) || (
              ?infend > "2013-02-01T00:00:00.0"^^xsd:dateTime &&
              ?wbcmaxtime <= "2013-02-01T00:00:00.0"^^xsd:dateTime &&
              ?wbcmaxtime >= (?infstart - "P2DT0H0M0S"^^xsd:dayTimeDuration)
            )
          )
        }
      }
      GROUP BY ?infid
    } .
}

Now I want to add a part that selects wbc values. For the given respiratory infection that are below 75% of the peak value. Afterwards I only need to check if any such values were found so I will again group by ?infid and count them. I want to add the additional wbc values in an optional block so that I do get a reference to the infectionid even if there are no wbc values found.

To start I want to add the statement below to the query. But whenever I do this, stardog does not return any results. Is it valid to add this optional part? This optional part still has to be extended with a FILTER statement that filters ?wbcval < (?peakwbc * 0.75)

OPTIONAL {
      ?wbc leucocytes:isLeucocytesOf ?adm .
      ?wbc temporal:hasTime ?wbctime .
      ?wbc general:hasDoubleValue ?wbcval .
      ?wbc a leucocytes:Leucocytes .
}

This is then the full query:

PREFIX infection:    <http://www.uzalerting.intec.ugent.be/infection#>
PREFIX general:    <http://www.uzalerting.intec.ugent.be/general#>
PREFIX medication:    <http://www.uzalerting.intec.ugent.be/medication#>
PREFIX fevertype:    <http://www.uzalerting.intec.ugent.be/feverType#>
PREFIX bodytemperature:    <http://www.uzalerting.intec.ugent.be/bodyTemperature#>
PREFIX pao2fio2:    <http://www.uzalerting.intec.ugent.be/pao2fio2#>
PREFIX sputum:    <http://www.uzalerting.intec.ugent.be/sputum#>
PREFIX leucocytes:    <http://www.uzalerting.intec.ugent.be/leucocytes#>
PREFIX temporal:    <http://swrl.stanford.edu/ontologies/built-ins/3.3/temporal.owl#>
select *
where { 
    ?inf a infection:RespiratoryInfection .
    ?inf infection:hasInfectionEnd ?infend .
    ?inf infection:hasInfectionId ?infid .
    ?inf infection:isInfectionOf ?adm .
    ?adm a general:PatientAdmission .
    {
      select ?infid (count(?wbcmaxval) as ?peakwbcc) (max(?wbcmaxval) as ?peakwbc)
      where {
        ?inf a infection:RespiratoryInfection .
        ?inf infection:hasInfectionStart ?infstart .
        ?inf infection:hasInfectionEnd ?infend .
        ?inf infection:hasInfectionId ?infid .
        ?inf infection:isInfectionOf ?adm .
        ?adm a general:PatientAdmission .
        OPTIONAL {
          ?wbcmax leucocytes:isLeucocytesOf ?adm .
          ?wbcmax a leucocytes:Leucocytes .
          ?wbcmax temporal:hasTime ?wbcmaxtime .
          ?wbcmax general:hasDoubleValue ?wbcmaxval .
          FILTER(
            (
              ?infend <= "2013-02-01T00:00:00.0"^^xsd:dateTime &&
              ?wbcmaxtime <= ?infend &&
              ?wbcmaxtime >= (?infstart - "P2DT0H0M0S"^^xsd:dayTimeDuration)
            ) || (
              ?infend > "2013-02-01T00:00:00.0"^^xsd:dateTime &&
              ?wbcmaxtime <= "2013-02-01T00:00:00.0"^^xsd:dateTime &&
              ?wbcmaxtime >= (?infstart - "P2DT0H0M0S"^^xsd:dayTimeDuration)
            )
          )
        }
      }
      GROUP BY ?infid
    } .
    OPTIONAL {
      ?wbc leucocytes:isLeucocytesOf ?adm .
      ?wbc temporal:hasTime ?wbctime .
      ?wbc general:hasDoubleValue ?wbcval .
      ?wbc a leucocytes:Leucocytes .
    } .
}

Evren Sirin

unread,
Mar 4, 2015, 10:37:56 AM3/4/15
to Stardog
Adding an optional should not reduce the number of results unless
there is some interaction with filters which does not look like the
case here. Is there a way for you to share the data with us so we can
take a look?

Best,
Evren
> --
> -- --
> You received this message because you are subscribed to the C&P "Stardog"
> group.
> To post to this group, send email to sta...@clarkparsia.com
> To unsubscribe from this group, send email to
> stardog+u...@clarkparsia.com
> For more options, visit this group at
> http://groups.google.com/a/clarkparsia.com/group/stardog?hl=en
Reply all
Reply to author
Forward
0 new messages