Class: Bio::NeXML::Writer
- Inherits:
-
Object
- Object
- Bio::NeXML::Writer
- Defined in:
- lib/bio/db/nexml/writer.rb
Instance Method Summary collapse
-
#<<(object) ⇒ Object
Add one or more otus, trees, or characters objects to
self
. -
#add_characters(characters) ⇒ Object
Add a single characters object to
self
>> writer = Bio::NeXML::Writer.new >> writer.add_characters( nexml.characters.first ). -
#add_otus(otus) ⇒ Object
Add a single otus object to
self
. -
#add_trees(trees) ⇒ Object
Add a single trees object to
self
>> writer = Bio::NeXML::Writer.new >> writer.add_trees( nexml.trees.first ). -
#attributes(object, *names) ⇒ Object
Returns a hash of attributes for the given object.
-
#characters=(characters) ⇒ Object
Add one or more characters objects to
self
. -
#create_node(name, attributes = {}) ⇒ Object
Create a XML::Node object with the given name and the attributes.
-
#initialize(filename = nil, indent = true) ⇒ Writer
constructor
DESCRIPTION Bio::NeXML::Writer class provides a wrapper over libxml-ruby to create any NeXML document.
-
#otus=(otus) ⇒ Object
Add one or more otus objects to
self
. -
#root ⇒ Object
Create the root nexml node.
-
#save(filename = nil, indent = false) ⇒ Object
Write to file.
-
#trees=(trees) ⇒ Object
Add one or more trees objects to
self
.
Constructor Details
#initialize(filename = nil, indent = true) ⇒ Writer
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”
98 99 100 101 102 103 104 |
# 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 |
Instance Method Details
#<<(object) ⇒ Object
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
116 117 118 119 120 121 122 123 124 125 126 |
# 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_characters(characters) ⇒ Object
Add a single characters object to self
>> writer = Bio::NeXML::Writer.new >> writer.add_characters( nexml.characters.first )
195 196 197 |
# File 'lib/bio/db/nexml/writer.rb', line 195 def add_characters( characters ) @root << characters.to_xml end |
#add_otus(otus) ⇒ Object
Add a single otus object to self
. >> writer = Bio::NeXML::Writer.new >> writer.add_otus( nexml.otus.first )
181 182 183 |
# File 'lib/bio/db/nexml/writer.rb', line 181 def add_otus( otus ) @root << otus.to_xml end |
#add_trees(trees) ⇒ Object
Add a single trees object to self
>> writer = Bio::NeXML::Writer.new >> writer.add_trees( nexml.trees.first )
188 189 190 |
# File 'lib/bio/db/nexml/writer.rb', line 188 def add_trees( trees ) @root << trees.to_xml end |
#attributes(object, *names) ⇒ Object
Returns a hash of attributes for the given object. See example in unit tests.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# 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 |
#characters=(characters) ⇒ Object
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
170 171 172 173 174 175 176 |
# 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_node(name, attributes = {}) ⇒ Object
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"/>”
255 256 257 258 259 |
# 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 |
#otus=(otus) ⇒ Object
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
146 147 148 149 150 151 152 |
# 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 |
#root ⇒ Object
Create the root nexml node. >> writer = Bio::NeXML::Writer.new >> writer.root
202 203 204 205 206 207 |
# 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 |
#save(filename = nil, indent = false) ⇒ Object
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.
136 137 138 139 140 |
# 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 |
#trees=(trees) ⇒ Object
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
158 159 160 161 162 163 164 |
# 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 |