rbbt-util 5.14.32 → 5.14.33

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cb343242db38c910d25a554cd9e4bec1efbe8b1
4
- data.tar.gz: 1da7fa8d52a241541bc8266145826acd133004c0
3
+ metadata.gz: e156f608c54a1a71f1d7892428698d857075c4e7
4
+ data.tar.gz: 535f2b8b01c561226a389050cbb90b31b82ac9e2
5
5
  SHA512:
6
- metadata.gz: 18a7224b05d0c8cf4b73c3a8f08c2dc65cc3894e63225e8e5348e67ec8c26fbe6749277f3a18197b64a514744e0f70c5b76141bfce501d4c12faf0f3d1292174
7
- data.tar.gz: 6718fb810d5e471361e93fb1563b190627e09a7be17e95b5f6b5ec6722fddc025a09d466ecbc1e5d64bd928ca47fee8f6ca4b23ec1456b3cfd09489ea9dbb90b
6
+ metadata.gz: 8bad7add5780e3764172ffb11c87b8b3cebc5267fe2f11dbe0efcdd46cebd92019b669346d31ff08a0ab32321356c2fc21ef5f776d1ce188d8527f2b1a4044c9
7
+ data.tar.gz: 543e58c6022ccacbbd164aa5a97df5ab7ab9c168582ec212b5b5cfce7714b02ca1f5b74b2c59a50c27297aa9113ccbf548ce62e3aafa3d530371361acb180bc3
@@ -288,10 +288,15 @@ module Association
288
288
  def self.index(file, options = {}, persist_options = nil)
289
289
  options = {} if options.nil?
290
290
  options = Misc.add_defaults options, :persist => true, :undirected => false
291
- persist_options = Misc.pull_keys options, persist_options if persist_options.nil?
291
+ persist_options = Misc.pull_keys options, :persist if persist_options.nil?
292
292
 
293
- Persist.persist_tsv(file, nil, options, {:persist => true, :prefix => "Association Index"}.merge(persist_options).merge(:engine => TokyoCabinet::BDB, :serializer => :clean)) do |assocs|
293
+ expanded_persist_options = {:persist => true, :prefix => "Association Index"}.
294
+ merge(persist_options).
295
+ merge(:engine => TokyoCabinet::BDB, :serializer => :clean)
296
+
297
+ Persist.persist_tsv(file, nil, options, expanded_persist_options) do |assocs|
294
298
  undirected = options[:undirected]
299
+ recycle = options[:recycle]
295
300
  if file
296
301
  tsv = TSV === file ? file : Association.open(file, options, persist_options.merge(:persist => false))
297
302
 
@@ -330,8 +335,18 @@ module Association
330
335
  next if values.empty?
331
336
  next if source.nil? or source.empty?
332
337
  next if values.empty?
338
+
333
339
  targets = values.first
334
- rest = Misc.zip_fields values[1..-1]
340
+
341
+ rest = values[1..-1]
342
+
343
+ size = values.first ? values.first.length : 0
344
+
345
+ rest.each_with_index do |list,i|
346
+ list.replace [list.first] * size if list.length == 1
347
+ end if recycle and size > 1
348
+
349
+ rest = Misc.zip_fields rest
335
350
 
336
351
  annotations = rest.length > 1 ?
337
352
  targets.zip(rest) :
@@ -23,7 +23,7 @@ module TSV
23
23
  when other.type == :single
24
24
  new_values = [other[key]]
25
25
  else
26
- other_values = other[key]
26
+ other_values = other[key] || [nil] * other.fields.length
27
27
  new_values = field_positions.collect do |pos|
28
28
  pos == :key ? key : other_values[pos]
29
29
  end
@@ -1,6 +1,14 @@
1
+ require 'rbbt/association'
2
+
1
3
  module TSV
2
- def self.read_matrix(tsv, field_format = "ID", value_format = "Value")
4
+ def self.read_matrix(tsv, field_format = "ID", value_format = "Value", *others)
3
5
  tsv = TSV.open(tsv) unless TSV === tsv
