bio-nexml 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'rake/testtask'
13
+ Rake::TestTask.new(:test) do |test|
14
+ test.libs << 'lib' << 'test'
15
+ test.pattern = 'test/**/test_*.rb'
16
+ test.verbose = true
17
+ end
18
+
19
+ # require 'rcov/rcovtask'
20
+ # Rcov::RcovTask.new do |test|
21
+ # test.libs << 'test'
22
+ # test.pattern = 'test/**/test_*.rb'
23
+ # test.verbose = true
24
+ # end
25
+
26
+ task :default => :test
27
+
28
+ require 'rake/rdoctask'
29
+ Rake::RDocTask.new do |rdoc|
30
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
31
+
32
+ rdoc.rdoc_dir = 'rdoc'
33
+ rdoc.title = "bio-nexml #{version}"
34
+ rdoc.rdoc_files.include('README*')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
@@ -0,0 +1,6 @@
1
+ * it would be good if objects could only be instantiated in the right nesting
2
+ order, e.g. you wouldn't do cell = Cell.new, but you'd do
3
+ cell = Row.create_cell and the nesting is automatically handled for you.
4
+ I think the way to do this is to require the containing object to be passed
5
+ (recursively) to the constructors, while create_foo internally does this,
6
+ passing self to the constructor.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,27 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ # meta
8
+ s.name = 'bio-nexml'
9
+ s.version = '1.1.0'
10
+ s.authors = ['Rutger Vos', 'Anurag Priyam']
11
+ s.email = ['rutgeraldo@gmail.com', 'anurag08priyam@gmail.com']
12
+ s.homepage = 'https://github.com/nexml/bio-nexml'
13
+ s.license = 'MIT'
14
+
15
+ s.summary = %q{BioRuby plugin for reading and writing NeXML (http://nexml.org)}
16
+ s.description = %q{This plugin reads, writes and generates NeXML}
17
+
18
+ # dependencies
19
+ s.add_dependency('bio', '>= 1.4.1')
20
+ s.add_dependency('libxml-ruby', '= 1.1.4')
21
+
22
+ # gem
23
+ s.files = `git ls-files`.split("\n") rescue ''
24
+ s.test_file = "test/test_bio-nexml.rb"
25
+ s.require_paths = ["lib"]
26
+ end
27
+
@@ -1,10 +1,11 @@
1
- #load XML library for parser and serializer
2
1
  require 'xml'
2
+ require 'bio/db/nexml/parser'
3
+ require 'bio/db/nexml/writer'
4
+ require 'bio/db/nexml/mapper'
5
+ require 'bio/db/nexml/taxa'
6
+ require 'bio/db/nexml/trees'
7
+ require 'bio/db/nexml/matrix'
3
8
 
4
- #load required class and module definitions
5
- require "bio/db/nexml/mapper"
6
-
7
- #Autoload definition
8
9
  module Bio
9
10
  module NeXML
10
11
 
@@ -24,20 +25,6 @@ module Bio
24
25
  attr_accessor :xml_space
25
26
  end #end module Base
26
27
 
27
- # Autoload multiple modules that reside in the same file.
28
- def self.mautoload( modules, file )
29
- modules.each do |m|
30
- autoload m, file
31
- end
32
- end
33
-
34
- mautoload %w|Otu Otus|, 'bio/db/nexml/taxa.rb'
35
- mautoload %w|Node Edge Tree Network Trees|, 'bio/db/nexml/trees.rb'
36
- mautoload %w|State Char States Cell Sequence Format Characters|, 'bio/db/nexml/matrix.rb'
37
-
38
- autoload :Parser, 'bio/db/nexml/parser'
39
- autoload :Writer, 'bio/db/nexml/writer'
40
-
41
28
  class Nexml
42
29
  @@writer = Bio::NeXML::Writer.new
43
30
  include Mapper
@@ -133,21 +133,18 @@ module Bio
133
133
  # def each( &block )
134
134
  # @targets.each( &block )
135
135
  # end
136
- class_eval <<_END_OF_EVAL_
137
- def each_#{name}( &block )
138
- repository( "#{target}", #{type} ).each( &block )
139
- end
140
- _END_OF_EVAL_
136
+ define_method("each_#{name}") do |&block|
137
+ repository(target, type).each( &block )
138
+ end
141
139
 
142
140
  if type == HashRepository
143
141
  define_method( "get_#{name}_by_id" ) do |id,|
144
142
  repository( target, type )[ id ]
145
143
  end
146
- class_eval <<_END_OF_EVAL_
147
- def each_#{name}_with_id( &block )
148
- repository( #{target }, #{type } ).each_with_id( &block )
144
+
145
+ define_method("each_#{name}_with_id") do |&block|
146
+ repository(target, type).each_with_id(&block)
149
147
  end
150
- _END_OF_EVAL_
151
148
  end
152
149
  end
153
150
 
@@ -0,0 +1,69 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1"?>
2
+ <nex:nexml
3
+ version="0.9"
4
+ generator="eclipse"
5
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6
+ xmlns:xml="http://www.w3.org/XML/1998/namespace"
7
+ xmlns:nex="http://www.nexml.org/2009"
8
+ xmlns="http://www.nexml.org/2009"
9
+ xsi:schemaLocation="http://www.nexml.org/2009 http://www.nexml.org/2009/nexml.xsd">
10
+
11
+ <otus id="taxa1" label="A taxa block">
12
+ <otu id="o1" label="A taxon"/>
13
+ <otu id="o2" label="A taxon"/>
14
+ </otus>
15
+ <trees otus="taxa1" id="trees1" label="A tree container">
16
+ <tree id="tree1" xsi:type="nex:FloatTree" label="A float tree">
17
+ <node id="n1" label="A node" root="true" otu="o1"/>
18
+ <node id="n2" label="A node"/>
19
+ <rootedge id="re1" label="A rootedge" target="n1" length="0.5"/>
20
+ <edge source="n1" target="n2" id="e1" length="0.4353" label="An edge"/>
21
+ </tree>
22
+ <network id="network1" xsi:type="nex:IntNetwork" label="An int network">
23
+ <node id="n1n1" label="A node" root="true" otu="o1"/>
24
+ <node id="n1n2" label="A node"/>
25
+ <edge source="n1n1" target="n1n2" id="n1e1" length="1" label="An edge"/>
26
+ <edge source="n1n2" target="n1n2" id="n1e2" length="0" label="An edge"/>
27
+ </network>
28
+ </trees>
29
+ <characters otus="taxa1" id="standardchars6" xsi:type="nex:StandardSeqs" label="Standard sequences">
30
+ <format>
31
+ <states id="sss1">
32
+ <state id="ss1" symbol="1"/>
33
+ <state id="ss2" symbol="2"/>
34
+ <polymorphic_state_set symbol="4" id="ss4">
35
+ <member state="ss1"/>
36
+ <member state="ss2"/>
37
+ </polymorphic_state_set>
38
+ <uncertain_state_set symbol="5" id="ss5">
39
+ <member state="ss2"/>
40
+ <member state="ss1"/>
41
+ </uncertain_state_set>
42
+ </states>
43
+ <char states="sss1" id="sc1"/>
44
+ <char states="sss1" id="sc2"/>
45
+ </format>
46
+ <matrix>
47
+ <row id="sr1" otu="o1">
48
+ <seq>1 2</seq>
49
+ </row>
50
+ <row id="sr2" otu="o2">
51
+ <seq>2 2</seq>
52
+ </row>
53
+ </matrix>
54
+ </characters>
55
+ <characters otus="taxa1" id="m2" xsi:type="nex:StandardCells" label="Categorical characters">
56
+ <format>
57
+ <states id="sss2">
58
+ <state id="ss6" symbol="1"/>
59
+ </states>
60
+ <char states="sss2" id="sc3"/>
61
+ </format>
62
+
63
+ <matrix>
64
+ <row id="sr3" otu="o1">
65
+ <cell char="sc3" state="ss6"/>
66
+ </row>
67
+ </matrix>
68
+ </characters>
69
+ </nex:nexml>
@@ -0,0 +1,119 @@
1
+ module Bio
2
+ module NeXML
3
+ class TestFactory < Test::Unit::TestCase
4
+ def setup
5
+ @nexml = Bio::NeXML::Nexml.new
6
+ @otus = @nexml.create_otus
7
+ end
8
+
9
+ def test_otus
10
+ assert @otus.kind_of? Bio::NeXML::Otus
11
+ end
12
+
13
+ def test_otu
14
+ otu = @otus.create_otu
15
+ assert otu.kind_of? Bio::NeXML::Otu
16
+ end
17
+
18
+ def test_trees
19
+ trees = @nexml.create_trees( :otus => @otus )
20
+ otu = @otus.create_otu
21
+ assert_equal @otus, trees.otus
22
+ assert trees.instance_of? Bio::NeXML::Trees
23
+
24
+ floattree = trees.create_tree
25
+ assert floattree.kind_of? Bio::NeXML::Tree
26
+ assert floattree.kind_of? Bio::Tree
27
+ assert floattree.kind_of? Bio::NeXML::FloatTree
28
+ floatnode = floattree.create_node( :otu => otu )
29
+ assert floatnode.kind_of? Bio::NeXML::Node
30
+ assert floatnode.kind_of? Bio::Tree::Node
31
+ assert_equal otu, floatnode.otu
32
+
33
+ floatedge = floattree.create_edge( :source => floatnode, :target => floatnode )
34
+ assert floatedge.kind_of? Bio::NeXML::Edge
35
+ assert floatedge.kind_of? Bio::Tree::Edge
36
+ assert floatedge.kind_of? Bio::NeXML::FloatEdge
37
+ floatrootedge = floattree.create_rootedge( :target => floatnode )
38
+ assert floatrootedge.kind_of? Bio::NeXML::Edge
39
+ assert floatrootedge.kind_of? Bio::Tree::Edge
40
+ assert floatrootedge.kind_of? Bio::NeXML::RootEdge
41
+ assert floatrootedge.kind_of? Bio::NeXML::FloatRootEdge
42
+
43
+ inttree = trees.create_tree( true )
44
+ assert inttree.kind_of? Bio::NeXML::Tree
45
+ assert inttree.kind_of? Bio::Tree
46
+ assert inttree.kind_of? Bio::NeXML::IntTree
47
+ intedge = inttree.create_edge
48
+ assert intedge.kind_of? Bio::NeXML::Edge
49
+ assert intedge.kind_of? Bio::Tree::Edge
50
+ assert intedge.kind_of? Bio::NeXML::IntEdge
51
+ introotedge = inttree.create_rootedge
52
+ assert introotedge.kind_of? Bio::NeXML::Edge
53
+ assert introotedge.kind_of? Bio::Tree::Edge
54
+ assert introotedge.kind_of? Bio::NeXML::RootEdge
55
+ assert introotedge.kind_of? Bio::NeXML::IntRootEdge
56
+
57
+ floatnetwork = trees.create_network
58
+ assert floatnetwork.kind_of? Bio::NeXML::Tree
59
+ assert floatnetwork.kind_of? Bio::Tree
60
+ assert floatnetwork.kind_of? Bio::NeXML::FloatNetwork
61
+ floatnedge = floatnetwork.create_edge
62
+ assert floatnedge.kind_of? Bio::NeXML::Edge
63
+ assert floatnedge.kind_of? Bio::Tree::Edge
64
+ assert floatnedge.kind_of? Bio::NeXML::FloatEdge
65
+
66
+ intnetwork = trees.create_network( true )
67
+ assert intnetwork.kind_of? Bio::NeXML::Tree
68
+ assert intnetwork.kind_of? Bio::Tree
69
+ assert intnetwork.kind_of? Bio::NeXML::IntNetwork
70
+ intnedge = intnetwork.create_edge
71
+ assert intnedge.kind_of? Bio::NeXML::Edge
72
+ assert intnedge.kind_of? Bio::Tree::Edge
73
+ assert intnedge.kind_of? Bio::NeXML::IntEdge
74
+ end
75
+ def test_characters_seqs
76
+ otu = @otus.create_otu
77
+ dnaseqs = @nexml.create_characters( "Dna", false, :otus => @otus )
78
+ assert dnaseqs.kind_of? Bio::NeXML::Characters
79
+ assert dnaseqs.kind_of? Bio::NeXML::Dna
80
+ assert dnaseqs.kind_of? Bio::NeXML::DnaSeqs
81
+ assert_equal @otus, dnaseqs.otus
82
+
83
+ format = dnaseqs.create_format
84
+ assert format.kind_of? Bio::NeXML::Format
85
+ assert_equal format, dnaseqs.format
86
+
87
+ matrix = dnaseqs.create_matrix
88
+ assert matrix.kind_of? Bio::NeXML::Matrix
89
+ assert matrix.kind_of? Bio::NeXML::SeqMatrix
90
+ assert_equal matrix, dnaseqs.matrix
91
+
92
+ row = matrix.create_row( :otu => otu )
93
+ assert row.kind_of? Bio::NeXML::Row
94
+ assert row.kind_of? Bio::NeXML::SeqRow
95
+ assert_equal otu, row.otu
96
+
97
+ seq = 'ACATGCAG'
98
+ newrow = dnaseqs.create_raw( seq )
99
+ assert_equal newrow.sequences.first.value, seq
100
+ end
101
+ def test_characters_cells
102
+ otu1 = @otus.create_otu
103
+ otu2 = @otus.create_otu
104
+ standardcells = @nexml.create_characters( "Standard", true, :otus => @otus )
105
+ newrow1 = standardcells.create_raw('1 2 3 4 5')
106
+ newrow1.otu = otu1
107
+ newrow2 = standardcells.create_raw('1 2 3 4 5')
108
+ newrow2.otu = otu2
109
+ assert_equal otu1, newrow1.otu
110
+ assert_equal otu2, newrow2.otu
111
+ assert newrow1.kind_of? Bio::NeXML::Row
112
+ assert newrow1.kind_of? Bio::NeXML::CellRow
113
+ assert standardcells.kind_of? Bio::NeXML::Characters
114
+ assert standardcells.kind_of? Bio::NeXML::Standard
115
+ assert standardcells.kind_of? Bio::NeXML::StandardCells
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,78 @@
1
+ module Bio
2
+ module NeXML
3
+ class TestMapper < Test::Unit::TestCase
4
+
5
+ class Target
6
+ include Bio::NeXML::Mapper
7
+
8
+ attr_accessor :id
9
+ belongs_to :source
10
+ end
11
+
12
+ class Source
13
+ include Bio::NeXML::Mapper
14
+
15
+ attr_accessor :id
16
+ has_n :targets
17
+ end
18
+
19
+ def setup
20
+ @s = Source.new
21
+ @t1 = Target.new
22
+ @t1.id = 't1'
23
+ @t2 = Target.new
24
+ @t2.id = 't2'
25
+ @s.add_target( @t1 )
26
+ end
27
+
28
+ def test_has_target
29
+ t2 = Target.new
30
+ assert @s.has_target?( @t1 )
31
+ assert !@s.has_target?( t2 )
32
+ end
33
+
34
+ def test_get_target_by_id
35
+ assert_equal( @t1, @s.get_target_by_id( 't1' ) )
36
+ assert_nil( @s.get_target_by_id( 't2' ) )
37
+ end
38
+
39
+ def test_add_target
40
+ t2 = Target.new
41
+ t2.id = 't2'
42
+ @s.add_target( t2 )
43
+
44
+ assert @s.has_target?( t2 )
45
+ assert_equal( @s, t2.source )
46
+ end
47
+
48
+ def test_target
49
+ assert_equal( [ @t1 ], @s.targets )
50
+ end
51
+
52
+ def test_target=
53
+ t2 = Target.new
54
+ @s.targets = [ t2 ]
55
+ assert_equal( [ t2 ], @s.targets )
56
+ assert_equal( @s, t2.source )
57
+ end
58
+
59
+ def test_delete_target
60
+ assert_equal( @t1, @s.delete_target( @t1 ) )
61
+ assert_nil( @t1.source )
62
+ end
63
+
64
+ def test_source
65
+ assert_equal( @s, @t1.source )
66
+ assert( @s.has_target?( @t1 ) )
67
+ end
68
+
69
+ def test_source=
70
+ t2 = Target.new
71
+ t2.id = 't2'
72
+ t2.source = @s
73
+ assert_equal( @s, t2.source )
74
+ assert( @s.has_target?( t2 ) )
75
+ end
76
+ end #end TestOtu
77
+ end #end NeXML
78
+ end #end Bio
@@ -0,0 +1,551 @@
1
+ module Bio
2
+ module NeXML
3
+ class TestState < Test::Unit::TestCase
4
+ def setup
5
+ @state = Bio::NeXML::State.new( 'stateA', 'A' )
6
+ end
7
+
8
+ def test_new1
9
+ # if one argument given use it as id
10
+ s = Bio::NeXML::State.new( 'stateA' )
11
+ assert_equal 'stateA', s.id
12
+ end
13
+
14
+ def test_new2
15
+ # if two argument given use them as id and sybmol respectively
16
+ s = Bio::NeXML::State.new( 'stateA', 'A' )
17
+ assert_equal 'stateA', s.id
18
+ assert_equal 'A', s.symbol
19
+ end
20
+
21
+ def test_new3
22
+ # options hash as third argument
23
+ # preferred
24
+ s = Bio::NeXML::State.new( 'stateA', 'A', :label => 'A label' )
25
+ assert_equal 'stateA', s.id
26
+ assert_equal 'A', s.symbol
27
+ assert_equal 'A label', s.label
28
+ end
29
+
30
+ def test_new4
31
+ # options hash as second argument
32
+ s = Bio::NeXML::State.new( 'stateA', :symbol => 'A', :label => 'A label' )
33
+ assert_equal 'stateA', s.id
34
+ assert_equal 'A', s.symbol
35
+ assert_equal 'A label', s.label
36
+ end
37
+
38
+ def test_id
39
+ @state.id = 'state1'
40
+ assert_equal 'state1', @state.id
41
+ end
42
+
43
+ def test_symbol
44
+ @state.symbol = 1
45
+ assert_equal 1, @state.symbol
46
+ end
47
+
48
+ def test_label
49
+ @state.label = 'State'
50
+ assert_equal 'State', @state.label
51
+ end
52
+
53
+ def test_ambiguous
54
+ assert !@state.ambiguous?
55
+ end
56
+
57
+ def test_ambiguity
58
+ assert_nil @state.ambiguity
59
+ end
60
+
61
+ def test_to_str
62
+ assert_equal 'A', @state.to_str
63
+ end
64
+ end
65
+
66
+ class TestAmbiguousState < TestState
67
+ def setup
68
+ @members = [
69
+ %w|s1 A|,
70
+ %w|s2 B|,
71
+ %w|s3 C|,
72
+ %w|s4 D|,
73
+ %w|s5 E|
74
+ ].map { | pair | Bio::NeXML::State.new( *pair ) }
75
+
76
+ @state = Bio::NeXML::State.uncertain( 'state1', '?' )
77
+ @state.members = @members
78
+ end
79
+
80
+ def test_new5
81
+ s = Bio::NeXML::State.new( 'stateA', 'A', :ambiguity => :polymorphic )
82
+ assert s.ambiguous?
83
+ assert_equal :polymorphic, s.ambiguity
84
+ end
85
+
86
+ def test_new6
87
+ s = Bio::NeXML::State.new( 'stateA', 'A', :ambiguity => :uncertain, :members => @members )
88
+ assert_equal @members, s.members
89
+ end
90
+
91
+ def test_uncertain
92
+ s = Bio::NeXML::State.uncertain( 'stateA', 'A', :members => @members )
93
+ assert s.ambiguous?
94
+ assert_equal :uncertain, s.ambiguity
95
+ assert_equal @members, s.members
96
+ end
97
+
98
+ def test_polymorphic
99
+ s = Bio::NeXML::State.polymorphic( 'stateA', 'A', :members => @members )
100
+ assert s.ambiguous?
101
+ assert_equal :polymorphic, s.ambiguity
102
+ assert_equal @members, s.members
103
+ end
104
+
105
+ def test_ambiguous
106
+ assert @state.ambiguous?
107
+ end
108
+
109
+ def test_ambiguity
110
+ assert_equal :uncertain, @state.ambiguity
111
+ end
112
+
113
+ def test_add_member
114
+ s = Bio::NeXML::State.new( 's6', 'F' )
115
+ @state.add_member( s )
116
+ assert @state.has_member?( s )
117
+ assert_equal @state, s.state_set
118
+ end
119
+
120
+ def test_delete_member
121
+ s = @members[ 0 ]
122
+ assert_equal( s, @state.delete_member( s ) )
123
+ assert_nil s.state_set
124
+ end
125
+
126
+ def test_members
127
+ s = @members[ 0 ]
128
+ @state.members = [ s ]
129
+ assert_equal [ s ], @state.members
130
+ assert_equal @state, s.state_set
131
+ end
132
+
133
+ def test_include
134
+ assert @state.include?( @members[ 0 ] )
135
+ end
136
+
137
+ def test_count
138
+ assert_equal @members.length, @state.count
139
+ end
140
+
141
+ def test_each
142
+ end
143
+
144
+ def test_each_with_symbol
145
+ end
146
+
147
+ def test_to_str
148
+ assert_equal '?', @state.to_str
149
+ end
150
+ end #end class TestAmbiguousState
151
+
152
+ class TestChar < Test::Unit::TestCase
153
+ def setup
154
+ @states = Bio::NeXML::States.new( 'states' )
155
+ @char = Bio::NeXML::Char.new( 'char1' )
156
+ end
157
+
158
+ def test_new1
159
+ # if one argument given use it as id
160
+ c = Bio::NeXML::Char.new( 'char1' )
161
+ assert_equal 'char1', c.id
162
+ end
163
+
164
+ def test_new2
165
+ # if two arguments given use the second as states
166
+ c = Bio::NeXML::Char.new( 'char1', @states )
167
+ assert_equal 'char1', c.id
168
+ assert_equal @states, c.states
169
+ end
170
+
171
+ def test_new3
172
+ # 3rd argument as optional hash
173
+ c = Bio::NeXML::Char.new( 'char1', @states, :label => 'A label' )
174
+ assert_equal 'char1', c.id
175
+ assert_equal @states, c.states
176
+ assert_equal 'A label', c.label
177
+ end
178
+
179
+ def test_new4
180
+ # 2nd argument as optional hash
181
+ c = Bio::NeXML::Char.new( 'char1', :states => @states, :label => 'A label' )
182
+ assert_equal 'char1', c.id
183
+ assert_equal @states, c.states
184
+ assert_equal 'A label', c.label
185
+ end
186
+
187
+ def test_id
188
+ @char.id = 'char2'
189
+ assert_equal 'char2', @char.id
190
+ end
191
+
192
+ def test_label
193
+ @char.label = 'A label'
194
+ assert_equal 'A label', @char.label
195
+ end
196
+
197
+ def test_states
198
+ @char.states = @states
199
+ assert_equal @states, @char.states
200
+ assert @states.has_char?( @char )
201
+ end
202
+
203
+ def test_cells
204
+ cell = Bio::NeXML::Cell.new( @char )
205
+ assert_equal [ cell ], @char.cells
206
+ end
207
+ end
208
+
209
+ class TestStates < Test::Unit::TestCase
210
+ def setup
211
+ @ss = [
212
+ %w|s1 A|,
213
+ %w|s2 B|,
214
+ %w|s3 C|,
215
+ %w|s4 D|,
216
+ %w|s5 E|
217
+ ].map { | pair | Bio::NeXML::State.new( *pair ) }
218
+ @states = Bio::NeXML::States.new( 'states', :states => @ss )
219
+ end
220
+
221
+ def test_new1
222
+ # one argument => id
223
+ states = Bio::NeXML::States.new( 'states' )
224
+ assert_equal 'states', states.id
225
+ end
226
+
227
+ def test_new2
228
+ # second argument => optional hash
229
+ states = Bio::NeXML::States.new( 'states', :label => 'state container' )
230
+ assert_equal 'states', states.id
231
+ assert_equal 'state container', states.label
232
+ end
233
+
234
+ def test_id
235
+ @states.id = 'states1'
236
+ assert_equal 'states1', @states.id
237
+ end
238
+
239
+ def test_label
240
+ @states.label = 'a label'
241
+ assert_equal 'a label', @states.label
242
+ end
243
+
244
+ def test_add_state
245
+ s = Bio::NeXML::State.new( 's6', 'F' )
246
+ @states.add_state( s )
247
+ assert @states.include?( s )
248
+ assert_equal @states, s.states
249
+ end
250
+
251
+ def test_delete_state
252
+ s = @ss[ 0 ]
253
+ assert_equal s, @states.delete_state( s )
254
+ assert_nil s.states
255
+ end
256
+
257
+ def test_states
258
+ s = @ss[ 0 ]
259
+ @states.states = [ s ]
260
+ assert_equal [ s ], @states.states
261
+ assert_equal @states, s.states
262
+ end
263
+
264
+ def test_get_state_by_id
265
+ s = @ss[ 0 ]
266
+ assert_equal s, @states.get_state_by_id( s.id )
267
+ end
268
+
269
+ def test_has_state
270
+ assert @states.has_state?( @ss[ 0 ] )
271
+ end
272
+
273
+ def test_chars
274
+ char = Bio::NeXML::Char.new( 'char', @states )
275
+ assert_equal @states, char.states
276
+ assert_equal [ char ], @states.chars
277
+ end
278
+ end
279
+
280
+ class TestCell_new < Test::Unit::TestCase
281
+ # test Bio::NeXML::Cell.new as a cell can be initialized in mulitple ways
282
+ # depending on type( bound/ unbound ) and need.
283
+
284
+ def test_new1
285
+ # unbound cell, no options
286
+ cell = Bio::NeXML::Cell.new( 'A' )
287
+ assert_equal 'A', cell.value
288
+ end
289
+
290
+ def test_new2
291
+ # unbound cell, with options
292
+ cell = Bio::NeXML::Cell.new( 'A', :label => 'label' )
293
+ assert_equal 'A', cell.value
294
+ assert_equal 'label', cell.label
295
+ end
296
+
297
+ def test_new3
298
+ # bound cell, no options
299
+ ch = Bio::NeXML::Char.new( 'ch' )
300
+ s = Bio::NeXML::State.new( 'ss' )
301
+ cell = Bio::NeXML::Cell.new( ch, s )
302
+ assert_equal ch, cell.char
303
+ assert_equal s, cell.state
304
+ end
305
+
306
+ def test_new4
307
+ # bound cell, with options
308
+ ch = Bio::NeXML::Char.new( 'ch' )
309
+ s = Bio::NeXML::State.new( 'ss' )
310
+ cell = Bio::NeXML::Cell.new( ch, s, :label => 'label' )
311
+ assert_equal ch, cell.char
312
+ assert_equal s, cell.state
313
+ assert_equal 'label', cell.label
314
+ end
315
+
316
+ def test_new5
317
+ # all keyword args
318
+ ch = Bio::NeXML::Char.new( 'ch' )
319
+ s = Bio::NeXML::State.new( 'ss' )
320
+ cell = Bio::NeXML::Cell.new( :char => ch, :state => s, :label => 'label' )
321
+ assert_equal ch, cell.char
322
+ assert_equal s, cell.state
323
+ assert_equal 'label', cell.label
324
+ end
325
+ end
326
+
327
+ class TestUnboundCell < Test::Unit::TestCase
328
+ def setup
329
+ @cell = Bio::NeXML::Cell.new( 'A', :label => 'label' )
330
+ end
331
+
332
+ def test_state
333
+ assert_nil @cell.state
334
+ end
335
+
336
+ def test_char
337
+ assert_nil @cell.char
338
+ end
339
+
340
+ def test_bound
341
+ assert !@cell.bound?
342
+ end
343
+
344
+ def test_value
345
+ @cell.value = 'B'
346
+ assert_equal 'B', @cell.value
347
+ end
348
+ end
349
+
350
+ class TestBoundCell < Test::Unit::TestCase
351
+ def setup
352
+ @stateA = Bio::NeXML::State.new( 'stateA', 'A' )
353
+ @char = Bio::NeXML::Char.new( 'char' )
354
+ @cell = Bio::NeXML::Cell.new( :char => @char, :state => @stateA )
355
+ end
356
+
357
+ def test_state
358
+ s = Bio::NeXML::State.new( 'stateB', 'B' )
359
+ @cell.state = s
360
+ assert_equal s, @cell.state
361
+ end
362
+
363
+ def test_char
364
+ c = Bio::NeXML::Char.new( 'ch' )
365
+ @cell.char = c
366
+ assert_equal c, @cell.char
367
+ end
368
+
369
+ def test_value
370
+ assert_equal @stateA.symbol, @cell.value
371
+ @cell.value = 'B'
372
+ assert_not_equal 'B', @cell.value # since it is a bound cell
373
+ @stateA.symbol = 'B'
374
+ assert_equal 'B', @cell.value # a bound cell reflects the value of its state
375
+ end
376
+ end
377
+
378
+ class TestRow < Test::Unit::TestCase
379
+ def setup
380
+ @row = Bio::NeXML::Row.new( 'seq' )
381
+ end
382
+
383
+ def test_id
384
+ @row.id = 'seq1'
385
+ assert_equal 'seq1', @row.id
386
+ end
387
+
388
+ def test_label
389
+ @row.label = 'a sequence'
390
+ assert_equal 'a sequence', @row.label
391
+ end
392
+
393
+ def test_otu
394
+ otu = Bio::NeXML::Otu.new( 'otu' )
395
+ @row.otu = otu
396
+ assert_equal otu, @row.otu
397
+ assert_equal [ @row ], otu.rows
398
+ end
399
+ end
400
+
401
+ class TestMatrix < Test::Unit::TestCase
402
+ def setup
403
+ @ss = [
404
+ %w|s1 A|,
405
+ %w|s2 B|,
406
+ %w|s3 C|,
407
+ %w|s4 D|,
408
+ %w|s5 E|
409
+ ].map { | pair | Bio::NeXML::State.new( *pair ) }
410
+ @states = Bio::NeXML::States.new( 'states', :states => @ss )
411
+
412
+ @char1 = Bio::NeXML::Char.new( 'char1', :states => @states )
413
+ @char2 = Bio::NeXML::Char.new( 'char2', :states => @states )
414
+
415
+ @sequence = Bio::NeXML::Sequence.new( :value => 'ABCDE' )
416
+ @row = Bio::NeXML::SeqRow.new( 'row1' )
417
+ @matrix = Bio::NeXML::Protein.new( 'matrix1' )
418
+ @format = Bio::NeXML::Format.new
419
+ @format.add_states( @states )
420
+ @format.chars = [ @char1, @char2 ]
421
+ @row.add_sequence( @sequence )
422
+ end
423
+
424
+ def test_id
425
+ @matrix.id = 'id'
426
+ assert_equal 'id', @matrix.id
427
+ end
428
+
429
+ def test_label
430
+ @matrix.label = 'label'
431
+ assert_equal 'label', @matrix.label
432
+ end
433
+
434
+ def test_nexml; end
435
+
436
+ def test_otus
437
+ otus = Bio::NeXML::Otus.new( 'otus' )
438
+ @matrix.otus = otus
439
+ assert_equal otus, @matrix.otus
440
+ assert_equal [ @matrix ], otus.characters
441
+ end
442
+
443
+ def test_add_states
444
+ ss = Bio::NeXML::States.new( 'ss' )
445
+ @format.add_states( ss )
446
+ assert @format.has_states?( ss )
447
+ assert_equal @format, ss.format
448
+ end
449
+
450
+ def test_add_char
451
+ ch = Bio::NeXML::Char.new( 'ch' )
452
+ @format.add_char( ch )
453
+ assert @format.has_char?( ch )
454
+ assert_equal @format, ch.format
455
+ end
456
+
457
+ def test_add_sequence
458
+ seq = Bio::NeXML::Sequence.new
459
+ @row.add_sequence( seq )
460
+ assert @row.has_sequence?( seq )
461
+ assert_equal @row, seq.seqrow
462
+ end
463
+
464
+ def test_delete_states
465
+ assert_equal @states, @format.delete_states( @states )
466
+ assert !@format.has_states?( @states )
467
+ assert_nil @states.format
468
+ end
469
+
470
+ def test_delete_char
471
+ assert_equal @char1, @format.delete_char( @char1 )
472
+ assert !@format.has_char?( @char1 )
473
+ assert_nil @char1.format
474
+ end
475
+
476
+ def test_delete_sequence
477
+ assert_equal @sequence, @row.delete_sequence( @sequence )
478
+ assert !@row.has_sequence?( @sequence )
479
+ assert_nil @sequence.row
480
+ end
481
+
482
+ def test_states
483
+ assert_equal [ @states ], @format.states
484
+ ss = Bio::NeXML::States.new( 'ss' )
485
+ @format.states = [ ss ]
486
+ assert_equal [ ss ], @format.states
487
+ end
488
+
489
+ def test_chars
490
+ assert_equal [ @char1, @char2 ], @format.chars
491
+ ch = Bio::NeXML::Char.new( 'ch' )
492
+ @format.chars = [ ch ]
493
+ assert_equal [ ch ], @format.chars
494
+ end
495
+
496
+ def test_sequences
497
+ assert_equal [ @sequence ], @row.sequences
498
+ seq = Bio::NeXML::Sequence.new
499
+ @row.sequences = [ seq ]
500
+ assert_equal [ seq ], @row.sequences
501
+ end
502
+
503
+ def test_get_states_by_id
504
+ assert_equal @states, @format.get_states_by_id( 'states' )
505
+ end
506
+
507
+ def test_get_char_by_id
508
+ assert_equal @char1, @format.get_char_by_id( 'char1' )
509
+ end
510
+
511
+ def test_each_states
512
+ c = 0
513
+ @format.each_states do |s|
514
+ assert @format.has_states?( s )
515
+ c += 1
516
+ end
517
+ assert_equal 1, c
518
+ end
519
+
520
+ def test_each_char
521
+ c = 0
522
+ @format.each_char do |ch|
523
+ assert @format.has_char?( ch )
524
+ c += 1
525
+ end
526
+ assert_equal 2, c
527
+ end
528
+
529
+ def test_each_sequence
530
+ c = 0
531
+ @row.each_sequence do |s|
532
+ assert @row.has_sequence?( s )
533
+ c += 1
534
+ end
535
+ assert_equal 1, c
536
+ end
537
+
538
+ def test_number_of_states
539
+ assert_equal 1, @format.number_of_states
540
+ end
541
+
542
+ def test_number_of_chars
543
+ assert_equal 2, @format.number_of_chars
544
+ end
545
+
546
+ def test_number_of_sequences
547
+ assert_equal 1, @row.number_of_sequences
548
+ end
549
+ end
550
+ end #end module NeXML
551
+ end #end module Bio