Module: GO

Defined in:
lib/rbbt/sources/go.rb

Overview

This module holds helper methods to deal with the Gene Ontology files. Right now all it does is provide a translation form id to the actual names.

Constant Summary collapse

MULTIPLE_VALUE_FIELDS =
%w(is_a)
TSV_GENE_ONTOLOGY =
File.join(Persist.cachedir, 'gene_ontology')

Class Method Summary collapse

Class Method Details

.ancestors_in(term, valid) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/rbbt/sources/go.rb', line 96

def self.ancestors_in(term, valid)
  ancestors = id2ancestors(term)
  return ancestors if FalseClass === valid
  valid_ancestors = ancestors & valid
  return valid_ancestors if valid_ancestors.any?
  ancestors.inject([]) do |acc,ancestor|
    valid_a = ancestors_in ancestor, valid
    acc = acc + valid_a
  end.uniq
end

.gotermsObject



47
48
49
# File 'lib/rbbt/sources/go.rb', line 47

def self.goterms
  info.keys
end

.group_genes(list, valid = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rbbt/sources/go.rb', line 107

def self.group_genes(list, valid = nil)
  valid = %w(GO:0005886 GO:0005634 GO:0005730 GO:0005829) if valid.nil?

  compartment_leaves = {}
  list.zip(list.go_cc_terms).each do |gene,terms|
    terms = [] if terms.nil?
    valid_terms = terms & valid
    valid_terms = terms.collect{|term| 
      (valid.include?(term) ? term : ancestors_in(term, valid))
    }.flatten
    #valid_terms - GOTerm.setup(valid_terms).flat_ancestry.flatten
    valid_terms.each do |term|
      compartment_leaves[term] ||= []
      compartment_leaves[term].push(gene)
    end
  end

  groups = {}
  while compartment_leaves.length > 1
    # Group common
    group = false
    new_compartment_leaves = {}
    compartment_leaves.each do |c,l|
      if l.length > 1
        groups[c] = l
        group = true
        new_compartment_leaves[c] = [c]
      else
        new_compartment_leaves[c] = l
      end
    end
    compartment_leaves = new_compartment_leaves

    # Pull-up leaves
    if group == false
      new_compartment_leaves = {}
      final = compartment_leaves.keys
      compartment_leaves
      compartment_leaves.each do |c,l|
        final = final - GOTerm.setup(c.dup).flat_ancestry
      end

      compartment_leaves.each do |c,l|
        if final.include? c
          valid_an = ancestors_in c, valid
          valid_an.each do |ancestor|
            new_compartment_leaves[ancestor] ||= []
            new_compartment_leaves[ancestor].push(c)
            groups[ancestor].push(c) if groups[ancestor]
          end
        else
          new_compartment_leaves[c] = l
        end
      end
      compartment_leaves = new_compartment_leaves
    end
  end
  ng = {}
  groups.keys.reverse.each do |k|
    ng[k] = {items: groups[k].uniq, id: k, name: id2name(k)}
  end
  ng
end

.id2ancestors(id) ⇒ Object



82
83
84
# File 'lib/rbbt/sources/go.rb', line 82

def self.id2ancestors(id)
  id2ancestors_by_type(id, 'is_a') + id2ancestors_by_type(id, 'relationship')
end

.id2ancestors_by_type(id, type = 'is_a') ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rbbt/sources/go.rb', line 60

def self.id2ancestors_by_type(id, type='is_a')
  if id.kind_of? Array
    info.values_at(*id).
      select{|i| ! i[type].nil?}.
      collect{|i| 
        res = i[type]
        res = [res] unless Array === res
        res.collect{|id| 
          id.match(/(GO:\d+)/)[1] if id.match(/(GO:\d+)/)
        }.compact
      }
  else
    return [] if id.nil? or info[id].nil? or info[id][type].nil?
    res = info[id][type]
    res = [res] unless Array === res
    res.
      collect{|id| 
      id.match(/(GO:\d+)/)[1] if id.match(/(GO:\d+)/)
    }.compact
  end
end

.id2name(id) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/rbbt/sources/go.rb', line 51

def self.id2name(id)
  if id.kind_of? Array
    info.values_at(*id).collect{|i| i['name'] if i}
  else
    return nil if info[id].nil?
    info[id]['name']
  end
end

.id2namespace(id) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/rbbt/sources/go.rb', line 86

def self.id2namespace(id)
  self.init unless info
  if id.kind_of? Array
    info.values_at(*id).collect{|i| i['namespace'] if i}
  else
    return nil if info[id].nil?
    info[id]['namespace']
  end
end

.infoObject



43
44
45
# File 'lib/rbbt/sources/go.rb', line 43

def self.info
  @@info ||= self.init
end

.initObject

This method needs to be called before any translations can be made, it is called automatically the first time the id2name method is called. It loads the gene_ontology.obo file and extracts all the fields, although right now, only the name field is used.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rbbt/sources/go.rb', line 19

def self.init
  Persist.persist_tsv(nil, 'gene_ontology', {}, :persist => true) do |info|
    info.serializer = :marshal if info.respond_to? :serializer
    Rbbt.share.databases.GO.gene_ontology.read.split(/\[Term\]/).each{|term| 
      term_info = {}

      term.split(/\n/). select{|l| l =~ /:/}.each{|l| 
        key, value = l.chomp.match(/(.*?):(.*)/).values_at(1,2)
        if MULTIPLE_VALUE_FIELDS.include? key.strip
          term_info[key.strip] ||= []
          term_info[key.strip] << value.strip
        else
          term_info[key.strip] = value.strip
        end
      }

      next if term_info["id"].nil?
      info[term_info["id"]] = term_info
    }

    info
  end.tap{|o| o.unnamed = true}
end