rbbt-util 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,3 @@
1
- require 'tsv'
2
-
3
1
  module WorkFlow
2
+
4
3
  end
@@ -0,0 +1,89 @@
1
+ rbbt.glob <- function(d, pattern){
2
+ d=sub("/$", '', d);
3
+ sapply(dir(d, pattern), function(file){paste(d,file,sep="/")});
4
+ }
5
+
6
+ rbbt.png_plot <- function(filename, width, height, p){
7
+ png(filename="temp.png", width=width, height=height);
8
+ eval(p);
9
+ dev.off();
10
+ }
11
+
12
+ rbbt.load.data <- function(filename, sep = "\t", ...){
13
+ data=read.table(file=filename, sep=sep, fill=TRUE, as.is=TRUE, ...);
14
+ return(data);
15
+ }
16
+
17
+ rbbt.tsv <- function(filename, sep = "\t", comment.char ="#", ...){
18
+ data=read.table(file=filename, sep=sep, fill=TRUE, as.is=TRUE, row.names=1, comment.char = comment.char, ...);
19
+ f = file(filename, 'r');
20
+ headers = readLines(f, 1);
21
+ close(f);
22
+ if (grep(paste("^", comment.char, sep=""), headers)){
23
+ fields = strsplit(headers, sep)[[1]];
24
+ fields = fields[2:length(fields)];
25
+ names(data) <- fields;
26
+ }
27
+ return(data);
28
+ }
29
+
30
+ rbbt.tsv.write <- function(filename, data, key.field = NULL){
31
+ if (is.null(key.field)){ key.field = "ID";}
32
+
33
+ f = file(filename, 'w');
34
+
35
+ header = paste("#", key.field, sep="");
36
+ for (name in colnames(data)){ header = paste(header, name, sep="\t");}
37
+ header = paste(header, "\n", sep="");
38
+ cat(header, file=f);
39
+ cat(header, file=stderr());
40
+
41
+ close(f);
42
+
43
+ write.table(data, file=filename, quote=FALSE, append=TRUE, col.names=FALSE, row.names=TRUE, sep="\t");
44
+
45
+ return(NULL);
46
+ }
47
+
48
+ rbbt.print.data <- function(data, file="", ...){
49
+ write.table(data, quote=FALSE, row.name=FALSE,col.name=FALSE,file=file, ...);
50
+ }
51
+
52
+ rbbt.percent <- function(values){
53
+ values=values/sum(values);
54
+ }
55
+
56
+ rbbt.sort_by_field <- function(data, field, is.numeric=TRUE){
57
+ if (is.numeric){
58
+ field.data=as.numeric(data[,field]);
59
+ }else{
60
+ field.data=data[,field];
61
+ }
62
+ index = sort(field.data, index.return = TRUE)$ix;
63
+ return(data[index,]);
64
+ }
65
+
66
+ rbbt.add <- function(data, new){
67
+ if (is.null(data)){
68
+ return(new);
69
+ }else{
70
+ return(c(data, new));
71
+ }
72
+ }
73
+
74
+ rbbt.acc <- function(data, new){
75
+ if (is.null(data)){
76
+ return(new);
77
+ }else{
78
+ return(unique(c(data, new)));
79
+ }
80
+ }
81
+
82
+ rbbt.init <- function(data, new){
83
+ if (is.null(data)){
84
+ return(new);
85
+ }else{
86
+ return(data);
87
+ }
88
+ }
89
+
@@ -0,0 +1,9 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
+ require 'rbbt/util/R'
3
+
4
+ class TestR < Test::Unit::TestCase
5
+ def test_sum
6
+ assert_equal "6", R.run('cat(3+3)').read
7
+ end
8
+ end
9
+
@@ -0,0 +1,136 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
+ require 'rbbt/util/bed'
3
+ require 'rbbt/util/tmpfile'
4
+
5
+ class TestBed < Test::Unit::TestCase
6
+ def load_data(data)
7
+ Log.debug("Data:\n#{Open.read(data)}")
8
+ tsv = TSV.new(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
17
+ end
18
+
19
+ def test_point
20
+ data =<<-EOF
21
+ #ID Pos
22
+ a 1
23
+ b 10
24
+ c 20
25
+ d 12
26
+ e 26
27
+ f 11
28
+ g 25
29
+ EOF
30
+
31
+ TmpFile.with_file(data) do |f|
32
+ bed = Bed.new TSV.new(f, :list, :sep=>" "), :key => "Pos" , :value => "ID"
33
+
34
+ assert_equal %w(), bed[0].sort
35
+ assert_equal %w(b), bed[10].sort
36
+ assert_equal %w(a b c d f), bed[(0..20)].sort
37
+ end
38
+ end
39
+
40
+ def test_range
41
+ data =<<-EOF
42
+ #ID:Range
43
+ #:012345678901234567890
44
+ a: ______
45
+ b: ______
46
+ c: _______
47
+ d: ____
48
+ e: ______
49
+ f: ___
50
+ g: ____
51
+ EOF
52
+
53
+ TmpFile.with_file(data) do |f|
54
+ bed = Bed.new load_data(f), :range => ["Start" , "End"], :value => "ID"
55
+
56
+ assert_equal %w(), bed[0].sort
57
+ assert_equal %w(b), bed[1].sort
58
+ assert_equal %w(), bed[20].sort
59
+ assert_equal %w(), bed[(20..100)].sort
60
+ assert_equal %w(a b d), bed[3].sort
61
+ assert_equal %w(a b c d e), bed[(3..4)].sort
62
+ end
63
+ end
64
+
65
+ def test_format
66
+ entry = Bed::Entry.new "Hola", 0, 10, nil
67
+ format = Bed::FixWidthTable.format(entry,1, 100)
68
+ assert_equal entry, Bed::FixWidthTable.unformat(format, 100)
69
+ end
70
+
71
+ def test_table
72
+ TmpFile.with_file do |f|
73
+ table = Bed::FixWidthTable.new(f, 100, false)
74
+ table.add Bed::Entry.new("Entry1", 0, 10, nil), 0
75
+ table.add Bed::Entry.new("Entry2", 10, 20, nil), 10
76
+ table.add Bed::Entry.new("Entry3", 10, 20, nil), 10
77
+
78
+ table.read
79
+
80
+ assert_equal "Entry1", table[0].value
81
+ assert_equal "Entry2", table[1].value
82
+ assert_equal "Entry3", table[2].value
83
+ end
84
+
85
+ end
86
+
87
+ def test_range_point
88
+ data =<<-EOF
89
+ #ID Pos
90
+ a 1
91
+ b 10
92
+ c 20
93
+ d 12
94
+ e 26
95
+ f 11
96
+ g 25
97
+ EOF
98
+
99
+ TmpFile.with_file(data) do |f|
100
+ bed = Bed.new TSV.new(f,:list, :sep=>" "), :key => "Pos" , :value => "ID", :persistence => true
101
+
102
+ bed = Bed.new TSV.new(f,:list, :sep=>" "), :key => "Pos" , :value => "ID", :persistence => true
103
+
104
+ assert_equal %w(), bed[0].sort
105
+ assert_equal %w(a b c d f), bed[(0..20)].sort
106
+ assert_equal %w(b), bed[10].sort
107
+ end
108
+ end
109
+
110
+ def test_range_persistence
111
+ data =<<-EOF
112
+ #ID:Range
113
+ #:012345678901234567890
114
+ a: ______
115
+ b: ______
116
+ c: _______
117
+ d: ____
118
+ e: ______
119
+ f: ___
120
+ g: ____
121
+ EOF
122
+
123
+ TmpFile.with_file(data) do |f|
124
+ bed = Bed.new load_data(f), :range => ["Start" , "End"], :value => "ID", :persistence => true
125
+
126
+ bed = Bed.new load_data(f), :range => ["Start" , "End"], :value => "ID", :persistence => true
127
+
128
+ assert_equal %w(), bed[0].sort
129
+ assert_equal %w(b), bed[1].sort
130
+ assert_equal %w(), bed[20].sort
131
+ assert_equal %w(), bed[(20..100)].sort
132
+ assert_equal %w(a b d), bed[3].sort
133
+ assert_equal %w(a b c d e), bed[(3..4)].sort
134
+ end
135
+ end
136
+ end
@@ -4,6 +4,12 @@ require 'rbbt/util/data_module'
4
4
  require 'test/unit'
5
5
  require 'fileutils'
6
6
 
7
+ SHAREDIR = File.join(PKGData.sharedir_for_file(__FILE__), 'install/DataTest')
8
+ FileUtils.mkdir_p SHAREDIR
9
+ File.open(File.join(SHAREDIR, 'Rakefile'), 'w') do |f|
10
+ f.puts "file :file1 do |t| File.open(t.name, 'w') do |f| f.write 'File 1' end end"
11
+ end
12
+
7
13
  module DataTest
8
14
  extend DataModule
9
15
 
@@ -16,28 +22,22 @@ end
16
22
 
17
23
  class TestDataModule < Test::Unit::TestCase
18
24
 
19
- SHAREDIR = File.join(PKGData.sharedir_for_file(__FILE__), 'install', 'DataTest')
20
-
21
25
  def setup
26
+
22
27
  FileUtils.mkdir_p SHAREDIR
23
28
  File.open(File.join(SHAREDIR, 'Rakefile'), 'w') do |f|
24
- f.puts "task :file1 do |t| File.open(t.name, 'w') do |f| f.write 'File 1' end end"
29
+ f.puts "file :file1 do |t| File.open(t.name, 'w') do |f| f.write 'File 1' end end"
25
30
  end
26
31
  end
27
32
 
28
- def test_module
29
- assert_equal "File 1", Open.read(DataTest.file1).chomp
30
- assert_equal "Hello world", DataTest.salute("world")
33
+ def test_rakefile
34
+ assert_equal "File 1", Rbbt.files.DataTest.file1.read
31
35
  assert_equal "Hello world", DataTest.salute("world")
32
36
  assert_equal "Hello world", DataTest::with_key("world").salute
33
37
  assert_equal "Hello world", DataTest::World.salute
34
38
  FileUtils.rm_rf File.join(Rbbt.datadir, 'DataTest')
35
39
  end
36
40
 
37
- def test_method_missing
38
- assert_raise NoMethodError do DataTest.missing end
39
- end
40
-
41
41
  def teardown
42
42
  FileUtils.rm_rf SHAREDIR
43
43
  end
@@ -24,6 +24,7 @@ class TestMisc < Test::Unit::TestCase
24
24
  assert(Misc.string2hash("a=b")["a"] == 'b')
25
25
  assert(Misc.string2hash("a=b#c=d#:h=j")["c"] == 'd')
26
26
  assert(Misc.string2hash("a=b#c=d#:h=j")[:h] == 'j')
27
+ assert(Misc.string2hash("a=b#c=d#:h=:j")[:h] == :j)
27
28
  end
28
29
 
29
30
  def test_named_array
@@ -0,0 +1,60 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
+ require 'rbbt/util/persistence'
3
+
4
+ class TestPersistence < Test::Unit::TestCase
5
+
6
+ def test_string
7
+ string = "test string"
8
+ TmpFile.with_file do |f|
9
+ Persistence.persist("token_file", :Test, :string, :persistence_file => f) do string end
10
+ assert File.exists? f
11
+ assert_equal string, Open.read(f)
12
+
13
+ rm f
14
+ end
15
+ end
16
+
17
+ def test_yaml
18
+ object = [1,2,2]
19
+ TmpFile.with_file do |f|
20
+ Persistence.persist("token_file", :Test, :yaml, :persistence_file => f) do object end
21
+ assert File.exists? f
22
+ assert_equal object, YAML.load(File.open(f))
23
+ assert_equal YAML.dump(object), Open.read(f)
24
+
25
+ rm f
26
+ end
27
+ end
28
+
29
+ def test_marshal
30
+ object = [1,2,2]
31
+ TmpFile.with_file do |f|
32
+ Persistence.persist("token_file", :Test, :marshal, :persistence_file => f) do object end
33
+ assert File.exists? f
34
+ assert_equal object, Marshal.load(File.open(f))
35
+ assert_equal Marshal.dump(object), Open.read(f)
36
+
37
+ rm f
38
+ end
39
+ end
40
+
41
+ def test_tsv
42
+ object = {:a => 1, :b => 2}
43
+ TmpFile.with_file do |f|
44
+ Persistence.persist("token_file", :Test, :tsv, :persistence_file => f) do
45
+ [object, {:fields => ["Number"], :key_field => "Letter", :type => :list, :filename => "foo"}]
46
+ end
47
+
48
+ assert File.exists? f
49
+ new, extra = Persistence.persist("token_file", :Test, :tsv, :persistence_file => f)
50
+
51
+ assert_equal 1, new["a"]
52
+ assert_equal "Letter", new.key_field
53
+
54
+ rm f
55
+ end
56
+ end
57
+
58
+
59
+ end
60
+
@@ -0,0 +1,113 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
+ require 'rbbt'
3
+ require 'rbbt/util/pkg_data'
4
+
5
+ class TestPKGData < Test::Unit::TestCase
6
+ def _test_claims
7
+ begin
8
+ assert Rbbt.claims.empty?
9
+ Rbbt.claim :foo, "bar"
10
+ assert_equal 1, Rbbt.claims.length
11
+ rescue
12
+ Rbbt.declaim Rbbt.files.foo
13
+ end
14
+ end
15
+
16
+ def _test_path
17
+ assert_equal File.join(Rbbt.datadir, 'Organism/Hsa'), Rbbt.files.Organism.Hsa
18
+ Rbbt.files.Organism.Hsa.identifiers.produce
19
+ end
20
+
21
+ def _test_claim_proc
22
+ begin
23
+ assert_nil Rbbt.reclaim(Rbbt.files.foo)
24
+
25
+ Rbbt.claim :foo, proc{"bar"}
26
+ assert_not_nil Rbbt.reclaim Rbbt.files.foo
27
+
28
+ assert Hash === Rbbt.reclaim(Rbbt.files.foo).last
29
+ assert_equal "bar", Rbbt.files.foo.read
30
+ ensure
31
+ Rbbt.declaim Rbbt.files.foo
32
+ FileUtils.rm Rbbt.files.foo
33
+ end
34
+ end
35
+
36
+ def _test_claim_cp
37
+ begin
38
+ Open.write File.join(Rbbt.rootdir, 'share', 'foo'), "bar"
39
+ Rbbt.claim :foo
40
+ assert_equal "bar", Rbbt.files.foo.read
41
+ ensure
42
+ Rbbt.declaim Rbbt.files.foo
43
+ FileUtils.rm Rbbt.files.foo if File.exists? Rbbt.files.foo
44
+ FileUtils.rm File.join(Rbbt.rootdir, 'share', 'foo') if File.exists? File.join(Rbbt.rootdir, 'share', 'foo')
45
+ end
46
+ end
47
+
48
+ def _test_claim_tsv
49
+ begin
50
+ Rbbt.claim :foo, TSV.new({:a => 1, :b => 2})
51
+ assert File.exists? Rbbt.files.foo
52
+ assert_equal "1", Rbbt.files.foo.tsv(:single => true)["a"]
53
+ ensure
54
+ FileUtils.rm Rbbt.files.foo if File.exists? Rbbt.files.foo
55
+ end
56
+ end
57
+
58
+ def _test_claim_rakefile
59
+ begin
60
+ FileUtils.mkdir_p File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/')
61
+ Open.write(File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile'), "file :foo do |t| Open.write(t.name, 'bar') end")
62
+ Rbbt.claim :foo, :Rakefile, 'test/Rake'
63
+ assert_equal "bar", Rbbt.files.test.Rake.foo.read
64
+ ensure
65
+ begin
66
+ FileUtils.rm File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile')
67
+ FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake')
68
+ FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test')
69
+ FileUtils.rm Rbbt.files.test.Rake.foo
70
+ FileUtils.rm_r Rbbt.files.test.Rake
71
+ FileUtils.rm_r Rbbt.files.test
72
+ rescue
73
+ end
74
+ end
75
+ end
76
+
77
+ def test_claim_rakefile2
78
+ begin
79
+ FileUtils.mkdir_p File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/')
80
+ Open.write(File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile'), "file :foo do |t| Open.write(t.name, 'bar') end")
81
+ Rbbt.claim :foo, "test/Rake/Rakefile", 'test'
82
+ assert_equal "bar", Rbbt.files.test.foo.read
83
+ ensure
84
+ begin
85
+ FileUtils.rm File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile')
86
+ FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake')
87
+ FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test')
88
+ FileUtils.rm Rbbt.files.test.foo
89
+ FileUtils.rm_r Rbbt.files.test
90
+ rescue
91
+ end
92
+ end
93
+ end
94
+
95
+ def _test_claim_rakefile3
96
+ begin
97
+ FileUtils.mkdir_p File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/')
98
+ Open.write(File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile'), "file :foo do |t| Open.write(t.name, 'bar') end")
99
+ Rbbt.claim :all, "test/Rake/Rakefile", 'test'
100
+ assert_equal "bar", Rbbt.files.test.foo.read
101
+ ensure
102
+ begin
103
+ FileUtils.rm File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake/Rakefile')
104
+ FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test/Rake')
105
+ FileUtils.rmdir File.join(PKGData.sharedir_for_file(__FILE__), 'test')
106
+ FileUtils.rm Rbbt.files.test.foo
107
+ FileUtils.rm_r Rbbt.files.test
108
+ rescue
109
+ end
110
+ end
111
+ end
112
+ end
113
+