class Bio::PhyloXML::Writer
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
Constants
- SCHEMA_LOCATION
Attributes
write_branch_length_as_subelement[RW]
Public Class Methods
generate_xml(root, elem, subelement_array)
click to toggle source
Used by to_xml methods of PhyloXML
element classes. Generally not to be invoked directly.
# File lib/bio-phyloxml/phyloxml_writer.rb 165 def self.generate_xml(root, elem, subelement_array) 166 #example usage: generate_xml(node, self, [[ :complex,'accession', ], [:simple, 'name', @name], [:simple, 'location', @location]]) 167 subelement_array.each do |subelem| 168 if subelem[0] == :simple 169 root << XML::Node.new(subelem[1], subelem[2].to_s) if subelem[2] != nil and not subelem[2].to_s.empty? 170 171 elsif subelem[0] == :complex 172 root << subelem[2].send("to_xml") if subelem[2] != nil 173 174 elsif subelem[0] == :pattern 175 #seq, self, [[:pattern, 'symbol', @symbol, "\S{1,10}"] 176 if subelem[2] != nil 177 if subelem[2] =~ subelem[3] 178 root << XML::Node.new(subelem[1], subelem[2]) 179 else 180 raise "#{subelem[2]} is not a valid value of #{subelem[1]}. It should follow pattern #{subelem[3]}" 181 end 182 end 183 184 elsif subelem[0] == :objarr 185 #[:objarr, 'annotation', 'annotations']]) 186 obj_arr = elem.send(subelem[2]) 187 obj_arr.each do |arr_elem| 188 root << arr_elem.to_xml 189 end 190 191 elsif subelem[0] == :simplearr 192 # [:simplearr, 'common_name', @common_names] 193 subelem[2].each do |elem_val| 194 root << XML::Node.new(subelem[1], elem_val) 195 end 196 elsif subelem[0] == :attr 197 #[:attr, 'rooted'] 198 obj = elem.send(subelem[1]) 199 if obj != nil 200 root[subelem[1]] = obj.to_s 201 end 202 else 203 raise "Not supported type of element by method generate_xml." 204 end 205 end 206 return root 207 end
new(filename, indent=true)
click to toggle source
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.
# File lib/bio-phyloxml/phyloxml_writer.rb 82 def initialize(filename, indent=true) 83 @write_branch_length_as_subelement = true #default value 84 @filename = filename 85 @indent = indent 86 87 @doc = XML::Document.new() 88 @doc.root = XML::Node.new('phyloxml') 89 @root = @doc.root 90 @root['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance' 91 @root['xsi:schemaLocation'] = SCHEMA_LOCATION 92 @root['xmlns'] = 'http://www.phyloxml.org' 93 94 #@todo save encoding to be UTF-8. (However it is the default one). 95 #it gives error NameError: uninitialized constant LibXML::XML::Encoding 96 #@doc.encoding = XML::Encoding::UTF_8 97 98 @doc.save(@filename, :indent => true) 99 end
Public Instance Methods
write(tree)
click to toggle source
Write a tree to a file in phyloxml format.
require 'Bio' writer = Bio::PhyloXML::Writer.new writer.write(tree)
# File lib/bio-phyloxml/phyloxml_writer.rb 108 def write(tree) 109 @root << phylogeny = XML::Node.new('phylogeny') 110 111 PhyloXML::Writer.generate_xml(phylogeny, tree, [ 112 [:attr, 'rooted'], 113 [:simple, 'name', tree.name], 114 [:complex, 'id', tree.phylogeny_id], 115 [:simple, 'description', tree.description], 116 [:simple, 'date', tree.date], 117 [:objarr, 'confidence', 'confidences']]) 118 119 root_clade = tree.root.to_xml(nil, @write_branch_length_as_subelement) 120 121 phylogeny << root_clade 122 123 tree.children(tree.root).each do |node| 124 root_clade << node_to_xml(tree, node, tree.root) 125 end 126 127 Bio::PhyloXML::Writer::generate_xml(phylogeny, tree, [ 128 [:objarr, 'clade_relation', 'clade_relations'], 129 [:objarr, 'sequence_relation', 'sequence_relations'], 130 [:objarr, 'property', 'properties']] ) 131 132 @doc.save(@filename, :indent => @indent) 133 end
write_other(other_arr)
click to toggle source
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)
# File lib/bio-phyloxml/phyloxml_writer.rb 152 def write_other(other_arr) 153 other_arr.each do |other_obj| 154 @root << other_obj.to_xml 155 end 156 @doc.save(@filename, :indent => @indent) 157 end
Private Instance Methods
node_to_xml(tree, node, parent)
click to toggle source
# File lib/bio-phyloxml/phyloxml_writer.rb 211 def node_to_xml(tree, node, parent) 212 edge = tree.get_edge(parent, node) 213 branch_length = edge.distance 214 215 clade = node.to_xml(branch_length, @write_branch_length_as_subelement) 216 217 tree.children(node).each do |new_node| 218 clade << node_to_xml(tree, new_node, node) 219 end 220 221 return clade 222 end