bio-phyloxml 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,334 @@
1
+ #
2
+ # = test/unit/bio/test_phyloxml_writer.rb - Unit test for Bio::PhyloXML::Writer
3
+ #
4
+ # Copyright:: Copyright (C) 2009
5
+ # Diana Jaunzeikare <latvianlinuxgirl@gmail.com>
6
+ # License:: The Ruby License
7
+ #
8
+
9
+ # loading helper routine for testing bioruby
10
+ require 'pathname'
11
+ load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 2,
12
+ 'helper.rb')).cleanpath.to_s
13
+
14
+ # libraries needed for the tests
15
+ require 'test/unit'
16
+ require 'singleton'
17
+ require 'bio/command'
18
+
19
+ begin
20
+ require 'libxml'
21
+ rescue LoadError
22
+ end
23
+
24
+ if defined?(LibXML) then
25
+ require 'bio/phyloxml'
26
+ end
27
+
28
+ module Bio
29
+ class TestPhyloXMLWriter_Check_LibXML < Test::Unit::TestCase
30
+ def test_libxml
31
+ assert(defined?(LibXML),
32
+ "Error: libxml-ruby library is not present. Please install libxml-ruby library. It is needed for Bio::PhyloXML module. Unit test for PhyloXML will not be performed.")
33
+ end
34
+ end #class TestPhyloXMLWriter_Check_LibXML
35
+ end #module Bio
36
+
37
+ module Bio
38
+
39
+ module TestPhyloXMLWriterData
40
+
41
+ PHYLOXML_WRITER_TEST_DATA = Pathname.new(File.join(BioRubyTestDataPath, 'phyloxml')).cleanpath.to_s
42
+
43
+ def self.example_xml
44
+ File.join PHYLOXML_WRITER_TEST_DATA, 'phyloxml_examples.xml'
45
+ end
46
+
47
+ def self.mollusca_short_xml
48
+ File.join PHYLOXML_WRITER_TEST_DATA, 'ncbi_taxonomy_mollusca_short.xml'
49
+ end
50
+
51
+ def self.made_up_xml
52
+ File.join PHYLOXML_WRITER_TEST_DATA, 'made_up.xml'
53
+ end
54
+
55
+ end #end module TestPhyloXMLWriterData
56
+
57
+ class TestPhyloXMLWriter < Test::Unit::TestCase
58
+
59
+ # helper class to write files using temporary directory
60
+ class WriteTo
61
+ include Singleton
62
+
63
+ def initialize
64
+ @leave_tmpdir = ENV['BIORUBY_TEST_DEBUG'].to_s.empty? ? false : true
65
+ @tests = nil
66
+ @tests_passed = 0
67
+ @tmpdir = nil
68
+ end
69
+
70
+ attr_accessor :tests
71
+
72
+ def test_passed
73
+ @tests_passed += 1
74
+ if !@leave_tmpdir and @tmpdir and @tests and
75
+ @tests_passed >= @tests then
76
+ #$stderr.print "Removing #{@tmpdir.path}\n"
77
+ @tmpdir.close!
78
+ @tmpdir = nil
79
+ @tests_passed = 0
80
+ end
81
+ end
82
+
83
+ def tmpdir
84
+ @tmpdir ||= Bio::Command::Tmpdir.new('PhyloXML')
85
+ @tmpdir
86
+ end
87
+
88
+ def file(f)
89
+ File.join(self.tmpdir.path, f)
90
+ end
91
+
92
+ def example_xml_test
93
+ self.file('phyloxml_examples_written.xml')
94
+ end
95
+ end #class WriteTo
96
+
97
+ def setup
98
+ @writeto = WriteTo.instance
99
+ @writeto.tests ||= self.methods.collect { |x|
100
+ x.to_s }.find_all { |y|
101
+ /\Atest\_/ =~ y }.size
102
+ end
103
+
104
+ def teardown
105
+ @writeto.test_passed
106
+ end
107
+
108
+ # def test_write
109
+ # # @todo this is test for Tree.write
110
+ # tree = Bio::PhyloXML::Tree.new
111
+ # filename = @writeto.file('test.xml')
112
+ # tree.write(filename)
113
+ # end
114
+
115
+ def test_init
116
+ filename = @writeto.file("test2.xml")
117
+ writer = Bio::PhyloXML::Writer.new(filename)
118
+
119
+ tree = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.mollusca_short_xml) { |px| px.next_tree }
120
+
121
+ writer.write(tree)
122
+
123
+ assert_nothing_thrown do
124
+ Bio::PhyloXML::Parser.open(filename) { |px| true }
125
+ end
126
+
127
+ #File.delete(filename)
128
+ end
129
+
130
+ def test_simple_xml
131
+ filename = @writeto.file("sample.xml")
132
+ writer = Bio::PhyloXML::Writer.new(filename)
133
+ tree = Bio::PhyloXML::Tree.new
134
+ tree.rooted = true
135
+ tree.name = "Test tree"
136
+ root_node = Bio::PhyloXML::Node.new
137
+ tree.root = root_node
138
+ root_node.name = "A"
139
+ #root_node.taxonomies[0] = Bio::PhyloXML::Taxonomy.new
140
+ root_node.taxonomies << Bio::PhyloXML::Taxonomy.new
141
+ root_node.taxonomies[0].scientific_name = "Animal animal"
142
+ node2 = Bio::PhyloXML::Node.new
143
+ node2.name = "B"
144
+ tree.add_node(node2)
145
+ tree.add_edge(root_node, node2)
146
+ writer.write(tree)
147
+
148
+ lines = File.readlines(filename)
149
+ assert_equal("<phyloxml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd\" xmlns=\"http://www.phyloxml.org\">",
150
+ lines[1].chomp)
151
+ assert_equal(" <phylogeny rooted=\"true\">", lines[2].chomp)
152
+ assert_equal(" <name>Test tree</name>", lines[3].chomp)
153
+ assert_equal(" <clade>", lines[4].chomp)
154
+ assert_equal(" <name>A</name>", lines[5].chomp)
155
+ assert_equal(" <taxonomy>", lines[6].chomp)
156
+ assert_equal(" <scientific_name>Animal animal</scientific_name>", lines[7].chomp)
157
+ assert_equal(" </taxonomy>", lines[8].chomp)
158
+ assert_equal(" <name>B</name>", lines[10].chomp)
159
+ assert_equal(" </clade>", lines[12].chomp)
160
+ assert_equal(" </phylogeny>", lines[13].chomp)
161
+ assert_equal("</phyloxml>", lines[14].chomp)
162
+
163
+ #File.delete(filename)
164
+ end
165
+
166
+ def test_phyloxml_examples_tree1
167
+ tree = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.example_xml) { |px| px.next_tree }
168
+
169
+ filename = @writeto.file('example_tree1.xml')
170
+ writer = Bio::PhyloXML::Writer.new(filename)
171
+ writer.write_branch_length_as_subelement = false
172
+ writer.write(tree)
173
+
174
+ assert_nothing_thrown do
175
+ tree2 = Bio::PhyloXML::Parser.open(filename) { |px| true }
176
+ end
177
+
178
+ #File.delete(filename)
179
+
180
+ #@todo check if branch length is written correctly
181
+ end
182
+
183
+ def test_phyloxml_examples_tree2
184
+ phyloxml = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.example_xml)
185
+ 2.times do
186
+ @tree = phyloxml.next_tree
187
+ end
188
+ phyloxml.close
189
+
190
+ filename = @writeto.file('example_tree2.xml')
191
+ writer = Bio::PhyloXML::Writer.new(filename)
192
+ writer.write(@tree)
193
+
194
+ assert_nothing_thrown do
195
+ tree2 = Bio::PhyloXML::Parser.open(filename) { |px| true }
196
+ end
197
+
198
+ #File.delete(filename)
199
+ end
200
+
201
+ def test_phyloxml_examples_tree4
202
+ phyloxml = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.example_xml)
203
+ 4.times do
204
+ @tree = phyloxml.next_tree
205
+ end
206
+ phyloxml.close
207
+ #@todo tree = phyloxml[4]
208
+ filename = @writeto.file('example_tree4.xml')
209
+ writer = Bio::PhyloXML::Writer.new(filename)
210
+ writer.write(@tree)
211
+ assert_nothing_thrown do
212
+ @tree2 = Bio::PhyloXML::Parser.open(filename) { |px| px.next_tree }
213
+ end
214
+ assert_equal(@tree.name, @tree2.name)
215
+ assert_equal(@tree.get_node_by_name('A').taxonomies[0].scientific_name, @tree2.get_node_by_name('A').taxonomies[0].scientific_name)
216
+ assert_equal(@tree.get_node_by_name('B').sequences[0].annotations[0].desc,
217
+ @tree2.get_node_by_name('B').sequences[0].annotations[0].desc)
218
+ # assert_equal(@tree.get_node_by_name('B').sequences[0].annotations[0].confidence.value,@tree2.get_node_by_name('B').sequences[0].annotations[0].confidence.value)
219
+ #File.delete(filename)
220
+ end
221
+
222
+ def test_phyloxml_examples_sequence_relation
223
+ phyloxml = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.example_xml)
224
+ filename = @writeto.example_xml_test
225
+ writer = Bio::PhyloXML::Writer.new(filename)
226
+ phyloxml.each do |tree|
227
+ writer.write(tree)
228
+ end
229
+ phyloxml.close
230
+
231
+ assert_nothing_thrown do
232
+ @phyloxml_test = Bio::PhyloXML::Parser.open(filename)
233
+ end
234
+
235
+ 5.times do
236
+ @tree = @phyloxml_test.next_tree
237
+ end
238
+
239
+ @phyloxml_test.close
240
+
241
+ assert_equal("x", @tree.sequence_relations[0].id_ref_0)
242
+ assert_equal("z", @tree.sequence_relations[1].id_ref_1)
243
+ assert_equal(nil, @tree.sequence_relations[2].distance)
244
+ assert_equal("orthology", @tree.sequence_relations[2].type)
245
+
246
+ #File.delete(filename)
247
+ end
248
+
249
+ def test_generate_xml_with_sequence
250
+ tree = Bio::PhyloXML::Tree.new
251
+ r = Bio::PhyloXML::Node.new
252
+ tree.add_node(r)
253
+ tree.root = r
254
+ n = Bio::PhyloXML::Node.new
255
+ tree.add_node(n)
256
+ tree.add_edge(tree.root, n)
257
+ tree.rooted = true
258
+
259
+ n.name = "A"
260
+ seq = PhyloXML::Sequence.new
261
+ n.sequences[0] = seq
262
+ seq.annotations[0] = PhyloXML::Annotation.new
263
+ seq.annotations[0].desc = "Sample annotation"
264
+ seq.name = "sequence name"
265
+ seq.location = "somewhere"
266
+ seq.accession = PhyloXML::Accession.new
267
+ seq.accession.source = "ncbi"
268
+ seq.accession.value = "AAB80874"
269
+ seq.symbol = "adhB"
270
+ seq.mol_seq = "TDATGKPIKCMAAIAWEAKKPLSIEEVEVAPPKSGEVRIKILHSGVCHTD"
271
+ seq.uri = PhyloXML::Uri.new
272
+ seq.uri.desc = "EMBL REPTILE DATABASE"
273
+ seq.uri.uri = "http://www.embl-heidelberg.de/~uetz/families/Varanidae.html"
274
+ seq.domain_architecture = PhyloXML::DomainArchitecture.new
275
+ seq.domain_architecture.length = 1249
276
+ domain1 = PhyloXML::ProteinDomain.new
277
+ seq.domain_architecture.domains << domain1
278
+ domain1.from = 6
279
+ domain1.to = 90
280
+ domain1.confidence = "7.0E-26"
281
+ domain1.value = "CARD"
282
+ domain2 = PhyloXML::ProteinDomain.new
283
+ seq.domain_architecture.domains << domain2
284
+ domain2.from = 109
285
+ domain2.to = 414
286
+ domain2.confidence = "7.2E-117"
287
+ domain2.value = "NB-ARC"
288
+
289
+ filename = @writeto.file('sequence.xml')
290
+ Bio::PhyloXML::Writer.new(filename).write(tree)
291
+
292
+ assert_nothing_thrown do
293
+ Bio::PhyloXML::Parser.open(filename) { |px| px.next_tree }
294
+ end
295
+
296
+ #File.delete(filename)
297
+ end
298
+
299
+ def test_phyloxml_examples_file
300
+ outputfn = "phyloxml_examples_generated_in_test.xml"
301
+ phyloxml = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.example_xml)
302
+ filename = @writeto.file(outputfn)
303
+ writer = Bio::PhyloXML::Writer.new(filename)
304
+ phyloxml.each do |tree|
305
+ writer.write(tree)
306
+ end
307
+ writer.write_other(phyloxml.other)
308
+
309
+ assert_nothing_thrown do
310
+ Bio::PhyloXML::Parser.open(filename) { |px| true }
311
+ end
312
+ # The output file is not deleted since it might be used in the phyloxml
313
+ # parser test. But since the order of tests can't be assumed, I can't
314
+ # hard code it in.
315
+ end
316
+
317
+ def test_made_up_xml_file
318
+ phyloxml = Bio::PhyloXML::Parser.open(TestPhyloXMLWriterData.made_up_xml)
319
+ filename = @writeto.file("made_up_generated_in_test.xml")
320
+ writer = Bio::PhyloXML::Writer.new(filename)
321
+ # The output file is not deleted since it might be used in the phyloxml
322
+ # parser test. But since the order of tests can't be assumed, I can't
323
+ # hard code it in.
324
+ phyloxml.each do |tree|
325
+ writer.write(tree)
326
+ end
327
+ phyloxml.close
328
+ end
329
+
330
+ end
331
+
332
+
333
+ end if defined?(LibXML) #end module Bio
334
+
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bio-phyloxml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Diana Jaunzeikare
9
+ - Clayton Wheeler
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-05-24 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bio
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 1.4.2
31
+ - !ruby/object:Gem::Dependency
32
+ name: libxml-ruby
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 2.3.2
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.2
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '3.12'
63
+ - !ruby/object:Gem::Dependency
64
+ name: bundler
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 1.1.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 1.1.0
79
+ - !ruby/object:Gem::Dependency
80
+ name: jeweler
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 1.8.3
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 1.8.3
95
+ description: Provides PhyloXML support for BioRuby.
96
+ email: cswh@umich.edu
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.md
102
+ - README.rdoc
103
+ files:
104
+ - .document
105
+ - .travis.yml
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - README.rdoc
110
+ - Rakefile
111
+ - VERSION
112
+ - lib/bio-phyloxml.rb
113
+ - lib/bio/phyloxml.rb
114
+ - lib/bio/phyloxml/elements.rb
115
+ - lib/bio/phyloxml/parser.rb
116
+ - lib/bio/phyloxml/phyloxml.xsd
117
+ - lib/bio/phyloxml/writer.rb
118
+ - sample/test_phyloxml_big.rb
119
+ - test/data/phyloxml/apaf.xml
120
+ - test/data/phyloxml/bcl_2.xml
121
+ - test/data/phyloxml/made_up.xml
122
+ - test/data/phyloxml/ncbi_taxonomy_mollusca_short.xml
123
+ - test/data/phyloxml/phyloxml_examples.xml
124
+ - test/helper.rb
125
+ - test/unit/bio/test_phyloxml.rb
126
+ - test/unit/bio/test_phyloxml_writer.rb
127
+ homepage: http://github.com/csw/bioruby-phyloxml
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ segments:
141
+ - 0
142
+ hash: 1827548371593570525
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 1.8.24
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: PhyloXML plugin for BioRuby
155
+ test_files: []