Submit Feedback/Bug Report

myExperiment Logo How To SPARQL

                    Back to Contents Page

5. FILTER

The FILTER clause is used within the curly parenthesis {} as a subclause of the WHERE clause. As its name suggests, it allows you to filter the results based on certain conditions. Most commonly you may want to filter on text but you can also filter on numbers and dates.

5.1. On Text

An example of a query where you want to filter on text is when you want to find all Taverna workflows (both 1 and 2):

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?workflow
WHERE{
  ?workflow rdf:type mecontrib:Workflow ;
    mebase:has-content-type ?ct .
  ?ct dcterms:title ?ct_title 
  FILTER regex(?ct_title,'^taverna','i')
}
[Run]
[Hide Example Results]
workflowct_title
http://www.myexperiment.org/workflows/167Taverna 1
http://www.myexperiment.org/workflows/932Taverna 2 beta
http://www.myexperiment.org/workflows/193Taverna 1
http://www.myexperiment.org/workflows/874Taverna 1
http://www.myexperiment.org/workflows/519Taverna 1
http://www.myexperiment.org/workflows/545Taverna 1

The regex operand allows you to compare two text strings. In the example above this compares the value of ?ct_title with the text string 'taverna'. The caret (^) sign is used to indicate that the string for ?ct_title must start with 'taverna', not just have it somewhere within the string. The 'i' as the third parameter for the regex operand means that the regular expression is case insentive, if you wish it to be case sensitive only the first two parameters are required.

Sometimes you may want to pattern match to a variable that is a URI rather than a literal to do this you need to use the str operand to convert the URI into a string. An example of this may be to find all the accepted membership where a group asked a user to join:

PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?membership ?requester
WHERE{
  ?membership rdf:type mebase:Membership ;
    mebase:has-requester ?requester ;
    mebase:has-accepter ?accepter ;
    mebase:accepted-at ?accepted_at
  FILTER regex(str(?requester),'Group','i')
}
[Run]
[Hide Example Results]
membershiprequester
http://www.myexperiment.org/users/30/memberships/96http://www.myexperiment.org/groups/51
http://www.myexperiment.org/users/2646/memberships/825http://www.myexperiment.org/groups/195
http://www.myexperiment.org/users/2213/memberships/884http://www.myexperiment.org/groups/211
http://www.myexperiment.org/users/2611/memberships/745http://www.myexperiment.org/groups/187
http://www.myexperiment.org/users/13/memberships/721http://www.myexperiment.org/groups/194

5.2. On Numbers

In some cases you may want to query on a number being greater (or less than) a particular value. The FILTER clause allows inequalities (and equalities) to be defined as criteria for filtering on. An example of this may be to find all the workflows that have a rating that is 4 or greater:

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX meannot: <http://rdf.myexperiment.org/ontologies/annotations/>
PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?workflow ?ct_title
WHERE{
  ?workflow rdf:type mecontrib:Workflow ;
    mebase:has-content-type ?ct .
  ?ct dcterms:title ?ct_title .
  ?rating rdf:type meannot:Rating ;
    mebase:annotates ?workflow ;
    meannot:rating-score ?score 
  FILTER (?score >= 4)
}
[Run]
[Hide Example Results]
workflowct_title
http://www.myexperiment.org/workflows/244Taverna 1
http://www.myexperiment.org/workflows/742SimileXMLv3
http://www.myexperiment.org/workflows/19Taverna 1
http://www.myexperiment.org/workflows/133Taverna 1
http://www.myexperiment.org/workflows/735Excel 2007 Macro-Enabled Workbook
http://www.myexperiment.org/workflows/90Taverna 1
http://www.myexperiment.org/workflows/549Chemistry Plan

You may want to filter on more than one criteria. For the above example you may only want Taverna 1 workflows that are rated at least 4 out of 5:

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX meannot: <http://rdf.myexperiment.org/ontologies/annotations/>
PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?workflow ?ct_title
WHERE{
  ?workflow rdf:type mecontrib:Workflow ;
    mebase:has-content-type ?ct .
  ?ct dcterms:title ?ct_title .
  ?rating rdf:type meannot:Rating ;
    mebase:annotates ?workflow ;
    meannot:rating-score ?score
  FILTER (?score >= 4 && regex(?ct_title,'^Taverna 1'))
}
[Run]
[Hide Example Results]
workflowct_title
http://www.myexperiment.org/workflows/173Taverna 1
http://www.myexperiment.org/workflows/40Taverna 1
http://www.myexperiment.org/workflows/66Taverna 1
http://www.myexperiment.org/workflows/235Taverna 1

5.3. On Dates

Often one of the most useful filters is for finding things that have been added/modified before or after a certain date. You may want to find all the workflows that have been added since the beginning of September 2009:

PREFIX mecontrib: <http://rdf.myexperiment.org/ontologies/contributions/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?workflow ?added
WHERE { 
  ?workflow rdf:type mecontrib:Workflow ;
    dcterms:created ?added
  FILTER ( ?added >= xsd:dateTime('2009-09-01T00:00:00Z') )
}
[Run]
[Hide Example Results]
workflowadded
http://www.myexperiment.org/workflows/9322009-10-20T17:00:59Z
http://www.myexperiment.org/workflows/9522009-11-16T12:37:25Z
http://www.myexperiment.org/workflows/9062009-09-08T15:12:57Z
http://www.myexperiment.org/workflows/9552009-11-16T18:10:54Z
http://www.myexperiment.org/workflows/9122009-09-15T11:32:52Z
                    Back to Contents Page