class Bio::Shell::ColoredCodonTable

Attributes

table[R]

Public Class Methods

new(number, cuhash = nil) click to toggle source
# 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

Public Instance Methods

generate_colored_text() click to toggle source
# 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
generate_mono_text() click to toggle source
# 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
output() click to toggle source
# 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
setup_colors() click to toggle source
# 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