class InterMine::PathQuery::Template

Attributes

comment[RW]
longDescription[RW]

Public Class Methods

new(model, root=nil, service=nil) click to toggle source
Calls superclass method InterMine::PathQuery::Query::new
     # File lib/intermine/query.rb
1617 def initialize(model, root=nil, service=nil)
1618     super
1619     @constraint_factory = TemplateConstraintFactory.new(self)
1620     @url = (@service.nil?) ? nil : @service.root + Service::TEMPLATE_RESULTS_PATH
1621 end
parser(model) click to toggle source
     # File lib/intermine/query.rb
1623 def self.parser(model)
1624     return TemplateLoader.new(model)
1625 end

Public Instance Methods

active_constraints() click to toggle source
     # File lib/intermine/query.rb
1638 def active_constraints
1639     return coded_constraints.select {|con| con.switchable != "off"}
1640 end
clone() click to toggle source

Return a clone of the current template. Changes made to the clone will not effect the cloned query.

Calls superclass method
     # File lib/intermine/query.rb
1754 def clone
1755     other = super
1756     other.instance_variable_set(:@constraints, @constraints.map {|c| c.clone})
1757     return other
1758 end
count(params = {}) click to toggle source

Return the number of result rows this query will return in its current state. This makes a very small request to the webservice, and is the most efficient method of getting the size of the result set.

     # File lib/intermine/query.rb
1747 def count(params = {})
1748     runner = (params.empty?) ? self : get_adjusted(params)
1749     runner.results_reader.get_size
1750 end
each_result(params = {}, start=0, size=nil) { |r| ... } click to toggle source
     # File lib/intermine/query.rb
1697 def each_result(params = {}, start=0, size=nil) 
1698     runner = (params.empty?) ? self : get_adjusted(params)
1699     runner.results_reader(start, size).each_result {|r| yield r}
1700 end
each_row(params = {}, start=0, size=nil) { |r| ... } click to toggle source
     # File lib/intermine/query.rb
1679 def each_row(params = {}, start=0, size=nil)
1680     runner = (params.empty?) ? self : get_adjusted(params)
1681     runner.results_reader(start, size).each_row {|r| yield r}
1682 end
editable_constraints() click to toggle source
     # File lib/intermine/query.rb
1634 def editable_constraints
1635     return coded_constraints.select {|con| con.editable}
1636 end
params() click to toggle source
     # File lib/intermine/query.rb
1642 def params 
1643     p = {"name" => @name}
1644     actives = active_constraints
1645     actives.each_index do |idx|
1646         con = actives[idx]
1647         count = (idx + 1).to_s
1648         p["constraint" + count] = con.path.to_s
1649         p["op" + count] = con.template_param_op
1650         if con.respond_to? :value
1651             p["value" + count] = con.value
1652         elsif con.respond_to? :values
1653             p["value" + count] = con.values
1654         elsif con.respond_to? :loopPath
1655             p["loopPath" + count] = con.loopPath.to_s
1656         end
1657         if con.respond_to? :extra_value and !con.extra_value.nil?
1658             p["extra" + count] = con.extra_value
1659         end
1660     end
1661     return p
1662 end
results(params = {}, start=0, size=nil) click to toggle source

Return objects corresponding to the type of data requested, starting at the given row offset. Returns an Enumerable of InterMineObject, where each value is read one at a time from the connection.

template.results("A" => "eve").each {|gene|
    puts gene.symbol
}
     # File lib/intermine/query.rb
1692 def results(params = {}, start=0, size=nil)
1693     runner = (params.empty?) ? self : get_adjusted(params)
1694     return self.class.superclass.instance_method(:results).bind(runner).call(start, size)
1695 end
rows(params = {}, start=0, size=nil) click to toggle source

Returns an Enumerable of ResultRow objects containing the data returned by running this query, starting at the given offset and containing up to the given maximum size.

The webservice enforces a maximum page-size of 10,000,000 rows, independent of any size you specify - this can be obviated with paging for large result sets.

names = template.rows("A" => "eve").map {|r| r["name"]}
     # File lib/intermine/query.rb
1674 def rows(params = {}, start=0, size=nil)
1675     runner = (params.empty?) ? self : get_adjusted(params)
1676     return self.class.superclass.instance_method(:rows).bind(runner).call(start, size)
1677 end
summaries(path, params = {}, start=0, size=nil) click to toggle source

Return an Enumerable of summary items starting at the given offset.

summary = template.summary_items("chromosome.primaryIdentifier", {"A" => "Pentose Phosphate Pathway"})
top_chromosome = summary[0]["item"]
no_in_top_chrom = summary[0]["count"]

This can be made more efficient by passing in a size - ie, if you only want the top item, pass in an offset of 0 and a size of 1 and only that row will be fetched.

     # File lib/intermine/query.rb
1712 def summaries(path, params = {}, start=0, size=nil)
1713     runner = (params.empty?) ? self : get_adjusted(params)
1714     return self.class.superclass.instance_method(:summaries).bind(runner).call(path, start, size)
1715 end
summarise(path, params={}, start=0, size=nil) click to toggle source

Return a summary for a column as a Hash

For numeric values the hash has four keys: “average”, “stdev”, “min”, and “max”.

summary = template.summarise("length", {"A" => "eve"})
puts summary["average"]

For non-numeric values, the hash will have each possible value as a key, and the count of the occurrences of that value in this query's result set as the corresponding value:

summary = template.summarise("chromosome.primaryIdentifier", {"A" => "eve"})
puts summary["2L"]

To limit the size of the result set you can use start and size as per normal queries - this has no real effect with numeric queries, which always return the same information.

     # File lib/intermine/query.rb
1735 def summarise(path, params={}, start=0, size=nil)
1736     t = make_path(add_prefix(path)).end_type
1737     if InterMine::Metadata::Model::NUMERIC_TYPES.include? t
1738         return Hash[summaries(path, params, start, size).first.map {|k, v| [k, v.to_f]}]
1739     else
1740         return Hash[summaries(path, params, start, size).map {|sum| [sum["item"], sum["count"]]}]
1741     end
1742 end
to_xml() click to toggle source
Calls superclass method InterMine::PathQuery::Query#to_xml
     # File lib/intermine/query.rb
1627 def to_xml
1628     doc = REXML::Document.new
1629     t = doc.add_element 'template', {"name" => @name, "title" => @title, "longDescription" => @longDescription, "comment" => @comment}.reject {|k,v| v.nil?}
1630     t.add_element super
1631     return t
1632 end

Private Instance Methods

get_adjusted(params) click to toggle source
     # File lib/intermine/query.rb
1762 def get_adjusted(params)
1763     adjusted = clone
1764     params.each do |k,v| 
1765         con = adjusted.get_constraint(k)
1766         raise ArgumentError, "There is no constraint with code #{k} in this query" unless con
1767         path = con.path.to_s
1768         adjusted.remove_constraint(k)
1769         adjusted.where(path => v)
1770         adjusted.constraints.last.code = k
1771     end
1772     return adjusted
1773 end