Class: Bio::PhyloXML::Parser

Inherits:
Object
  • Object
show all
Includes:
LibXML
Defined in:
lib/bio/db/phyloxml/phyloxml_parser.rb

Overview

Description

Bio::PhyloXML::Parser is for parsing phyloXML 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')

# Print the names of all trees in the file
phyloxml.each do |tree|
  puts tree.name
end

References

www.phyloxml.org/documentation/version_100/phyloxml.xsd.html

Defined Under Namespace

Classes: ClosedPhyloXMLParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, validate = true) ⇒ Parser

Initializes LibXML::Reader and reads the PhyloXML-formatted string until it reaches the first phylogeny element.

Create a new Bio::PhyloXML::Parser object.

str = File.read("./phyloxml_examples.xml")
p = Bio::PhyloXML::Parser.new(str)

Deprecated usage: Reads data from a file. <em>str<em> is a filename.

p = Bio::PhyloXML::Parser.new("./phyloxml_examples.xml")

Taking filename is deprecated. Use Bio::PhyloXML::Parser.open(filename).


Arguments:

  • (required) str: PhyloXML-formatted string

  • (optional) validate: Whether to validate the file against schema or not. Default value is true.

Returns

Bio::PhyloXML::Parser object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 271

def initialize(str, validate=true)

  @other = []

  return unless str

  # For compatibility, if filename-like string is given,
  # treat it as a filename.
  if /[\<\>\r\n]/ !~ str and File.exist?(str) then
    # assume that str is filename
    warn "Bio::PhyloXML::Parser.new(filename) is deprecated. Use Bio::PhyloXML::Parser.open(filename)."
    filename = _secure_filename(str)
    _validate(:file, filename) if validate
    @reader = XML::Reader.file(filename)
    _skip_leader
    return
  end

  # initialize for string
  @reader = XML::Reader.string(str,
                               { :options =>
                                 LibXML::XML::Parser::Options::NONET })
  _skip_leader
end

Instance Attribute Details

#otherObject (readonly)

After parsing all the trees, if there is anything else in other xml format, it is saved in this array of PhyloXML::Other objects



76
77
78
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 76

def other
  @other
end

Class Method Details

.for_io(io, validate = true) ⇒ Object

Initializes LibXML::Reader and reads from the IO until it reaches the first phylogeny element.

Create a new Bio::PhyloXML::Parser object.

p = Bio::PhyloXML::Parser.for_io($stdin)

Arguments:

  • (required) io: IO object

  • (optional) validate: For IO reader, the “validate” option is ignored and no validation is executed.

Returns

Bio::PhyloXML::Parser object



171
172
173
174
175
176
177
178
179
180
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 171

def self.for_io(io, validate=true)
  obj = new(nil, validate)
  obj.instance_eval {
    @reader = XML::Reader.io(io,
                             { :options =>
                               LibXML::XML::Parser::Options::NONET })
    _skip_leader
  }
  obj
end

.open(filename, validate = true) ⇒ Object

Initializes LibXML::Reader and reads the file until it reaches the first phylogeny element.

Create a new Bio::PhyloXML::Parser object.

p = Bio::PhyloXML::Parser.open("./phyloxml_examples.xml")

Arguments:

  • (required) filename: Path to the file to parse.

  • (optional) validate: Whether to validate the file against schema or not. Default value is true.

Returns

Bio::PhyloXML::Parser object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 90

def self.open(filename, validate=true)
  obj = new(nil, validate)
  obj.instance_eval {
    filename = _secure_filename(filename)
    _validate(:file, filename) if validate
    # XML::Parser::Options::NONET for security reason
    @reader = XML::Reader.file(filename,
                               { :options =>
                                 LibXML::XML::Parser::Options::NONET })
    _skip_leader
  }
  obj
end

.open_uri(uri, validate = true) ⇒ Object

Initializes LibXML::Reader and reads the file until it reaches the first phylogeny element.

Create a new Bio::PhyloXML::Parser object.

p = Bio::PhyloXML::Parser.open_uri("http://www.phyloxml.org/examples/apaf.xml")

Arguments:

  • (required) uri: (URI or String) URI to the data to parse

  • (optional) validate: For URI reader, the “validate” option is ignored and no validation is executed.

Returns

Bio::PhyloXML::Parser object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 116

def self.open_uri(uri, validate=true)
  case uri
  when URI
    uri = uri.to_s
  else
    # raises error if not a String
    uri = uri.to_str
    # raises error if invalid URI
    URI.parse(uri)
  end

  obj = new(nil, validate)
  obj.instance_eval {
    @reader = XML::Reader.file(uri)
    _skip_leader
  }
  obj
end

Instance Method Details

#[](i) ⇒ Object

Access the specified tree in the file. It parses trees until the specified tree is reached.

# Get 3rd tree in the file (starts counting from 0).
parser = PhyloXML::Parser.open('phyloxml_examples.xml')
tree = parser[2]


317
318
319
320
321
322
323
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 317

def [](i)
  tree = nil
  (i+1).times do
   tree =  self.next_tree
  end
  return tree
end

#closeObject

Closes the LibXML::Reader inside the object. It also closes the opened file if it is created by using Bio::PhyloXML::Parser.open method.

