Method: Matrix#differential

Defined in:
lib/rbbt/matrix/differential.rb

#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 => Matrix.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