Sha256: be9f914d1acfd87c17fda4fe1580fc2df8a51c36be50a2a301fc92297009dae5

Contents?: true

Size: 1.91 KB

Versions: 9

Compression:

Stored size: 1.91 KB

Contents

require 'rbbt/util/R'

class RbbtMatrix
  def barcode(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, :monitor => true)
  end

  def barcode_ruby(outfile, factor = 2)
    parser = TSV::Parser.new self.data_file
    dumper = TSV::Dumper.new parser.options.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
        bars = 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, bars]
      end
    end

    Misc.sensiblewrite(outfile, dumper.stream)
  end

  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, :monitor => true)
  end


end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rbbt-dm-1.2.7 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.2.6 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.2.4 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.2.3 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.2.1 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.1.63 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.1.62 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.1.61 lib/rbbt/matrix/barcode.rb
rbbt-dm-1.1.60 lib/rbbt/matrix/barcode.rb