File: test_codeml.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (175 lines) | stat: -rw-r--r-- 5,439 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# test/unit/bio/appl/paml/test_codeml.rb - Unit test for Bio::PAML::Codeml
#
# Copyright::  Copyright (C) 2008 Michael D. Barton <mail@michaelbarton.me.uk>
# License::    The Ruby License
#

# loading helper routine for testing bioruby
require 'pathname'
load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 4,
                            'bioruby_test_helper.rb')).cleanpath.to_s

# libraries needed for the tests
require 'test/unit'
require 'bio/appl/paml/codeml'

module Bio; module TestPAMLCodeml
  module TestCodemlData

    TEST_DATA = Pathname.new(File.join(BioRubyTestDataPath, 'paml', 'codeml')).cleanpath.to_s

    def self.example_control
      File.join TEST_DATA, 'control_file.txt'
    end

  end #module TestCodemlData

  class TestCodemlInitialize < Test::Unit::TestCase

    def test_new_with_one_argument
      factory = Bio::PAML::Codeml.new('echo')
      assert_instance_of(Bio::PAML::Codeml, factory)
      assert_equal('echo', factory.instance_eval { @program })
    end

    def test_new_with_two_argument
      factory = Bio::PAML::Codeml.new('echo', { :test => 'value' })
      assert_instance_of(Bio::PAML::Codeml, factory)
      assert_equal('echo', factory.instance_eval { @program })
      assert_equal('value', factory.parameters[:test])
    end

    def test_new_with_parameters
      factory = Bio::PAML::Codeml.new(nil, { :test => 'value' })
      assert_instance_of(Bio::PAML::Codeml, factory)
      assert_equal('codeml', factory.instance_eval { @program })
      assert_equal('value', factory.parameters[:test])
    end

    def test_new_without_argument
      factory = Bio::PAML::Codeml.new
      assert_instance_of(Bio::PAML::Codeml, factory)
      assert_equal('codeml', factory.instance_eval { @program })
    end

  end #class TestCodemlInitialize

  class TestCodeml < Test::Unit::TestCase
    def setup
      @codeml = Bio::PAML::Codeml.new
    end

    def test_parameters
      params = { :verbose => 1 }
      @codeml.parameters = params
      assert_equal(params, @codeml.parameters)
    end

    def test_load_parameters
      str = " seqfile = test.aa \n verbose = 1 \n"
      params = { :seqfile => 'test.aa', :verbose => '1' }
      assert_equal(params, @codeml.load_parameters(str))
    end

    def test_set_default_parameters
      assert_equal(Bio::PAML::Codeml::DEFAULT_PARAMETERS,
                   @codeml.set_default_parameters)
      # modifying parameters should not affect DEFAULT_PARAMETERS
      @codeml.parameters[:only_for_test] = 'this is test'
      assert_not_equal(Bio::PAML::Codeml::DEFAULT_PARAMETERS,
                       @codeml.parameters)
    end

    def test_dump_parameters
      params = { :seqfile => 'test.aa', :verbose => '1' }
      @codeml.parameters = params
      assert_equal("seqfile = test.aa\nverbose = 1\n",
                   @codeml.dump_parameters)
    end

  end #class TestCodeml

  class TestCodemlControlGeneration < Test::Unit::TestCase

    TEST_DATA = TestCodemlData::TEST_DATA

    def generate_control_file
      @tempfile_control = Tempfile.new('codeml_control')
      @tempfile_control.close(false)
      @tempfile_outfile = Tempfile.new('codeml_test')
      @tempfile_outfile.close(false)

      test_control = @tempfile_control.path
      Bio::PAML::Codeml.create_control_file({
        :model       => 1,
        :fix_kappa   => 1,
        :aaRatefile  => File.join(TEST_DATA, 'wag.dat'),
        :seqfile     => File.join(TEST_DATA, 'abglobin.aa'),
        :treefile    => File.join(TEST_DATA, 'abglobin.trees'),
        :outfile     => @tempfile_outfile.path,
      }, test_control)
      test_control
    end
    private :generate_control_file

    def setup
      @example_control = generate_control_file
    end

    def teardown
      @tempfile_control.close(true)
      @tempfile_outfile.close(true)
    end

    def test_control_file_generated
      assert_not_nil(File.size?(@example_control))
    end

    def test_expected_parameters_set_in_control_file
      produced_control = File.open(@example_control) do |f|
        f.inject(Hash.new) do |hash,line|
          hash.store(*line.strip.split(' = '))
          hash
        end
      end
      assert_equal(File.join(TEST_DATA, 'abglobin.aa'),
                   produced_control['seqfile'])
      assert_equal('1', produced_control['fix_kappa'])
      assert_equal('1', produced_control['model'])
    end
  end #class TestCodemlControlGeneration

  class TestControlFileUsage < Test::Unit::TestCase
    
    def setup
      @codeml = Bio::PAML::Codeml.new
      @codeml.load_parameters(File.read(TestCodemlData.example_control))
    end

    def test_parameters_should_be_loaded_from_control
      assert_not_nil(@codeml.parameters)
    end

    def test_correct_parameters_should_be_loaded
      assert_equal('abglobin.aa', @codeml.parameters[:seqfile])
      assert_equal('1', @codeml.parameters[:fix_kappa])
      assert_equal('1', @codeml.parameters[:model])
    end

  end #class TestControlFileUsage

  class TestExpectedErrorsThrown < Test::Unit::TestCase
  
    def test_error_thrown_if_seqfile_does_not_specified
      codeml = Bio::PAML::Codeml.new('echo')
      codeml.load_parameters(File.read(TestCodemlData.example_control))
      codeml.parameters[:seqfile] = nil
      assert_raises RuntimeError do
        codeml.query_by_string()
      end
    end
  
  end #class TestExpectedErrorsThrown

end; end #module TestPAMLCodeml; module Bio