class Bio::Sequence

DESCRIPTION

Bio::Sequence objects represent annotated sequences in bioruby. A Bio::Sequence object is a wrapper around the actual sequence, represented as either a Bio::Sequence::NA or a Bio::Sequence::AA object. For most users, this encapsulation will be completely transparent. Bio::Sequence responds to all methods defined for Bio::Sequence::NA/AA objects using the same arguments and returning the same values (even though these methods are not documented specifically for Bio::Sequence).

USAGE

# Create a nucleic or amino acid sequence
dna = Bio::Sequence.auto('atgcatgcATGCATGCAAAA')
rna = Bio::Sequence.auto('augcaugcaugcaugcaaaa')
aa = Bio::Sequence.auto('ACDEFGHIKLMNPQRSTVWYU')

# Print it out
puts dna.to_s
puts aa.to_s

# Get a subsequence, bioinformatics style (first nucleotide is '1')
puts dna.subseq(2,6)

# Get a subsequence, informatics style (first nucleotide is '0')
puts dna[2,6]

# Print in FASTA format
puts dna.output(:fasta)

# Print all codons
dna.window_search(3,3) do |codon|
  puts codon
end

# Splice or otherwise mangle your sequence
puts dna.splicing("complement(join(1..5,16..20))")
puts rna.splicing("complement(join(1..5,16..20))")

# Convert a sequence containing ambiguity codes into a 
# regular expression you can use for subsequent searching
puts aa.to_re

# These should speak for themselves
puts dna.complement
puts dna.composition
puts dna.molecular_weight
puts dna.translate
puts dna.gc_percent

Public Class Methods

adapter(source_data, adapter_module) click to toggle source

Normally, users should not call this method directly. Use Bio::*#to_biosequence (e.g. Bio::GenBank#to_biosequence).

Creates a new Bio::Sequence object from database data with an adapter module.

# File lib/bio/sequence.rb, line 463
def self.adapter(source_data, adapter_module)
  biosequence = self.new(nil)
  biosequence.instance_eval {
    remove_instance_variable(:@seq)
    @source_data = source_data
  }
  biosequence.extend(adapter_module)
  biosequence
end
auto(str) click to toggle source

Given a sequence String, guess its type, Amino Acid or Nucleic Acid, and return a new Bio::Sequence object wrapping a sequence of the guessed type (either Bio::Sequence::AA or Bio::Sequence::NA)

s = Bio::Sequence.auto('atgc')
puts s.seq.class                        #=> Bio::Sequence::NA

Arguments:

  • (required) str: String or Bio::Sequence::NA/AA object

Returns

Bio::Sequence object

# File lib/bio/sequence.rb, line 283
def self.auto(str)
  seq = self.new(str)
  seq.auto
  return seq
end
guess(str, *args) click to toggle source

Guess the class of a given sequence. Returns the class (Bio::Sequence::AA or Bio::Sequence::NA) guessed. In general, used by developers only, but if you know what you are doing, feel free.

puts .guess('atgc')        #=> Bio::Sequence::NA

There are three optional parameters: `threshold`, `length`, and `index`.

The `threshold` value (defaults to 0.9) is the frequency of nucleic acid bases [AGCTUagctu] required in the sequence for this method to produce a Bio::Sequence::NA “guess”. In the default case, if less than 90% of the bases (after excluding [Nn]) are in the set [AGCTUagctu], then the guess is Bio::Sequence::AA.

puts Bio::Sequence.guess('atgcatgcqq')      #=> Bio::Sequence::AA
puts Bio::Sequence.guess('atgcatgcqq', 0.8) #=> Bio::Sequence::AA
puts Bio::Sequence.guess('atgcatgcqq', 0.7) #=> Bio::Sequence::NA

The `length` value is how much of the total sequence to use in the guess (default 10000). If your sequence is very long, you may want to use a smaller amount to reduce the computational burden.

# limit the guess to the first 1000 positions
puts Bio::Sequence.guess('A VERY LONG SEQUENCE', 0.9, 1000)

The `index` value is where to start the guess. Perhaps you know there are a lot of gaps at the start…

puts Bio::Sequence.guess('-----atgcc')             #=> Bio::Sequence::AA
puts Bio::Sequence.guess('-----atgcc',0.9,10000,5) #=> Bio::Sequence::NA

Arguments:

  • (required) str: String or Bio::Sequence::NA/AA object

  • (optional) threshold: Float in range 0,1 (default 0.9)

  • (optional) length: Fixnum (default 10000)

  • (optional) index: Fixnum (default 1)

Returns

Bio::Sequence::NA/AA

# File lib/bio/sequence.rb, line 381
def self.guess(str, *args)
  self.new(str).guess(*args)
end
input(str, format = nil) click to toggle source

Create a new Bio::Sequence object from a formatted string (GenBank, EMBL, fasta format, etc.)

s = Bio::Sequence.input(str)

Arguments:

  • (required) str: string

  • (optional) format: format specification (class or nil)

Returns

Bio::Sequence object

# File lib/bio/sequence.rb, line 436
def self.input(str, format = nil)
  if format then
    klass = format
  else
    klass = Bio::FlatFile::AutoDetect.default.autodetect(str)
  end
  obj = klass.new(str)
  obj.to_biosequence
end
new(str) click to toggle source

Create a new Bio::Sequence object

s = Bio::Sequence.new('atgc')
puts s                                  #=> 'atgc'

Note that this method does not intialize the contained sequence as any kind of bioruby object, only as a simple string

