[BioRuby-cvs] bioruby/lib/bio/shell/plugin seq.rb,1.7,1.8

Katayama Toshiaki k at pub.open-bio.org
Tue Nov 15 07:56:41 EST 2005


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

Modified Files:
	seq.rb 
Log Message:
* htmlseq method is added which creates colored sequence using Bio::ColorScheme
* to_naseq, to_aaseq, fill, fold methods are added to String class by default
  in the bioruby shell (moved from lib/bio/extend.rb).


Index: seq.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/shell/plugin/seq.rb,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** seq.rb	14 Nov 2005 02:01:54 -0000	1.7
--- seq.rb	15 Nov 2005 12:56:39 -0000	1.8
***************
*** 28,31 ****
--- 28,32 ----
  
  require 'bio/sequence'
+ require 'bio/util/color_scheme'
  
  module Bio::Shell
***************
*** 66,69 ****
--- 67,100 ----
    end
  
+ 
+   # Convert sequence to colored HTML string
+   def htmlseq(str)
+     if str.instance_of?(String)
+       s = seq(str)
+     elsif str.kind_of?(Bio::Sequence)
+       s = str
+     end
+ 
+     if s.is_a?(Bio::Sequence::AA)
+       scheme = Bio::ColorScheme::Taylor
+     else
+       scheme = Bio::ColorScheme::Nucleotide
+     end
+ 
+     html = %Q[<div style="font-family:monospace;">\n]
+     s.fold(50).each_byte do |c|
+       case c.chr
+       when "\n"
+         html += "<br>\n"
+       else
+         color = scheme[c.chr]
+         html += %Q[<span style="background:\##{color};">#{c.chr}</span>\n]
+       end
+     end
+     html += "</div>\n"
+     return html
+   end
+ 
+ 
    # Displays some basic properties of the sequence.
    def seqstat(str)
***************
*** 131,133 ****
--- 162,230 ----
    end
  
+ end
+ 
+ 
+ class String
+ 
+   def to_naseq
+     Bio::Sequence::NA.new(self)
+   end
+ 
+   def to_aaseq
+     Bio::Sequence::AA.new(self)
+   end
+ 
+   # folding both line end justified
+   def fold(fill_column = 72, indent = 0)
+     str = ''
+ 
+     # size : allowed length of the actual text
+     unless (size = fill_column - indent) > 0
+       raise "[Error] indent > fill_column"
+     end
+ 
+     0.step(self.length - 1, size) do |n|
+       str << ' ' * indent + self[n, size] + "\n"
+     end
+ 
+     return str
+   end
+ 
+   # folding with conscious about word boundaries with prefix string
+   def fill(fill_column = 80, indent = 0, separater = ' ', prefix = '', first_line_only = true)
+ 
+     # size : allowed length of the actual text
+     unless (size = fill_column - indent) > 0
+       raise "[Error] indent > fill_column"
+     end
+ 
+     n = pos = 0
+     str = []
+     while n < self.length
+       pos = self[n, size].rindex(separater)
+ 
+       if self[n, size].length < size	# last line of the folded str
+         pos = nil
+       end
+ 
+       if pos
+         str << self[n, pos+separater.length]
+         n += pos + separater.length
+       else				# line too long or the last line
+         str << self[n, size]
+         n += size
+       end
+     end
+     str = str.join("\n")
+ 
+     str[0,0] = prefix + ' ' * (indent - prefix.length)
+     if first_line_only
+       head = ' ' * indent
+     else
+       head = prefix + ' ' * (indent - prefix.length)
+     end
+     str.gsub!("\n", "\n#{head}")
+ 
+     return str.chomp
+   end
  end



More information about the bioruby-cvs mailing list