Class: Nexml::Document

Inherits:
DummyBase show all
Defined in:
lib/nexml.rb

Overview

model of the NeXML file

Defined Under Namespace

Classes: Annotation, Char, Characters, Coding, Edge, Matrix, Node, Otu, Otus, State, Tree, Trees

Instance Attribute Summary collapse

Attributes inherited from DummyBase

#attributes, #text

Instance Method Summary collapse

Methods inherited from DummyBase

#attribute

Constructor Details

#initialize(options = {}) ⇒ Document

Returns a new instance of Document.

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/nexml.rb', line 111

def initialize(options = {}) 
  @opt = {
    :file => false,
    :url => false 
  }.merge!(options)
  @otus, @characters, @trees = [],[],[]
  @current_obj = nil
  @current_matrix, @current_tree, @current_trees, @current_otus, @current_characters = nil,nil, nil, nil, nil

  # require a file or a URL
  raise Nexml::NexmlError, "supply ONLY one of :file or :url" if @opt[:file] && @opt[:url]
    
  @nexml = ''
  if @opt[:file]
    @nexml = @opt[:file]
  elsif @opt[:url] 
    @nexml = Net::HTTP.get_response(URI.parse(@opt[:url])).body
  else
    raise Nexml::NexmlError, "supply one of :file or :url" if !@opt[:file] && !@opt[:url]
  end 

  @attributes = {}
  
  # do the work 
  REXML::Document.parse_stream(@nexml, Handler.new(self))
  true 
end

Instance Attribute Details

#charactersObject

these three are the top level grouping classes



105
106
107
# File 'lib/nexml.rb', line 105

def characters
  @characters
end

#current_charactersObject

stores the present state as we stream through the file, these may not all be necessary and can likely be abstracted down to a single :current_parent_object



109
110
111
# File 'lib/nexml.rb', line 109

def current_characters
  @current_characters
end

#current_matrixObject

stores the present state as we stream through the file, these may not all be necessary and can likely be abstracted down to a single :current_parent_object



109
110
111
# File 'lib/nexml.rb', line 109

def current_matrix
  @current_matrix
end

#current_objObject

stores the present state as we stream through the file, these may not all be necessary and can likely be abstracted down to a single :current_parent_object



109
110
111
# File 'lib/nexml.rb', line 109

def current_obj
  @current_obj
end

#current_otusObject

stores the present state as we stream through the file, these may not all be necessary and can likely be abstracted down to a single :current_parent_object



109
110
111
# File 'lib/nexml.rb', line 109

def current_otus
  @current_otus
end

#current_treeObject

stores the present state as we stream through the file, these may not all be necessary and can likely be abstracted down to a single :current_parent_object



109
110
111
# File 'lib/nexml.rb', line 109

def current_tree
  @current_tree
end

#current_treesObject

stores the present state as we stream through the file, these may not all be necessary and can likely be abstracted down to a single :current_parent_object



109
110
111
# File 'lib/nexml.rb', line 109

def current_trees
  @current_trees
end

#otusObject

these three are the top level grouping classes



105
106
107
# File 'lib/nexml.rb', line 105

def otus
  @otus
end

#treesObject

these three are the top level grouping classes



105
106
107
# File 'lib/nexml.rb', line 105

def trees
  @trees
end

Instance Method Details

#all_chrsObject



206
207
208
# File 'lib/nexml.rb', line 206

def all_chrs
  @characters.inject([]){|sum, o| sum.push(o.chars)}.flatten
end

#all_otusObject



202
203
204
# File 'lib/nexml.rb', line 202

def all_otus
  @otus.inject([]){|sum, o| sum.push(o.otus)}.flatten
end

#all_treesObject



210
211
212
# File 'lib/nexml.rb', line 210

def all_trees
  @trees.inject([]){|sum, o| sum.push(o.trees)}.flatten
end

#new_char(attrs) ⇒ Object



160
161
162
163
164
# File 'lib/nexml.rb', line 160

def new_char(attrs)
  chr = Char.new(:attrs => attrs)
  self.current_characters.chars.push(chr)
  chr 
end

#new_characters(attrs) ⇒ Object



154
155
156
157
158
# File 'lib/nexml.rb', line 154

def new_characters(attrs)
  characters = Characters.new(:attrs => attrs)
  self.characters.push(characters)
  characters 
end

#new_edge(attrs) ⇒ Object



184
185
186
187
188
# File 'lib/nexml.rb', line 184

def new_edge(attrs)
  node = Edge.new(:attrs => attrs)
  self.current_tree.edges.push(node)
  node 
end

#new_node(attrs) ⇒ Object



178
179
180
181
182
# File 'lib/nexml.rb', line 178

def new_node(attrs)
  node = Node.new(:attrs => attrs)
  self.current_tree.nodes.push(node)
  node 
end

#new_otu(attrs) ⇒ Object



148
149
150
151
152
# File 'lib/nexml.rb', line 148

def new_otu(attrs)
  otu = Otu.new(:attrs => attrs)
  self.current_otus.otus.push(otu)
  otu 
end

#new_otus(attrs) ⇒ Object

setters



142
143
144
145
146
# File 'lib/nexml.rb', line 142

def new_otus(attrs)
  otus = Otus.new(:attrs => attrs)
  self.otus.push(otus)
  otus 
end

#new_tree(attrs) ⇒ Object



172
173
174
175
176
# File 'lib/nexml.rb', line 172

def new_tree(attrs)
  tree = Tree.new(:attrs => attrs)
  self.current_trees.trees.push(tree)
  tree 
end

#new_trees(attrs) ⇒ Object



166
167
168
169
170
# File 'lib/nexml.rb', line 166

def new_trees(attrs)
   trees = Trees.new(:attrs => attrs)
   self.trees.push(trees)
   trees  
end

#otu_by_id(id) ⇒ Object

getters



192
193
194
195
# File 'lib/nexml.rb', line 192

def otu_by_id(id)
  foo = self.all_otus.collect{|o| o.attribute('id')}
  self.all_otus[foo.index(id)] 
end

#tree_by_id(id) ⇒ Object



197
198
199
200
# File 'lib/nexml.rb', line 197

def tree_by_id(id)
  foo = self.all_trees.collect{|o| o.attribute('id')}
  self.all_trees[foo.index(id)] 
end