Class: Bio::AssemblyGraphAlgorithms::ConnectivityBasedGraphFilter

Inherits:
Object
  • Object
show all
Includes:
FinishM::Logging
Defined in:
lib/assembly/coverage_based_graph_filter.rb

Instance Method Summary collapse

Methods included from FinishM::Logging

#log

Instance Method Details

#remove_unconnected_nodes(graph, whitelisted_nodes, options = {}) ⇒ Object

Remove parts of the graph that are unconnected to any whitelisted nodes

options: :leash_length: don’t explore more than this length away from each of the whitelisted_nodes. Defualt nil, no bounds



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/assembly/coverage_based_graph_filter.rb', line 41

def remove_unconnected_nodes(graph, whitelisted_nodes, options={})
  # Copy the whitelist
  all_whitelisted_nodes = Set.new whitelisted_nodes

  dij = Bio::AssemblyGraphAlgorithms::Dijkstra.new
  dij_options = {:ignore_directions => true}
  dij_options[:leash_length] = options[:leash_length]

  # Depth-first search of all the connected parts looking for nodes to keep
  whitelisted_nodes.each do |originally_whitelisted_node|
    onode = Bio::Velvet::Graph::OrientedNodeTrail::OrientedNode.new
    onode.node = originally_whitelisted_node
    onode.first_side = Bio::Velvet::Graph::OrientedNodeTrail::START_IS_FIRST #irrelevant which is first because :ignore_directions => true
    log.debug "Testing for connectivity from #{onode.node.node_id}" if log.debug?

    min_distances = dij.min_distances(graph, onode, dij_options)
    min_distances.each do |key, distance|
      all_whitelisted_nodes << graph.nodes[key[0]]
    end
  end

  # Delete all nodes that aren't in the
  graph.delete_nodes_if do |node|
    !all_whitelisted_nodes.include?(node)
  end
end