Templates are exactly like queries and can do everything that a query can do.
While writing queries of your own, you would have probably realized that there is a lot of duplication of effort. Templates are basically pre defined queries that can be run numerous times and certain values can also be changed easily.
Everything that you can do with a query - add new constraints, add new views, process the results using the results iterator can be done with templates as well.
We will begin with a simple example. Let's say you want to extract the publication information about various Genes related to an organism. There is already a set template for this process. We begin by importing the Service class and then create a template object. The parameter that we pass to the get template method is the name of the template.
from intermine.webservice import Service
service = Service("https://www.flymine.org/flymine/service")
template=service.get_template('All_Genes_In_Organism_To_Publications')
To check the columns that our results will have you can use template.views. If you want to add a column use the add_view/add_views method.
template.views
template.constraint_dict
If you want to look at the current constraints, use template.constraint_dict. There is only one constraint with is editable, i.e. you can change the value or operator if you want. However, even for editable constraints you are not allowed to change the path of the constraint.
To view the results we can use the results iterator as we learned previously.
for row in template.results(row="rr",size=10):
print(row)
Let's say that you want to extract information for Drosophila Erecta and not Drosophila melanogaster. You can edit the query while calling the results method. In the code shown below, A refers to the code of the constraint. This code can be viewed using "template.constraint_dict" as shown above.
for row in template.results(row="rr",A={"op":"=","value":"Drosophila erecta"},size=10):
print(row)
This is how you can use a pre defined query and modify it to work for you. I would suggest that you visit the flymine website and take a look at some of the templates that have been defined there. Try running them using Python and change the constraints and views.
While exploring through templates you may come across templates that can be switched on or off. To switch off a constraint that is already turned on you can use the following code: template.get_constraints('B').switch_off , where B is the code of the constraint in the constraint dictionary. In our example, the code is A since there is only one constraint.
To check if a particular constraint is switchable use the get_switchable_status method. This method can return three possible values - locked, on or off. Locked means that the particular constraint is fixed and cannot be switched on or off. If a particular constraint is switchable, it will return on or off depending on it's current status.
template.get_constraint('A').get_switchable_status()
If you try to switch off a constraint that is "locked" or not switchable, you will get an error.
# template.get_constraint('A').switch_off()
It is also possible to modify constraints on the templates, as discussed above. Both the operator and value maybe altered. Here is an example from the Gene Intron template-
We first see the status of the constraints on this template.
from intermine.webservice import Service
service=Service("https://www.flymine.org/flymine/service")
query=service.new_query('Gene')
template=service.get_template('Gene_IntronChromosome')
template.constraint_dict
Now we modify constraint A, such that it only contains ouput data with secondary identifier CG10023.
from intermine.webservice import Service
service = Service("https://www.flymine.org/flymine/service")
template = service.get_template('Gene_IntronChromosome')
rows = template.rows(
A = {"op": "=", "value": "CG10023"})
for row in rows:
print(row)
Thus the output only contains results as per the applied constraints.
This tutorial highlighted how templates help in automating commonly used queries and can make extremely efficient workflows.