Class: Bio::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/pathway.rb

Overview

Bio::Relation is a simple object storing two nodes and the relation of them. The nodes and the edge (relation) can be any Ruby object. You can also compare Bio::Relation objects if the edges have Comparable property.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node1, node2, edge) ⇒ Relation

Create new binary relation object consists of the two object ‘node1’ and ‘node2’ with the ‘edge’ object as the relation of them.



719
720
721
722
# File 'lib/bio/pathway.rb', line 719

def initialize(node1, node2, edge)
  @node = [node1, node2]
  @edge = edge
end

Instance Attribute Details

#edgeObject

Returns the value of attribute edge.



723
724
725
# File 'lib/bio/pathway.rb', line 723

def edge
  @edge
end

#nodeObject

Returns the value of attribute node.



723
724
725
# File 'lib/bio/pathway.rb', line 723

def node
  @node
end

Instance Method Details

#<=>(rel) ⇒ Object

Used by the each method to compare with another Bio::Relation object. This method is only usable when the edge objects have the property of the module Comparable.



774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/bio/pathway.rb', line 774

def <=>(rel)
  unless self.edge.kind_of? Comparable
    raise "[Error] edges are not comparable"
  end
  if self.edge > rel.edge
    return 1
  elsif self.edge < rel.edge
    return -1
  elsif self.edge == rel.edge
    return 0
  end
end

#===(rel) ⇒ Object Also known as: eql?

Compare with another Bio::Relation object whether havind same edges and same nodes. The == method compares Bio::Relation object’s id, however this case equality === method compares the internal property of the Bio::Relation object.



748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/bio/pathway.rb', line 748

def ===(rel)
  if self.edge == rel.edge
    if self.node[0] == rel.node[0] and self.node[1] == rel.node[1]
      return true
    elsif self.node[0] == rel.node[1] and self.node[1] == rel.node[0]
      return true
    else
      return false
    end
  else
    return false
  end
end

#fromObject

Returns one node.



726
727
728
# File 'lib/bio/pathway.rb', line 726

def from
  @node[0]
end

#hashObject

Used by eql? method



740
741
742
# File 'lib/bio/pathway.rb', line 740

def hash
  @node.sort.push(@edge).hash
end

#relationObject



735
736
737
# File 'lib/bio/pathway.rb', line 735

def relation
  @edge
end

#toObject

Returns another node.



731
732
733
# File 'lib/bio/pathway.rb', line 731

def to
  @node[1]
end