CQL Examples
The following are simple examples of CQL queries in XML format which highlight the features and syntax of CQL.
| |
|
|
| |
Table of Contents |
|
| |
|
|
Return all objects of a given type
The simplest CQL query defines only one Object. The data service will return all objects in its underlying data source of the given type.
This query will return all Gene objects in the data source:
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"/> </CQLQuery>
Count all objects of a given type
A variant of the same simple CQL query defines only the target object and a query modifier to count the number of results. Such a query may be useful in situations where the returned result set is potentially very large, and knowing the size of this set may determine handling ahead of actually retrieving results.
This query will return the number of all Gene objects in the data source:
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"/> <QueryModifier countOnly="true"/> </CQLQuery>
Objects with a single attribute restriction
A simple extension to the above query is to ask for all Objects with a single Attribute restriction. Here, a LIKE restriction is used. Note that the syntax for LIKE queries mimics that of SQL.
This query will return all Genes with a symbol starting with 'BRCA':
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"> <Attribute name="symbol" value="BRCA%" predicate="LIKE"/> </Target> </CQLQuery>
Distinct attributes of an object
The following query will return only distinct symbol names from all genes with symbol names starting with 'BRCA'.
This query will return distinct symbol values for all Genes with a symbol starting with 'BRCA':
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"> <Attribute name="symbol" value="BRCA%" predicate="LIKE"/> </Target> <QueryModifier countOnly="false"> <DistinctAttribute>symbol</DistinctAttribute> </QueryModifier> </CQLQuery>
Multiple attributes of a target object
The final query modification which can be used is one where multiple attributes of a target object are returned.
This query will both id and symbol values for all Genes with a symbol starting with 'BRCA':
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"> <Attribute name="symbol" value="BRCA%" predicate="LIKE"/> </Target> <QueryModifier countOnly="false"> <AttributeNames>symbol</AttributeNames> <AttributeNames>id</AttributeNames> </QueryModifier> </CQLQuery>
Objects with a single association
A more involved CQL query defines an Object and an Association to another Object. The data service will return all objects of the requested data type and meeting the restriction on their association.
This query will return all Genes with an associated Taxon whose id is equal to 6:
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"> <Association roleName="taxon" name="gov.nih.nci.cabio.domain.Taxon"> <Attribute name="id" value="6" predicate="EQUAL_TO"/> </Association> </Target> </CQLQuery>
Groups
Groups are required when more than one restriction on an Object is needed. In this example, a group is made with an AND logical operator, meaning that all conditions defined within the Group must be met.
This query will return all Genes with a symbol beginning with 'BRCA' and have an associated Taxon with an id equal to 6:
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery"> <Target name="gov.nih.nci.cabio.domain.Gene"> <Group logicRelation="AND"> <Attribute name="symbol" value="BRCA%" predicate="LIKE"/> <Association roleName="taxon" name="gov.nih.nci.cabio.domain.Taxon"> <Attribute name="id" value="6" predicate="EQUAL_TO"/> </Association> </Group> </Target> </CQLQuery>
Nested groups
Nested groups can be used to build up sophisticated compound queries. The groups may have differing logical relations to express restrictions on various portions of the object.
This query will return all Genes with an associated Taxon that has a scientificName attribute of 'Mus musculus' and have either a symbol like 'BRCA' or like 'ICR':
<CQLQuery xmlns="http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery" > <Target name="gov.nih.nci.cabio.domain.Gene"> <Group logicRelation="AND"> <Association roleName="taxon" name="gov.nih.nci.cabio.domain.Taxon"> <Attribute name="scientificName" value="Mus musculus" predicate="EQUAL_TO"/> </Association> <Group logicRelation="OR"> <Attribute name="symbol" value="BRCA%" predicate="LIKE"/> <Attribute name="symbol" value="ICR%" predicate="LIKE"/> </Group> </Group> </Target> </CQLQuery>





