Class String
In: bin/br_pmfetch.rb  (CVS)
lib/bio/shell/plugin/seq.rb  (CVS)
Parent: Object

formatting

Methods

fill   fill   fold   skip   step   to_aaseq   to_naseq  

Public Instance methods

[Source]

# File bin/br_pmfetch.rb, line 23
  def fill(fill_column = 80, prefix = '', separater = ' ')
    prefix = ' ' * prefix if prefix.is_a?(Integer)
    maxlen = fill_column - prefix.length
    raise "prefix is longer than fill_column" if maxlen <= 0

    cursor = pos = 0
    lines = []
    while cursor < self.length
      line = self[cursor, maxlen]
      pos = line.rindex(separater)
      pos = nil if line.length < maxlen
      if pos
        len = pos + separater.length
        lines << self[cursor, len]
        cursor += len
      else
        lines << self[cursor, maxlen]
        cursor += maxlen
      end
    end
    return lines.join("\n#{prefix}")
  end

folding with conscious about word boundaries with prefix string

[Source]

# File lib/bio/shell/plugin/seq.rb, line 208
  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

folding both line end justified

[Source]

# File lib/bio/shell/plugin/seq.rb, line 190
  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

[Source]

# File lib/bio/shell/plugin/seq.rb, line 171
  def skip(window_size, step_size = 1)
    i = 0
    0.step(self.length - window_size, step_size) do |i|
      yield [self[i, window_size], i + 1, i + window_size]
    end
    from = i + step_size
    to  = [self.length, i + step_size + window_size].min
    yield [self[from, window_size], from + 1, to] if from + 1 <= to
  end

[Source]

# File lib/bio/shell/plugin/seq.rb, line 163
  def step(window_size)
    i = 0
    0.step(self.length - window_size, window_size) do |i|
      yield self[i, window_size]
    end
    yield self[i + window_size .. -1] if i + window_size < self.length
  end

[Source]

# File lib/bio/shell/plugin/seq.rb, line 185
  def to_aaseq
    Bio::Sequence::AA.new(self)
  end

[Source]

# File lib/bio/shell/plugin/seq.rb, line 181
  def to_naseq
    Bio::Sequence::NA.new(self)
  end

[Validate]