6
+
7
+
8
+ if others.any?
9
+ other_tsv = tsv.slice(others)
10
+ tsv = tsv.slice(tsv.fields - others)
11
+ end
4
12
 
5
13
  key_field, *fields = tsv.all_fields
6
14
  options = tsv.options.merge(:key_field => key_field, :fields => [field_format, value_format], :type => :double, :cast => nil)
@@ -15,6 +23,26 @@ module TSV
15
23
  [key, [fields, values]]
16
24
  end
17
25
 
18
- dumper.stream
26
+ res = TSV.open(dumper.stream, options)
27
+ if others.any?
28
+ other_tsv = other_tsv.to_double
29
+ res.attach other_tsv, :one2one => true
30
+ else
31
+ res
32
+ end
33
+ end
34
+
35
+ def matrix_melt(*args)
36
+ tsv = TSV.read_matrix(self, *args)
37
+
38
+ melt = Association.index tsv, :persist => false, :recycle => true
39
+ source_field,_sep,target_field = melt.key_field.partition "~"
40
+ melt.add_field source_field do |k,v|
41
+ k.partition("~").first
42
+ end
43
+ melt.add_field target_field do |k,v|
44
+ k.partition("~").last
45
+ end
46
+ melt
19
47
  end
20
48
  end
data/lib/rbbt/tsv/util.rb CHANGED
@@ -69,8 +69,12 @@ module TSV
69
69
  case
70
70
  when (defined? Step and Step === file)
71
71
  file.path
72
- when String === file
72
+ when Path === file
73
73
  file
74
+ when (String === file and (Open.exists? file or Open.remote? file))
75
+ file
76
+ when String === file
77
+ "String-#{Misc.digest file}"
74
78
  when file.respond_to?(:filename)
75
79
  file.filename
76
80
  when file.respond_to?(:gets)
@@ -12,41 +12,90 @@ module Rserve
12
12
  end
13
13
 
14
14
  module R
15
- PID = Process.pid
15
+ SESSION = ENV["RServe-session"] || "Session-PID-" + Process.pid.to_s
16
+
17
+ def self.socket_file
18
+ @@socket_file ||= Rbbt.tmp.R_sockets[R::SESSION].find
19
+ end
20
+
21
+ def self.lockfile
22
+ @@lockfile ||= socket_file + '.lock'
23
+ end
24
+
25
+ def self.workdir
26
+ @@workdir ||= socket_file + '.wd'
27
+ end
28
+
29
+ def self.pid_file
30
+ @@pidfile ||= File.join(workdir, 'pid')
31
+ end
32
+
33
+ def self.clear
34
+ Log.warn "Clearing Rserver session #{SESSION}, PID #{@@instance_process}"
35
+ @@instance = nil
36
+ if defined? @@instance_process and @@instance_process and Misc.pid_exists? @@instance_process
37
+ begin
38
+ Process.kill :INT, @@instance_process
39
+ rescue Exception
40
+ end
41
+ end
42
+ FileUtils.rm_rf pid_file if File.exists? pid_file
43
+ FileUtils.rm_rf socket_file if File.exists? socket_file
44
+ FileUtils.rm_rf lockfile if File.exists? lockfile
45
+ FileUtils.rm_rf workdir if File.exists? workdir
46
+ end
47
+
16
48
  def self.instance
17
49
  @@instance ||= begin
18
- @@socket_file = Rbbt.tmp.R_sockets[R::PID].find
19
50
 
20
- FileUtils.mkdir_p File.dirname(@@socket_file) unless File.directory?(File.dirname(@@socket_file))
51
+ clear if File.exists? pid_file and ! Misc.pid_exists?(Open.read(pid_file).strip.to_i)
52
+
53
+ FileUtils.mkdir_p File.dirname(socket_file) unless File.directory?(File.dirname(socket_file))
54
+ FileUtils.mkdir_p workdir unless File.directory? workdir
55
+
56
+ at_exit do
57
+ self.clear
58
+ end unless defined? @@instance_process
21
59
 
22
60
  begin
23
61
 