When closed object is closed again, or closed object is used, it raises LibXML::XML::Error.


Returns

nil



153
154
155
156
157
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 153

def close
  @reader.close
  @reader = ClosedPhyloXMLParser.new
  nil
end

#eachObject

Iterate through all trees in the file.

phyloxml = Bio::PhyloXML::Parser.open('example.xml')
phyloxml.each do |tree|
  puts tree.name
end


304
305
306
307
308
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 304

def each
  while tree = next_tree
    yield tree
  end
end

#next_treeObject

Parse and return the next phylogeny tree. If there are no more phylogeny element, nil is returned. If there is something else besides phylogeny elements, it is saved in the PhyloXML::Parser#other.

p = Bio::PhyloXML::Parser.open("./phyloxml_examples.xml")
tree = p.next_tree

Returns

Bio::PhyloXML::Tree



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/bio/db/phyloxml/phyloxml_parser.rb', line 334

def next_tree()

  if not is_element?('phylogeny')
    if @reader.node_type == XML::Reader::TYPE_END_ELEMENT
      if is_end_element?('phyloxml')
        return nil
      else
        @reader.read
        @reader.read
        if is_end_element?('phyloxml')
          return nil
        end
      end
    end        
    # phyloxml can hold only phylogeny and "other" elements. If this is not
    # phylogeny element then it is other. Also, "other" always comes after
    # all phylogenies        
    @other << parse_other        
    #return nil for tree, since this is not valid phyloxml tree.
    return nil
  end

  tree = Bio::PhyloXML::Tree.new

  # keep track of current node in clades array/stack. Current node is the
  # last element in the clades array
  clades = []
  clades.push tree
  
  #keep track of current edge to be able to parse branch_length tag
  current_edge = nil

  # we are going to parse clade iteratively by pointing (and changing) to
  # the current node in the tree. Since the property element is both in
  # clade and in the phylogeny, we need some boolean to know if we are
  # parsing the clade (there can be only max 1 clade in phylogeny) or
  # parsing phylogeny
  parsing_clade = false

  while not is_end_element?('phylogeny') do
    break if is_end_element?('phyloxml')
    
    # parse phylogeny elements, except clade
    if not parsing_clade

      if is_element?('phylogeny')
        @reader["rooted"] == "true" ? tree.rooted = true : tree.rooted = false
        @reader["rerootable"] == "true" ? tree.rerootable = true : tree.rerootable = false
        parse_attributes(tree, ["branch_length_unit", 'type'])
      end

      parse_simple_elements(tree, [ "name", 'description', "date"])

      if is_element?('confidence')
        tree.confidences << parse_confidence
      end

    end

    if @reader.node_type == XML::Reader::TYPE_ELEMENT
      case @reader.name
      when 'clade'
        #parse clade element

        parsing_clade = true

        node= Bio::PhyloXML::Node.new

        branch_length = @reader['branch_length']

        parse_attributes(node, ["id_source"])

        #add new node to the tree
        tree.add_node(node)
        # The first clade will always be root since by xsd schema phyloxml can
        # have 0 to 1 clades in it.
        if tree.root == nil
          tree.root = node
        else
          current_edge = tree.add_edge(clades[-1], node,
                                       Bio::Tree::Edge.new(branch_length))
        end
        clades.push node
        #end if clade element
      else
       parse_clade_elements(clades[-1], current_edge) if parsing_clade
      end
    end

    #end clade element, go one parent up
    if is_end_element?('clade')

       #if we have reached the closing tag of the top-most clade, then our
      # curent node should point to the root, If thats the case, we are done
      # parsing the clade element
      if clades[-1] == tree.root
        parsing_clade = false
      else
        # set current node (clades[-1) to the previous clade in the array
        clades.pop
      end
    end          

    #parsing phylogeny elements
    if not parsing_clade

      if @reader.node_type == XML::Reader::TYPE_ELEMENT
        case @reader.name
        when 'property'
          tree.properties << parse_property

        when 'clade_relation'
          clade_relation = CladeRelation.new
          parse_attributes(clade_relation, ["id_ref_0", "id_ref_1", "distance", "type"])

          #@ add unit test for this
          if not @reader.empty_element?
            @reader.read
            if is_element?('confidence')
              clade_relation.confidence = parse_confidence
            end
          end
          tree.clade_relations << clade_relation

        when 'sequence_relation'
          sequence_relation = SequenceRelation.new
          parse_attributes(sequence_relation, ["id_ref_0", "id_ref_1", "distance", "type"])
          if not @reader.empty_element?
            @reader.read
            if is_element?('confidence')
              sequence_relation.confidence = parse_confidence
            end
          end
          tree.sequence_relations << sequence_relation
        when 'phylogeny'
          #do nothing
        else
          tree.other << parse_other
          #puts "Not recognized element. #{@reader.name}"
        end
      end
    end
    # go to next element        
    @reader.read    
  end #end while not </phylogeny>
  #move on to the next tag after /phylogeny which is text, since phylogeny
  #end tag is empty element, which value is nil, therefore need to move to
  #the next meaningful element (therefore @reader.read twice)
  @reader.read 
  @reader.read

  return tree
end