Difference between revisions of "NeXML and RDF API for BioRuby"

From Phyloinformatics
Jump to: navigation, search
(Reader)
(Parsing)
Line 3: Line 3:
  
 
==Parsing==
 
==Parsing==
The parsed document is abstracted with <code>Bio::NeXML::Parser</code> class. A file, i/o object, URL or string is parsed transparently while instantiating <code>Bio::NeXML::Parser</code> or with the shortcut, <code>Bio::NeXML.parse</code> method.
+
Parse an NeXML file.
  
<code>
+
  doc = Bio::NeXML::Parser.new( "trees.xml" )
nexml = Bio::NeXML.parse( "trees.xml" )
+
  nexml = doc.parse
</code>
+
  nexml.class #Bio::NeXML::Nexml
  
<code>otus</code> can be iterated over by calling the <code>each_otus</code> iterator on <code>nexml</code>.
+
Read taxa blocks by calling <code>otus</code> method on an <code>nexml</code> object. This returns an array of <code>otus</code> objects.
  
<code>
+
  nexml.otus.each do |taxa|
nexml.each_otus{ |otus| puts otus.id }
+
    puts taxa.id
</code>
+
    puts taxa.label
 +
  end
  
===OTUS and OTU===
+
An array of <code>otu</code> objects can be obtained by calling <code>otu</code> method on an <code>otus</code> object.
An <code>otus</code> is modeled by <code>Bio::NeXML::Otus</code> and an <code>otu</code> by <code>Bio::NeXML::Otu</code>. With an <code>otus</code> object all the <code>otu</code> nested in it can be iterated over, with the <code>each_otu</code> iterator.
 
  
<code>
+
  taxa1 = nexml.otus.first
otus.each_otu{ |otu| puts otu.id }
+
  taxa1.class #Bio::NeXML::Otus
</code>
+
  taxa1.otu.each do |taxon|
 +
    puts taxon.id
 +
    puts taxon.label
 +
  end
  
Bio::NeXML::Otu, inherits from Bio::Taxonomy.
+
Trees are accessible by calling <code>trees</code> method on an <code>nexml</code> object, which returns an array of <code>trees</code> objects.
 +
 
 +
  nexml.trees.each do |trees|
 +
    puts trees.id
 +
  end
 +
 
 +
The taxa block to which a <code>trees</code> object is linked to can be obtained.
 +
 
 +
  trees1 = nexml.trees.first
 +
  trees1.class #Bio::NeXML::Trees
 +
  trees1.otus
 +
 
 +
All the <code>tree</code> objects under a <code>trees</code> element are obtainable.
 +
 
 +
  trees1.tree.each do |tree|
 +
    puts tree.id
 +
  end
 +
 
 +
All the available instance methods from <code>[http://bioruby.org/rdoc/classes/Bio/Tree.html#M001688 Bio::Tree] class can be called on a <code>tree</code> object.
 +
  tree1 = trees1.tree.first
 +
  tree1.class #Bio::NeXML::IntTree or Bio::NeXML::FloatTree
 +
  node1 = tree.get_node_by_name "n3" #note name is same as id
 +
  tree1.parents node1
  
 
[[Category:NeXML and RDF API for BioRuby]]
 
[[Category:NeXML and RDF API for BioRuby]]

Revision as of 14:43, 2 June 2010

Preface

The following document discusses the implementation of an NeXML parser and serializer and an RDF API for BioRuby. Note that this document is not final yet.

Parsing

Parse an NeXML file.

 doc = Bio::NeXML::Parser.new( "trees.xml" )
 nexml = doc.parse
 nexml.class #Bio::NeXML::Nexml

Read taxa blocks by calling otus method on an nexml object. This returns an array of otus objects.

 nexml.otus.each do |taxa|
   puts taxa.id
   puts taxa.label
 end

An array of otu objects can be obtained by calling otu method on an otus object.

 taxa1 = nexml.otus.first
 taxa1.class #Bio::NeXML::Otus
 taxa1.otu.each do |taxon|
   puts taxon.id
   puts taxon.label
 end

Trees are accessible by calling trees method on an nexml object, which returns an array of trees objects.

 nexml.trees.each do |trees|
   puts trees.id
 end

The taxa block to which a trees object is linked to can be obtained.

 trees1 = nexml.trees.first
 trees1.class #Bio::NeXML::Trees
 trees1.otus

All the tree objects under a trees element are obtainable.

 trees1.tree.each do |tree|
   puts tree.id
 end

All the available instance methods from Bio::Tree class can be called on a <code>tree object.

 tree1 = trees1.tree.first
 tree1.class #Bio::NeXML::IntTree or Bio::NeXML::FloatTree
 node1 = tree.get_node_by_name "n3" #note name is same as id
 tree1.parents node1