Class: Bio::Shell::ColoredCodonTable

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/shell/plugin/codon.rb

Constant Summary

@@properties =
{
  :basic => %w( H K R ),
  :polar => %w( S T Y Q N S ),
  :acidic => %w( D E ),
  :nonpolar => %w( F L I M V P A C W G ),
  :stop => %w( * ),
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ColoredCodonTable) initialize(number, cuhash = nil)



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bio/shell/plugin/codon.rb', line 23

def initialize(number, cuhash = nil)
  @aacode = Bio::AminoAcid.names
  @table  = Bio::CodonTable[number]
  @number = number
  @cuhash = cuhash
  setup_colors
  if Bio::Shell.config[:color]
    generate_colored_text
  else
    generate_mono_text
  end
end

Instance Attribute Details

- (Object) table (readonly)

Returns the value of attribute table



35
36
37
# File 'lib/bio/shell/plugin/codon.rb', line 35

def table
  @table
end

Instance Method Details

- (Object) generate_colored_text



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
# File 'lib/bio/shell/plugin/codon.rb', line 76

def generate_colored_text
  @table.each do |codon, aa|
    property, = @@properties.detect {|key, list| list.include?(aa)}

    if aa == '*'
      if @cuhash
        color_code = "#{@colors[:stop]}STOP"
        color_aa = "#{@colors[:stop]}#{aa}"
      else
        color_code = "#{@colors[:stop]}STP"
        case codon
        when 'tga'
          color_aa = "#{@colors[:text]}U"
        when 'tag'
          color_aa = "#{@colors[:text]}O"
        else
          color_aa = "#{@colors[:text]}*"
        end
      end
    else
      color_code = "#{@colors[property]}#{@aacode[aa]}"
      if @table.start_codon?(codon)
        if @cuhash
          color_aa = "#{@colors[:aa]}#{aa}"
        else
          color_aa = "#{@colors[:start]}#{aa}"
        end
      else
        if @cuhash
          color_aa = "#{@colors[property]}#{aa}"
        else
          color_aa = "#{@colors[:aa]}#{aa}"
        end
      end
    end

    if @cuhash
      percent = @cuhash[codon].to_s.rjust(6)
      eval("@#{codon} = '#{color_aa}#{@colors[:text]}#{percent}'")
    else
      eval("@#{codon} = ' #{color_code} #{color_aa}#{@colors[:text]} '")
    end
  end

  @hydrophilic = [
    "#{@colors[:basic]}basic#{@colors[:text]},",
    "#{@colors[:polar]}polar#{@colors[:text]},",
    "#{@colors[:acidic]}acidic#{@colors[:text]}"
  ].join(" ")
  @hydrophobic = "#{@colors[:nonpolar]}nonpolar"
end

- (Object) generate_mono_text



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bio/shell/plugin/codon.rb', line 52

def generate_mono_text
  @table.each do |codon, aa|
    if aa == '*'
      code = 'STOP'
      aa = '' unless @cuhash
    else
      code = @aacode[aa]
    end
    if @cuhash
      percent = @cuhash[codon].to_s.rjust(6)
      eval("@#{codon} = '#{aa}#{percent}'")
    else
      eval("@#{codon} = ' #{code} #{aa} '")
    end
  end

  @hydrophilic = [
    @@properties[:basic].join(" "), "(basic),",
    @@properties[:polar].join(" "), "(polar),",
    @@properties[:acidic].join(" "), "(acidic)",
  ].join(" ")
  @hydrophobic = @@properties[:nonpolar].join(" ") + " (nonpolar)"
end

- (Object) output



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
# File 'lib/bio/shell/plugin/codon.rb', line 128

def output
  header = <<-END
    #
    # = Codon table #{@number} : #{@table.definition}
    #
    #   hydrophilic: #{@hydrophilic}
    #   hydrophobic: #{@hydrophobic}
  END
  table = <<-END
    #
    # *---------------------------------------------*
    # |       |              2nd              |     |
    # |  1st  |-------------------------------| 3rd |
    # |       |  U    |  C    |  A    |  G    |     |
    # |-------+-------+-------+-------+-------+-----|
    # | U   U |#{@ttt}|#{@tct}|#{@tat}|#{@tgt}|  u  |
    # | U   U |#{@ttc}|#{@tcc}|#{@tac}|#{@tgc}|  c  |
    # | U   U |#{@tta}|#{@tca}|#{@taa}|#{@tga}|  a  |
    # |  UUU  |#{@ttg}|#{@tcg}|#{@tag}|#{@tgg}|  g  |
    # |-------+-------+-------+-------+-------+-----|
    # |  CCCC |#{@ctt}|#{@cct}|#{@cat}|#{@cgt}|  u  |
    # | C     |#{@ctc}|#{@ccc}|#{@cac}|#{@cgc}|  c  |
    # | C     |#{@cta}|#{@cca}|#{@caa}|#{@cga}|  a  |
    # |  CCCC |#{@ctg}|#{@ccg}|#{@cag}|#{@cgg}|  g  |
    # |-------+-------+-------+-------+-------+-----|
    # |   A   |#{@att}|#{@act}|#{@aat}|#{@agt}|  u  |
    # |  A A  |#{@atc}|#{@acc}|#{@aac}|#{@agc}|  c  |
    # | AAAAA |#{@ata}|#{@aca}|#{@aaa}|#{@aga}|  a  |
    # | A   A |#{@atg}|#{@acg}|#{@aag}|#{@agg}|  g  |
    # |-------+-------+-------+-------+-------+-----|
    # |  GGGG |#{@gtt}|#{@gct}|#{@gat}|#{@ggt}|  u  |
    # | G     |#{@gtc}|#{@gcc}|#{@gac}|#{@ggc}|  c  |
    # | G GGG |#{@gta}|#{@gca}|#{@gaa}|#{@gga}|  a  |
    # |  GG G |#{@gtg}|#{@gcg}|#{@gag}|#{@ggg}|  g  |
    # *---------------------------------------------*
    #
  END
  if @cuhash
    text = table
  else
    text = header + table
  end
  if Bio::Shell.config[:color]
    text.gsub(/^\s+#/, @colors[:text])
  else
    text.gsub(/^\s+#/, '')
  end
end

- (Object) setup_colors



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bio/shell/plugin/codon.rb', line 37

def setup_colors
  c = Bio::Shell.colors

  @colors = {
    :text		=> c[:none],
    :aa		=> c[:green],
    :start		=> c[:red],
    :stop		=> c[:red],
    :basic		=> c[:cyan],
    :polar		=> c[:blue],
    :acidic		=> c[:magenta],
    :nonpolar	=> c[:yellow],
  }
end