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

From Phyloinformatics
Jump to: navigation, search
(Characters)
(update)
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Preface==
+
<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.
 
 
 
==Parsing==
 
Currently all the parsing is done at the start( i.e. no streaming ). This is likely to change later. Parse an NeXML file:
 
<ruby>
 
  doc = Bio::NeXML::Parser.new( "trees.xml" )
 
  nexml = doc.parse
 
  nexml.class #Bio::NeXML::Nexml
 
</ruby>
 
 
 
==Otus and Otu==
 
Taxa blocks and taxons are stored internally as a Ruby hash for faster 'id' based lookup.
 
<ruby>
 
  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
 
  end
 
 
 
  #find an otus by id
 
  taxa1 = nexml.get_otus_by_id "taxa1"
 
 
 
  taxa1.class #Bio::NeXML::Otus
 
</ruby>
 
Otu:
 
<ruby>
 
  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
 
  end
 
 
 
  #or iterate with id
 
  taxa1.each_with_id do |id, taxon|
 
    puts "#{id} => #{taxon.label}"
 
  end
 
 
 
  #check if an otu belongs to an otus or not
 
  #pass it an otu id
 
  #include? is an alias for has_otu?
 
  taxa1.has_otu? 'taxon1' # => true or false
 
</ruby>
 
 
 
Each <code>otus</code> object is an enumerable. This functionality could specially be useful with the support for <code>class</code> element.
 
<ruby>
 
  taxa1.map &:id
 
  taxa1.select {|t| t.class == "Lemurs" } #maybe in future
 
</ruby>
 
 
 
== Trees and Tree ==
 
Trees and tree and network are stored internally as a Ruby hash for faster 'id' based lookup.
 
<ruby>
 
  nexml.trees_set #return a hash of trees object indexed with 'id'
 
  nexml.trees #return an array of trees objects.
 
 
 
  #iterate over each trees object
 
  nexml.each_trees do |trees|
 
    puts trees.id
 
    puts trees.label
 
  end
 
 
 
  #find a trees by id
 
  trees1 = nexml.get_trees_by_id 'trees1'
 
 
 
  trees1.class #Bio::NeXML::Trees
 
 
 
  #get the taxa block to which the trees is linked to
 
  trees1.otus #returns an otus object
 
</ruby>
 
Tree:
 
<ruby>
 
  trees1.tree_set #return a hash or tree objects indexed with 'id'
 
  tress1.trees #return an arrayof trees object
 
 
 
  #iterate over each tree object
 
  trees1.each_tree do |t|
 
    puts t.id
 
    puts t.label
 
  end
 
 
 
  #get a tree object with its 'tree1'
 
  tree1 = trees1[ 'tree1' ]
 
  #or, with a conventional method call
 
  tree1 = trees1.get_tree_by_id 'tree1'
 
  #or, from a nexml object
 
  tree1 = nexml.get_tree_by_id 'tree1'
 
 
 
  tree1.class #Bio::NeXML::IntTree or Bio::NeXML::FloatTree
 
 
 
  #check if a tree belongs to a trees or not
 
  #pass it a tree id
 
  tree1.has_tree? 'tree1' #return true or false
 
 
 
  #get the number of treess
 
  trees1.number_of_trees
 
</ruby>
 
Network:
 
<ruby>
 
  trees1.network_set #return a hash or network objects indexed with 'id'
 
  tress1.networks #return an arrayof network objects
 
 
 
  #iterate over each network object
 
  trees1.each_network do |n|
 
    puts n.id
 
    puts n.label
 
  end
 
 
 
  #get a network object with its id
 
  network1 = trees1[ 'network1' ]
 
  #or, with a conventional method call
 
  network1 = trees1.get_network_by_id 'network1'
 
  #or, from a nexml object
 
  network1 = nexml.get_tree_by_id 'network1'
 
 
 
  network1.class #Bio::NeXML::IntTree or Bio::NeXML::FloatTree
 
 
 
  #check if a network belongs to a trees or not
 
  #pass it a network id
 
  trees1.has_network? 'network1' #return true or false
 
 
 
  #get the number of networks
 
  trees1.number_of_networks
 
</ruby>
 
Tree and Network:
 
<ruby>
 
  #iterate over both trees and networks
 
  trees1.each do |g|
 
    puts g.class
 
  end
 
 
 
  #find if a tree or a network belongs to a trees or not
 
  #include? is an alias for has?
 
  trees1.has? 'tree1' #return true or false
 
 
 
  #total number of trees and networks
 
  trees1.number_of_graphs
 
</ruby>
 
 
 
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.
 
<ruby>
 
  node1 = tree.get_node_by_name "n3" #note name is same as id
 
  tree1.parents node1
 
</ruby>
 
A <code>trees</code> object is an enumerable:
 
<ruby>
 
  trees1.map &:id
 
</ruby>
 
 
 
==Characters==
 
<ruby>
 
  nexml.characters_set #return a hash of characters object indexed with 'id'
 
  nexml.characters #return an array of characters object
 
 
 
  #iterate over each characters object
 
  nexml.each_characters do |ch|
 
    puts ch.id
 
    puts ch.label
 
  end
 
 
 
  #find a characters object by id
 
  characters = nexml.get_characters_by_id 'chars1'
 
 
 
  puts characters.class
 
 
 
  #get the taxa block to which the characters is linked to
 
  characters.otus #returns an otus object
 
 
 
  #get the child format element
 
  format = characters.format
 
 
 
  puts format.class
 
 
 
  #get the child matrix element
 
  matrix = characters.matrix
 
 
 
  puts matrix.class
 
</ruby>
 
 
 
Format
 
<ruby>
 
  format.states_set
 
  format.states
 
 
 
  format.each_states do |states|
 
    puts states.id
 
    puts states.label
 
  end
 
 
 
  format.char_set
 
  format.chars
 
 
 
  format.each_char do |char|
 
    puts char.id
 
    puts char.label
 
  end
 
 
 
  #all objects, including char and states can be iterated over with each
 
  #format is enumerable
 
  format.map &:id
 
</ruby>
 
  
 
[[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