24
- if not File.exists? @@socket_file
62
+ if not File.exists? socket_file
25
63
 
26
- @@instance_process = Process.fork do
27
- args = %w(CMD Rserve --vanilla --quiet --RS-socket)
28
- args << "'#{@@socket_file}'"
64
+ sh_pid = Process.fork do
65
+ #args = %w(CMD Rserve --vanilla --quiet --RS-socket)
66
+ args = %w(--vanilla --quiet --RS-socket)
67
+ args << "'#{socket_file}'"
68
+ args << "--RS-workdir"
69
+ args << "'#{workdir}'"
70
+ args << "--RS-pidfile"
71
+ args << "'#{pid_file}'"
29
72
 
30
- bin_path = "R"
73
+ bin_path = File.join(ENV["R_HOME"], "bin/Rserve")
31
74
  cmd = bin_path + " " + args*" "
32
75
  exec(ENV, cmd)
33
76
  end
34
- sleep 1
77
+ while not File.exists? pid_file
78
+ sleep 0.5
79
+ end
80
+ @@instance_process = Open.read(pid_file).to_i
81
+ Log.info "New Rserver session stated with PID (#{sh_pid}) #{@@instance_process}: #{SESSION}"
35
82
  end
36
83
 
84
+ i = Rserve::Connection.new :hostname => socket_file
85
+
37
86
  begin
38
- i = Rserve::Connection.new :hostname => @@socket_file
87
+ FileUtils.mkdir workdir unless File.exists? workdir
88
+ i.eval "setwd('#{workdir}');"
39
89
  i.eval "source('#{UTIL}');"
40
- wdir = @@socket_file + '.wd'
41
- FileUtils.mkdir wdir unless File.exists? wdir
42
- i.eval "setwd('#{wdir}');"
43
90
  i
44
91
  rescue Exception
92
+ Log.exception $!
45
93
  raise TryAgain
46
94
  end
47
95
  rescue Exception
96
+ Log.exception $!
48
97
  Process.kill :INT, @@instance_process if defined? @@instance_process and @@instance_process
49
- FileUtils.rm @@socket_file if File.exists? @@socket_file
98
+ FileUtils.rm socket_file if File.exists? socket_file
50
99
  retry if TryAgain === $!
51
100
  raise $!
52
101
  end
@@ -54,7 +103,20 @@ module R
54
103
  end
55
104
 
56
105
  def self._eval(cmd)
57
- instance.eval(cmd)
106
+ Misc.lock lockfile do
107
+ times = 2
108
+ begin
109
+ instance.eval(cmd)
110
+ rescue Rserve::Connection::EvalError
111
+ times = times - 1
112
+ if times > 0
113
+ clear
114
+ retry
115
+ else
116
+ raise $!
117
+ end
118
+ end
119
+ end
58
120
  end
59
121
 
60
122
  def self.eval_a(cmd)
@@ -16,11 +16,25 @@ module R
16
16
 
17
17
  self.model_dir = Rbbt.var.R.models
18
18
  class Model
19
+ R_METHOD = :debug
19
20
 
20
21
  attr_accessor :name, :formula
21
- def initialize(name, formula)
22
+ def initialize(name, formula, options = {})
22
23
  @name = name
23
24
  @formula = formula
25
+ @options = options || {}
26
+ end
27
+
28
+ def colClasses(tsv)
29
+ "c('character', " <<
30
+ (tsv.fields.collect{|f| R.ruby2R(@options[f] ? @options[f].to_s : ":NA") } * ", ") <<
31
+ ")"
32
+ end
33
+
34
+ def r_options(tsv)
35
+ {:R_open => "colClasses=#{colClasses(tsv)}",
36
+ :R_method => (@options[:R_method] || R_METHOD),
37
+ :source => @options[:source]}
24
38
  end
25
39
 
26
40
  def model_file
@@ -28,10 +42,11 @@ module R
28
42
  end
29
43
 
30
44
  def update(tsv, field = "Prediction")
