[BioRuby-cvs] bioruby/lib/bio/shell/plugin codon.rb,NONE,1.1

Katayama Toshiaki k at pub.open-bio.org
Sat Nov 5 03:24:26 EST 2005


Update of /home/repository/bioruby/bioruby/lib/bio/shell/plugin
In directory pub.open-bio.org:/tmp/cvs-serv29378/lib/bio/shell/plugin

Added Files:
	codon.rb 
Log Message:
* plugin to use codon table is added


--- NEW FILE: codon.rb ---
#
# = bio/shell/plugin/codon.rb - plugin for the codon table
#
# Copyright::	Copyright (C) 2005
#		Toshiaki Katayama <k at bioruby.org>
# Lisence::	LGPL
#
# $Id: codon.rb,v 1.1 2005/11/05 08:24:24 k Exp $
#
#--
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#++
#

require 'bio/data/codontable'
require 'bio/data/aa'

module Bio::Shell

  class ColoredCodonTable

    Color = Bio::Shell::Core::ESC_SEQ

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

    @@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( * ),
    }

    def initialize(number, color = true)
      @aacode = Bio::AminoAcid.names
      @table  = Bio::CodonTable[number]
      @number = number
      if color
        generate_colored_text
      else
        generate_mono_text
      end
    end
    attr_reader :table

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

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

    def generate_colored_text
      @table.each do |codon, aa|
        property, = @@properties.detect {|key, list| list.include?(aa)}
        if aa == '*'
          color_code = "#{@@colors[:stop]}STOP"
          color_aa = ""
        else
          color_code = "#{@@colors[property]}#{@aacode[aa]}"
          if @table.start_codon?(codon)
            color_aa = "#{@@colors[:start]}#{aa}"
          else
            color_aa = "#{@@colors[:aa]}#{aa}"
          end
        end
        eval("@#{codon} = ' #{color_code} #{color_aa}#{@@colors[:text]} '")
      end

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

    def output
      text = <<-END
        #
        # = Codon table #{@number} : #{@table.definition}
        #
        #   hydrophilic: #{@hydrophilic}
        #   hydrophobic: #{@hydrophobic}
        #
        # *---------------------------------------------*
        # |       |              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
      text.gsub(/^\s+#/, @@colors[:text])
    end

  end

  private

  def codontable(num = 1)
    cct = ColoredCodonTable.new(num, $bioruby_config[:COLOR])
    display cct.output
    return cct.table
  end

  def codontables
    Bio::CodonTable::Definitions.sort.each do |i, definition|
      puts "#{i}\t#{definition}"
    end
  end

end



More information about the bioruby-cvs mailing list