class Bio::NeXML::Writer
Public Class Methods
DESCRIPTION¶ ↑
Bio::NeXML::Writer
class provides a wrapper over libxml-ruby to create any NeXML
document. The document is populated with Bio::NeXML::* objects serialized to xml using their respective to_xml methods. To get the raw NeXML
representation to_s method should be called on the return value.
EXAMPLES¶ ↑
A lot of examples of creating nexml objects and then serializing them can be found in the tests “test/unit/bio/db/nexml/tc_writer.rb”
# File lib/bio/db/nexml/writer.rb, line 98 def initialize( filename = nil, indent = true ) @filename = filename @indent = indent @doc = XML::Document.new @root = root @doc.root = @root end
Public Instance Methods
Add one or more otus, trees, or characters objects to self
. This function delegates the actual addition to the otus=
, trees=
, or otus=
methods. >> doc1 = Bio::NeXML::Parser.new
‘test.xml’ >> nexml = doc1.parse >> doc1.close >> writer = Bio::NeXML::Writer.new
>> writer << nexml.otus >> writer << nexml.trees >> writer << nexml.characters
# File lib/bio/db/nexml/writer.rb, line 116 def <<( object ) test = object.instance_of?( Array ) ? object.first : object case test when Otus self.otus = object when Trees self.trees = object when Characters self.characters = object end end
Add a single characters object to self
>> writer = Bio::NeXML::Writer.new
>> writer.add_characters( nexml.characters.first )
# File lib/bio/db/nexml/writer.rb, line 195 def add_characters( characters ) @root << characters.to_xml end
Add a single otus object to self
. >> writer = Bio::NeXML::Writer.new
>> writer.add_otus( nexml.otus.first )
# File lib/bio/db/nexml/writer.rb, line 181 def add_otus( otus ) @root << otus.to_xml end
Add a single trees object to self
>> writer = Bio::NeXML::Writer.new
>> writer.add_trees( nexml.trees.first )
# File lib/bio/db/nexml/writer.rb, line 188 def add_trees( trees ) @root << trees.to_xml end
Returns a hash of attributes for the given object. See example in unit tests.
# File lib/bio/db/nexml/writer.rb, line 211 def attributes( object, *names ) attributes = {} names.each do |name| case name when :id attributes[ name ] = object.send( name ) when :symbol # a symbol maybe an integer or a string # a length will always be a string attributes[ name ] = object.send( name ).to_s when :length # a length will never be a string value = object.send( name ) attributes[ name ] = value.to_s if value when :label # a label is optional so the returned value may be nil value = object.send( name ) attributes[ name ] = value if value when :"xsi:type" attributes[ name ] = object.class.to_s.sub( /Bio::NeXML::/, 'nex:' ) when :otu, :otus, :states, :source, :target, :char # an object is returned but in nexml we need the objects id obj = object.send( name ) attributes[ name ] = obj.id if obj when :state # a state can be a complex object - use id # or a string - use the same value obj = object.send( name ) attributes[ name ] = obj.instance_of?( String ) ? obj : obj.id when :root value = object.send( :root? ) attributes[ name ] = value.to_s if value end end attributes end
Add one or more characters objects to self
. This function delegates the actual addition to add_characters
method. >> writer = Bio::NeXML::Writer.new
>> writer << nexml.characters
# File lib/bio/db/nexml/writer.rb, line 170 def characters=( characters ) if characters.instance_of? Array characters.each{ |c| add_characters( c ) } else add_characters( characters ) end end
Create a XML::Node object with the given name and the attributes. >> writer = Bio::NeXML::Writer.new
>> node = writer.send( :create_node, ‘nexml’, :version => ‘0.9’ ) >> node.to_s
> “<nexml version="0.9"/>”¶ ↑
# File lib/bio/db/nexml/writer.rb, line 255 def create_node( name, attributes = {} ) node = XML::Node.new( name ) node.attributes = attributes unless attributes.empty? node end
Add one or more otus objects to self
. This function delegates the actual addition to add_otus
method. >> writer = Bio::NeXML::Writer.new
>> writer << nexml.otus
# File lib/bio/db/nexml/writer.rb, line 146 def otus=( otus ) if otus.instance_of? Array otus.each{ |o| add_otus( o ) } else add_otus( otus ) end end
Create the root nexml node. >> writer = Bio::NeXML::Writer.new
>> writer.root
# File lib/bio/db/nexml/writer.rb, line 202 def root root = create_node( "nexml", :"xsi:schemaLocation" => "http://www.nexml.org/2009 http://www.nexml.org/2009/xsd/nexml.xsd", :generator => "bioruby", :version => "0.9" ) root.namespaces = { nil => "http://www.nexml.org/2009", :xsi => "http://www.w3.org/2001/XMLSchema-instance", :xlink => "http://www.w3.org/1999/xlink", :nex => "http://www.nexml.org/2009" } root end
Write to file. >> writer.save( ‘sample.xml’, true )
Arguments:
-
filename( optional ) - the filename to write to. This need not be given if
a filename was provided while initializing Writer
.
-
indent( optional ) - wether to indent the output
NeXML
. This options assumes
true
by default.
# File lib/bio/db/nexml/writer.rb, line 136 def save( filename = nil, indent = false ) filename ||= @filename indent ||= @indent @doc.save( filename, :indent => indent ) end
Add one or more trees objects to self
. This function delegates the actual addition to add_trees
method. >> writer = Bio::NeXML::Writer.new
>> writer << nexml.trees
# File lib/bio/db/nexml/writer.rb, line 158 def trees=( trees ) if trees.instance_of? Array trees.each{ |t| add_trees( t ) } else add_trees( trees ) end end