Module: Graph

Included in:
Chem::CDK::CDKMolecule, Chem::Molecule
Defined in:
lib/graph.rb,
lib/graph/utils.rb,
lib/graph/morgan.rb,
lib/graph/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adjacencies(atom) ⇒ Object

Returns the value of attribute adjacencies.



16
17
18
# File 'lib/graph.rb', line 16

def adjacencies
  @adjacencies
end

#edgesObject

Returns the value of attribute edges.



16
17
18
# File 'lib/graph.rb', line 16

def edges
  @edges
end

#nodesObject

Returns the value of attribute nodes.



16
17
18
# File 'lib/graph.rb', line 16

def nodes
  @nodes
end

Instance Method Details

#adjacent_to(atom) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graph.rb', line 24

def adjacent_to(atom)
  if @adjacencies == nil
    @adjacencies = Hash.new
    edges.each do |bond, atom_a, atom_b|
      (@adjacencies[atom_a] ||= []).push([bond, atom_b])
      (@adjacencies[atom_b] ||= []).push([bond, atom_a])
    end
  end
  @adjacencies[atom] ||= []
  @adjacencies[atom]
end

#clustering_coefficientObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/graph/cluster.rb', line 4

def clustering_coefficient
  cc = {} # clustering coefficient
  @nodes.each do |node|
    c = 0
    adj = adjacent_to(node)
    adj_nodes = adj.collect{|e, n| n}
    adj.each do |ed, nd|
      adjacent_to(nd).each do |e, n|
        c += 1 if adj_nodes.include?(n)
      end
    end
    cc[node] = c
  end
  cc
end

#eachObject



18
19
20
21
22
# File 'lib/graph.rb', line 18

def each
  nodes.each do |atom|
    yield atom
  end
end

#morganObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graph/morgan.rb', line 3

def morgan
  ec = {}  # extended connectivity
  tec = {} # trial extended connectivity

  @nodes.each{ |a| tec[a] = 1 }

  k = 0
  k2 = k + 1

  while k2 > k
    k = k2
    @nodes.each{ |a| ec[a] = tec[a] }

    @nodes.each do |a|
      tec[a] = adjacent_to(a).inject(0){|ret, (b, n)| ret + ec[n]}
    end
    k2 = @nodes.collect{|a| tec[a]}.uniq.length
  end

  #      calc morgan tree
  max = @nodes.max{|a, b| tec[a] <=> tec[b]}

  queue = [ max ]
  traversed = [ max ]

  while from = queue.shift
    adjacent_to(from).sort{|(b1, n), (b2, m)| tec[m] <=> tec[n]}.each do |bond, atom|
      unless traversed.include?(atom)
        queue.push(atom)
        traversed.push(atom)
      end
    end
  end
  [ec, tec, traversed]
end

#terminal_nodesObject

Returns Terminal Nodes



5
6
7
# File 'lib/graph/utils.rb', line 5

def terminal_nodes
  self.nodes.find_all{|node| self.adjacent_to(node).length == 1}
end