31
- tsv.R <<-EOF, :R_method => :eval
45
+ tsv.R <<-EOF, r_options(tsv)
32
46
  model = rbbt.model.load('#{model_file}');
33
47
  model = update(model, data);
34
48
  save(model, file='#{model_file}');
49
+ data = NULL
35
50
  EOF
36
51
  end
37
52
 
@@ -49,9 +64,10 @@ save(model, file='#{model_file}');
49
64
 
50
65
  def predict(tsv, field = "Prediction")
51
66
  tsv = Model.groom tsv, formula
52
- tsv.R <<-EOF, :R_method => :eval, :key => tsv.key_field
67
+ tsv.R <<-EOF, r_options(tsv)
53
68
  model = rbbt.model.load('#{model_file}');
54
- data$#{field} = predict(model, data);
69
+ data.groomed = rbbt.model.groom(data,formula=#{formula})
70
+ data$#{field} = predict(model, data.groomed);
55
71
  EOF
56
72
  end
57
73
 
@@ -67,9 +83,10 @@ data$#{field} = predict(model, data);
67
83
  tsv = Model.groom(tsv, formula)
68
84
 
69
85
  FileUtils.mkdir_p File.dirname(model_file) unless File.exists?(File.dirname(model_file))
70
- tsv.R <<-EOF, :R_method => :shell
86
+ tsv.R <<-EOF, r_options(tsv)
71
87
  model = rbbt.model.fit(data, #{formula}, method=#{method}#{args_str})
72
88
  save(model, file='#{model_file}')
89
+ data = NULL
73
90
  EOF
74
91
  end
75
92
  end
data/lib/rbbt/util/R.rb CHANGED
@@ -93,7 +93,7 @@ source('#{UTIL}');
93
93
  when Symbol
94
94
  "#{ object }"
95
95
  when String
96
- "'#{ object }'"
96
+ object[0] == ":" ? object[1..-1] : "'#{ object }'"
97
97
  when Fixnum, Float
98
98
  object
99
99
  when TrueClass
@@ -120,6 +120,7 @@ module TSV
120
120
  def R(script, source = nil, open_options = {})
121
121
  open_options, source = source, nil if Hash === source
122
122
 
123
+ source ||= Misc.process_options open_options, :source
123
124
  source = [source] if String === source
124
125
 
125
126
  require_sources = source.collect{|source|
@@ -153,6 +154,7 @@ data = rbbt.tsv('#{f}'#{tsv_R_option_str});
153
154
 
154
155
  ## Resaving data
155
156
  if (! is.null(data)){ rbbt.tsv.write('#{f}', data); }
157
+ NULL
156
158
  EOF
157
159
 
158
160
 
@@ -319,8 +319,8 @@ class Step
319
319
  set_info :pid, nil
320
320
  ensure
321
321
  RbbtSemaphore.post_semaphore(semaphore) if semaphore
322
+ Kernel.exit! 0
322
323
  end
323
- Kernel.exit! 0
324
324
  end
325
325
  Process.detach(@pid)
326
326
  self
data/share/Rlib/util.R CHANGED
@@ -228,6 +228,29 @@ rbbt.parse <- function(filename){
228
228
  return(parse(text=paste(lines[from:to],sep="\n")));
229
229
  }
230
230
 
231
+ rbbt.pull.keys <- function(items, key){
232
+ pulled = list()
233
+ rest = list()
234
+
235
+ names = names(items)
236
+
237
+ prefix = paste("^",key,'.',sep='')
238
+ matches = grep(prefix, names)
239
+
240
+ for (i in seq(1,length(names))){
241
+ if (i %in% matches){
242
+ name = names[i]
243
+ name = sub(prefix, "", name)
244
+ pulled[[name]] = items[[i]]
245
+ }else{
246
+ name = names[i]
247
+ rest[[name]] = items[[i]]
248
+ }
249
+ }
250
+
251
+ list(pulled=pulled, rest=rest)
252
+ }
253
+
231
254
  rbbt.run <- function(filename){
232
255
  rbbt.reload();
233
256
  eval(rbbt.parse(filename), envir=globalenv());
@@ -314,6 +337,14 @@ rbbt.ddd <- function(o){
314
337
  cat("\n", file = stderr())
315
338
  }
316
339
 
340
+ # From: http://ryouready.wordpress.com/2008/12/18/generate-random-string-name/
341
+ rbbt.random_string <- function(n=1, length=12){
342
+ randomString <- c(1:n)
343
+ for (i in 1:n){
344
+ randomString[i] <- paste(sample(c(0:9, letters, LETTERS), length, replace=TRUE), collapse="")
345
+ }
346
+ return(randomString)
347
+ }
317
348
 
318
349
 
319
350
  # {{{ MODELS
@@ -332,7 +363,7 @@ rbbt.model.groom <- function(data, variables = NULL, classes = NULL, formula = N
332
363
  variables = names[names %in% all.vars(formula)]
333
364
  }
334
365
 
335
- data.groomed = data[,variables]
366
+ data.groomed = data[,variables,drop=F]
336
367
 
337
368
  if (! is.null(classes)){
338
369
  if (is.character(classes)){ classes = rep(classes,dim(data.groomed)[2]) }
@@ -362,29 +393,6 @@ rbbt.model.load <- function(file, force = F){
362
393
  rbbt.loaded.models[[file]]
363
394
  }
364
395
 
365
- rbbt.pull.keys <- function(items, key){
366
- pulled = list()
367
- rest = list()
368
-
369
- names = names(items)
370
-
371
- prefix = paste("^",key,'.',sep='')
372
- matches = grep(prefix, names)
373
-
374
- for (i in seq(1,length(names))){
375
- if (i %in% matches){
376
- name = names[i]
377
- name = sub(prefix, "", name)
378
- pulled[[name]] = items[[i]]
379
- }else{
380
- name = names[i]
381
- rest[[name]] = items[[i]]
382
- }
383
- }
384
-
385
- list(pulled=pulled, rest=rest)
386
- }
387
-
388
396
  rbbt.model.add_fit <- function(data, formula, method, classes=NULL, ...){
389
397
  data.groomed = rbbt.model.groom(data, formula=formula, classes = classes);
390
398
 
@@ -3,11 +3,14 @@
3
3
  require 'rbbt-util'
4
4
  require 'rbbt/util/simpleopt'
5
5
 
6
- options = SOPT.get "-e--environment*:-p--port*:-s--server*:-f--finder"
6
+ options = SOPT.get "-e--environment*:-p--port*:-s--server*:-f--finder-R--Rserve_session*"
7
7
  options[:Port] ||= options[:port]
8
8
 
9
+
9
10
  app = ARGV.shift
10
11
 
12
+ ENV["RServe-session"] = options[:RServe_session] || app
13
+
11
14
  app_dir = Rbbt.etc.app_dir.exists? ? Path.setup(Rbbt.etc.app_dir.read.strip) : Rbbt.apps.find
12
15
 
13
16
  app_dir = app_dir[app]
@@ -13,10 +13,14 @@ require 'rbbt/rest/file_server'
13
13
  require 'rbbt/rest/knowledge_base'
14
14
  require 'rbbt/rest/helpers'
15
15
 
16
- options = SOPT.get "-e--environment*:-p--port*:-s--server*:-b--bind*:-e--environment*"
16
+ options = SOPT.get "-e--environment*:-p--port*:-s--server*:-b--bind*:-e--environment*:-R--RServe_session*"
17
17
 
18
18
  workflow = ARGV.first
19
19
 
20
+ ENV["RServe-session"] = options[:RServe_session] || workflow
21
+
22
+ raise rbbt_usage unless workflow
23
+
20
24
  wf = Workflow.require_workflow workflow
21
25
 
22
26
  $title = wf.to_s
@@ -0,0 +1,20 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
+ require 'rbbt/tsv'
3
+ require 'rbbt/tsv/matrix'
4
+
5
+ class TestClass < Test::Unit::TestCase
6
+ def test_matrix
7
+ tsv = TSV.open <<-EOF, :sep => " ", :type => :list
8
+ #X Y Z Type Case
9
+ a 2 3 v l
10
+ A 4 7 v L
11
+ m 2 3 c l
12
+ n 4 7 c L
13
+ EOF
14
+ tsv = TSV.open(tsv.to_s)
15
+
16
+ melt = tsv.matrix_melt("Letter", "Value", "Type", "Case")
17
+ assert_equal "L", melt["A~Y"][2]
18
+ end
19
+ end
20
+
@@ -25,7 +25,7 @@ class TestRModel < Test::Unit::TestCase
25
25
  assert pred > y and pred < y + 4
26
26
  end
27
27
 
28
- def test_add_fit
28
+ def _test_add_fit
29
29
  tsv = TSV.open datafile_test('dose_response'), :type => :list
30
30
  tsv = tsv.slice(["Dose", "Response"])
31
31
 
@@ -49,4 +49,41 @@ data = rbbt.model.inpute(data, CI ~ Dose, method=drm, classes='numeric', fct=LL.
49
49
 
50
50
  assert_equal result.size, result.column("CI").values.flatten.reject{|p| p.nil? or p.empty? or p == "NA"}.length
51
51
  end
52
+
53
+ def test_ab_surv_corr
54
+ require 'rbbt/workflow'
55
+ Workflow.require_workflow "Miller"
56
+
57
+ require 'rbbt/util/R/model'
58
+ require 'rbbt/util/R/svg'
59
+
60
+ antibody = "14-3-3-Zeta(C)_GBL9006927"
61
+
62
+ rppa = Miller.RPPA.data.tsv
63
+ rppa.attach Miller.RPPA.labels
64
+ viability = Miller.Viability.data.tsv
65
+
66
+ viability.add_field "Perturbation" do |compound,values|
67
+ values["Dose"].collect do |dose|
68
+ compound.split("-").flatten.zip(dose.split("-")).collect{|p| p * "="} * "-"
69
+ end
70
+ end
71
+
72
+ viability = viability.reorder "Perturbation", nil, :zipped => true
73
+ compounds = viability.column("Compound").flatten.uniq
74
+
75
+ rppa = rppa.select("Compound"){|c| ! c.include? "-" and compounds.include? c}
76
+ rppa = rppa.slice([antibody,"Compound", "Dose"])
77
+ rppa.rename_field antibody, "RPPA"
78
+
79
+ model = R::Model.new "viability", "Effect ~ Dose * Compound", "Compound" => :factor
80
+
81
+ model.fit(viability.select("Compound"){|c| ! c.include? "-"}, 'drm', :fct => ":LL.4()")
82
+
83
+ rppa = model.predict(rppa, "Prediction")
84
+
85
+ plot_script = "plot<-ggplot(data=data) + geom_point(aes(x=RPPA, y=Prediction, color=Compound));"
86
+
87
+ puts R::SVG.ggplotSVG rppa, plot_script, 7, 7, :R_method => :eval
88
+ end
52
89
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.14.32
4
+ version: 5.14.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-02 00:00:00.000000000 Z
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -336,6 +336,7 @@ files:
336
336
  - test/rbbt/tsv/test_filter.rb
337
337
  - test/rbbt/tsv/test_index.rb
338
338
  - test/rbbt/tsv/test_manipulate.rb
339
+ - test/rbbt/tsv/test_matrix.rb
339
340
  - test/rbbt/tsv/test_parallel.rb
340
341
  - test/rbbt/tsv/test_parser.rb
341
342
  - test/rbbt/tsv/test_stream.rb
@@ -448,6 +449,7 @@ test_files:
448
449
  - test/rbbt/tsv/test_field_index.rb
449
450
  - test/rbbt/tsv/test_parallel.rb
450
451
  - test/rbbt/tsv/test_index.rb
452
+ - test/rbbt/tsv/test_matrix.rb
451
453
  - test/rbbt/tsv/test_change_id.rb
452
454
  - test/rbbt/tsv/test_parser.rb
453
455
  - test/rbbt/tsv/test_stream.rb