Class: RbbtMatrix
- Inherits:
-
Object
- Object
- RbbtMatrix
- Defined in:
- lib/rbbt/matrix.rb,
lib/rbbt/matrix/barcode.rb,
lib/rbbt/matrix/differential.rb
Class Attribute Summary collapse
-
.matrix_dir ⇒ Object
Returns the value of attribute matrix_dir.
Instance Attribute Summary collapse
-
#data_file ⇒ Object
Returns the value of attribute data_file.
-
#format ⇒ Object
Returns the value of attribute format.
-
#identifiers ⇒ Object
Returns the value of attribute identifiers.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#organism ⇒ Object
Returns the value of attribute organism.
-
#value_type ⇒ Object
Returns the value of attribute value_type.
Instance Method Summary collapse
- #activity_cluster(outfile, clusters = 2) ⇒ Object
- #all_fields ⇒ Object
- #barcode(outfile, factor = 2) ⇒ Object
- #barcode_ruby(outfile, factor = 2) ⇒ Object
- #comparison(main, contrast, subsets = nil) ⇒ Object
- #differential(main, contrast, path = nil) ⇒ Object
- #fields ⇒ Object
-
#initialize(data_file, labels = nil, value_type = nil, format = nil, organism = nil, identifiers = nil) ⇒ RbbtMatrix
constructor
A new instance of RbbtMatrix.
- #key_field ⇒ Object
- #samples ⇒ Object
- #subsets ⇒ Object
- #subsets=(subsets) ⇒ Object
- #to_activity(clusters = 2) ⇒ Object
- #to_average(identifiers = nil) ⇒ Object
- #to_barcode(factor = 2) ⇒ Object
- #to_barcode_ruby(factor = 2) ⇒ Object
- #to_gene(identifiers = nil) ⇒ Object
- #to_name(identifiers = nil) ⇒ Object
- #tsv(to_gene = true, identifiers = nil) ⇒ Object
Constructor Details
#initialize(data_file, labels = nil, value_type = nil, format = nil, organism = nil, identifiers = nil) ⇒ RbbtMatrix
Returns a new instance of RbbtMatrix.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rbbt/matrix.rb', line 14 def initialize(data_file, labels = nil, value_type = nil, format = nil, organism=nil, identifiers=nil) @data_file = data_file @labels = labels @value_type = value_type || 'count' @format = format _header = nil @format ||= begin _header ||= TSV.parse_header(@data_file) _header.key_field || "ID" end @organism = organism @organism ||= begin _header ||= TSV.parse_header(@data_file) _header.namespace || Organism.default_code("Hsa") end @identifiers = identifiers end |
Class Attribute Details
.matrix_dir ⇒ Object
Returns the value of attribute matrix_dir.
7 8 9 |
# File 'lib/rbbt/matrix.rb', line 7 def matrix_dir @matrix_dir end |
Instance Attribute Details
#data_file ⇒ Object
Returns the value of attribute data_file.
13 14 15 |
# File 'lib/rbbt/matrix.rb', line 13 def data_file @data_file end |
#format ⇒ Object
Returns the value of attribute format.
13 14 15 |
# File 'lib/rbbt/matrix.rb', line 13 def format @format end |
#identifiers ⇒ Object
Returns the value of attribute identifiers.
13 14 15 |
# File 'lib/rbbt/matrix.rb', line 13 def identifiers @identifiers end |
#labels ⇒ Object
Returns the value of attribute labels.
13 14 15 |
# File 'lib/rbbt/matrix.rb', line 13 def labels @labels end |
#organism ⇒ Object
Returns the value of attribute organism.
13 14 15 |
# File 'lib/rbbt/matrix.rb', line 13 def organism @organism end |
#value_type ⇒ Object
Returns the value of attribute value_type.
13 14 15 |
# File 'lib/rbbt/matrix.rb', line 13 def value_type @value_type end |
Instance Method Details
#activity_cluster(outfile, clusters = 2) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rbbt/matrix/barcode.rb', line 48 def activity_cluster(outfile, clusters = 2) clusters = Array === clusters ? clusters : (2..clusters).to_a FileUtils.mkdir_p File.dirname(outfile) unless outfile.nil? or File.exists? File.dirname(outfile) cmd =<<-EOF source('#{Rbbt.share.R['barcode.R'].find}') rbbt.GE.activity_cluster(#{ R.ruby2R self.data_file }, #{ R.ruby2R outfile }, #{R.ruby2R key_field}, #{R.ruby2R clusters}) EOF R.run(cmd) end |
#all_fields ⇒ Object
32 33 34 |
# File 'lib/rbbt/matrix.rb', line 32 def all_fields @all_fields ||= TSV.parse_header(@data_file).all_fields end |
#barcode(outfile, factor = 2) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rbbt/matrix/barcode.rb', line 4 def (outfile, factor = 2) FileUtils.mkdir_p File.dirname(outfile) unless outfile.nil? or File.exists? File.dirname(outfile) cmd =<<-EOF source('#{Rbbt.share.R['barcode.R'].find}') rbbt.GE.barcode.mode(#{ R.ruby2R self.data_file }, #{ R.ruby2R outfile }, #{ R.ruby2R factor }) EOF R.run(cmd) end |
#barcode_ruby(outfile, factor = 2) ⇒ Object
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 42 43 44 45 46 |
# File 'lib/rbbt/matrix/barcode.rb', line 15 def (outfile, factor = 2) parser = TSV::Parser.new self.data_file dumper = TSV::Dumper.new parser..merge(:type => :list, :cast => :to_i) dumper.init TSV.traverse parser, :into => dumper, :bar => "Barcoding #{self.data_file}" do |key,values| clean_values = values.flatten.compact.collect{|v| v.to_f} modes = R.eval("rbbt.get.modes(#{R.ruby2R clean_values})$modes") mode = Array === modes ? modes.first : modes mode_values = clean_values.select{|v| v.to_f <= mode} mode_values.concat mode_values.collect{|v| v+mode} sd = Misc.sd mode_values if sd.nil? [key, [nil] * values.length] else threshold = mode + sd = if Array === values.compact.first values.collect do |v| Misc.mean(v.compact.collect{|v| v.to_f}) > threshold ? 1 : 0 end else values.collect do |v| v.to_f > threshold ? 1 : 0 end end key = key.first if Array === key [key, ] end end Misc.sensiblewrite(outfile, dumper.stream) end |
#comparison(main, contrast, subsets = nil) ⇒ Object
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 |
# File 'lib/rbbt/matrix.rb', line 109 def comparison(main, contrast, subsets = nil) subsets ||= self.subsets if main.index "=" main_factor, main_value = main.split "=" raise ParameterException, "Main selection not understood" if subsets[main_factor].nil? or subsets[main_factor][main_value].nil? value = subsets[main_factor][main_value] main_samples = String === value ? value.split(',') : value else main_samples = main.split(/[|,\n]/) end if contrast if contrast.index "=" contrast_factor, contrast_value = contrast.split "=" raise ParameterException, "Contrast selection not understood" if subsets[contrast_factor].nil? or subsets[contrast_factor][contrast_value].nil? value = subsets[contrast_factor][contrast_value] contrast_samples = String === value ? value.split(',') : value else contrast_samples = contrast.split(/[|,\n]/) end else if subsets and main_factor contrast_samples = subsets[main_factor].values.flatten.collect{|s| s.split ',' }.flatten.uniq - main_samples else contrast_samples = samples - main_samples end end main_samples = main_samples.compact.reject{|m| m.empty? }.collect{|m| m.strip } contrast_samples = contrast_samples.compact.reject{|m| m.empty? }.collect{|m| m.strip } [main_samples, contrast_samples] end |
#differential(main, contrast, path = nil) ⇒ Object
4 5 6 7 8 9 10 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 42 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 |
# File 'lib/rbbt/matrix/differential.rb', line 4 def differential(main, contrast, path = nil) all_samples = self.samples if Array === main and Array === contrast main_samples, contrast_samples = main, contrast else main_samples, contrast_samples = comparison main, contrast end name = data_file =~ /:>/ ? File.basename(data_file) : data_file main_samples = main_samples & all_samples contrast_samples = contrast_samples & all_samples raise "No main samples found" if main_samples.empty? raise "No contrast samples found" if contrast_samples.empty? Persist.persist(name, :tsv, :persist => true, :file => path, :other => {:main => main_samples, :contrast => contrast_samples}, :prefix => "Diff", :dir => RbbtMatrix.matrix_dir.differential, :no_load => true) do |file| raise if file.nil? case value_type when 'two_channel' log2 = true trend = false two_channel = true when nil, 'count', 'counts' log2 = true trend = false two_channel = false when 'fpkm' log2 = true trend = true two_channel = false when 'log2 ratio', 'transformed count' log2 = false trend = false two_channel = false else Log.warn "Unkown value_type: #{value_type}" log2 = true trend = false two_channel = false end file = file.find if Path === file FileUtils.mkdir_p File.dirname(file) unless file.nil? or File.exists? File.dirname(file) cmd = <<-EOS source('#{Rbbt.share.R["MA.R"].find(:lib)}') data = rbbt.dm.matrix.differential(#{ R.ruby2R data_file }, main = #{R.ruby2R(main_samples)}, contrast = #{R.ruby2R(contrast_samples)}, log2=#{ R.ruby2R log2 }, outfile = #{R.ruby2R file}, key.field = #{R.ruby2R format}, two.channel = #{R.ruby2R two_channel}, namespace = #{R.ruby2R organism}, eBayes.trend = #{R.ruby2R trend} ) EOS R.run(cmd, :monitor => true) end end |
#fields ⇒ Object
36 37 38 |
# File 'lib/rbbt/matrix.rb', line 36 def fields all_fields[1..-1] end |
#key_field ⇒ Object
40 41 42 |
# File 'lib/rbbt/matrix.rb', line 40 def key_field all_fields.first end |
#samples ⇒ Object
44 45 46 |
# File 'lib/rbbt/matrix.rb', line 44 def samples @samples ||= TSV.parse_header(@data_file).fields end |
#subsets ⇒ Object
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rbbt/matrix.rb', line 52 def subsets @subsets ||= begin subsets = {} case @labels when Path if @labels.exists? labels = @labels.tsv factors = labels.fields labels.through do |sample,values| factors.zip(values).each do |factor,value| subsets[factor] ||= {} subsets[factor][value] ||= [] subsets[factor][value] << sample end end end when TSV factors = @labels.fields @labels.through do |sample,values| factors.zip(values).each do |factor,value| subsets[factor] ||= {} subsets[factor][value] ||= [] subsets[factor][value] << sample end end when Hash @labels.each do |factor,info| subsets[factors] ||= {} info.each do |value, samples| subsets[factors][value] = case samples when Array samples when String samples.split ',' else raise "Format of samples not understood: #{Misc.finguerprint samples}" end end end end clean_subsets = {} subsets.each do |factor,values| next if values.nil? or values.size < 2 values.each do |level,samples| next if samples.nil? or samples.length < 2 clean_subsets[factor] ||= {} clean_subsets[factor][level] = samples end end clean_subsets end end |
#subsets=(subsets) ⇒ Object
48 49 50 |
# File 'lib/rbbt/matrix.rb', line 48 def subsets=(subsets) @subsets = subsets end |
#to_activity(clusters = 2) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rbbt/matrix.rb', line 223 def to_activity(clusters = 2) require 'rbbt/tsv/change_id' name = data_file =~ /:>/ ? File.basename(data_file) : data_file file = Persist.persist(data_file, :tsv, :prefix => "Activity #{clusters.inspect}", :check => [data_file], :dir => RbbtMatrix.matrix_dir., :no_load => true) do |filename| activity_cluster(filename, clusters) end subsets = self.subsets matrix = RbbtMatrix.new file, labels, value_type, key_field, organism matrix.subsets = subsets matrix end |
#to_average(identifiers = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rbbt/matrix.rb', line 143 def to_average(identifiers = nil) name = data_file =~ /:>/ ? File.basename(data_file) : data_file file = Persist.persist(data_file, :tsv, :prefix => "Average", :check => [data_file], :dir => RbbtMatrix.matrix_dir.values, :no_load => true) do data = data_file.tsv(:cast => :to_f, :type => :double) data.to_list{|v| v.length > 1 ? Misc.mean(v) : v } end subsets = self.subsets matrix = RbbtMatrix.new file, labels, value_type, key_field, organism matrix.subsets = subsets matrix end |
#to_barcode(factor = 2) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/rbbt/matrix.rb', line 211 def (factor = 2) name = data_file =~ /:>/ ? File.basename(data_file) : data_file file = Persist.persist(data_file, :tsv, :prefix => "Barcode R #{factor}", :check => [data_file], :dir => RbbtMatrix.matrix_dir., :no_load => true) do |filename| (filename, factor) end subsets = self.subsets matrix = RbbtMatrix.new file, labels, value_type, key_field, organism matrix.subsets = subsets matrix end |
#to_barcode_ruby(factor = 2) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/rbbt/matrix.rb', line 199 def (factor = 2) name = data_file =~ /:>/ ? File.basename(data_file) : data_file file = Persist.persist(data_file, :tsv, :prefix => "Barcode #{factor}", :check => [data_file], :dir => RbbtMatrix.matrix_dir., :no_load => true) do |filename| (filename, factor) end subsets = self.subsets matrix = RbbtMatrix.new file, labels, value_type, key_field, organism matrix.subsets = subsets matrix end |
#to_gene(identifiers = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rbbt/matrix.rb', line 158 def to_gene(identifiers = nil) require 'rbbt/tsv/change_id' name = data_file =~ /:>/ ? File.basename(data_file) : data_file file = Persist.persist(data_file, :tsv, :prefix => "Gene", :check => [data_file], :dir => RbbtMatrix.matrix_dir.values, :no_load => true) do data = data_file.tsv(:cast => :to_f) identifiers = [identifiers, @identifiers, data.identifiers, Organism.identifiers(organism)].flatten.compact.uniq data.change_key("Ensembl Gene ID", :identifiers => identifiers.reverse) do |v| Misc.mean(v.compact) end end subsets = self.subsets matrix = RbbtMatrix.new file, labels, value_type, "Ensembl Gene ID", organism matrix.subsets = subsets matrix end |
#to_name(identifiers = nil) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rbbt/matrix.rb', line 179 def to_name(identifiers = nil) require 'rbbt/tsv/change_id' name = data_file =~ /:>/ ? File.basename(data_file) : data_file file = Persist.persist(data_file, :tsv, :prefix => "Name", :check => [data_file], :dir => RbbtMatrix.matrix_dir.values, :no_load => true) do data = data_file.tsv(:cast => :to_f) identifiers = [identifiers, @identifiers, data.identifiers, Organism.identifiers(organism)].flatten.compact.uniq data.change_key("Associated Gene Name", :identifiers => identifiers.reverse) do |v| Misc.mean(v.compact) end end subsets = self.subsets matrix = RbbtMatrix.new file, labels, value_type, "Associated Gene Name", organism matrix.subsets = subsets matrix end |
#tsv(to_gene = true, identifiers = nil) ⇒ Object
237 238 239 240 241 242 243 244 |
# File 'lib/rbbt/matrix.rb', line 237 def tsv(to_gene=true, identifiers = nil) if to_gene and key_field != "Ensembl Gene ID" file = self.to_gene(identifiers).data_file file.tsv :persist => true, :persist_dir => RbbtMatrix.matrix_dir.persist, :type => :double, :merge => true else self.data_file.tsv :persist => true, :persist_dir => RbbtMatrix.matrix_dir.persist, :merge => true end end |