rbbt-util 1.2.1 → 2.0.1
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/lib/rbbt-util.rb +2 -1
- data/lib/rbbt/util/R.rb +18 -1
- data/lib/rbbt/util/cmd.rb +7 -6
- data/lib/rbbt/util/data_module.rb +31 -11
- data/lib/rbbt/util/fix_width_table.rb +209 -0
- data/lib/rbbt/util/log.rb +12 -2
- data/lib/rbbt/util/misc.rb +91 -12
- data/lib/rbbt/util/open.rb +18 -9
- data/lib/rbbt/util/path.rb +152 -0
- data/lib/rbbt/util/persistence.rb +282 -75
- data/lib/rbbt/util/pkg_data.rb +16 -59
- data/lib/rbbt/util/pkg_software.rb +15 -1
- data/lib/rbbt/util/rake.rb +5 -1
- data/lib/rbbt/util/tc_hash.rb +129 -59
- data/lib/rbbt/util/tsv.rb +109 -1284
- data/lib/rbbt/util/tsv/accessor.rb +273 -0
- data/lib/rbbt/util/tsv/attach.rb +228 -0
- data/lib/rbbt/util/tsv/index.rb +303 -0
- data/lib/rbbt/util/tsv/manipulate.rb +271 -0
- data/lib/rbbt/util/tsv/parse.rb +258 -0
- data/share/lib/R/util.R +5 -3
- data/test/rbbt/util/test_R.rb +9 -1
- data/test/rbbt/util/test_data_module.rb +5 -0
- data/test/rbbt/util/test_fix_width_table.rb +107 -0
- data/test/rbbt/util/test_misc.rb +43 -0
- data/test/rbbt/util/test_open.rb +0 -1
- data/test/rbbt/util/test_path.rb +10 -0
- data/test/rbbt/util/test_persistence.rb +63 -2
- data/test/rbbt/util/test_pkg_data.rb +29 -8
- data/test/rbbt/util/test_tc_hash.rb +52 -0
- data/test/rbbt/util/test_tsv.rb +55 -678
- data/test/rbbt/util/tsv/test_accessor.rb +109 -0
- data/test/rbbt/util/tsv/test_attach.rb +271 -0
- data/test/rbbt/util/tsv/test_index.rb +158 -0
- data/test/rbbt/util/tsv/test_manipulate.rb +226 -0
- data/test/rbbt/util/tsv/test_parse.rb +72 -0
- data/test/test_helper.rb +1 -0
- metadata +25 -4
data/test/rbbt/util/test_open.rb
CHANGED
@@ -41,12 +41,12 @@ class TestPersistence < Test::Unit::TestCase
|
|
41
41
|
def test_tsv
|
42
42
|
object = {:a => 1, :b => 2}
|
43
43
|
TmpFile.with_file do |f|
|
44
|
-
Persistence.persist("token_file", :Test, :
|
44
|
+
Persistence.persist("token_file", :Test, :tsv_extra, :persistence_file => f) do
|
45
45
|
[object, {:fields => ["Number"], :key_field => "Letter", :type => :list, :filename => "foo"}]
|
46
46
|
end
|
47
47
|
|
48
48
|
assert File.exists? f
|
49
|
-
new, extra = Persistence.persist("token_file", :Test, :
|
49
|
+
new, extra = Persistence.persist("token_file", :Test, :tsv_extra, :persistence_file => f)
|
50
50
|
|
51
51
|
assert_equal 1, new["a"]
|
52
52
|
assert_equal "Letter", new.key_field
|
@@ -54,7 +54,68 @@ class TestPersistence < Test::Unit::TestCase
|
|
54
54
|
rm f
|
55
55
|
end
|
56
56
|
end
|
57
|
+
|
58
|
+
def test_tsv2
|
59
|
+
content =<<-EOF
|
60
|
+
#Id ValueA ValueB OtherID
|
61
|
+
row1 a|aa|aaa b Id1|Id2
|
62
|
+
row2 A B Id3
|
63
|
+
EOF
|
57
64
|
|
65
|
+
TmpFile.with_file(content) do |filename|
|
66
|
+
tsv = TSV.new(filename, :sep => /\s+/, :key => "OtherID")
|
67
|
+
tsv2 = Persistence.persist_tsv_string(tsv, 'Test', {}) do tsv end
|
68
|
+
tsv2 = Persistence.persist_tsv_string(tsv, 'Test', {}) do tsv end
|
69
|
+
|
70
|
+
(Object::TSV::EXTRA_ACCESSORS + [:fields, :key_field]).each do |key|
|
71
|
+
assert_equal tsv.send(key), tsv2.send(key)
|
72
|
+
end
|
73
|
+
tsv.each do |key,values|
|
74
|
+
assert_equal values, tsv2[key]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_tsv3
|
80
|
+
content =<<-EOF
|
81
|
+
#Id ValueA ValueB OtherID
|
82
|
+
row1 a|aa|aaa b Id1|Id2
|
83
|
+
row2 A B Id3
|
84
|
+
EOF
|
85
|
+
|
86
|
+
TmpFile.with_file(content) do |filename|
|
87
|
+
tsv = TSV.new(filename, :sep => /\s+/, :key => "OtherID")
|
88
|
+
tsv2 = Persistence.persist_tsv(tsv, 'Test', {}) do tsv end
|
89
|
+
tsv2 = Persistence.persist_tsv(tsv, 'Test', {}) do tsv end
|
90
|
+
|
91
|
+
(Object::TSV::EXTRA_ACCESSORS + [:fields, :key_field]).each do |key|
|
92
|
+
assert_equal tsv.send(key), tsv2.send(key)
|
93
|
+
end
|
94
|
+
tsv.each do |key,values|
|
95
|
+
assert_equal values, tsv2[key]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_tsv4
|
101
|
+
content =<<-EOF
|
102
|
+
#Id ValueA ValueB OtherID
|
103
|
+
row1 a|aa|aaa b Id1|Id2
|
104
|
+
row2 A B Id3
|
105
|
+
EOF
|
106
|
+
|
107
|
+
TmpFile.with_file(content) do |filename|
|
108
|
+
tsv = Persistence.persist_tsv(filename, 'Test', {}) do TSV.new(filename, :sep => /\s+/, :key => "OtherID") end
|
109
|
+
tsv2 = Persistence.persist_tsv(filename, 'Test', {}) do tsv end
|
110
|
+
|
111
|
+
(Object::TSV::EXTRA_ACCESSORS + [:fields, :key_field]).each do |key|
|
112
|
+
assert_equal tsv.send(key), tsv2.send(key)
|
113
|
+
end
|
114
|
+
tsv.each do |key,values|
|
115
|
+
assert_equal values, tsv2[key]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
58
119
|
|
59
120
|
end
|
60
121
|
|
@@ -3,7 +3,7 @@ require 'rbbt'
|
|
3
3
|
require 'rbbt/util/pkg_data'
|
4
4
|
|
5
5
|
class TestPKGData < Test::Unit::TestCase
|
6
|
-
def
|
6
|
+
def test_claims
|
7
7
|
begin
|
8
8
|
assert Rbbt.claims.empty?
|
9
9
|
Rbbt.claim :foo, "bar"
|
@@ -13,12 +13,12 @@ class TestPKGData < Test::Unit::TestCase
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def test_path
|
17
17
|
assert_equal File.join(Rbbt.datadir, 'Organism/Hsa'), Rbbt.files.Organism.Hsa
|
18
18
|
Rbbt.files.Organism.Hsa.identifiers.produce
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
21
|
+
def test_claim_proc
|
22
22
|
begin
|
23
23
|
assert_nil Rbbt.reclaim(Rbbt.files.foo)
|
24
24
|
|
@@ -33,7 +33,7 @@ class TestPKGData < Test::Unit::TestCase
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
36
|
+
def test_claim_cp
|
37
37
|
begin
|
38
38
|
Open.write File.join(Rbbt.rootdir, 'share', 'foo'), "bar"
|
39
39
|
Rbbt.claim :foo
|
@@ -45,17 +45,17 @@ class TestPKGData < Test::Unit::TestCase
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
def
|
48
|
+
def test_claim_tsv
|
49
49
|
begin
|
50
50
|
Rbbt.claim :foo, TSV.new({:a => 1, :b => 2})
|
51
51
|
assert File.exists? Rbbt.files.foo
|
52
|
-
assert_equal "1", Rbbt.files.foo.tsv(:
|
52
|
+
assert_equal "1", Rbbt.files.foo.tsv(:type => :single)["a"]
|
53
53
|
ensure
|
54
54
|
FileUtils.rm Rbbt.files.foo if File.exists? Rbbt.files.foo
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
58
|
+
def test_claim_rakefile
|
59
59
|
begin
|
60
60
|
FileUtils.mkdir_p File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/')
|
61
61
|
Open.write(File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile'), "file :foo do |t| Open.write(t.name, 'bar') end")
|
@@ -92,7 +92,7 @@ class TestPKGData < Test::Unit::TestCase
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
-
def
|
95
|
+
def test_claim_rakefile3
|
96
96
|
begin
|
97
97
|
FileUtils.mkdir_p File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/')
|
98
98
|
Open.write(File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile'), "file :foo do |t| Open.write(t.name, 'bar') end")
|
@@ -109,5 +109,26 @@ class TestPKGData < Test::Unit::TestCase
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
end
|
112
|
+
|
113
|
+
def test_claim_namespace_identifiers
|
114
|
+
begin
|
115
|
+
FileUtils.mkdir_p File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/')
|
116
|
+
Open.write(File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile'), "
|
117
|
+
file :foo do |t| Open.write(t.name, 'bar') end
|
118
|
+
file :identifiers do |t| Open.write(t.name, 'bar') end
|
119
|
+
")
|
120
|
+
Rbbt.claim :all, "test/Rake/Rakefile", 'test'
|
121
|
+
assert_equal 1, Rbbt.files.test.foo.identifier_files.length
|
122
|
+
ensure
|
123
|
+
begin
|
124
|
+
FileUtils.rm File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile')
|
125
|
+
FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake')
|
126
|
+
FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test')
|
127
|
+
FileUtils.rm Rbbt.files.test.foo
|
128
|
+
FileUtils.rm_r Rbbt.files.test
|
129
|
+
rescue
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
112
133
|
end
|
113
134
|
|
@@ -14,5 +14,57 @@ class TestTCHash < Test::Unit::TestCase
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
|
+
|
18
|
+
def test_serializer
|
19
|
+
TmpFile.with_file do |f|
|
20
|
+
t = TCHash.new f, TCHash::StringSerializer
|
21
|
+
t["1"] = 2
|
22
|
+
t["2"] = 3
|
23
|
+
t.read
|
24
|
+
|
25
|
+
t.collect do |k,v|
|
26
|
+
["1", "2"].include? k
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_stringArraySerializer
|
32
|
+
TmpFile.with_file do |f|
|
33
|
+
t = TCHash.get f, true, TCHash::StringArraySerializer
|
34
|
+
t["1"] = [1,2]
|
35
|
+
t["2"] = [3,4]
|
36
|
+
|
37
|
+
t = TCHash.get f
|
38
|
+
t.collect do |k,v|
|
39
|
+
assert_equal ["1", "2"], t["1"]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_stringDoubleArraySerializer
|
45
|
+
TmpFile.with_file do |f|
|
46
|
+
t = TCHash.get f, true, TCHash::StringDoubleArraySerializer
|
47
|
+
t["1"] = [[1],[2]]
|
48
|
+
t["2"] = [[3],[4,5]]
|
49
|
+
|
50
|
+
t = TCHash.get f
|
51
|
+
t.collect do |k,v|
|
52
|
+
assert_equal [["3"],["4","5"]], t["2"]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_serializer_alias
|
58
|
+
TmpFile.with_file do |f|
|
59
|
+
t = TCHash.get f, true, :double
|
60
|
+
t["1"] = [[1],[2]]
|
61
|
+
t["2"] = [[3],[4,5]]
|
62
|
+
|
63
|
+
t = TCHash.get f
|
64
|
+
t.collect do |k,v|
|
65
|
+
assert_equal [["3"],["4","5"]], t["2"]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
17
69
|
end
|
18
70
|
|
data/test/rbbt/util/test_tsv.rb
CHANGED
@@ -3,124 +3,76 @@ require 'rbbt/util/tsv'
|
|
3
3
|
require 'rbbt/util/tmpfile'
|
4
4
|
|
5
5
|
class TestTSV < Test::Unit::TestCase
|
6
|
-
def test_keep_empty
|
7
|
-
content =<<-EOF
|
8
|
-
#Id ValueA ValueB Comment
|
9
|
-
row1 a|aa|aaa b c
|
10
|
-
row2 A B
|
11
|
-
EOF
|
12
|
-
|
13
|
-
TmpFile.with_file(content) do |filename|
|
14
|
-
data = {}
|
15
|
-
data, extra = TSV.parse(File.open(filename), :sep => /\s+/, :keep_empty => true)
|
16
|
-
assert_equal ["ValueA", "ValueB", "Comment"], extra[:fields]
|
17
|
-
assert_equal ["c"], data["row1"][2]
|
18
|
-
assert_equal [""], data["row2"][2]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_slice
|
23
|
-
content =<<-EOF
|
24
|
-
#ID ValueA ValueB Comment
|
25
|
-
row1 a b c
|
26
|
-
row2 A B C
|
27
|
-
EOF
|
28
6
|
|
29
|
-
|
30
|
-
tsv = TSV.new(File.open(filename), :double, :sep => /\s/)
|
31
|
-
assert_equal [["a"],["c"]], tsv.reorder(:main, ["ValueA", "Comment"])["row1"]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_headers
|
36
|
-
content =<<-EOF
|
37
|
-
#ID ValueA ValueB Comment
|
38
|
-
row1 a b c
|
39
|
-
row2 A B C
|
40
|
-
EOF
|
41
|
-
|
42
|
-
TmpFile.with_file(content) do |filename|
|
43
|
-
assert_equal ['ID', 'ValueA', 'ValueB', 'Comment'], TSV.headers(filename, :sep => ' ')
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_headerless
|
7
|
+
def test_tsv
|
48
8
|
content =<<-EOF
|
49
|
-
|
50
|
-
|
9
|
+
#Id ValueA ValueB OtherID
|
10
|
+
row1 a|aa|aaa b Id1|Id2
|
11
|
+
row2 A B Id3
|
51
12
|
EOF
|
52
13
|
|
53
14
|
TmpFile.with_file(content) do |filename|
|
54
|
-
|
15
|
+
tsv = TSV.new(File.open(filename), :double, :sep => /\s+/, :key => "OtherID")
|
16
|
+
assert_equal :double, tsv.type
|
17
|
+
assert_equal "OtherID", tsv.key_field
|
18
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
19
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id1"][1]
|
20
|
+
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
55
21
|
end
|
56
22
|
end
|
57
23
|
|
58
|
-
def
|
24
|
+
def test_grep
|
59
25
|
content =<<-EOF
|
60
|
-
#Id ValueA ValueB
|
61
|
-
row1 a|aa|aaa b
|
62
|
-
row2 A B
|
26
|
+
#Id ValueA ValueB OtherID
|
27
|
+
row1 a|aa|aaa b Id1|Id2
|
28
|
+
row2 A B Id3
|
63
29
|
EOF
|
64
30
|
|
65
31
|
TmpFile.with_file(content) do |filename|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
assert_equal ["ValueA", "ValueB"], extra[:fields]
|
70
|
-
assert_equal ["a", "aa", "aaa"], data["row1"][0]
|
32
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :grep => %w(row1))
|
33
|
+
assert tsv.keys.include? "row1"
|
34
|
+
assert( ! tsv.keys.include?("row2"))
|
71
35
|
end
|
72
36
|
end
|
73
37
|
|
74
|
-
def
|
38
|
+
def test_open_stringoptions
|
75
39
|
content =<<-EOF
|
76
40
|
#Id ValueA ValueB OtherID
|
77
41
|
row1 a|aa|aaa b Id1|Id2
|
78
42
|
row2 A B Id3
|
43
|
+
row3 a C Id4
|
79
44
|
EOF
|
80
45
|
|
81
46
|
TmpFile.with_file(content) do |filename|
|
82
|
-
tsv = TSV.new(
|
83
|
-
assert_equal "
|
84
|
-
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
85
|
-
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
47
|
+
tsv = TSV.new(filename + '#:sep=/\s+/')
|
48
|
+
assert_equal ["A"], tsv["row2"]["ValueA"]
|
86
49
|
end
|
87
50
|
end
|
88
51
|
|
89
|
-
def
|
52
|
+
def test_headers
|
90
53
|
content =<<-EOF
|
91
|
-
#
|
92
|
-
row1
|
93
|
-
row2
|
54
|
+
#ID ValueA ValueB Comment
|
55
|
+
row1 a b c
|
56
|
+
row2 A B C
|
94
57
|
EOF
|
95
58
|
|
96
59
|
TmpFile.with_file(content) do |filename|
|
97
|
-
|
98
|
-
assert_equal
|
99
|
-
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
100
|
-
assert_equal ["a", "aa", "aaa"], tsv["Id1"][1]
|
101
|
-
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
60
|
+
assert_equal ['ID', 'ValueA', 'ValueB', 'Comment'], TSV.headers(filename, :sep => ' ')
|
61
|
+
assert_equal nil, TSV.headers(filename, :sep => ' ', :header_hash => "##")
|
102
62
|
end
|
103
63
|
end
|
104
64
|
|
105
|
-
def
|
65
|
+
def test_headerless
|
106
66
|
content =<<-EOF
|
107
|
-
|
108
|
-
|
109
|
-
row2 A B Id3
|
110
|
-
row3 a C Id4
|
67
|
+
row1 a b c
|
68
|
+
row2 A B C
|
111
69
|
EOF
|
112
70
|
|
113
71
|
TmpFile.with_file(content) do |filename|
|
114
|
-
|
115
|
-
assert_equal "OtherID", tsv.key_field
|
116
|
-
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
117
|
-
assert_equal ["a", "aa", "aaa"], tsv["Id1"][1]
|
118
|
-
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
72
|
+
assert_equal 3, TSV.new(filename, :sep => ' ')['row1'].length
|
119
73
|
end
|
120
74
|
end
|
121
75
|
|
122
|
-
|
123
|
-
|
124
76
|
def test_extra
|
125
77
|
content =<<-EOF
|
126
78
|
#Id ValueA ValueB OtherID
|
@@ -129,9 +81,9 @@ row2 A B Id3
|
|
129
81
|
EOF
|
130
82
|
|
131
83
|
TmpFile.with_file(content) do |filename|
|
132
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :
|
84
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :fields => 2)
|
133
85
|
assert_equal ["b"], tsv["Id2"][0]
|
134
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :
|
86
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :fields => 'ValueB')
|
135
87
|
assert_equal ["b"], tsv["Id2"][0]
|
136
88
|
end
|
137
89
|
end
|
@@ -149,6 +101,11 @@ row2 A B Id3
|
|
149
101
|
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
150
102
|
assert_equal ["a", "aa", "aaa"], tsv["id1"][1]
|
151
103
|
assert_equal ["a", "aa", "aaa"], tsv["Id2"][1]
|
104
|
+
|
105
|
+
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :case_insensitive => false)
|
106
|
+
assert_equal "OtherID", tsv.key_field
|
107
|
+
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
108
|
+
assert_nil tsv["id1"]
|
152
109
|
end
|
153
110
|
end
|
154
111
|
|
@@ -162,6 +119,8 @@ row2 A B Id3
|
|
162
119
|
TmpFile.with_file(content) do |filename|
|
163
120
|
tsv = TSV.new(filename, :sep => /\s+/, :key => "OtherID", :persistence => true)
|
164
121
|
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
122
|
+
|
123
|
+
tsv.write
|
165
124
|
tsv['Id4'] = [["row3"],["aA"],["bB","bbBB"]]
|
166
125
|
assert_equal ["aA"], tsv["Id4"][1]
|
167
126
|
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :persistence => true)
|
@@ -174,79 +133,6 @@ row2 A B Id3
|
|
174
133
|
end
|
175
134
|
end
|
176
135
|
|
177
|
-
def test_index_headerless
|
178
|
-
content =<<-EOF
|
179
|
-
row1 a|aa|aaa b Id1|Id2
|
180
|
-
row2 A B Id3
|
181
|
-
EOF
|
182
|
-
|
183
|
-
TmpFile.with_file(content) do |filename|
|
184
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
185
|
-
index = tsv.index(:case_insensitive => true, :target => 2)
|
186
|
-
assert index["row1"].include? "Id1"
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
|
191
|
-
def test_index
|
192
|
-
content =<<-EOF
|
193
|
-
#Id ValueA ValueB OtherID
|
194
|
-
row1 a|aa|aaa b Id1|Id2
|
195
|
-
row2 A B Id3
|
196
|
-
EOF
|
197
|
-
|
198
|
-
TmpFile.with_file(content) do |filename|
|
199
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :persistence => false)
|
200
|
-
index = tsv.index(:case_insensitive => true)
|
201
|
-
assert index["row1"].include? "Id1"
|
202
|
-
assert_equal "OtherID", index.key_field
|
203
|
-
end
|
204
|
-
|
205
|
-
TmpFile.with_file(content) do |filename|
|
206
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID")
|
207
|
-
index = tsv.index(:case_insensitive => true)
|
208
|
-
assert index["row1"].include? "Id1"
|
209
|
-
assert_equal "OtherID", index.key_field
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_best_index
|
214
|
-
content =<<-EOF
|
215
|
-
#Id ValueA ValueB OtherID
|
216
|
-
row1 a|aa|aaa b|A Id1
|
217
|
-
row2 A a|B Id3
|
218
|
-
EOF
|
219
|
-
|
220
|
-
TmpFile.with_file(content) do |filename|
|
221
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :persistence => true)
|
222
|
-
index = tsv.index(:case_insensitive => false, :order => true)
|
223
|
-
assert_equal "Id1", index['a'].first
|
224
|
-
assert_equal "Id3", index['A'].first
|
225
|
-
assert_equal "OtherID", index.key_field
|
226
|
-
end
|
227
|
-
|
228
|
-
TmpFile.with_file(content) do |filename|
|
229
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID")
|
230
|
-
index = tsv.index(:case_insensitive => true)
|
231
|
-
assert index["row1"].include? "Id1"
|
232
|
-
assert_equal "OtherID", index.key_field
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
def test_values_at
|
237
|
-
content =<<-EOF
|
238
|
-
#Id ValueA ValueB OtherID
|
239
|
-
row1 a|aa|aaa b Id1|Id2
|
240
|
-
row2 A B Id3
|
241
|
-
EOF
|
242
|
-
|
243
|
-
TmpFile.with_file(content) do |filename|
|
244
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :key => "OtherID", :persistence => true)
|
245
|
-
index = tsv.index(:case_insensitive => true)
|
246
|
-
assert index.values_at(*["row1"]).first.include? "Id1"
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
136
|
def test_named_array
|
251
137
|
content =<<-EOF
|
252
138
|
#Id ValueA ValueB OtherID
|
@@ -260,548 +146,39 @@ row2 A B Id3
|
|
260
146
|
assert_equal ["Id", "ValueA", "ValueB"], tsv.fields
|
261
147
|
assert_equal ["a", "aa", "aaa"], tsv["id1"][1]
|
262
148
|
assert_equal ["a", "aa", "aaa"], tsv["Id2"]["ValueA"]
|
263
|
-
|
264
|
-
tsv_sliced = tsv.reorder(:main, ["ValueA", "ValueB"])
|
265
|
-
|
266
|
-
assert_equal ["ValueA", "ValueB"], tsv_sliced.fields
|
267
|
-
assert_equal ["a", "aa", "aaa"], tsv_sliced["id1"][0]
|
268
|
-
assert_equal ["a", "aa", "aaa"], tsv_sliced["Id2"]["ValueA"]
|
269
|
-
end
|
270
|
-
end
|
271
|
-
|
272
|
-
def test_helpers
|
273
|
-
begin
|
274
|
-
require 'rbbt/sources/organism'
|
275
|
-
filename = File.join(Organism.datadir('Sce'), 'identifiers')
|
276
|
-
missing = true
|
277
|
-
index = TSV.index(filename, :persistence => true, :key => "Associated Gene Name")
|
278
|
-
assert index['1020'].include? 'CDK5'
|
279
|
-
index = TSV.index(filename, :persistence => true, :key => "Associated Gene Name")
|
280
|
-
assert index[[nil,'1020']].include? 'CDK5'
|
281
|
-
index = TSV.index(filename, :persistence => true, :key => "Associated Gene Name")
|
282
|
-
assert index[['MISSING','1020']].include? 'CDK5'
|
283
|
-
rescue Exception
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_grep
|
288
|
-
content =<<-EOF
|
289
|
-
#Id ValueA ValueB OtherID
|
290
|
-
row1 a|aa|aaa b Id1|Id2
|
291
|
-
row2 A B Id3
|
292
|
-
EOF
|
293
|
-
|
294
|
-
TmpFile.with_file(content) do |filename|
|
295
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :grep => %w(row1))
|
296
|
-
assert tsv.keys.include? "row1"
|
297
|
-
assert( ! tsv.keys.include?("row2"))
|
298
|
-
end
|
149
|
+
end
|
299
150
|
end
|
300
151
|
|
301
|
-
|
302
|
-
|
303
|
-
def test_sort
|
152
|
+
def test_one_col
|
304
153
|
content =<<-EOF
|
305
|
-
#Id
|
306
|
-
row1
|
307
|
-
row2
|
154
|
+
#Id
|
155
|
+
row1
|
156
|
+
row2
|
308
157
|
EOF
|
309
158
|
|
310
159
|
TmpFile.with_file(content) do |filename|
|
311
160
|
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
312
|
-
assert_equal "
|
313
|
-
assert_equal
|
314
|
-
|
315
|
-
|
316
|
-
TmpFile.with_file(content) do |filename|
|
317
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
318
|
-
assert_equal "row2", tsv.sort_by{|k,v| v["ValueB"]}.first[0]
|
319
|
-
assert_equal "B", tsv.sort_by{|k,v| v["ValueB"]}.first[1]["ValueB"].first
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
def test_to_s
|
324
|
-
content =<<-EOF
|
325
|
-
#Id ValueA ValueB OtherID
|
326
|
-
row1 a|aa|aaa b Id1|Id2
|
327
|
-
row2 A B Id3
|
328
|
-
EOF
|
329
|
-
TmpFile.with_file(content) do |filename|
|
330
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
331
|
-
assert_equal content, tsv.to_s
|
332
|
-
end
|
333
|
-
end
|
334
|
-
|
335
|
-
def test_to_s_ordered
|
336
|
-
content =<<-EOF
|
337
|
-
#Id ValueA ValueB OtherID
|
338
|
-
row1 a|aa|aaa b Id1|Id2
|
339
|
-
row2 A B Id3
|
340
|
-
EOF
|
341
|
-
|
342
|
-
content2 =<<-EOF
|
343
|
-
#Id ValueA ValueB OtherID
|
344
|
-
row2 A B Id3
|
345
|
-
row1 a|aa|aaa b Id1|Id2
|
346
|
-
EOF
|
347
|
-
|
348
|
-
|
349
|
-
TmpFile.with_file(content) do |filename|
|
350
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
351
|
-
assert_equal content, tsv.to_s(%w(row1 row2))
|
352
|
-
assert_not_equal content, tsv.to_s(%w(row2 row1))
|
353
|
-
assert_equal content2, tsv.to_s(%w(row2 row1))
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
def test_smart_merge_single
|
358
|
-
content1 =<<-EOF
|
359
|
-
#Id ValueA ValueB
|
360
|
-
row1 a|aa|aaa b
|
361
|
-
row2 A B
|
362
|
-
EOF
|
363
|
-
|
364
|
-
content2 =<<-EOF
|
365
|
-
#ValueC ValueB OtherID
|
366
|
-
c|cc|ccc b Id1|Id2
|
367
|
-
C B Id3
|
368
|
-
EOF
|
369
|
-
|
370
|
-
tsv1 = tsv2 = nil
|
371
|
-
TmpFile.with_file(content1) do |filename|
|
372
|
-
tsv1 = TSV.new(File.open(filename), :list, :sep => /\s+/)
|
373
|
-
end
|
374
|
-
|
375
|
-
TmpFile.with_file(content2) do |filename|
|
376
|
-
tsv2 = TSV.new(File.open(filename), :list, :sep => /\s+/)
|
377
|
-
end
|
378
|
-
|
379
|
-
tsv1.smart_merge tsv2, "ValueB"
|
380
|
-
|
381
|
-
assert_equal "C", tsv1["row2"]["ValueC"]
|
382
|
-
assert %w(c cc ccc).include? tsv1["row1"]["ValueC"]
|
383
|
-
assert_equal "Id1", tsv1["row1"]["OtherID"]
|
384
|
-
end
|
385
|
-
|
386
|
-
def test_smart_merge
|
387
|
-
content1 =<<-EOF
|
388
|
-
#Id ValueA ValueB
|
389
|
-
row1 a|aa|aaa b
|
390
|
-
row2 A B
|
391
|
-
EOF
|
392
|
-
|
393
|
-
content2 =<<-EOF
|
394
|
-
#ValueC ValueB OtherID
|
395
|
-
c|cc|ccc b Id1|Id2
|
396
|
-
C B Id3
|
397
|
-
EOF
|
398
|
-
|
399
|
-
tsv1 = tsv2 = nil
|
400
|
-
TmpFile.with_file(content1) do |filename|
|
401
|
-
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
402
|
-
end
|
403
|
-
|
404
|
-
TmpFile.with_file(content2) do |filename|
|
405
|
-
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
406
|
-
end
|
407
|
-
|
408
|
-
tsv1.smart_merge tsv2, "ValueB"
|
409
|
-
|
410
|
-
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
411
|
-
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
161
|
+
assert_equal "Id", tsv.key_field
|
162
|
+
assert_equal [], tsv.fields
|
163
|
+
end
|
412
164
|
end
|
413
165
|
|
414
|
-
def test_smart_merge_through_index_find_headers
|
415
|
-
content1 =<<-EOF
|
416
|
-
#Id ValueA ValueBB
|
417
|
-
row1 a|aa|aaa bb
|
418
|
-
row2 A BB
|
419
|
-
EOF
|
420
|
-
|
421
|
-
content2 =<<-EOF
|
422
|
-
#ValueC ValueB OtherID ValueA
|
423
|
-
c|cc|ccc b Id1|Id2 aaaa
|
424
|
-
C B Id3 AA
|
425
|
-
EOF
|
426
|
-
|
427
|
-
index =<<-EOF
|
428
|
-
#ValueB ValueBB
|
429
|
-
b bb
|
430
|
-
B BB
|
431
|
-
EOF
|
432
|
-
|
433
|
-
tsv1 = tsv2 = nil
|
434
|
-
TmpFile.with_file(content1) do |filename|
|
435
|
-
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
436
|
-
end
|
437
|
-
|
438
|
-
TmpFile.with_file(content2) do |filename|
|
439
|
-
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
440
|
-
end
|
441
|
-
|
442
|
-
TmpFile.with_file(index) do |filename|
|
443
|
-
index = TSV.index(filename, :sep => /\s+/)
|
444
|
-
end
|
445
|
-
|
446
|
-
tsv1.smart_merge tsv2, index
|
447
|
-
|
448
|
-
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
449
|
-
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
450
|
-
|
451
|
-
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
452
|
-
end
|
453
|
-
|
454
|
-
|
455
|
-
def test_smart_merge_through_string_find_headers
|
456
|
-
content1 =<<-EOF
|
457
|
-
#Id ValueA ValueBB
|
458
|
-
row1 a|aa|aaa bb
|
459
|
-
row2 A BB
|
460
|
-
EOF
|
461
|
-
|
462
|
-
content2 =<<-EOF
|
463
|
-
#ValueC ValueB OtherID ValueA
|
464
|
-
c|cc|ccc b Id1|Id2 aaaa
|
465
|
-
C B Id3 AA
|
466
|
-
EOF
|
467
|
-
|
468
|
-
index =<<-EOF
|
469
|
-
#ValueB ValueBB
|
470
|
-
b bb
|
471
|
-
B BB
|
472
|
-
EOF
|
473
|
-
|
474
|
-
tsv1 = tsv2 = nil
|
475
|
-
TmpFile.with_file(content1) do |filename|
|
476
|
-
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
477
|
-
end
|
478
166
|
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
TmpFile.with_file(index) do |filename|
|
484
|
-
tsv1.smart_merge tsv2, "through:#{filename}#:sep=/\\s+/"
|
485
|
-
end
|
486
|
-
|
487
|
-
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
488
|
-
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
489
|
-
|
490
|
-
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
491
|
-
end
|
492
|
-
|
493
|
-
def test_smart_merge_through_string
|
494
|
-
content1 =<<-EOF
|
495
|
-
#Id ValueA ValueBB
|
496
|
-
row1 a|aa|aaa bb
|
497
|
-
row2 A BB
|
498
|
-
EOF
|
499
|
-
|
500
|
-
content2 =<<-EOF
|
501
|
-
#ValueC ValueB OtherID ValueA
|
502
|
-
c|cc|ccc b Id1|Id2 aaaa
|
503
|
-
C B Id3 AA
|
504
|
-
EOF
|
505
|
-
|
506
|
-
index =<<-EOF
|
507
|
-
#ValueB ValueBB
|
508
|
-
b bb
|
509
|
-
B BB
|
510
|
-
EOF
|
511
|
-
|
512
|
-
tsv1 = tsv2 = nil
|
513
|
-
TmpFile.with_file(content1) do |filename|
|
514
|
-
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
515
|
-
end
|
516
|
-
|
517
|
-
TmpFile.with_file(content2) do |filename|
|
518
|
-
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
519
|
-
end
|
520
|
-
|
521
|
-
TmpFile.with_file(index) do |filename|
|
522
|
-
tsv1.smart_merge tsv2, "through:#{filename}#:sep=/\\s+/#using:ValueBB"
|
523
|
-
end
|
524
|
-
|
525
|
-
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
526
|
-
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
527
|
-
|
528
|
-
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
529
|
-
end
|
530
|
-
|
531
|
-
def test_smart_merge_common_fields
|
532
|
-
content1 =<<-EOF
|
167
|
+
def test_case_insensitive
|
168
|
+
content1 =<<-EOF
|
169
|
+
#: :sep=/\\s+/#:case_insensitive=false
|
533
170
|
#Id ValueA ValueB
|
534
171
|
row1 a|aa|aaa b
|
535
172
|
row2 A B
|
536
173
|
EOF
|
537
174
|
|
538
|
-
|
539
|
-
#ValueC ValueB OtherID ValueA
|
540
|
-
c|cc|ccc b Id1|Id2 aaaa
|
541
|
-
C B Id3 AA
|
542
|
-
EOF
|
543
|
-
|
544
|
-
tsv1 = tsv2 = nil
|
545
|
-
TmpFile.with_file(content1) do |filename|
|
546
|
-
tsv1 = TSV.new(File.open(filename), :sep => /\s+/)
|
547
|
-
end
|
548
|
-
|
549
|
-
TmpFile.with_file(content2) do |filename|
|
550
|
-
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
551
|
-
end
|
552
|
-
|
553
|
-
tsv1.smart_merge tsv2, "ValueB"
|
554
|
-
|
555
|
-
assert_equal %w(Id1 Id2), tsv1["row1"]["OtherID"]
|
556
|
-
assert_equal %w(C), tsv1["row2"]["ValueC"]
|
557
|
-
|
558
|
-
assert_equal %w(a aa aaa aaaa), tsv1["row1"]["ValueA"]
|
559
|
-
end
|
560
|
-
|
561
|
-
def test_smart_merge_headerless
|
562
|
-
content1 =<<-EOF
|
563
|
-
row1 a|aa|aaa b
|
564
|
-
row2 A B
|
565
|
-
EOF
|
566
|
-
|
567
|
-
content2 =<<-EOF
|
568
|
-
c|cc|ccc b Id1|Id2
|
569
|
-
C B Id3
|
570
|
-
EOF
|
571
|
-
|
572
|
-
tsv1 = tsv2 = nil
|
175
|
+
tsv1 = tsv2 = identifiers = nil
|
573
176
|
TmpFile.with_file(content1) do |filename|
|
574
|
-
tsv1 = TSV.new(File.open(filename), :
|
177
|
+
tsv1 = TSV.new(File.open(filename), :key => "ValueA")
|
178
|
+
assert !tsv1.case_insensitive
|
179
|
+
assert tsv1.include? "A"
|
575
180
|
end
|
576
181
|
|
577
|
-
TmpFile.with_file(content2) do |filename|
|
578
|
-
tsv2 = TSV.new(File.open(filename), :sep => /\s+/)
|
579
|
-
end
|
580
|
-
|
581
|
-
tsv1.smart_merge tsv2, 1
|
582
|
-
|
583
|
-
assert_equal %w(C), tsv1["row2"][2]
|
584
|
-
assert_equal %w(Id1 Id2), tsv1["row1"][3]
|
585
|
-
end
|
586
|
-
|
587
|
-
|
588
|
-
def test_reorder_simple
|
589
|
-
content =<<-EOF
|
590
|
-
#Id ValueA ValueB OtherID
|
591
|
-
row1 a|aa|aaa b Id1|Id2
|
592
|
-
row2 A B Id3
|
593
|
-
row3 a C Id4
|
594
|
-
EOF
|
595
|
-
|
596
|
-
TmpFile.with_file(content) do |filename|
|
597
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
598
|
-
|
599
|
-
tsv1 = tsv.reorder("ValueA")
|
600
|
-
|
601
|
-
assert_equal "ValueA", tsv1.key_field
|
602
|
-
assert_equal %w(Id ValueB OtherID), tsv1.fields
|
603
|
-
assert_equal ["B"], tsv1["A"]["ValueB"]
|
604
|
-
assert_equal ["b","C"], tsv1["a"]["ValueB"]
|
605
|
-
assert_equal ["b"], tsv1["aa"]["ValueB"]
|
606
|
-
|
607
|
-
end
|
608
|
-
end
|
609
|
-
|
610
|
-
def test_reorder_simple_headerless
|
611
|
-
content =<<-EOF
|
612
|
-
row1 a|aa|aaa b Id1|Id2
|
613
|
-
row2 A B Id3
|
614
|
-
row3 a C Id4
|
615
|
-
EOF
|
616
|
-
|
617
|
-
TmpFile.with_file(content) do |filename|
|
618
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
619
|
-
|
620
|
-
tsv1 = tsv.reorder(0)
|
621
|
-
|
622
|
-
assert_nil tsv1.key_field
|
623
|
-
assert_equal ["B"], tsv1["A"][1]
|
624
|
-
assert_equal ["b","C"], tsv1["a"][1]
|
625
|
-
assert_equal ["b"], tsv1["aa"][1]
|
626
|
-
assert_equal ["row1"], tsv1["aa"][0]
|
627
|
-
assert_equal ["row1","row3"], tsv1["a"][0]
|
628
|
-
end
|
629
|
-
end
|
630
|
-
|
631
|
-
|
632
|
-
def test_reorder_remove_field
|
633
|
-
content =<<-EOF
|
634
|
-
#Id ValueA ValueB OtherID
|
635
|
-
row1 a|aa|aaa b Id1|Id2
|
636
|
-
row2 A B Id3
|
637
|
-
row3 a C Id4
|
638
|
-
EOF
|
639
|
-
|
640
|
-
TmpFile.with_file(content) do |filename|
|
641
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
642
|
-
|
643
|
-
tsv1 = tsv.reorder("ValueA", ["ValueB", "Id"])
|
644
|
-
|
645
|
-
assert_equal "ValueA", tsv1.key_field
|
646
|
-
assert_equal %w(ValueB Id), tsv1.fields
|
647
|
-
assert_equal ["B"], tsv1["A"]["ValueB"]
|
648
|
-
assert_equal ["b","C"], tsv1["a"]["ValueB"]
|
649
|
-
assert_equal ["row1"], tsv1["aa"]["Id"]
|
650
|
-
assert_equal ["row1","row3"], tsv1["a"]["Id"]
|
651
|
-
end
|
652
|
-
end
|
653
|
-
|
654
|
-
def test_through
|
655
|
-
content =<<-EOF
|
656
|
-
#Id ValueA ValueB OtherID
|
657
|
-
row1 a|aa|aaa b Id1|Id2
|
658
|
-
row2 A B Id3
|
659
|
-
row3 a C Id4
|
660
|
-
EOF
|
661
|
-
|
662
|
-
TmpFile.with_file(content) do |filename|
|
663
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
664
|
-
|
665
|
-
tsv.through "ValueA" do |key, values|
|
666
|
-
assert(tsv.keys.include? values["Id"].first)
|
667
|
-
end
|
668
|
-
end
|
669
|
-
end
|
670
|
-
|
671
|
-
def test_process
|
672
|
-
content =<<-EOF
|
673
|
-
#Id ValueA ValueB OtherID
|
674
|
-
row1 a|aa|aaa b Id1|Id2
|
675
|
-
row2 A B Id3
|
676
|
-
row3 a C Id4
|
677
|
-
EOF
|
678
|
-
|
679
|
-
TmpFile.with_file(content) do |filename|
|
680
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/)
|
681
|
-
|
682
|
-
tsv.process "ValueA" do |field_values,key,values|
|
683
|
-
field_values.collect{|v| "Pref:#{v}"}
|
684
|
-
end
|
685
|
-
|
686
|
-
assert_equal ["Pref:A"], tsv["row2"]["ValueA"]
|
687
|
-
end
|
688
|
-
end
|
689
|
-
|
690
|
-
def test_break_with_fix
|
691
|
-
content =<<-EOF
|
692
|
-
#Id ValueA ValueB OtherID
|
693
|
-
row1 a|aa|aaa b Id1|Id2
|
694
|
-
row2 A B Id3
|
695
|
-
row3 a C Id4
|
696
|
-
EOF
|
697
|
-
|
698
|
-
TmpFile.with_file(content) do |filename|
|
699
|
-
tsv = TSV.new(File.open(filename), :sep => /\s+/, :fix => proc{|l| l =~ /^row2/? nil : l})
|
700
|
-
assert_equal %w(row1), tsv.keys
|
701
|
-
end
|
702
|
-
end
|
703
|
-
|
704
|
-
def test_open_stringoptions
|
705
|
-
content =<<-EOF
|
706
|
-
#Id ValueA ValueB OtherID
|
707
|
-
row1 a|aa|aaa b Id1|Id2
|
708
|
-
row2 A B Id3
|
709
|
-
row3 a C Id4
|
710
|
-
EOF
|
711
|
-
|
712
|
-
TmpFile.with_file(content) do |filename|
|
713
|
-
tsv = TSV.new(filename + '#:sep=/\s+/')
|
714
|
-
|
715
|
-
tsv.process "ValueA" do |field_values,key,values|
|
716
|
-
field_values.collect{|v| "Pref:#{v}"}
|
717
|
-
end
|
718
|
-
|
719
|
-
assert_equal ["Pref:A"], tsv["row2"]["ValueA"]
|
720
|
-
end
|
721
|
-
end
|
722
|
-
|
723
|
-
def test_select
|
724
|
-
content =<<-EOF
|
725
|
-
#Id ValueA ValueB OtherID
|
726
|
-
row1 a|aa|aaa b Id1|Id2
|
727
|
-
row2 A B Id3
|
728
|
-
row3 a C Id4
|
729
|
-
EOF
|
730
|
-
|
731
|
-
TmpFile.with_file(content) do |filename|
|
732
|
-
tsv = TSV.new(filename + '#:sep=/\s+/')
|
733
|
-
assert tsv.type == :double
|
734
|
-
|
735
|
-
new = tsv.select %w(b Id4)
|
736
|
-
assert_equal %w(row1 row3).sort, new.keys
|
737
|
-
|
738
|
-
new = tsv.select "ValueB" => %w(b Id4)
|
739
|
-
assert_equal %w(row1).sort, new.keys
|
740
|
-
|
741
|
-
new = tsv.select /b|Id4/
|
742
|
-
assert_equal %w(row1 row3).sort, new.keys
|
743
|
-
|
744
|
-
new = tsv.select "ValueB" => /b|Id4/
|
745
|
-
assert_equal %w(row1).sort, new.keys
|
746
|
-
|
747
|
-
tsv = TSV.new(filename + '#:sep=/\s+/#:type=:flat')
|
748
|
-
assert tsv.type != :double
|
749
|
-
|
750
|
-
new = tsv.select %w(b Id4)
|
751
|
-
end
|
752
|
-
end
|
753
|
-
|
754
|
-
def test_field_compare
|
755
|
-
content =<<-EOF
|
756
|
-
#Id LetterValue#ValueA LetterValue#ValueB OtherID
|
757
|
-
row1 a|aa|aaa b Id1|Id2
|
758
|
-
row2 A B Id3
|
759
|
-
row3 a C Id4
|
760
|
-
EOF
|
761
|
-
|
762
|
-
TmpFile.with_file(content) do |filename|
|
763
|
-
tsv = TSV.new(filename + '#:sep=/\s+/')
|
764
|
-
|
765
|
-
assert tsv.fields.include?("LetterValue")
|
766
|
-
end
|
767
|
-
end
|
768
|
-
|
769
|
-
def test_add_field
|
770
|
-
content =<<-EOF
|
771
|
-
#Id LetterValue#ValueA LetterValue#ValueB OtherID
|
772
|
-
row1 a|aa|aaa b Id1|Id2
|
773
|
-
row2 A B Id3
|
774
|
-
row3 a C Id4
|
775
|
-
EOF
|
776
|
-
|
777
|
-
TmpFile.with_file(content) do |filename|
|
778
|
-
tsv = TSV.new(filename + '#:sep=/\s+/')
|
779
|
-
tsv.add_field "Str length" do |k,v|
|
780
|
-
(v.flatten * " ").length
|
781
|
-
end
|
782
|
-
|
783
|
-
assert tsv.fields.include?("Str length")
|
784
|
-
end
|
785
|
-
|
786
|
-
end
|
787
|
-
|
788
|
-
def test_cast
|
789
|
-
content =<<-EOF
|
790
|
-
#Id LetterValue#ValueA LetterValue#ValueB OtherID
|
791
|
-
row1 a|aa|aaa b Id1|Id2
|
792
|
-
row2 A B Id3
|
793
|
-
row3 a C Id4
|
794
|
-
EOF
|
795
|
-
|
796
|
-
TmpFile.with_file(content) do |filename|
|
797
|
-
tsv = TSV.new(filename + '#:sep=/\s+/#:cast="to_sym"')
|
798
|
-
assert tsv['row1']["OtherID"].include?(:Id1)
|
799
|
-
assert ! tsv['row1']["OtherID"].include?("Id1")
|
800
|
-
|
801
|
-
tsv = TSV.new(filename + '#:sep=/\s+/')
|
802
|
-
assert tsv['row1']["OtherID"].include?("Id1")
|
803
|
-
assert ! tsv['row1']["OtherID"].include?(:Id1)
|
804
|
-
end
|
805
182
|
end
|
806
183
|
end
|
807
184
|
|