Class: InterMine::PathQuery::Template

Inherits:
Query
  • Object
show all
Defined in:
lib/intermine/query.rb

Constant Summary

Constants inherited from Query

Query::HIGHEST_CODE, Query::LOWEST_CODE

Instance Attribute Summary collapse

Attributes inherited from Query

#constraints, #joins, #list_append_uri, #list_upload_uri, #logic, #model, #name, #root, #service, #size, #sort_order, #start, #title, #views

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Query

#add_constraint, #add_join, #add_prefix, #add_sort_order, #add_views, #all, #all_rows, #coded_constraints, #eql?, #first, #first_row, #get_constraint, #inspect, is_valid_code, #limit, #make_path, #next_code, #offset, #outerjoin, #path, #remove_constraint, #results_reader, #sequences, #set_logic, #sortOrder=, #subclass_constraints, #subclasses, #to_s, #used_codes, #view=, #where

Constructor Details

#initialize(model, root = nil, service = nil) ⇒ Template

Returns a new instance of Template.


1617
1618
1619
1620
1621
# File 'lib/intermine/query.rb', line 1617

def initialize(model, root=nil, service=nil)
    super
    @constraint_factory = TemplateConstraintFactory.new(self)
    @url = (@service.nil?) ? nil : @service.root + Service::TEMPLATE_RESULTS_PATH
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment


1615
1616
1617
# File 'lib/intermine/query.rb', line 1615

def comment
  @comment
end

#longDescriptionObject

Returns the value of attribute longDescription


1615
1616
1617
# File 'lib/intermine/query.rb', line 1615

def longDescription
  @longDescription
end

Class Method Details

.parser(model) ⇒ Object


1623
1624
1625
# File 'lib/intermine/query.rb', line 1623

def self.parser(model)
    return TemplateLoader.new(model)
end

Instance Method Details

#active_constraintsObject


1638
1639
1640
# File 'lib/intermine/query.rb', line 1638

def active_constraints
    return coded_constraints.select {|con| con.switchable != "off"}
end

#cloneObject

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


1754
1755
1756
1757
1758
# File 'lib/intermine/query.rb', line 1754

def clone
    other = super
    other.instance_variable_set(:@constraints, @constraints.map {|c| c.clone})
    return other
end

#count(params = {}) ⇒ Object

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.


1747
1748
1749
1750
# File 'lib/intermine/query.rb', line 1747

def count(params = {})
    runner = (params.empty?) ? self : get_adjusted(params)
    runner.results_reader.get_size
end

#each_result(params = {}, start = 0, size = nil) ⇒ Object


1697
1698
1699
1700
# File 'lib/intermine/query.rb', line 1697

def each_result(params = {}, start=0, size=nil) 
    runner = (params.empty?) ? self : get_adjusted(params)
    runner.results_reader(start, size).each_result {|r| yield r}
end

#each_row(params = {}, start = 0, size = nil) ⇒ Object


1679
1680
1681
1682
# File 'lib/intermine/query.rb', line 1679

def each_row(params = {}, start=0, size=nil)
    runner = (params.empty?) ? self : get_adjusted(params)
    runner.results_reader(start, size).each_row {|r| yield r}
end

#editable_constraintsObject


1634
1635
1636
# File 'lib/intermine/query.rb', line 1634

def editable_constraints
    return coded_constraints.select {|con| con.editable}
end

#paramsObject


1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
# File 'lib/intermine/query.rb', line 1642

def params 
    p = {"name" => @name}
    actives = active_constraints
    actives.each_index do |idx|
        con = actives[idx]
        count = (idx + 1).to_s
        p["constraint" + count] = con.path.to_s
        p["op" + count] = con.template_param_op
        if con.respond_to? :value
            p["value" + count] = con.value
        elsif con.respond_to? :values
            p["value" + count] = con.values
        elsif con.respond_to? :loopPath
            p["loopPath" + count] = con.loopPath.to_s
        end
        if con.respond_to? :extra_value and !con.extra_value.nil?
            p["extra" + count] = con.extra_value
        end
    end
    return p
end

#results(params = {}, start = 0, size = nil) ⇒ Object

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
}

1692
1693
1694
1695
# File 'lib/intermine/query.rb', line 1692

def results(params = {}, start=0, size=nil)
    runner = (params.empty?) ? self : get_adjusted(params)
    return self.class.superclass.instance_method(:results).bind(runner).call(start, size)
end

#rows(params = {}, start = 0, size = nil) ⇒ Object

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"]}

1674
1675
1676
1677
# File 'lib/intermine/query.rb', line 1674

def rows(params = {}, start=0, size=nil)
    runner = (params.empty?) ? self : get_adjusted(params)
    return self.class.superclass.instance_method(:rows).bind(runner).call(start, size)
end

#summaries(path, params = {}, start = 0, size = nil) ⇒ Object

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.


1712
1713
1714
1715
# File 'lib/intermine/query.rb', line 1712

def summaries(path, params = {}, start=0, size=nil)
    runner = (params.empty?) ? self : get_adjusted(params)
    return self.class.superclass.instance_method(:summaries).bind(runner).call(path, start, size)
end

#summarise(path, params = {}, start = 0, size = nil) ⇒ Object

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.


1735
1736
1737
1738
1739
1740
1741
1742
# File 'lib/intermine/query.rb', line 1735

def summarise(path, params={}, start=0, size=nil)
    t = make_path(add_prefix(path)).end_type
    if InterMine::Metadata::Model::NUMERIC_TYPES.include? t
        return Hash[summaries(path, params, start, size).first.map {|k, v| [k, v.to_f]}]
    else
        return Hash[summaries(path, params, start, size).map {|sum| [sum["item"], sum["count"]]}]
    end
end

#to_xmlObject


1627
1628
1629
1630
1631
1632
# File 'lib/intermine/query.rb', line 1627

def to_xml
    doc = REXML::Document.new
    t = doc.add_element 'template', {"name" => @name, "title" => @title, "longDescription" => @longDescription, "comment" => @comment}.reject {|k,v| v.nil?}
    t.add_element super
    return t
end