Class: Bio::PhyloXML::Writer
- Includes:
- LibXML
- Defined in:
- lib/bio/db/phyloxml/phyloxml_writer.rb
Overview
Description
Bio::PhyloXML::Writer is for writing phyloXML (version 1.10) format files.
Requirements
Libxml2 XML parser is required. Install libxml-ruby bindings from libxml.rubyforge.org or
gem install -r libxml-ruby
Usage
require 'bio'
# Create new phyloxml parser
phyloxml = Bio::PhyloXML::Parser.open('example.xml')
# Read in some trees from file
tree1 = phyloxml.next_tree
tree2 = phyloxml.next_tree
# Create new phyloxml writer
writer = Bio::PhyloXML::Writer.new('tree.xml')
# Write tree to the file tree.xml
writer.write(tree1)
# Add another tree to the file
writer.write(tree2)
References
www.phyloxml.org/documentation/version_100/phyloxml.xsd.html
Constant Summary collapse
- SCHEMA_LOCATION =
'http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd'
Instance Attribute Summary collapse
-
#write_branch_length_as_subelement ⇒ Object
Returns the value of attribute write_branch_length_as_subelement.
Class Method Summary collapse
-
.generate_xml(root, elem, subelement_array) ⇒ Object
Used by to_xml methods of PhyloXML element classes.
Instance Method Summary collapse
-
#initialize(filename, indent = true) ⇒ Writer
constructor
Create new Writer object.
-
#write(tree) ⇒ Object
Write a tree to a file in phyloxml format.
-
#write_other(other_arr) ⇒ Object
PhyloXML Schema allows to save data in different xml format after all phylogeny elements.
Constructor Details
#initialize(filename, indent = true) ⇒ Writer
Create new Writer object. As parameters provide filename of xml file you wish to create. Optional parameter is whether to indent or no. Default is true. By default branch_length is written as subelement of clade element.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bio/db/phyloxml/phyloxml_writer.rb', line 83 def initialize(filename, indent=true) @write_branch_length_as_subelement = true #default value @filename = filename @indent = indent @doc = XML::Document.new() @doc.root = XML::Node.new('phyloxml') @root = @doc.root @root['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance' @root['xsi:schemaLocation'] = SCHEMA_LOCATION @root['xmlns'] = 'http://www.phyloxml.org' #@todo save encoding to be UTF-8. (However it is the default one). #it gives error NameError: uninitialized constant LibXML::XML::Encoding #@doc.encoding = XML::Encoding::UTF_8 @doc.save(@filename, :indent => true) end |
Instance Attribute Details
#write_branch_length_as_subelement ⇒ Object
Returns the value of attribute write_branch_length_as_subelement.
75 76 77 |
# File 'lib/bio/db/phyloxml/phyloxml_writer.rb', line 75 def write_branch_length_as_subelement @write_branch_length_as_subelement end |
Class Method Details
.generate_xml(root, elem, subelement_array) ⇒ Object
Used by to_xml methods of PhyloXML element classes. Generally not to be invoked directly.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/bio/db/phyloxml/phyloxml_writer.rb', line 166 def self.generate_xml(root, elem, subelement_array) #example usage: generate_xml(node, self, [[ :complex,'accession', ], [:simple, 'name', @name], [:simple, 'location', @location]]) subelement_array.each do |subelem| if subelem[0] == :simple root << XML::Node.new(subelem[1], subelem[2].to_s) if subelem[2] != nil and not subelem[2].to_s.empty? elsif subelem[0] == :complex root << subelem[2].send("to_xml") if subelem[2] != nil elsif subelem[0] == :pattern #seq, self, [[:pattern, 'symbol', @symbol, "\S{1,10}"] if subelem[2] != nil if subelem[2] =~ subelem[3] root << XML::Node.new(subelem[1], subelem[2]) else raise "#{subelem[2]} is not a valid value of #{subelem[1]}. It should follow pattern #{subelem[3]}" end end elsif subelem[0] == :objarr #[:objarr, 'annotation', 'annotations']]) obj_arr = elem.send(subelem[2]) obj_arr.each do |arr_elem| root << arr_elem.to_xml end elsif subelem[0] == :simplearr # [:simplearr, 'common_name', @common_names] subelem[2].each do |elem_val| root << XML::Node.new(subelem[1], elem_val) end elsif subelem[0] == :attr #[:attr, 'rooted'] obj = elem.send(subelem[1]) if obj != nil root[subelem[1]] = obj.to_s end else raise "Not supported type of element by method generate_xml." end end return root end |
Instance Method Details
#write(tree) ⇒ Object
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 |
# File 'lib/bio/db/phyloxml/phyloxml_writer.rb', line 109 def write(tree) @root << phylogeny = XML::Node.new('phylogeny') PhyloXML::Writer.generate_xml(phylogeny, tree, [ [:attr, 'rooted'], [:simple, 'name', tree.name], [:complex, 'id', tree.phylogeny_id], [:simple, 'description', tree.description], [:simple, 'date', tree.date], [:objarr, 'confidence', 'confidences']]) root_clade = tree.root.to_xml(nil, @write_branch_length_as_subelement) phylogeny << root_clade tree.children(tree.root).each do |node| root_clade << node_to_xml(tree, node, tree.root) end Bio::PhyloXML::Writer::generate_xml(phylogeny, tree, [ [:objarr, 'clade_relation', 'clade_relations'], [:objarr, 'sequence_relation', 'sequence_relations'], [:objarr, 'property', 'properties']] ) @doc.save(@filename, :indent => @indent) end |
#write_other(other_arr) ⇒ Object
PhyloXML Schema allows to save data in different xml format after all phylogeny elements. This method is to write these additional data.
parser = PhyloXML::Parser.open('phyloxml_examples.xml')
writer = PhyloXML::Writer.new('new.xml')
parser.each do |tree|
writer.write(tree)
end
# When all the trees are read in by the parser, whats left is saved at
# PhyloXML::Parser#other
writer.write(parser.other)
153 154 155 156 157 158 |
# File 'lib/bio/db/phyloxml/phyloxml_writer.rb', line 153 def write_other(other_arr) other_arr.each do |other_obj| @root << other_obj.to_xml end @doc.save(@filename, :indent => @indent) end |