module RGFA::RGL::ClassMethods

Public Instance Methods

from_rgl(g) click to toggle source

@param g [RGL::ImplicitGraph, RGL::DirectedAdjacencyGraph] an RGL graph.

@!macro from_rgl

<b>Accepted vertex formats</b>:

- RGFA::OrientedSegment, or Array which can be converted to it;
  where the first element is a <i>segment specifier</i> (see below)
- <i>segment specifier</i> alone: the orientation is assumed to be :+

The <i>segment specifier</i> can be:
- RGFA::Segment instance
- String, segment representation (e.g. "S\tsegment\t*")
- String, valid segment name (e.g. "segment")

@raise [RGFA::RGL::InvalidFormatError] if the graph cannot be
  converted

@return [RGFA] a new RGFA instance
# File lib/rgfa/rgl.rb, line 107
def from_rgl(g)
  gfa = RGFA.new
  if not (g.respond_to?(:each_vertex) and
          g.respond_to?(:each_edge))
    raise RGFA::RGL::InvalidFormatError,
      "#{g} is not a valid RGL graph"
  end
  if not g.directed?
    raise RGFA::RGL::InvalidFormatError,
      "#{g} is not a directed graph"
  end
  g.each_vertex {|v| add_segment_if_new(gfa, v)}
  g.each_edge do |s, t|
    gfa << RGFA::Line::Link.new(segment_name_and_orient(s) +
                                segment_name_and_orient(t) +
                                ["*"])
  end
  gfa
end

Private Instance Methods

add_segment_if_new(gfa, v) click to toggle source
# File lib/rgfa/rgl.rb, line 129
def add_segment_if_new(gfa, v)
  # RGFA::OrientedSegment or GFA::GraphVertex
  v = v.segment if v.respond_to?(:segment)
  if v.kind_of?(Symbol)
    # segment name as symbol
    return if gfa.segment_names.include?(v)
    v = RGFA::Line::Segment.new([v.to_s, "*"])
  elsif v.kind_of?(String)
    a = v.split("\t")
    if a[0] == "S"
      # string representation of segment
      return if gfa.segment_names.include?(a[1].to_sym)
      v = RGFA::Line::Segment.new(a[1..-1])
    else
      # segment name as string
      return if gfa.segment_names.include?(v.to_sym)
      v = RGFA::Line::Segment.new([v, "*"])
    end
  end
  return if gfa.segment_names.include?(v.name)
  gfa << v
end
segment_name_and_orient(s) click to toggle source
# File lib/rgfa/rgl.rb, line 152
def segment_name_and_orient(s)
  # default orientation
  o = s.respond_to?(:orient) ? s.orient.to_s : "+"
  # RGFA::Line::Segment (also embedded in RGFA::OrientedSegment)
  if s.respond_to?(:name)
    s = s.name.to_s
  elsif s.respond_to?(:segment)
    # GFA::GraphVertex
    s = s.segment.to_s
  elsif s.respond_to?(:split)
    a = s.split("\t")
    s = a[1] if a[0] == "S"
  else
    s = s.to_s
  end
  return s, o
end