rbbt-util 5.2.4 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +8 -8
  2. data/bin/rbbt +23 -10
  3. data/bin/rbbt_monitor.rb +8 -8
  4. data/lib/rbbt/annotations.rb +22 -1
  5. data/lib/rbbt/annotations/util.rb +1 -1
  6. data/lib/rbbt/entity.rb +162 -0
  7. data/lib/rbbt/fix_width_table.rb +7 -0
  8. data/lib/rbbt/persist.rb +16 -9
  9. data/lib/rbbt/persist/tsv.rb +14 -8
  10. data/lib/rbbt/resource.rb +1 -6
  11. data/lib/rbbt/resource/path.rb +23 -27
  12. data/lib/rbbt/tsv.rb +33 -4
  13. data/lib/rbbt/tsv/accessor.rb +100 -57
  14. data/lib/rbbt/tsv/attach.rb +3 -1
  15. data/lib/rbbt/tsv/attach/util.rb +34 -10
  16. data/lib/rbbt/tsv/index.rb +12 -3
  17. data/lib/rbbt/tsv/manipulate.rb +25 -1
  18. data/lib/rbbt/tsv/parser.rb +1 -0
  19. data/lib/rbbt/util/R.rb +36 -6
  20. data/lib/rbbt/util/cmd.rb +2 -1
  21. data/lib/rbbt/util/color.rb +250 -0
  22. data/lib/rbbt/util/colorize.rb +57 -0
  23. data/lib/rbbt/util/misc.rb +57 -19
  24. data/lib/rbbt/util/named_array.rb +66 -14
  25. data/lib/rbbt/util/open.rb +134 -10
  26. data/lib/rbbt/util/semaphore.rb +71 -0
  27. data/lib/rbbt/workflow.rb +34 -7
  28. data/lib/rbbt/workflow/accessor.rb +12 -8
  29. data/lib/rbbt/workflow/step.rb +44 -28
  30. data/lib/rbbt/workflow/usage.rb +3 -0
  31. data/share/lib/R/util.R +31 -0
  32. data/share/rbbt_commands/app/start +5 -4
  33. data/share/rbbt_commands/study/task +222 -0
  34. data/share/rbbt_commands/tsv/attach +13 -0
  35. data/share/rbbt_commands/tsv/change_id +15 -0
  36. data/share/rbbt_commands/tsv/info +3 -1
  37. data/share/rbbt_commands/workflow/task +14 -15
  38. data/test/rbbt/test_entity.rb +221 -0
  39. data/test/rbbt/test_tsv.rb +2 -1
  40. data/test/rbbt/test_workflow.rb +0 -2
  41. data/test/rbbt/tsv/test_accessor.rb +2 -2
  42. data/test/rbbt/util/test_R.rb +9 -2
  43. data/test/rbbt/util/test_colorize.rb +12 -0
  44. data/test/rbbt/util/test_misc.rb +0 -5
  45. data/test/rbbt/util/test_open.rb +31 -0
  46. data/test/rbbt/workflow/test_step.rb +32 -0
  47. metadata +13 -2
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rbbt-util'
4
+ require 'rbbt/util/simpleopt'
5
+
6
+
7
+ file = ARGV.shift
8
+ identifiers = ARGV.shift
9
+ format = ARGV.shift
10
+
11
+
12
+ puts TSV.open(file).attach identifiers, :fields => [format]
13
+
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rbbt-util'
4
+ require 'rbbt/util/simpleopt'
5
+
6
+
7
+ file = ARGV.shift
8
+ identifiers = ARGV.shift
9
+ format = ARGV.shift
10
+
11
+
12
+ tsv = TSV.open(file).attach identifiers, :fields => [format]
13
+
14
+ puts tsv.reorder(format, tsv.fields - [format])
15
+
@@ -16,5 +16,7 @@ header.fields.each_with_index do |f,i|
16
16
  end
