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

From Phyloinformatics
Jump to: navigation, search
(34 intermediate revisions by the same user not shown)
Line 1: Line 1:
<strong>Note:</strong> This page has moved to: https://github.com/rvosa/bio-nexml/wiki/NeXML-API-for-BioRuby
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.
Currently all the parsing is done at the start( i.e. no streaming ). This is likely to change later. Parse an NeXML file:
  doc = Bio::NeXML::Parser.new( "trees.xml" )
  nexml = doc.parse
  nexml.class #Bio::NeXML::Nexml
==Otus and Otu==
Taxa blocks and taxons are stored internally as a Ruby hash for faster 'id' based lookup.
  nexml.otus_set #a hash of otus objects indexed with 'id'
  nexml.otus #an array of otus objects
  #iterate over each otus object
  nexml.each_otus do |taxa|
    puts taxa.id
    puts taxa.label
  #find an otus by id
  taxa1 = nexml.get_otus_by_id "taxa1"
  taxa1.class #Bio::NeXML::Otus
  taxa1.otu_set #a hash of otu objects indexed with 'id
  taxa1.otus #an array of otu objects
  #get an individual otu object given its id
  taxon1 = taxa1[ 'taxon1' ]
  #or, a conventional method call
  taxon1 = taxa1.get_otu_by_id 'taxon1'
  #or, get an otu from nexml object
  taxon1 = nexml.get_otu_by_id 'taxon1'
  #or iterate over each otu object
  #each_otu is an alias for each
  taxa1.each do |taxon|
    puts taxon.id
    puts taxon.label
  #or iterate with id
  taxa1.each_with_id do |id, taxon|
    puts "#{id} => #{taxon.label}"
  #check if an otu belongs to an otus or not
  #pass it an otu id
  taxa1.has_otu? 'taxon1' # => true or false
  #or pass an otu object
  otu = Bio::NeXML::Otu 't1'
  taxa1.include? otu  #include? is an alias for has_otu?
Each <code>otus</code> object is an enumerable. This functionality could specially be useful with the support for <code>class</code> element.
  taxa1.map &:id
  taxa1.select {|t| t.class == "Lemurs" } #maybe in future
== Trees and Tree ==
Get a <code>trees</code> object:
  nexml.trees #return an array of trees objects.
  trees1 = nexml.trees[0]
  trees1.class #Bio::NeXML::Trees
  #get the taxa to which the trees is linked to
Currently a <code>tree</code> can have only one root node. To work with an individual <code>tree</code> :
  #get a tree object with its 'id'
  tree1 = trees1[ 'tree1' ]
  tree1.class #Bio::NeXML::IntTree or Bio::NeXML::FloatTree
  #or iterate over each tree object
  trees1.each do |tree|
    puts tree.id
    puts tree.label
All the available methods from <code>[http://bioruby.org/rdoc/classes/Bio/Tree.html#M001688 Bio::Tree]</code> class can be called on a <code>tree</code> object.
  node1 = tree.get_node_by_name "n3" #note name is same as id
  tree1.parents node1
A <code>trees</code> object is an enumerable:
  trees1.map &:id
[[Category:NeXML and RDF API for BioRuby]]
[[Category:NeXML and RDF API for BioRuby]]

Latest revision as of 19:06, 22 September 2011

Note: This page has moved to: https://github.com/rvosa/bio-nexml/wiki/NeXML-API-for-BioRuby