Class: Bio::Sequence
- Defined in:
- lib/bio/sequence.rb,
lib/bio/sequence/aa.rb,
lib/bio/sequence/na.rb,
lib/bio/sequence/common.rb,
lib/bio/sequence/compat.rb,
lib/bio/sequence/format.rb,
lib/bio/sequence/generic.rb
Overview
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
Defined Under Namespace
Modules: Common, Format Classes: AA, Generic, NA
Instance Attribute Summary collapse
-
#comments ⇒ Object
A comment String.
-
#date ⇒ Object
Date from sequence source.
-
#dblinks ⇒ Object
An Array of Strings; links to other database entries.
-
#definition ⇒ Object
A String with a description of the sequence.
-
#entry_id ⇒ Object
The sequence identifier.
-
#features ⇒ Object
An Array of Bio::Feature objects.
-
#keywords ⇒ Object
An Array of Strings.
-
#moltype ⇒ Object
Bio::Sequence::NA/AA.
-
#references ⇒ Object
An Array of Bio::Reference objects.
-
#seq ⇒ Object
The sequence object, usually Bio::Sequence::NA/AA, but could be a simple String.
-
#taxonomy ⇒ Object
A taxonomy String.
Class Method Summary collapse
-
.auto(str) ⇒ Object
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).
-
.guess(str, *args) ⇒ Object
Guess the class of a given sequence.
Instance Method Summary collapse
-
#aa ⇒ Object
Transform the sequence wrapped in the current Bio::Sequence object into a Bio::Sequence::NA object.
-
#auto ⇒ Object
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.
-
#guess(threshold = 0.9, length = 10000, index = 0) ⇒ Object
Guess the class of the current sequence.
-
#initialize(str) ⇒ Sequence
constructor
Create a new Bio::Sequence object.
-
#method_missing(sym, *args, &block) ⇒ Object
Pass any unknown method calls to the wrapped sequence object.
-
#na ⇒ Object
Transform the sequence wrapped in the current Bio::Sequence object into a Bio::Sequence::NA object.
-
#output(style) ⇒ Object
Using Bio::Sequence::Format, return a String with the Bio::Sequence object formatted in the given style.
-
#to_s ⇒ Object
(also: #to_str)
Return sequence as String.
Constructor Details
#initialize(str) ⇒ Sequence
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 Bio::Sequence#na, Bio::Sequence#aa, and Bio::Sequence#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
91 92 93 |
# File 'lib/bio/sequence.rb', line 91 def initialize(str) @seq = str end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Pass any unknown method calls to the wrapped sequence object. see www.rubycentral.com/book/ref_c_object.html#Object.method_missing
97 98 99 |
# File 'lib/bio/sequence.rb', line 97 def method_missing(sym, *args, &block) #:nodoc: @seq.send(sym, *args, &block) end |
Instance Attribute Details
#comments ⇒ Object
A comment String
115 116 117 |
# File 'lib/bio/sequence.rb', line 115 def comments @comments end |
#date ⇒ Object
Date from sequence source. Often date of deposition.
118 119 120 |
# File 'lib/bio/sequence.rb', line 118 def date @date end |
#dblinks ⇒ Object
An Array of Strings; links to other database entries.
124 125 126 |
# File 'lib/bio/sequence.rb', line 124 def dblinks @dblinks end |
#definition ⇒ Object
A String with a description of the sequence
106 107 108 |
# File 'lib/bio/sequence.rb', line 106 def definition @definition end |
#entry_id ⇒ Object
The sequence identifier. For example, for a sequence of Genbank origin, this is the accession number.
103 104 105 |
# File 'lib/bio/sequence.rb', line 103 def entry_id @entry_id end |
#features ⇒ Object
An Array of Bio::Feature objects
109 110 111 |
# File 'lib/bio/sequence.rb', line 109 def features @features end |
#keywords ⇒ Object
An Array of Strings
121 122 123 |
# File 'lib/bio/sequence.rb', line 121 def keywords @keywords end |
#moltype ⇒ Object
Bio::Sequence::NA/AA
130 131 132 |
# File 'lib/bio/sequence.rb', line 130 def moltype @moltype end |
#references ⇒ Object
An Array of Bio::Reference objects
112 113 114 |
# File 'lib/bio/sequence.rb', line 112 def references @references end |
#seq ⇒ Object
The sequence object, usually Bio::Sequence::NA/AA, but could be a simple String
134 135 136 |
# File 'lib/bio/sequence.rb', line 134 def seq @seq end |
#taxonomy ⇒ Object
A taxonomy String
127 128 129 |
# File 'lib/bio/sequence.rb', line 127 def taxonomy @taxonomy end |
Class Method Details
.auto(str) ⇒ Object
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
193 194 195 196 197 |
# File 'lib/bio/sequence.rb', line 193 def self.auto(str) seq = self.new(str) seq.auto return seq end |
.guess(str, *args) ⇒ Object
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
291 292 293 |
# File 'lib/bio/sequence.rb', line 291 def self.guess(str, *args) self.new(str).guess(*args) end |
Instance Method Details
#aa ⇒ Object
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
332 333 334 335 |
# File 'lib/bio/sequence.rb', line 332 def aa @seq = AA.new(@seq) @moltype = AA end |
#auto ⇒ Object
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
174 175 176 177 178 179 180 181 |
# File 'lib/bio/sequence.rb', line 174 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) ⇒ Object
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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/bio/sequence.rb', line 238 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 ⇒ Object
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
311 312 313 314 |
# File 'lib/bio/sequence.rb', line 311 def na @seq = NA.new(@seq) @moltype = NA end |
#output(style) ⇒ Object
Using Bio::Sequence::Format, return a String with the Bio::Sequence object formatted in the given style.
Formats currently implemented are: ‘fasta’, ‘genbank’, and ‘embl’
s = Bio::Sequence.new('atgc')
puts s.output(:fasta) #=> "> \natgc\n"
The style argument is given as a Ruby Symbol(www.ruby-doc.org/core/classes/Symbol.html)
Arguments:
-
(required) style: :fasta, :genbank, or :embl
- Returns
-
String object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bio/sequence.rb', line 150 def output(style) extend Bio::Sequence::Format case style when :fasta format_fasta when :gff format_gff when :genbank format_genbank when :embl format_embl end end |