Module: RGFA::Multiplication

Included in:
RGFA
Defined in:
lib/rgfa/multiplication.rb

Overview

Method for the RGFA class, which allow to split a segment into multiple copies.

Instance Method Summary collapse

Instance Method Details

#multiply(segment, factor, copy_names: :lowcase, conserve_components: true) ⇒ RGFA

Create multiple copies of a segment.

Automatic computation of the copy names

  • Can be overridden, by providing an array of copy names.

  • First, it is checked if the name of the original segment ends with a relevant string, i.e. a lower case letter (for :lowcase), an upper case letter (for :upcase), a digit (for :number), or the string “_copy” plus one or more optional digits (for :copy).

  • If so, it is assumed, it was already a copy, and it is not altered.

  • If not, then a (for :lowcase), A (for :upcase), 1 (for :number), _copy (for :copy) is appended to the string.

  • Then, in all cases, next (*) is called on the string, until a valid, non-existant name is found for each of the segment copies

  • (*) = except for :copy, where for the first copy no digit is present, but for the following is, i.e. the segment names will be :copy, :copy2, :copy3, etc.

Parameters:

  • factor (Integer)

    multiplication factor; if 0, delete the segment; if 1; do nothing; if > 1; number of copies to create

  • segment (String, RGFA::Line::Segment)

    segment name or instance

  • copy_names (:lowcase, :upcase, :number, :copy, Array<String>) (defaults to: :lowcase)

    (Defaults to: :lowcase) Array of names for the copies of the segment, or a symbol, which defines a system to compute the names from the name of the original segment. See “automatic computation of the copy names”.

  • conserve_components (Boolean) (defaults to: true)

    (Defaults to: true) If factor == 0 (i.e. deletion), delete segment only if Connectivity#cut_segment?(segment) is false.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rgfa/multiplication.rb', line 43

def multiply(segment, factor, copy_names: :lowcase,
             conserve_components: true)
  segment_name = segment.kind_of?(RGFA::Line) ? segment.name : segment
  if factor < 2
    return self if factor == 1
    return self if cut_segment?(segment_name) and conserve_components
    return delete_segment(segment_name)
  end
  s = segment!(segment_name)
  divide_segment_and_connection_counts(s, factor)
  copy_names = compute_copy_names(copy_names, segment_name, factor)
  copy_names.each {|cn| clone_segment_and_connections(s, cn)}
  return self
end