rbbt-util 3.2.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +65 -0
- data/bin/run_workflow.rb +142 -69
- data/lib/rbbt-util.rb +3 -3
- data/lib/rbbt.rb +12 -3
- data/lib/rbbt/annotations.rb +215 -0
- data/lib/rbbt/{util/fix_width_table.rb → fix_width_table.rb} +17 -13
- data/lib/rbbt/persist.rb +164 -0
- data/lib/rbbt/persist/tsv.rb +135 -0
- data/lib/rbbt/resource.rb +100 -0
- data/lib/rbbt/resource/path.rb +180 -0
- data/lib/rbbt/resource/rake.rb +48 -0
- data/lib/rbbt/resource/util.rb +111 -0
- data/lib/rbbt/resource/with_key.rb +28 -0
- data/lib/rbbt/tsv.rb +134 -0
- data/lib/rbbt/tsv/accessor.rb +345 -0
- data/lib/rbbt/tsv/attach.rb +183 -0
- data/lib/rbbt/tsv/attach/util.rb +277 -0
- data/lib/rbbt/{util/tsv/filters.rb → tsv/filter.rb} +76 -37
- data/lib/rbbt/tsv/index.rb +453 -0
- data/lib/rbbt/tsv/manipulate.rb +361 -0
- data/lib/rbbt/tsv/parser.rb +231 -0
- data/lib/rbbt/tsv/serializers.rb +79 -0
- data/lib/rbbt/tsv/util.rb +67 -0
- data/lib/rbbt/util/R.rb +3 -3
- data/lib/rbbt/util/chain_methods.rb +64 -0
- data/lib/rbbt/util/cmd.rb +17 -13
- data/lib/rbbt/util/excel2tsv.rb +4 -3
- data/lib/rbbt/util/log.rb +1 -0
- data/lib/rbbt/util/misc.rb +296 -285
- data/lib/rbbt/util/open.rb +9 -2
- data/lib/rbbt/util/persistence.rb +1 -1
- data/lib/rbbt/util/task/job.rb +3 -1
- data/lib/rbbt/workflow.rb +193 -0
- data/lib/rbbt/workflow/accessor.rb +249 -0
- data/lib/rbbt/workflow/annotate.rb +60 -0
- data/lib/rbbt/workflow/soap.rb +100 -0
- data/lib/rbbt/workflow/step.rb +102 -0
- data/lib/rbbt/workflow/task.rb +76 -0
- data/test/rbbt/resource/test_path.rb +12 -0
- data/test/rbbt/test_annotations.rb +106 -0
- data/test/rbbt/{util/test_fix_width_table.rb → test_fix_width_table.rb} +8 -9
- data/test/rbbt/test_resource.rb +66 -0
- data/test/rbbt/test_tsv.rb +332 -0
- data/test/rbbt/test_workflow.rb +102 -0
- data/test/rbbt/tsv/test_accessor.rb +163 -0
- data/test/rbbt/{util/tsv → tsv}/test_attach.rb +86 -43
- data/test/rbbt/{util/tsv/test_filters.rb → tsv/test_filter.rb} +31 -13
- data/test/rbbt/tsv/test_index.rb +284 -0
- data/test/rbbt/{util/tsv → tsv}/test_manipulate.rb +35 -105
- data/test/rbbt/util/test_R.rb +1 -1
- data/test/rbbt/util/test_chain_methods.rb +22 -0
- data/test/rbbt/util/test_filecache.rb +0 -1
- data/test/rbbt/util/test_misc.rb +97 -79
- data/test/rbbt/util/test_open.rb +1 -0
- data/test/rbbt/util/test_tmpfile.rb +1 -1
- data/test/rbbt/workflow/test_soap.rb +103 -0
- data/test/rbbt/workflow/test_step.rb +142 -0
- data/test/rbbt/workflow/test_task.rb +84 -0
- data/test/test_helper.rb +7 -7
- metadata +80 -54
- data/lib/rbbt/util/rake.rb +0 -176
- data/lib/rbbt/util/resource.rb +0 -355
- data/lib/rbbt/util/task.rb +0 -183
- data/lib/rbbt/util/tc_hash.rb +0 -324
- data/lib/rbbt/util/tsv.rb +0 -236
- data/lib/rbbt/util/tsv/accessor.rb +0 -312
- data/lib/rbbt/util/tsv/attach.rb +0 -416
- data/lib/rbbt/util/tsv/index.rb +0 -419
- data/lib/rbbt/util/tsv/manipulate.rb +0 -300
- data/lib/rbbt/util/tsv/misc.rb +0 -41
- data/lib/rbbt/util/tsv/parse.rb +0 -324
- data/lib/rbbt/util/tsv/resource.rb +0 -88
- data/lib/rbbt/util/workflow.rb +0 -135
- data/lib/rbbt/util/workflow/soap.rb +0 -116
- data/test/rbbt/util/test_persistence.rb +0 -201
- data/test/rbbt/util/test_rake.rb +0 -54
- data/test/rbbt/util/test_resource.rb +0 -77
- data/test/rbbt/util/test_task.rb +0 -133
- data/test/rbbt/util/test_tc_hash.rb +0 -144
- data/test/rbbt/util/test_tsv.rb +0 -221
- data/test/rbbt/util/test_workflow.rb +0 -135
- data/test/rbbt/util/tsv/test_accessor.rb +0 -150
- data/test/rbbt/util/tsv/test_index.rb +0 -241
- data/test/rbbt/util/tsv/test_parse.rb +0 -87
- data/test/rbbt/util/tsv/test_resource.rb +0 -9
@@ -1,5 +1,5 @@
|
|
1
|
-
require File.join(File.expand_path(File.dirname(__FILE__)), '
|
2
|
-
require 'rbbt/
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
|
2
|
+
require 'rbbt/tsv/filter'
|
3
3
|
|
4
4
|
class TestTSVFilters < Test::Unit::TestCase
|
5
5
|
def test_collect
|
@@ -11,7 +11,7 @@ row2 A B
|
|
11
11
|
EOF
|
12
12
|
|
13
13
|
TmpFile.with_file(content1) do |filename|
|
14
|
-
tsv = TSV.
|
14
|
+
tsv = TSV.open filename
|
15
15
|
tsv.filter
|
16
16
|
tsv.add_filter "field:ValueA", ["A"]
|
17
17
|
assert_equal 1, tsv.collect.size
|
@@ -27,7 +27,7 @@ row2 A B
|
|
27
27
|
EOF
|
28
28
|
|
29
29
|
TmpFile.with_file(content1) do |filename|
|
30
|
-
tsv = TSV.
|
30
|
+
tsv = TSV.open filename
|
31
31
|
tsv.filter
|
32
32
|
tsv.add_filter "field:ValueA", ["A"]
|
33
33
|
elem = []
|
@@ -46,7 +46,7 @@ row3 A C
|
|
46
46
|
EOF
|
47
47
|
|
48
48
|
TmpFile.with_file(content1) do |filename|
|
49
|
-
tsv = TSV.
|
49
|
+
tsv = TSV.open filename
|
50
50
|
tsv.filter
|
51
51
|
tsv.add_filter "field:ValueA", ["A"]
|
52
52
|
|
@@ -72,9 +72,9 @@ row3 A C
|
|
72
72
|
EOF
|
73
73
|
|
74
74
|
TmpFile.with_file(content1) do |filename|
|
75
|
-
tsv = TSV.
|
75
|
+
tsv = TSV.open filename
|
76
76
|
tsv.filter
|
77
|
-
tsv.add_filter "field:ValueA", ["A"], Rbbt.tmp.test.Filter.fieldValueA
|
77
|
+
tsv.add_filter "field:ValueA", ["A"], Rbbt.tmp.test.Filter.fieldValueA.find(:user)
|
78
78
|
|
79
79
|
elem = []
|
80
80
|
tsv.through do |k,v| elem << k end
|
@@ -86,9 +86,9 @@ row3 A C
|
|
86
86
|
tsv.through do |k,v| elem << k end
|
87
87
|
assert_equal 1, elem.size
|
88
88
|
|
89
|
-
tsv = TSV.
|
89
|
+
tsv = TSV.open filename
|
90
90
|
tsv.filter
|
91
|
-
tsv.add_filter "field:ValueA", ["A"], Rbbt.tmp.test.Filter.fieldValueA
|
91
|
+
tsv.add_filter "field:ValueA", ["A"], Rbbt.tmp.test.Filter.fieldValueA.find(:user)
|
92
92
|
|
93
93
|
elem = []
|
94
94
|
tsv.through do |k,v| elem << k end
|
@@ -105,7 +105,7 @@ row2 A B
|
|
105
105
|
EOF
|
106
106
|
|
107
107
|
TmpFile.with_file(content1) do |filename|
|
108
|
-
tsv = TSV.
|
108
|
+
tsv = TSV.open filename
|
109
109
|
tsv.filter
|
110
110
|
tsv.add_filter "field:ValueA", ["A"], Rbbt.tmp.test.Filter.fieldValueA.find(:user)
|
111
111
|
|
@@ -125,7 +125,7 @@ row2 A B
|
|
125
125
|
tsv.through do |k,v| elem << k end
|
126
126
|
assert_equal 1, elem.size
|
127
127
|
|
128
|
-
tsv = TSV.
|
128
|
+
tsv = TSV.open filename
|
129
129
|
tsv.filter
|
130
130
|
tsv.add_filter "field:ValueA", ["A"], Rbbt.tmp.test.Filter.fieldValueA.find(:user)
|
131
131
|
|
@@ -143,12 +143,12 @@ row2 A B C
|
|
143
143
|
EOF
|
144
144
|
|
145
145
|
TmpFile.with_file(content) do |filename|
|
146
|
-
tsv = TSV.
|
146
|
+
tsv = TSV.open(File.open(filename), :double, :sep => /\s/)
|
147
147
|
assert_equal 2, tsv.keys.length
|
148
148
|
tsv.delete "row2"
|
149
149
|
assert_equal 1, tsv.keys.length
|
150
150
|
|
151
|
-
tsv = TSV.
|
151
|
+
tsv = TSV.open(File.open(filename), :double, :sep => /\s/)
|
152
152
|
tsv.filter
|
153
153
|
tsv.add_filter "field:ValueA", ["A"]
|
154
154
|
|
@@ -163,5 +163,23 @@ row2 A B C
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
+
def test_filename
|
167
|
+
content1 =<<-EOF
|
168
|
+
#: :sep=/\\s+/#:case_insensitive=false
|
169
|
+
#Id ValueA ValueB
|
170
|
+
row1 a|aa|aaa b
|
171
|
+
row2 A B
|
172
|
+
EOF
|
173
|
+
|
174
|
+
TmpFile.with_file(content1) do |filename|
|
175
|
+
tsv = TSV.open filename
|
176
|
+
tsv.filter
|
177
|
+
tsv.add_filter "field:ValueA", ["A"]
|
178
|
+
assert tsv.filename =~ /ValueA/
|
179
|
+
tsv.pop_filter
|
180
|
+
assert tsv.filename !~ /ValueA/
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
166
184
|
end
|
167
185
|
|
@@ -0,0 +1,284 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
|
2
|
+
require 'rbbt/tsv'
|
3
|
+
require 'rbbt/tsv/index'
|
4
|
+
|
5
|
+
class TestTSVManipulate < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def load_segment_data(data)
|
8
|
+
tsv = TSV.open(data, :list, :sep=>":", :cast => proc{|e| e =~ /(\s*)(_*)/; ($1.length..($1.length + $2.length - 1))})
|
9
|
+
tsv.add_field "Start" do |key, values|
|
10
|
+
values["Range"].first
|
11
|
+
end
|
12
|
+
tsv.add_field "End" do |key, values|
|
13
|
+
values["Range"].last
|
14
|
+
end
|
15
|
+
|
16
|
+
tsv = tsv.slice ["Start", "End"]
|
17
|
+
|
18
|
+
tsv
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_index
|
22
|
+
content =<<-EOF
|
23
|
+
#Id ValueA ValueB OtherID
|
24
|
+
row1 a|aa|aaa b Id1|Id2
|
25
|
+
row2 A B Id3
|
26
|
+
EOF
|
27
|
+
|
28
|
+
TmpFile.with_file(content) do |filename|
|
29
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/, :key_field => "OtherID", :persistence => false)
|
30
|
+
index = tsv.index(:case_insensitive => true, :persistence => true)
|
31
|
+
assert index["row1"].include? "Id1"
|
32
|
+
assert_equal "OtherID", index.fields.first
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_best_index
|
38
|
+
content =<<-EOF
|
39
|
+
#Id ValueA ValueB OtherID
|
40
|
+
row1 a|aa|aaa b|A Id1
|
41
|
+
row2 A a|B Id3
|
42
|
+
row3 A a|B Id4
|
43
|
+
EOF
|
44
|
+
|
45
|
+
TmpFile.with_file(content) do |filename|
|
46
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/, :key_field => "OtherID")
|
47
|
+
index = tsv.index(:order => true)
|
48
|
+
assert_equal "Id1", index['a'].first
|
49
|
+
assert_equal "Id3", index['A'].first
|
50
|
+
assert_equal "OtherID", index.fields.first
|
51
|
+
|
52
|
+
index = tsv.index(:order => false)
|
53
|
+
assert_equal "Id1", index['a'].first
|
54
|
+
end
|
55
|
+
|
56
|
+
TmpFile.with_file(content) do |filename|
|
57
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/, :key_field => "OtherID")
|
58
|
+
index = tsv.index
|
59
|
+
assert index["row1"].include? "Id1"
|
60
|
+
assert_equal "OtherID", index.fields.first
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
def test_index_from_persit
|
66
|
+
content =<<-EOF
|
67
|
+
#Id ValueA ValueB OtherID
|
68
|
+
row1 a|aa|aaa b|A Id1
|
69
|
+
row2 A a|B Id3
|
70
|
+
row3 A a|B Id4
|
71
|
+
EOF
|
72
|
+
|
73
|
+
TmpFile.with_file(content) do |filename|
|
74
|
+
tsv = TSV.open(Open.open(filename), :sep => /\s+/, :key_field => "OtherID", :persist => true)
|
75
|
+
index = tsv.index(:order => true)
|
76
|
+
assert_equal "Id1", index['a'].first
|
77
|
+
assert_equal "Id3", index['A'].first
|
78
|
+
assert_equal "OtherID", index.fields.first
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_index_to_persist
|
83
|
+
content =<<-EOF
|
84
|
+
#Id ValueA ValueB OtherID
|
85
|
+
row1 a|aa|aaa b|A Id1
|
86
|
+
row2 A a|B Id3
|
87
|
+
row3 A a|B Id4
|
88
|
+
EOF
|
89
|
+
|
90
|
+
TmpFile.with_file(content) do |filename|
|
91
|
+
tsv = TSV.open(Open.open(filename), :sep => /\s+/, :key_field => "OtherID", :persist => true)
|
92
|
+
|
93
|
+
index = tsv.index(:order => true, :persist => true)
|
94
|
+
assert_equal "Id1", index['a'].first
|
95
|
+
assert_equal "Id3", index['A'].first
|
96
|
+
assert_equal "OtherID", index.fields.first
|
97
|
+
|
98
|
+
tsv.delete "Id1"
|
99
|
+
|
100
|
+
index = tsv.index(:order => true, :persist => true)
|
101
|
+
assert_equal "Id1", index['a'].first
|
102
|
+
assert_equal "Id3", index['A'].first
|
103
|
+
assert_equal "OtherID", index.fields.first
|
104
|
+
|
105
|
+
index = tsv.index(:order => true, :persist => false)
|
106
|
+
assert_equal "Id3", index['a'].first
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_index_static
|
111
|
+
content =<<-EOF
|
112
|
+
#Id ValueA ValueB OtherID
|
113
|
+
row1 a|aa|aaa b|A Id1
|
114
|
+
row2 A a|B Id3
|
115
|
+
row3 A a|B Id4
|
116
|
+
EOF
|
117
|
+
|
118
|
+
TmpFile.with_file(content) do |filename|
|
119
|
+
index = TSV.index(filename, :target => "OtherID", :data_sep => /\s+/, :order => true, :persist => false)
|
120
|
+
assert_equal "Id1", index['a'].first
|
121
|
+
assert_equal "Id3", index['A'].first
|
122
|
+
assert_equal "OtherID", index.fields.first
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_index_static_persist
|
127
|
+
content =<<-EOF
|
128
|
+
#Id ValueA ValueB OtherID
|
129
|
+
row1 a|aa|aaa b|A Id1
|
130
|
+
row2 A a|B Id3
|
131
|
+
row3 A a|B Id4
|
132
|
+
EOF
|
133
|
+
|
134
|
+
TmpFile.with_file(content) do |filename|
|
135
|
+
index = TSV.index(filename, :target => "OtherID", :data_sep => /\s+/, :order => true, :persist => false)
|
136
|
+
assert_equal "Id1", index['a'].first
|
137
|
+
assert_equal "Id3", index['A'].first
|
138
|
+
assert_equal "OtherID", index.fields.first
|
139
|
+
|
140
|
+
index = TSV.index(filename, :target => "OtherID", :data_sep => /\s+/, :order => true, :persist => true)
|
141
|
+
assert_equal "Id1", index['a'].first
|
142
|
+
assert_equal "Id3", index['A'].first
|
143
|
+
assert_equal "OtherID", index.fields.first
|
144
|
+
|
145
|
+
Open.write(filename, Open.read(filename).sub(/row1.*Id1\n/,''))
|
146
|
+
|
147
|
+
index = TSV.index(filename, :target => "OtherID", :data_sep => /\s+/, :order => true, :persist => true)
|
148
|
+
assert_equal "Id1", index['a'].first
|
149
|
+
assert_equal "Id3", index['A'].first
|
150
|
+
assert_equal "OtherID", index.fields.first
|
151
|
+
assert index.include?('aaa')
|
152
|
+
|
153
|
+
index = TSV.index(filename, :target => "OtherID", :data_sep => /\s+/, :order => true, :persist => false)
|
154
|
+
assert ! index.include?('aaa')
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_pos_index
|
159
|
+
content =<<-EOF
|
160
|
+
#Id ValueA ValueB Pos
|
161
|
+
row1 a|aa|aaa b 0|10
|
162
|
+
row2 A B 30
|
163
|
+
EOF
|
164
|
+
|
165
|
+
TmpFile.with_file(content) do |filename|
|
166
|
+
tsv = TSV.open(File.open(filename), :double, :sep => /\s+/)
|
167
|
+
index = tsv.pos_index("Pos")
|
168
|
+
assert_equal ["row1"], index[10]
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_range_index
|
173
|
+
content =<<-EOF
|
174
|
+
#Id ValueA ValueB Pos1 Pos2
|
175
|
+
row1 a|aa|aaa b 0|10 10|30
|
176
|
+
row2 A B 30 35
|
177
|
+
EOF
|
178
|
+
|
179
|
+
TmpFile.with_file(content) do |filename|
|
180
|
+
tsv = TSV.open(File.open(filename), :double, :sep => /\s+/)
|
181
|
+
index = tsv.pos_index("Pos1")
|
182
|
+
assert_equal ["row1"], index[10]
|
183
|
+
|
184
|
+
index = tsv.range_index("Pos1", "Pos2")
|
185
|
+
assert_equal ["row1"], index[20]
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_range_index2
|
190
|
+
data =<<-EOF
|
191
|
+
#ID:Range
|
192
|
+
#:012345678901234567890
|
193
|
+
a: ______
|
194
|
+
b: ______
|
195
|
+
c: _______
|
196
|
+
d: ____
|
197
|
+
e: ______
|
198
|
+
f: ___
|
199
|
+
g: ____
|
200
|
+
EOF
|
201
|
+
TmpFile.with_file(data) do |datafile|
|
202
|
+
tsv = load_segment_data(datafile)
|
203
|
+
f = tsv.range_index("Start", "End", :persist => true)
|
204
|
+
|
205
|
+
assert_equal %w(), f[0].sort
|
206
|
+
assert_equal %w(b), f[1].sort
|
207
|
+
assert_equal %w(), f[20].sort
|
208
|
+
assert_equal %w(), f[(20..100)].sort
|
209
|
+
assert_equal %w(a b d), f[3].sort
|
210
|
+
assert_equal %w(a b c d e), f[(3..4)].sort
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_range_index_persistent
|
215
|
+
data =<<-EOF
|
216
|
+
#ID:Range
|
217
|
+
#:012345678901234567890
|
218
|
+
a: ______
|
219
|
+
b: ______
|
220
|
+
c: _______
|
221
|
+
d: ____
|
222
|
+
e: ______
|
223
|
+
f: ___
|
224
|
+
g: ____
|
225
|
+
EOF
|
226
|
+
TmpFile.with_file(data) do |datafile|
|
227
|
+
TmpFile.with_file(load_segment_data(datafile)) do |tsvfile|
|
228
|
+
f = TSV.range_index(tsvfile, "Start", "End", :persistence => true)
|
229
|
+
|
230
|
+
assert_equal %w(), f[0].sort
|
231
|
+
assert_equal %w(b), f[1].sort
|
232
|
+
assert_equal %w(), f[20].sort
|
233
|
+
assert_equal %w(), f[(20..100)].sort
|
234
|
+
assert_equal %w(a b d), f[3].sort
|
235
|
+
assert_equal %w(a b c d e), f[(3..4)].sort
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
|
241
|
+
# #{{{ Test Attach
|
242
|
+
# def test_index_headerless
|
243
|
+
# content =<<-EOF
|
244
|
+
#row1 a|aa|aaa b Id1|Id2
|
245
|
+
#row2 A B Id3
|
246
|
+
# EOF
|
247
|
+
#
|
248
|
+
# TmpFile.with_file(content) do |filename|
|
249
|
+
# tsv = TSV.open(File.open(filename), :sep => /\s+/)
|
250
|
+
# index = tsv.index(:target => 2)
|
251
|
+
# assert index["row1"].include? "Id1"
|
252
|
+
# end
|
253
|
+
# end
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
def test_range_index_persistent_with_filter
|
258
|
+
data =<<-EOF
|
259
|
+
#ID:Range
|
260
|
+
#:012345678901234567890
|
261
|
+
a: ______
|
262
|
+
b: ______
|
263
|
+
c: _______
|
264
|
+
d: ____
|
265
|
+
e: ______
|
266
|
+
f: ___
|
267
|
+
g: ____
|
268
|
+
EOF
|
269
|
+
TmpFile.with_file(data) do |datafile|
|
270
|
+
TmpFile.with_file(load_segment_data(datafile)) do |tsvfile|
|
271
|
+
f = TSV.range_index(tsvfile, "Start", "End", :filters => [["field:Start", "3"]])
|
272
|
+
|
273
|
+
assert_equal %w(), f[0].sort
|
274
|
+
assert_equal %w(), f[1].sort
|
275
|
+
assert_equal %w(), f[20].sort
|
276
|
+
assert_equal %w(), f[(20..100)].sort
|
277
|
+
assert_equal %w(a), f[3].sort
|
278
|
+
assert_equal %w(a), f[(3..4)].sort
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
@@ -1,23 +1,28 @@
|
|
1
|
-
require File.join(File.expand_path(File.dirname(__FILE__)), '
|
2
|
-
require 'rbbt/
|
3
|
-
require 'rbbt/
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), '../../', 'test_helper.rb')
|
2
|
+
require 'rbbt/tsv'
|
3
|
+
require 'rbbt/tsv/manipulate'
|
4
4
|
|
5
5
|
class TestTSVManipulate < Test::Unit::TestCase
|
6
6
|
|
7
|
-
def
|
7
|
+
def test_through
|
8
8
|
content =<<-EOF
|
9
|
-
#
|
10
|
-
row1
|
11
|
-
row2
|
9
|
+
#Id ValueA ValueB OtherID
|
10
|
+
row1 a|aa|aaa b Id1|Id2
|
11
|
+
row2 A B Id3
|
12
|
+
row3 a C Id4
|
12
13
|
EOF
|
13
14
|
|
14
15
|
TmpFile.with_file(content) do |filename|
|
15
|
-
tsv = TSV.
|
16
|
-
|
16
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/)
|
17
|
+
|
18
|
+
new_key, new_fields = tsv.through "ValueA" do |key, values|
|
19
|
+
assert(tsv.keys.include? values["Id"].first)
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_equal "ValueA", new_key
|
17
23
|
end
|
18
24
|
end
|
19
25
|
|
20
|
-
|
21
26
|
def test_reorder_simple
|
22
27
|
content =<<-EOF
|
23
28
|
#Id ValueA ValueB OtherID
|
@@ -27,41 +32,19 @@ row3 a C Id4
|
|
27
32
|
EOF
|
28
33
|
|
29
34
|
TmpFile.with_file(content) do |filename|
|
30
|
-
tsv = TSV.
|
35
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/)
|
31
36
|
|
32
37
|
tsv1 = tsv.reorder("ValueA")
|
33
38
|
|
34
39
|
assert_equal "ValueA", tsv1.key_field
|
35
|
-
assert_equal %w(Id ValueB OtherID), tsv1.fields
|
36
40
|
assert_equal ["B"], tsv1["A"]["ValueB"]
|
37
41
|
assert_equal ["b","C"], tsv1["a"]["ValueB"]
|
38
42
|
assert_equal ["b"], tsv1["aa"]["ValueB"]
|
43
|
+
assert_equal %w(Id ValueB OtherID), tsv1.fields
|
39
44
|
|
40
45
|
end
|
41
46
|
end
|
42
47
|
|
43
|
-
def test_reorder_simple_headerless
|
44
|
-
content =<<-EOF
|
45
|
-
row1 a|aa|aaa b Id1|Id2
|
46
|
-
row2 A B Id3
|
47
|
-
row3 a C Id4
|
48
|
-
EOF
|
49
|
-
|
50
|
-
TmpFile.with_file(content) do |filename|
|
51
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
52
|
-
|
53
|
-
tsv1 = tsv.reorder 0
|
54
|
-
|
55
|
-
assert_nil tsv1.key_field
|
56
|
-
assert_equal ["B"], tsv1["A"][1]
|
57
|
-
assert_equal ["b","C"], tsv1["a"][1]
|
58
|
-
assert_equal ["b"], tsv1["aa"][1]
|
59
|
-
assert_equal ["row1"], tsv1["aa"][0]
|
60
|
-
assert_equal ["row1","row3"], tsv1["a"][0]
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
48
|
def test_reorder_remove_field
|
66
49
|
content =<<-EOF
|
67
50
|
#Id ValueA ValueB OtherID
|
@@ -71,7 +54,7 @@ row3 a C Id4
|
|
71
54
|
EOF
|
72
55
|
|
73
56
|
TmpFile.with_file(content) do |filename|
|
74
|
-
tsv = TSV.
|
57
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/)
|
75
58
|
|
76
59
|
tsv1 = tsv.reorder("ValueA", ["ValueB", "Id"])
|
77
60
|
|
@@ -84,24 +67,6 @@ row3 a C Id4
|
|
84
67
|
end
|
85
68
|
end
|
86
69
|
|
87
|
-
def test_through
|
88
|
-
content =<<-EOF
|
89
|
-
#Id ValueA ValueB OtherID
|
90
|
-
row1 a|aa|aaa b Id1|Id2
|
91
|
-
row2 A B Id3
|
92
|
-
row3 a C Id4
|
93
|
-
EOF
|
94
|
-
|
95
|
-
TmpFile.with_file(content) do |filename|
|
96
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
97
|
-
|
98
|
-
tsv.through "ValueA" do |key, values|
|
99
|
-
assert(tsv.keys.include? values["Id"].first)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
70
|
def test_slice
|
106
71
|
content =<<-EOF
|
107
72
|
#ID ValueA ValueB Comment
|
@@ -110,28 +75,11 @@ row2 A B C
|
|
110
75
|
EOF
|
111
76
|
|
112
77
|
TmpFile.with_file(content) do |filename|
|
113
|
-
tsv = TSV.
|
78
|
+
tsv = TSV.open(File.open(filename), :type => :double, :sep => /\s/)
|
114
79
|
assert_equal [["a"],["c"]], tsv.reorder(:key, ["ValueA", "Comment"])["row1"]
|
115
80
|
end
|
116
81
|
end
|
117
82
|
|
118
|
-
def test_sort
|
119
|
-
content =<<-EOF
|
120
|
-
#Id ValueA ValueB OtherID Pos
|
121
|
-
row1 a|aa|aaa b Id1|Id2 2
|
122
|
-
row2 A B Id3 1
|
123
|
-
row3 A|AA|AAA|AAA B Id3 3
|
124
|
-
EOF
|
125
|
-
|
126
|
-
TmpFile.with_file(content) do |filename|
|
127
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
128
|
-
assert_equal %w(row1 row2 row3), tsv.sort
|
129
|
-
assert_equal %w(row1 row2 row3), tsv.sort(:key)
|
130
|
-
assert_equal %w(row2 row1 row3), tsv.sort("Pos")
|
131
|
-
assert_equal %w(row3 row2 row1), tsv.sort("ValueA") do |values| values["ValueA"].length end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
83
|
def test_select
|
136
84
|
content =<<-EOF
|
137
85
|
#Id ValueA ValueB OtherID
|
@@ -141,7 +89,7 @@ row3 a C Id4
|
|
141
89
|
EOF
|
142
90
|
|
143
91
|
TmpFile.with_file(content) do |filename|
|
144
|
-
tsv = TSV.
|
92
|
+
tsv = TSV.open(filename, :sep => /\s+/)
|
145
93
|
assert tsv.type == :double
|
146
94
|
|
147
95
|
new = tsv.select %w(b Id4)
|
@@ -156,13 +104,12 @@ row3 a C Id4
|
|
156
104
|
new = tsv.select "ValueB" => /b|Id4/
|
157
105
|
assert_equal %w(row1).sort, new.keys
|
158
106
|
|
159
|
-
tsv = TSV.
|
107
|
+
tsv = TSV.open(filename, :sep => /\s+/, :type => :flat)
|
160
108
|
assert tsv.type != :double
|
161
109
|
|
162
110
|
new = tsv.select %w(b Id4)
|
163
111
|
assert_equal %w(row1 row3).sort, new.keys.sort
|
164
112
|
|
165
|
-
|
166
113
|
new = tsv.select do |k,v| v["ValueA"].include? "A" end
|
167
114
|
assert_equal %w(row2).sort, new.keys.sort
|
168
115
|
end
|
@@ -177,7 +124,7 @@ row3 a C Id4
|
|
177
124
|
EOF
|
178
125
|
|
179
126
|
TmpFile.with_file(content) do |filename|
|
180
|
-
tsv = TSV.
|
127
|
+
tsv = TSV.open(File.open(filename), :sep => /\s+/)
|
181
128
|
|
182
129
|
tsv.process "ValueA" do |field_values,key,values|
|
183
130
|
field_values.collect{|v| "Pref:#{v}"}
|
@@ -196,7 +143,7 @@ row3 a C Id4
|
|
196
143
|
EOF
|
197
144
|
|
198
145
|
TmpFile.with_file(content) do |filename|
|
199
|
-
tsv = TSV.
|
146
|
+
tsv = TSV.open(filename, :sep => /\s+/)
|
200
147
|
tsv.add_field "Str length" do |k,v|
|
201
148
|
(v.flatten * " ").length
|
202
149
|
end
|
@@ -205,23 +152,6 @@ row3 a C Id4
|
|
205
152
|
end
|
206
153
|
end
|
207
154
|
|
208
|
-
def test_slice_namespace
|
209
|
-
content =<<-EOF
|
210
|
-
#Id LetterValue:ValueA LetterValue:ValueB OtherID
|
211
|
-
row1 a|aa|aaa b Id1|Id2
|
212
|
-
row2 A B Id3
|
213
|
-
row3 a C Id4
|
214
|
-
EOF
|
215
|
-
|
216
|
-
TmpFile.with_file(content) do |filename|
|
217
|
-
tsv = TSV.new(filename + '#:sep=/\s+/')
|
218
|
-
|
219
|
-
tsv_ns = tsv.slice_namespace "LetterValue"
|
220
|
-
assert_equal %w(LetterValue:ValueA LetterValue:ValueB), tsv_ns.fields
|
221
|
-
end
|
222
|
-
|
223
|
-
end
|
224
|
-
|
225
155
|
def test_add_field_double_with_list_result
|
226
156
|
content =<<-EOF
|
227
157
|
#Id LetterValue:ValueA LetterValue:ValueB OtherID
|
@@ -231,7 +161,7 @@ row3 a C Id4
|
|
231
161
|
EOF
|
232
162
|
|
233
163
|
TmpFile.with_file(content) do |filename|
|
234
|
-
tsv = TSV.
|
164
|
+
tsv = TSV.open(filename, :sep => /\s+/)
|
235
165
|
|
236
166
|
tsv.add_field "Test" do
|
237
167
|
"test"
|
@@ -239,26 +169,26 @@ row3 a C Id4
|
|
239
169
|
|
240
170
|
assert Array === tsv["row1"]["Test"]
|
241
171
|
end
|
242
|
-
|
243
172
|
end
|
244
173
|
|
245
|
-
def
|
174
|
+
def test_through_headless
|
246
175
|
content =<<-EOF
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
row3 a2
|
176
|
+
row1 a|aa|aaa b Id1|Id2
|
177
|
+
row2 A B Id3
|
178
|
+
row3 a C Id4
|
251
179
|
EOF
|
252
180
|
|
253
181
|
TmpFile.with_file(content) do |filename|
|
254
|
-
tsv = TSV.
|
182
|
+
tsv = TSV.open(filename, :sep => /\s+/)
|
183
|
+
|
184
|
+
test = false
|
185
|
+
tsv.through do
|
186
|
+
test = true
|
187
|
+
end
|
188
|
+
assert test
|
255
189
|
|
256
|
-
#TODO: WTF!!!
|
257
|
-
assert_equal "row3", tsv.reorder("LetterValue", :key)[97]
|
258
190
|
end
|
259
191
|
|
260
192
|
end
|
261
193
|
|
262
|
-
|
263
194
|
end
|
264
|
-
|