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, #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.



1600
1601
1602
1603
1604
# File 'lib/intermine/query.rb', line 1600

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.



1598
1599
1600
# File 'lib/intermine/query.rb', line 1598

def comment
  @comment
end

#longDescriptionObject

Returns the value of attribute longDescription.



1598
1599
1600
# File 'lib/intermine/query.rb', line 1598

def longDescription
  @longDescription
end

Class Method Details

.parser(model) ⇒ Object



1606
1607
1608
# File 'lib/intermine/query.rb', line 1606

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

Instance Method Details

#active_constraintsObject



1621
1622
1623
# File 'lib/intermine/query.rb', line 1621

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.



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

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.



1730
1731
1732
1733
# File 'lib/intermine/query.rb', line 1730

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

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



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

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



1662
1663
1664
1665
# File 'lib/intermine/query.rb', line 1662

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



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

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

#paramsObject



1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
# File 'lib/intermine/query.rb', line 1625

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
}


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

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


1657
1658
1659
1660
# File 'lib/intermine/query.rb', line 1657

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.



1695
1696
1697
1698
# File 'lib/intermine/query.rb', line 1695

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.



1718
1719
1720
1721
1722
1723
1724
1725
# File 'lib/intermine/query.rb', line 1718

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



1610
1611
1612
1613
1614
1615
# File 'lib/intermine/query.rb', line 1610

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