Class: WingenderTFClass::OBO::TFClassification
- Inherits:
-
Object
- Object
- WingenderTFClass::OBO::TFClassification
- Defined in:
- lib/WingenderTFClass/obo/tf_classification.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<<(term) ⇒ Object
- #children(term_id) ⇒ Object
-
#initialize ⇒ TFClassification
constructor
terms by ids.
- #leaf?(term_id) ⇒ Boolean
- #root ⇒ Object
- #term(term_id) ⇒ Object
- #term_by_name(name) ⇒ Object
- #tf_groups(slice_deepness) ⇒ Object
Constructor Details
#initialize ⇒ TFClassification
terms by ids
19 20 21 22 23 24 25 26 27 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 19 def initialize() @terms_by_id = {} @children_by_id = Hash.new{|h,k| h[k] = [] } @terms_by_name = Hash.new{|h,k| h[k] = [] } # @terms_by_id.each{|term_id, term| # @children_by_id[term.parent_id] << term if term.parent_id # } self << Term.new(self, '', '', 'Root', '', nil, [], []) end |
Class Method Details
.by_species(species) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 7 def self.by_species(species) case species.to_s.downcase when 'human' OBO::TFClassification.from_file(FilePaths::TFOntologyHuman) when 'mouse' OBO::TFClassification.from_file(FilePaths::TFOntologyMouse) else raise "Unknown species `#{species}`" end end |
.from_file(filename) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 36 def self.from_file(filename) tf_ontology = self.new terms = File.readlines(filename) .map(&:chomp) .slice_before{|line| line.start_with?('[Term]') }.drop(1) .map{|enumerator| Term.from_line_array(tf_ontology, enumerator.to_a) } terms.each{|term| tf_ontology << term } tf_ontology end |
Instance Method Details
#<<(term) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 29 def <<(term) raise "Duplicate id #{term.id}" if @terms_by_id[term.id] @terms_by_id[term.id] = term @terms_by_name[term.name] << term @children_by_id[term.parent_id] << term if term.parent_id end |
#children(term_id) ⇒ Object
60 61 62 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 60 def children(term_id) @children_by_id[term_id] end |
#leaf?(term_id) ⇒ Boolean
68 69 70 71 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 68 def leaf?(term_id) raise "Term #{term_id} does not exist" unless @terms_by_id[term_id] @children_by_id[term_id].empty? end |
#root ⇒ Object
64 65 66 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 64 def root term('') end |
#term(term_id) ⇒ Object
56 57 58 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 56 def term(term_id) @terms_by_id[term_id] end |
#term_by_name(name) ⇒ Object
52 53 54 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 52 def term_by_name(name) @terms_by_name[name] end |
#tf_groups(slice_deepness) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/WingenderTFClass/obo/tf_classification.rb', line 73 def tf_groups(slice_deepness) @terms_by_id.each_value.select{|term| term.deepness >= slice_deepness && (!term.parent || term.parent.deepness < slice_deepness) }.map{|term| [term, term.subtree_nodes] }.to_h end |