17
17
  puts "Rows: #{`wc -l #{ file }|cut -f 1 -d' '`}"
18
18
  puts "First line:"
19
- puts header.first_line
19
+ parts = []
20
+ header.first_line.split(header.sep).each_with_index{|p,i| parts << "(#{i}) #{p}"}
21
+ puts parts * "\t"
20
22
 
@@ -20,6 +20,8 @@ def usage(workflow = nil, task = nil)
20
20
  puts workflow.to_s
21
21
  puts "=" * workflow.to_s.length
22
22
  puts
23
+ puts workflow.workflow_description
24
+ puts
23
25
  workflow.doc(task)
24
26
  end
25
27
 
@@ -32,7 +34,7 @@ def SOPT_options(workflow, task)
32
34
  short = name.to_s.chars.first
33
35
  boolean = workflow.rec_input_types(task.name)[name].to_sym == :boolean
34
36
 
35
- sopt_options << "-#{short}--#{name}#{boolean ? '' : '*'}"
37
+ sopt_options << "-#{short}--#{name}*"
36
38
  end
37
39
 
38
40
  sopt_options * ":"
@@ -45,6 +47,8 @@ def fix_options(workflow, task, job_options)
45
47
 
46
48
  job_options.each do |name, value|
47
49
  value = case option_types[name].to_sym
50
+ when :boolean
51
+ %w(true TRUE T yes).include? value
48
52
  when :float
49
53
  value.to_f
50
54
  when :integer
@@ -53,7 +57,7 @@ def fix_options(workflow, task, job_options)
53
57
  case
54
58
  when value == '-'
55
59
  STDIN.read
56
- when (String === value and File.exists?(value))
60
+ when (String === value and File.exists?(value) and not File.directory?(value))
57
61
  Open.read(value)
58
62
  else
59
63
  value
@@ -78,18 +82,13 @@ def fix_options(workflow, task, job_options)
78
82
  end
79
83
  end
80
84
  when :tsv
81
- if TSV === value
85
+ case value
86
+ when TSV
82
87
  value
88
+ when '-'
89
+ TSV.open(STDIN)
83
90
  else
84
- begin
85
- if value == '-'
86
- TSV.open(STDIN).to_s :sort
87
- else
88
- TSV.new(value).to_s :sort
89
- end
90
- rescue
91
- value
92
- end
91
+ TSV.open(value)
93
92
  end
94
93
  else
95
94
  value
@@ -114,7 +113,6 @@ usage if workflow.nil?
114
113
 
115
114
  task = ARGV.shift
116
115
 
117
-
118
116
  # Set log, fork, clean, recursive_clean and help
119
117
  help = !!options.delete(:help)
120
118
  do_fork = !!options.delete(:fork)
@@ -136,7 +134,8 @@ if remote_workflows.include? workflow
136
134
  workflow = WorkflowRESTClient.new remote_workflows[workflow], workflow
137
135
  else
138
136
  Workflow.require_workflow workflow
139
- workflow = Workflow.workflows.last
137
+ workflow = Workflow.workflows.select{|mod| Misc.snake_case(mod.to_s) == Misc.snake_case(workflow)}.first
138
+ workflow = Workflow.workflows.last if workflow.nil?
140
139
  end
141
140
 
142
141
  # Set task
@@ -215,7 +214,7 @@ else
215
214
  end
216
215
 
217
216
  if Step === res
218
- puts Open.read(res.path)
217
+ puts Open.read(res.path) if File.exists? res.path
219
218
  else
220
219
  puts res
221
220
  end
