class String

Public Instance Methods

fill(fill_column = 80, indent = 0, separater = ' ', prefix = '', first_line_only = true) click to toggle source

folding with conscious about word boundaries with prefix string

# File lib/bio/shell/plugin/seq.rb, line 209
def fill(fill_column = 80, indent = 0, separater = ' ', prefix = '', first_line_only = true)

  # size : allowed length of the actual text
  unless (size = fill_column - indent) > 0
    warn "Error: indent > fill_column (indent is set to 0)"
    size = fill_column
    indent = 0
  end

  n = pos = 0
  ary = []
  while n < self.length
    pos = self[n, size].rindex(separater)

    if self[n, size].length < size    # last line of the folded str
      pos = nil
    end

    if pos
      ary << self[n, pos+separater.length]
      n += pos + separater.length
    else                              # line too long or the last line
      ary << self[n, size]
      n += size
    end
  end
  str = ary.join("\n")

  str[0,0] = prefix + ' ' * (indent - prefix.length)
  if first_line_only
    head = ' ' * indent
  else
    head = prefix + ' ' * (indent - prefix.length)
  end
  str.gsub!("\n", "\n#{head}")

  return str.chomp
end
fold(fill_column = 72, indent = 0) click to toggle source

folding both line end justified

# File lib/bio/shell/plugin/seq.rb, line 191
def fold(fill_column = 72, indent = 0)
  str = ''

  # size : allowed length of the actual text
  unless (size = fill_column - indent) > 0
    warn "Error: indent > fill_column (indent is set to 0)"
    size = fill_column
    indent = 0
  end

  0.step(self.length - 1, size) do |n|
    str << ' ' * indent + self[n, size] + "\n"
  end

  return str
end
skip(window_size, step_size = 1) { |self, i + 1, i + window_size| ... } click to toggle source
# File lib/bio/shell/plugin/seq.rb, line 171
def skip(window_size, step_size = 1)
  j = 0
  0.step(self.length - window_size, step_size) do |i|
    yield [self[i, window_size], i + 1, i + window_size]
    j = i
  end
  from = j + step_size
  to  = [self.length, j + step_size + window_size].min
  yield [self[from, window_size], from + 1, to] if from + 1 <= to
end
step(window_size) { |self| ... } click to toggle source
# File lib/bio/shell/plugin/seq.rb, line 162
def step(window_size)
  j = 0
  0.step(self.length - window_size, window_size) do |i|
    yield self[i, window_size]
    j = i
  end
  yield self[j + window_size .. -1] if j + window_size < self.length
end
to_aaseq() click to toggle source
# File lib/bio/shell/plugin/seq.rb, line 186
def to_aaseq
  Bio::Sequence::AA.new(self)
end
to_naseq() click to toggle source
# File lib/bio/shell/plugin/seq.rb, line 182
def to_naseq
  Bio::Sequence::NA.new(self)
end