Class | Bio::PhyloXML::Writer
In: | lib/bio/db/phyloxml/phyloxml_writer.rb
Parent: | Object

## 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

### Methods

generate_xml new write write_other

### Included Modules

LibXML

### Constants

SCHEMA_LOCATION | = | 'http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd'

### Attributes

write_branch_length_as_subelement | [RW] |

### Public Class methods

generate_xml(root, elem, subelement_array)

Used by to_xml methods of PhyloXML element classes. Generally not to be invoked directly.

[Source]

         # File lib/bio/db/phyloxml/phyloxml_writer.rb, line 166
    166:       def self.generate_xml(root, elem, subelement_array)
    167:        #example usage: generate_xml(node, self, [[ :complex,'accession', ], [:simple, 'name',  @name], [:simple, 'location', @location]])
    168:       subelement_array.each do |subelem|
    169:         if subelem[0] == :simple
    170:           root << XML::Node.new(subelem[1], subelem[2].to_s) if subelem[2] != nil and not subelem[2].to_s.empty?
    171:
    172:         elsif subelem[0] == :complex
    173:           root << subelem[2].send("to_xml") if subelem[2] != nil
    174:
    175:         elsif subelem[0] == :pattern
    176:           #seq, self, [[:pattern, 'symbol', @symbol, "\S{1,10}"]
    177:           if subelem[2] != nil
    178:             if subelem[2] =~ subelem[3]
    179:               root << XML::Node.new(subelem[1], subelem[2])
    180:             else
    181:               raise "#{subelem[2]} is not a valid value of #{subelem[1]}. It should follow pattern #{subelem[3]}"
    182:             end
    183:           end
    184:
    185:         elsif subelem[0] == :objarr
    186:           #[:objarr, 'annotation', 'annotations']])
    187:           obj_arr = elem.send(subelem[2])
    188:           obj_arr.each do |arr_elem|
    189:             root << arr_elem.to_xml
    190:           end
    191:
    192:         elsif subelem[0] == :simplearr
    193:           #  [:simplearr, 'common_name', @common_names]
    194:           subelem[2].each do |elem_val|
    195:             root << XML::Node.new(subelem[1], elem_val)
    196:           end
    197:         elsif subelem[0] == :attr
    198:           #[:attr, 'rooted']
    199:           obj = elem.send(subelem[1])
    200:           if obj != nil
    201:             root[subelem[1]] = obj.to_s
    202:           end
    203:         else
    204:           raise "Not supported type of element by method generate_xml."
    205:         end
    206:       end
    207:       return root
    208:      end

new(filename, indent=true)

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.

[Source]

         # File lib/bio/db/phyloxml/phyloxml_writer.rb, line 83
     83:       def initialize(filename, indent=true)
     84:       @write_branch_length_as_subelement = true #default value
     85:       @filename = filename
     86:       @indent = indent
     87:
     88:       @doc = XML::Document.new()
     89:       @doc.root = XML::Node.new('phyloxml')
     90:       @root = @doc.root
     91:       @root['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'
     92:       @root['xsi:schemaLocation'] = SCHEMA_LOCATION
     93:       @root['xmlns'] = 'http://www.phyloxml.org'
     94:
     95:       #@todo save encoding to be UTF-8. (However it is the default one).
     96:       #it gives error NameError: uninitialized constant LibXML::XML::Encoding
     97:       #@doc.encoding = XML::Encoding::UTF_8
     98:
     99:       @doc.save(@filename, :indent => true)
    100:       end

### Public Instance methods

write(tree)

Write a tree to a file in phyloxml format.

     require 'Bio'
     writer = Bio::PhyloXML::Writer.new
     writer.write(tree)

[Source]

         # File lib/bio/db/phyloxml/phyloxml_writer.rb, line 109
    109:       def write(tree)
    110:         @root << phylogeny = XML::Node.new('phylogeny')
    111:
    112:         PhyloXML::Writer.generate_xml(phylogeny, tree, [
    113:             [:attr, 'rooted'],
    114:             [:simple, 'name', tree.name],
    115:             [:complex, 'id', tree.phylogeny_id],
    116:             [:simple, 'description', tree.description],
    117:             [:simple, 'date', tree.date],
    118:             [:objarr, 'confidence', 'confidences']])
    119:
    120:         root_clade = tree.root.to_xml(nil, @write_branch_length_as_subelement)
    121:
    122:         phylogeny << root_clade
    123:
    124:         tree.children(tree.root).each do |node|
    125:           root_clade << node_to_xml(tree, node, tree.root)
    126:         end
    127:
    128:         Bio::PhyloXML::Writer::generate_xml(phylogeny, tree, [
    129:             [:objarr, 'clade_relation', 'clade_relations'],
    130:             [:objarr, 'sequence_relation', 'sequence_relations'],
    131:             [:objarr, 'property', 'properties']] )
    132:
    133:         @doc.save(@filename, :indent => @indent)
    134:       end

write_other(other_arr)

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)

[Source]

         # File lib/bio/db/phyloxml/phyloxml_writer.rb, line 153
    153:       def write_other(other_arr)
    154:         other_arr.each do |other_obj|
    155:           @root << other_obj.to_xml
    156:         end
    157:         @doc.save(@filename, :indent => @indent)
    158:       end

[Validate]