Module: GO
- Extended by:
- Resource
- 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)
Class Method Summary collapse
- .ancestors_in(term, valid = false) ⇒ Object
- .descendants(id) ⇒ Object
- .goterms ⇒ Object
- .group_genes(list, valid = nil) ⇒ Object
- .id2ancestors(id) ⇒ Object
- .id2ancestors_by_type(id, type = 'is_a') ⇒ Object
- .id2name(id) ⇒ Object
- .id2namespace(id) ⇒ Object
- .info ⇒ Object
-
.init ⇒ Object
This method needs to be called before any translations can be made, it is called automatically the first time the id2name method is called.
Class Method Details
.ancestors_in(term, valid = false) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rbbt/sources/go.rb', line 120 def self.ancestors_in(term, valid = false) 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 |
.descendants(id) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rbbt/sources/go.rb', line 69 def self.descendants(id) list = Set.new new = Set.new new << id while new.any? list += new new = Set.new info.each do |new_id,values| next unless values['is_a'] new << new_id if values['is_a'].select{|e| list.include? e.split("!").first[/GO:\d+/] }.any? && ! list.include?(new_id) end end list end |
.goterms ⇒ Object
56 57 58 |
# File 'lib/rbbt/sources/go.rb', line 56 def self.goterms info.keys end |
.group_genes(list, valid = nil) ⇒ Object
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rbbt/sources/go.rb', line 131 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
106 107 108 |
# File 'lib/rbbt/sources/go.rb', line 106 def self.id2ancestors(id) id2ancestors_by_type(id, 'is_a') + id2ancestors_by_type(id, 'relationship') end |
.id2ancestors_by_type(id, type = 'is_a') ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rbbt/sources/go.rb', line 84 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
60 61 62 63 64 65 66 67 |
# File 'lib/rbbt/sources/go.rb', line 60 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
110 111 112 113 114 115 116 117 118 |
# File 'lib/rbbt/sources/go.rb', line 110 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 |
.info ⇒ Object
52 53 54 |
# File 'lib/rbbt/sources/go.rb', line 52 def self.info @@info ||= self.init end |
.init ⇒ Object
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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rbbt/sources/go.rb', line 27 def self.init Persist.persist_tsv(nil, 'gene_ontology', {}, :persist => true, serializer: :marshal) do |info| #info.serializer = :marshal if info.respond_to? :serializer Rbbt.share.databases.GO.gene_ontology.produce.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[term_info["alt_id"]] = term_info if term_info["alt_id"] } info end.tap{|o| o.unnamed = true} end |