Class: KnowledgeBase

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/matrix/knowledge_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#matrix_registryObject

Returns the value of attribute matrix_registry.



6
7
8
# File 'lib/rbbt/matrix/knowledge_base.rb', line 6

def matrix_registry
  @matrix_registry
end

Instance Method Details

#matrix(name) ⇒ Object



11
12
13
14
15
16
17
18
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/matrix/knowledge_base.rb', line 11

def matrix(name)
  matrix, options = @matrix_registry[name]

  return matrix if Matrix === matrix

  Path.setup(matrix) if not Path === matrix and File.exists? matrix

  raise "Registered matrix is strange: #{Misc.fingerprint matrix}" unless Path === matrix

  path = matrix

  raise "Registered path not found: #{path.find}" unless path.exists?
  
  if path.find.directory?
    data, labels, value_type, format, organism, identifiers = Misc.process_options options, :data, :labels, :value_type, :format, :organism, :identifiers 

    data ||= path.data if path.data.exists?
    data ||= path.values if path.values.exists?

    labels ||= path.labels if path.labels.exists?
    labels ||= path.samples if path.samples.exists?

    identifiers ||= path.identifiers if path.identifiers.exists?

    value_type = TSV.parse_header(data.find).key_field if data
    value_type ||= "Unknown ID"

    Matrix.new data, labels, value_type, format, organism, identifiers
  else
  end
end

#register_matrix(name, matrix, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rbbt/matrix/knowledge_base.rb', line 43

def register_matrix(name, matrix, options = {})
  options = Misc.add_defaults options, :sample_format => "Sample"
  sample_format = Misc.process_options options, :sample_format

  @matrix_registry ||= IndiferentHash.setup({})
  @matrix_registry[name] = [matrix, options]


  register name do
    matrix = matrix(name)
    TSV.read_matrix matrix.data_file, sample_format
  end

  register name.to_s + '_activity' do
    matrix = matrix(name)
    TmpFile.with_file do |tmpfile|
      matrix.activity_cluster(tmpfile)
      tsv = TSV.open(TSV.read_matrix(tmpfile, sample_format))
      tsv.identifiers ||= matrix.data_file.identifier_files.first
      tsv.identifiers = tsv.identifiers.find if tsv.identifiers.respond_to? :find
      
      tsv = tsv.add_field "Activity" do |k,p|
        samples, values = p
        values = values.collect{|v| v.to_i }
        new_values = case Misc.max(values) 
                     when 1
                       [''] * samples.length
                     when 2
                       values.collect{|v| v == 2 ? "active" : '' }
                     else
                       values.collect{|v|
                         case v
                         when 1
                           "inactive"
                         when 2
                           ''
                         else
                           "active"
                         end
                       }
        end
      end

      tsv
    end
  end
end