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



208
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
# 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

#fold(fill_column = 72, indent = 0) ⇒ Object

folding both line end justified



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# 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

#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
# 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

#step(window_size) {|| ... } ⇒ Object

Yields:

  • ()


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

#to_aaseqObject



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

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

#to_naseqObject



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

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