Class: String

Inherits:
Object show all
Defined in:
lib/bio/shell/plugin/seq.rb

Instance Method Summary collapse

Instance Method Details

#fill(fill_column = 80, indent = 0, separater = ' ', prefix = '', first_line_only = true) ⇒ Object

folding with conscious about word boundaries with prefix string



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# 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) ⇒ Object

folding both line end justified



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 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[from, window_size], from + 1, to]| ... } ⇒ Object

Yields:

  • ([self[from, window_size], from + 1, to])


171
172
173
174
175
176
177
178
179
180
# 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) {|| ... } ⇒ Object

Yields:

  • ()


162
163
164
165
166
167
168
169
# 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_aaseqObject



186
187
188
# File 'lib/bio/shell/plugin/seq.rb', line 186

def to_aaseq
  Bio::Sequence::AA.new(self)
end

#to_naseqObject



182
183
184
# File 'lib/bio/shell/plugin/seq.rb', line 182

def to_naseq
  Bio::Sequence::NA.new(self)
end