@@ -0,0 +1,221 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+ require 'rbbt'
3
+ require 'rbbt/entity'
4
+ require 'rbbt/util/tmpfile'
5
+ require 'test/unit'
6
+
7
+ class TestA
8
+ attr_accessor :foo, :bar
9
+ def initialize(foo, bar)
10
+ @foo = foo
11
+ @bar = bar
12
+ end
13
+ end
14
+
15
+ module ReversableString
16
+ extend Entity
17
+
18
+ self.annotation :foo, :bar
19
+
20
+ property :reverse_text_ary => :array do
21
+ $count += 1
22
+ self.collect{|s| s.reverse}
23
+ end
24
+
25
+ property :reverse_text_single => :single do
26
+ $count += 1
27
+ self.reverse
28
+ end
29
+
30
+ property :reverse_text_ary_p => :array do
31
+ $count += 1
32
+ self.collect{|s| s.reverse}
33
+ end
34
+
35
+ property :reverse_text_single_p => :single do
36
+ $count += 1
37
+ self.reverse
38
+ end
39
+
40
+ property :reverse_text_ary_p_array => :array do
41
+ $count += 1
42
+ self.collect{|s| s.reverse}
43
+ end
44
+
45
+ property :random => :single do
46
+ rand
47
+ end
48
+
49
+ property :annotation_list => :single do
50
+ self.chars.to_a.collect{|c|
51
+ ReversableString.setup(c)
52
+ }
53
+ end
54
+
55
+ persist :reverse_text_ary_p
56
+ persist :reverse_text_ary_p
57
+ persist :reverse_text_ary_p
58
+ persist :reverse_text_single_p
59
+
60
+ persist :reverse_text_ary_p_array, :array, :dir => TmpFile.tmp_file
61
+
62
+ persist :annotation_list, :annotations, :dir => TmpFile.tmp_file
63
+ end
64
+
65
+ class TestEntity < Test::Unit::TestCase
66
+
67
+ def test_property_ary
68
+ a = ["String1", "String2"]
69
+ ReversableString.setup(a)
70
+
71
+ $count = 0
72
+
73
+ assert_equal "2gnirtS", a.reverse_text_ary.last
74
+ assert_equal 1, $count
75
+ a._ary_property_cache.clear
76
+ assert_equal "2gnirtS", a[1].reverse_text_ary
77
+ assert_equal 2, $count
78
+ a._ary_property_cache.clear
79
+
80
+ $count = 0
81
+ a.each do |string|
82
+ string.reverse_text_ary
83
+ assert_equal 1, $count
84
+ end
85
+ end
86
+
87
+ def test_property_single
88
+ a = ["String1", "String2"]
89
+ ReversableString.setup a
90
+
91
+ $count = 0
92
+
93
+ assert_equal "2gnirtS", a.reverse_text_single.last
94
+ assert_equal 2, $count
95
+ assert_equal "2gnirtS", a[1].reverse_text_single
96
+ assert_equal 3, $count
97
+ end
98
+
99
+ def test_property_ary_p
100
+ a = ["String1", "String2"]
101
+ ReversableString.setup a
102
+
103
+ $count = 0
104
+
105
+ assert_equal "2gnirtS", a.reverse_text_ary_p.last
106
+ assert_equal "2gnirtS", a[1].reverse_text_ary_p
107
+ assert_equal 1, $count
108
+ end
109
+
110
+ def test_property_single_p
111
+ a = ["String1", "String2"]
112
+ ReversableString.setup a
113
+
114
+ $count = 0
115
+
116
+ assert_equal "2gnirtS", a.reverse_text_single_p.last
117
+ assert_equal 2, $count
118
+ assert_equal "2gnirtS", a[1].reverse_text_single_p
119
+ assert_equal 2, $count
120
+ end
121
+
122
+ def test_property_ary_p_array
123
+ a = ["String1", "String2"]
124
+ ReversableString.setup a
125
+
126
+ $count = 0
127
+
128
+ assert_equal "2gnirtS", a.reverse_text_ary_p_array.last
129
+ assert_equal 1, $count
130
+ assert_equal "2gnirtS", a.reverse_text_ary_p_array.last
131
+ assert_equal 1, $count
132
+ end
133
+
134
+ def test_unpersist
135
+ a = ["String1", "String2"]
136
+ ReversableString.setup a
137
+
138
+ # Before persist
139
+ assert(! ReversableString.persisted?(:random))
140
+
141
+ r1 = a.random
142
+ r2 = a.random
143
+ assert_not_equal r1, r2
144
+
145
+ # After persist
146
+ ReversableString.persist :random
147
+ assert(ReversableString.persisted?(:random))
148
+
149
+ r1 = a.random
150
+ r2 = a.random
151
+ assert_equal r1, r2
152
+
153
+ # After unpersist
154
+ ReversableString.unpersist :random
155
+ assert(! ReversableString.persisted?(:random))
156
+
157
+ r1 = a.random
158
+ r2 = a.random
159
+ assert_not_equal r1, r2
160
+
161
+ end
162
+
163
+ def test_persist_annotations
164
+ string = 'aaabbbccc'
165
+ ReversableString.setup(string)
166
+ assert_equal string.length, string.annotation_list.length
167
+ assert_equal string.length, string.annotation_list.length
168
+ end
169
+
170
+ def __test_performance
171
+ require 'rbbt/entity/gene'
172
+ Misc.profile(:min_percent => 2) do
173
+ 1000.times do
174
+ TestA.new "foo", "bar"
175
+ end
176
+ end
177
+
178
+ Misc.profile(:min_percent => 2) do
179
+ 1000.times do
180
+ Gene.setup_positional("", "foo", "bar")
181
+ end
182
+ end
183
+
184
+ Misc.benchmark(100000) do
185
+ Gene.setup("", :foo => "foo", :bar => "bar")
186
+ end
187
+
188
+ Misc.benchmark(100000) do
189
+ TestA.new "foo", "bar"
190
+ end
191
+ end
192
+
193
+ def __test_clean_annotations
194
+ Workflow.require_workflow "StudyExplorer"
195
+
196
+ s = Study.setup("CLL")
197
+ mutations = s.cohort.metagenotype
198
+
199
+ mis = mutations.mutated_isoforms.compact.flatten
200
+
201
+ Misc.profile(:min_percent => 1) do
202
+ mis.each{|m| m}
203
+ end
204
+
205
+
206
+ Misc.benchmark(10) do
207
+ mis.each{|m| m}
208
+ end
209
+
210
+ Misc.benchmark(10) do
211
+ mis.clean_annotations.each{|m| m}
212
+ end
213
+
214
+ m = mutations.first
215
+
216
+ assert_equal m.split(":")[1], m.position.to_s
217
+ assert_raise NoMethodError do
218
+ m.clean_annotations.position
219
+ end
220
+ end
221
+ end
@@ -210,7 +210,7 @@ row2 4
210
210
  TmpFile.with_file(content) do |filename|