puts s.seq.class                        #=> String

See #na, #aa, and #auto for methods to transform the basic String of a just created Bio::Sequence object to a proper bioruby object


Arguments:

  • (required) str: String or Bio::Sequence::NA/AA object

Returns

Bio::Sequence object

# File lib/bio/sequence.rb, line 99
def initialize(str)
  @seq = str
end
read(str, format = nil) click to toggle source

alias of ::input

# File lib/bio/sequence.rb, line 447
def self.read(str, format = nil)
  input(str, format)
end

Public Instance Methods

aa() click to toggle source

Transform the sequence wrapped in the current Bio::Sequence object into a Bio::Sequence::NA object. This method will change the current object. This method does not validate your choice, so be careful!

s = Bio::Sequence.new('atgc')
puts s.seq.class                        #=> String
s.aa
puts s.seq.class                        #=> Bio::Sequence::AA !!!

However, if you know your sequence type, this method may be constructively used after initialization,

s = Bio::Sequence.new('RRLE')
s.aa

Returns

Bio::Sequence::AA

# File lib/bio/sequence.rb, line 422
def aa
  @seq = AA.new(seq)
  @moltype = AA
end
accessions() click to toggle source

accession numbers of the sequence

Returns

Array of String

# File lib/bio/sequence.rb, line 454
def accessions
  [ primary_accession, secondary_accessions ].flatten.compact
end
auto() click to toggle source

Guess the type of sequence, Amino Acid or Nucleic Acid, and create a new sequence object (Bio::Sequence::AA or Bio::Sequence::NA) on the basis of this guess. This method will change the current Bio::Sequence object.

s = Bio::Sequence.new('atgc')
puts s.seq.class                        #=> String
s.auto
puts s.seq.class                        #=> Bio::Sequence::NA

Returns

Bio::Sequence::NA/AA object

# File lib/bio/sequence.rb, line 264
def auto
  @moltype = guess
  if @moltype == NA
    @seq = NA.new(seq)
  else
    @seq = AA.new(seq)
  end
end
guess(threshold = 0.9, length = 10000, index = 0) click to toggle source

Guess the class of the current sequence. Returns the class (Bio::Sequence::AA or Bio::Sequence::NA) guessed. In general, used by developers only, but if you know what you are doing, feel free.

s = Bio::Sequence.new('atgc')
puts s.guess                            #=> Bio::Sequence::NA

There are three parameters: `threshold`, `length`, and `index`.

The `threshold` value (defaults to 0.9) is the frequency of nucleic acid bases [AGCTUagctu] required in the sequence for this method to produce a Bio::Sequence::NA “guess”. In the default case, if less than 90% of the bases (after excluding [Nn]) are in the set [AGCTUagctu], then the guess is Bio::Sequence::AA.

s = Bio::Sequence.new('atgcatgcqq')
puts s.guess                            #=> Bio::Sequence::AA
puts s.guess(0.8)                       #=> Bio::Sequence::AA
puts s.guess(0.7)                       #=> Bio::Sequence::NA

The `length` value is how much of the total sequence to use in the guess (default 10000). If your sequence is very long, you may want to use a smaller amount to reduce the computational burden.

s = Bio::Sequence.new(A VERY LONG SEQUENCE)
puts s.guess(0.9, 1000)  # limit the guess to the first 1000 positions

The `index` value is where to start the guess. Perhaps you know there are a lot of gaps at the start…

s = Bio::Sequence.new('-----atgcc')
puts s.guess                            #=> Bio::Sequence::AA
puts s.guess(0.9,10000,5)               #=> Bio::Sequence::NA

Arguments:

  • (optional) threshold: Float in range 0,1 (default 0.9)

  • (optional) length: Fixnum (default 10000)

  • (optional) index: Fixnum (default 1)

Returns

Bio::Sequence::NA/AA

# File lib/bio/sequence.rb, line 328
def guess(threshold = 0.9, length = 10000, index = 0)
  str = seq.to_s[index,length].to_s.extend Bio::Sequence::Common
  cmp = str.composition

  bases = cmp['A'] + cmp['T'] + cmp['G'] + cmp['C'] + cmp['U'] +
          cmp['a'] + cmp['t'] + cmp['g'] + cmp['c'] + cmp['u']

  total = str.length - cmp['N'] - cmp['n']

  if bases.to_f / total > threshold
    return NA
  else
    return AA
  end
end
na() click to toggle source

Transform the sequence wrapped in the current Bio::Sequence object into a Bio::Sequence::NA object. This method will change the current object. This method does not validate your choice, so be careful!

s = Bio::Sequence.new('RRLE')
puts s.seq.class                        #=> String
s.na
puts s.seq.class                        #=> Bio::Sequence::NA !!!

However, if you know your sequence type, this method may be constructively used after initialization,

s = Bio::Sequence.new('atgc')
s.na

Returns

Bio::Sequence::NA

# File lib/bio/sequence.rb, line 401
def na
  @seq = NA.new(seq)
  @moltype = NA
end
to_s() click to toggle source

Return sequence as String. The original sequence is unchanged.

seq = Bio::Sequence.new('atgc')
puts s.to_s                             #=> 'atgc'
puts s.to_s.class                       #=> String
puts s                                  #=> 'atgc'
puts s.class                            #=> Bio::Sequence

Returns

String object

# File lib/bio/sequence/compat.rb, line 27
def to_s
  String.new(self.seq)
end
Also aliased as: to_str
to_str()
Alias for: to_s