File: aa.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (123 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#
# = bio/sequence/aa.rb - amino acid sequence class
#
# Copyright::   Copyright (C) 2006
#               Toshiaki Katayama <k@bioruby.org>,
#               Ryan Raaum <ryan@raaum.org>
# License::     The Ruby License
#

module Bio

  autoload :AminoAcid, 'bio/data/aa' unless const_defined?(:AminoAcid)

  require 'bio/sequence' unless const_defined?(:Sequence)

class Sequence

# = DESCRIPTION
# Bio::Sequence::AA represents a bare Amino Acid sequence in bioruby.
#
# = USAGE
#   # Create an Amino Acid sequence.
#   aa = Bio::Sequence::AA.new('ACDEFGHIKLMNPQRSTVWYU')
#
#   # What are the three-letter codes for all the residues?
#   puts aa.codes
#
#   # What are the names of all the residues?
#   puts aa.names
#
#   # What is the molecular weight of this peptide?
#   puts aa.molecular_weight
class AA < String

  include Bio::Sequence::Common

  # Generate an amino acid sequence object from a string.
  #
  #   s = Bio::Sequence::AA.new("RRLEHTFVFLRNFSLMLLRY")
  #
  # or maybe (if you have an amino acid sequence in a file)
  #
  #   s = Bio::Sequence:AA.new(File.open('aa.txt').read)
  #
  # Amino Acid sequences are *always* all uppercase in bioruby
  #
  #   s = Bio::Sequence::AA.new("rrLeHtfV")
  #   puts s                                  #=> "RRLEHTFVF"
  #
  # Whitespace is stripped from the sequence
  #
  #   s = Bio::Sequence::AA.new("RRL\nELA\tRG\r  RL")
  #   puts s                                  #=> "RRLELARGRL"
  # ---
  # *Arguments*:
  # * (required) _str_: String
  # *Returns*:: Bio::Sequence::AA object
  def initialize(str)
    super
    self.upcase!
    self.tr!(" \t\n\r",'')
  end


  # Estimate molecular weight based on 
  # Fasman1976[http://www.genome.ad.jp/dbget-bin/www_bget?aaindex+FASG760101]
  #
  #   s = Bio::Sequence::AA.new("RRLE")
  #   puts s.molecular_weight             #=> 572.655
  # ---
  # *Returns*:: Float object
  def molecular_weight
    Bio::AminoAcid.weight(self)
  end

  # Create a ruby regular expression instance 
  # (Regexp)[http://corelib.rubyonrails.org/classes/Regexp.html]  
  #
  #   s = Bio::Sequence::AA.new("RRLE")
  #   puts s.to_re                        #=> /RRLE/
  # ---
  # *Returns*:: Regexp object
  def to_re
    Bio::AminoAcid.to_re(self)
  end

  # Generate the list of the names of each residue along with the
  # sequence (3 letters code).  Codes used in bioruby are found in the
  # Bio::AminoAcid::NAMES hash.
  #
  #   s = Bio::Sequence::AA.new("RRLE")
  #   puts s.codes                        #=> ["Arg", "Arg", "Leu", "Glu"]
  # ---
  # *Returns*:: Array object
  def codes
    array = []
    self.each_byte do |x|
      array.push(Bio::AminoAcid.names[x.chr])
    end
    return array
  end

  # Generate the list of the names of each residue along with the
  # sequence (full name).  Names used in bioruby are found in the
  # Bio::AminoAcid::NAMES hash.
  #
  #   s = Bio::Sequence::AA.new("RRLE")
  #   puts s.names  
  #               #=> ["arginine", "arginine", "leucine", "glutamic acid"]
  # ---
  # *Returns*:: Array object
  def names
    self.codes.map do |x|
      Bio::AminoAcid.names[x]
    end
  end

end # AA

end # Sequence

end # Bio