File: na.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 (198 lines) | stat: -rw-r--r-- 3,708 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#
# = bio/data/na.rb - Nucleic Acids
#
# Copyright::	Copyright (C) 2001, 2005
#		Toshiaki Katayama <k@bioruby.org>
# License::	The Ruby License
#
# $Id:$
#
# == Synopsis
#
# Bio::NucleicAcid class contains data related to nucleic acids.
#
# == Usage
#
# Examples:
#
#   require 'bio'
#
#   puts "### na = Bio::NucleicAcid.new"
#   na = Bio::NucleicAcid.new
#
#   puts "# na.to_re('yrwskmbdhvnatgc')"
#   p na.to_re('yrwskmbdhvnatgc')
#
#   puts "# Bio::NucleicAcid.to_re('yrwskmbdhvnatgc')"
#   p Bio::NucleicAcid.to_re('yrwskmbdhvnatgc')
#
#   puts "# na.weight('A')"
#   p na.weight('A')
#
#   puts "# Bio::NucleicAcid.weight('A')"
#   p Bio::NucleicAcid.weight('A')
#
#   puts "# na.weight('atgc')"
#   p na.weight('atgc')
#
#   puts "# Bio::NucleicAcid.weight('atgc')"
#   p Bio::NucleicAcid.weight('atgc')
#

module Bio

class NucleicAcid

  module Data

    # IUPAC code
    # * Faisst and Meyer (Nucleic Acids Res. 20:3-26, 1992)
    # * http://www.ncbi.nlm.nih.gov/collab/FT/

    NAMES = {

      'y'	=> '[tc]',
      'r'	=> '[ag]',
      'w'	=> '[at]',
      's'	=> '[gc]',
      'k'	=> '[tg]',
      'm'	=> '[ac]',

      'b'	=> '[tgc]',
      'd'	=> '[atg]',
      'h'	=> '[atc]',
      'v'	=> '[agc]',

      'n'	=> '[atgc]',

      'a'	=> 'a',
      't'	=> 't',
      'g'	=> 'g',
      'c'	=> 'c',
      'u'	=> 'u',

      'A'	=> 'Adenine',
      'T'	=> 'Thymine',
      'G'	=> 'Guanine',
      'C'	=> 'Cytosine',
      'U'	=> 'Uracil',

      'Y'	=> 'pYrimidine',
      'R'	=> 'puRine',
      'W'	=> 'Weak',
      'S'	=> 'Strong',
      'K'	=> 'Keto',
      'M'	=> 'aroMatic',

      'B'	=> 'not A',
      'D'	=> 'not C',
      'H'	=> 'not G',
      'V'	=> 'not T',
    }

    WEIGHT = {

      # Calculated by BioPerl's Bio::Tools::SeqStats.pm :-)

      'a'	=> 135.15,
      't'	=> 126.13,
      'g'	=> 151.15,
      'c'	=> 111.12,
      'u'	=> 112.10,

      :adenine	=> 135.15,
      :thymine	=> 126.13,
      :guanine	=> 151.15,
      :cytosine	=> 111.12,
      :uracil	=> 112.10,

      :deoxyribose_phosphate	=> 196.11,
      :ribose_phosphate		=> 212.11,

      :hydrogen	=> 1.00794,
      :water	=> 18.015,

    }

    def weight(x = nil, rna = nil)
      if x
        if x.length > 1
          if rna
            phosphate = WEIGHT[:ribose_phosphate]
          else
            phosphate = WEIGHT[:deoxyribose_phosphate]
          end
          hydrogen    = WEIGHT[:hydrogen]
          water       = WEIGHT[:water]

          total = 0.0
          x.each_byte do |byte|
            base = byte.chr.downcase
            if WEIGHT[base]
              total += WEIGHT[base] + phosphate - hydrogen * 2
            else
              raise "Error: invalid nucleic acid '#{base}'"
            end
          end
          total -= water * (x.length - 1)
        else
          WEIGHT[x.to_s.downcase]
        end
      else
        WEIGHT
      end
    end

    def [](x)
      NAMES[x]
    end

    # backward compatibility
    def names
      NAMES
    end
    alias na names

    def name(x)
      NAMES[x.to_s.upcase]
    end

    def to_re(seq, rna = false)
      replace = {
        'y' => '[tcy]',
        'r' => '[agr]',
        'w' => '[atw]',
        's' => '[gcs]',
        'k' => '[tgk]',
        'm' => '[acm]',
        'b' => '[tgcyskb]',
        'd' => '[atgrwkd]',
        'h' => '[atcwmyh]',
        'v' => '[agcmrsv]',
        'n' => '[atgcyrwskmbdhvn]'
      }
      replace.default = '.'

      str = seq.to_s.downcase
      str.gsub!(/[^atgcu]/) { |na|
        replace[na]
      }
      if rna
        str.tr!("t", "u")
      end
      Regexp.new(str)
    end

  end


  # as instance methods
  include Data

  # as class methods
  extend Data

end

end # module Bio