211
211
  tsv = TSV.open(filename, :sep => /\s+/, :cast => :to_i, :type => :single, :serializer => :integer)
212
212
  assert_equal 1, tsv["row1"]
213
- assert String === tsv.tsv_clean_get_brackets("row1")
213
+ assert String === tsv.send(:[], "row1", true)
214
214
  end
215
215
  end
216
216
 
@@ -233,6 +233,7 @@ row2 4
233
233
  #Id ValueA ValueB OtherID
234
234
  row1 a|aa|aaa b Id1|Id2
235
235
  row2 A B Id3
236
+ row3 AA B Id3
236
237
  EOF
237
238
 
238
239
  TmpFile.with_file(content.gsub(/ +/,"\t")) do |filename|
@@ -7,8 +7,6 @@ require 'test/unit'
7
7
  module TestWF
8
8
  extend Workflow
9
9
 
10
- ddd TestWF.methods.sort
11
-
12
10
  helper :user do
13
11
  "User"
14
12
  end
@@ -116,10 +116,10 @@ row2 A b C
116
116
 
117
117
  TmpFile.with_file(content) do |filename|
118
118
  tsv = TSV.open(File.open(filename), :double, :sep => /\s/)
119
- assert_equal %w(row2 row1), tsv.sort{|a,b|
119
+ assert_equal %w(row2 row1), tsv.tsv_sort{|a,b|
120
120
  a[1]["ValueA"] <=> b[1]["ValueA"]
121
121
  }.collect{|k,v| k}
122
- assert_equal %w(row1 row2), tsv.sort{|a,b|
122
+ assert_equal %w(row1 row2), tsv.tsv_sort{|a,b|
123
123
  a[1]["ValueB"] <=> b[1]["ValueB"]
124
124
  }.collect{|k,v| k}
125
125
  end
@@ -2,16 +2,23 @@ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helpe
2
2
  require 'rbbt/util/R'
3
3
 
4
4
  class TestR < Test::Unit::TestCase
5
- def test_sum
5
+ def _test_sum
6
6
  assert_equal "6", R.run('cat(3+3)').read.split(/\n/).last
7
7
  end
8
8
 
9
- def test_tsv_R
9
+ def _test_tsv_R
10
10
  tsv = TSV.setup({:a => 1, :b => 2})
11
11
  tsv2 = tsv.R <<-EOF
12
12
  data = data + 1
13
13
  EOF
14
14
  assert_equal "2", tsv2["a"].first
15
15
  end
16
+
17
+ def test_format_tsv
18
+ tsv = TSV.setup({"a" => [1], "b" => [2]}, :type => :list, :key_field => "Letter", :fields => ["Number"])
19
+ puts tsv.transpose "Field"
20
+ tsv.unnamed = true
21
+ puts R.ruby2R tsv
22
+ end
16
23
  end
17
24
 
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/colorize'
3
+ require 'test/unit'
4
+
5
+ class TestColorize < Test::Unit::TestCase
6
+ def test_color_array
7
+ a = [:red, :red, :blue, :blue, :yellow]
8
+ a = (0..16).to_a
9
+
10
+ ddd Colorize.distinct(a)
11
+ end
12
+ end
@@ -194,11 +194,6 @@ class TestMisc < Test::Unit::TestCase
194
194
  assert_equal 5, Misc.ordered_divide(%w(1 2 3 4 5 6 7 8 9),2).length
195
195
  end
196
196
 
197
- def test_setup
198
- require 'rbbt/entity/gene'
199
- g = Misc.prepare_entity("TP53", "Gene", :format => "Associated Gene Name", "organism" => "Hsa/jun2011")
200
- end
201
-
202
197
  def test_collapse_ranges
203
198
  ranges = [(0..100), (50..150), (51..61),(200..250), (300..324),(320..350)]
204
199
  assert_equal [(0..150),(200..250), (300..350)], Misc.collapse_ranges(ranges)
@@ -103,6 +103,37 @@ class TestOpen < Test::Unit::TestCase
103
103
  FileUtils.rm file + '.gz'
104
104
  end
105
105
  end
106
+
107
+ def test_repo_dir
108
+ file1 = "TEST"
109
+ file2 = "TEST" * 1000
110
+ TmpFile.with_file do |tmpdir|
111
+ tmpdir = "/home/mvazquezg/tmp/repo_dir"
112
+ normal = File.join(tmpdir, 'normal')
113
+ repo = File.join(tmpdir, 'repo')
114
+
115
+ Open.repository_dirs.push(repo)
116
+
117
+ Misc.benchmark(100) do
118
+ filename = "file" << (rand * 100).to_i.to_s
119
+ Open.write(File.join(normal, filename), file2)
120
+ 100.times do
121
+ Open.read(File.join(normal, filename))
122
+ end
123
+ end
124
+
125
+
126
+ Misc.benchmark(100) do
127
+ filename = "file" << (rand * 100).to_i.to_s
128
+ Open.write(File.join(repo, filename), file2)
129
+ 100.times do
130
+ Open.read(File.join(repo, filename))
131
+ end
132
+ end
133
+
134
+
135
+ end
136
+ end
106
137
 
107
138
  end
108
139