File: color_scheme_aa.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 (82 lines) | stat: -rw-r--r-- 1,789 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env ruby
#
# color_scheme_aa.rb - A Bio::ColorScheme demo script for Amino Acid sequences.
#
#  Usage:
#
#   % ruby color_scheme_aa.rb > cs-seq-faa.html
#
#   % cat seq.faa
#   >AA_sequence
#   MKRISTTITTTITITTGNGAG
#   % ruby color_scheme_aa.rb seq.faa > colored-seq-faa.html
#
#
# Copyright::  Copyright (C) 2005
#              Mitsuteru C. Nakao <n@bioruby.org>
# License::    The Ruby License
#

require 'bio'


# returns folded sequence with <br>.
def br(i, width = 80)
  return "<br\n>"  if i % width == 0
  ""
end


# returns sequence html doc
def display(seq, cs)
  html = '<p style="font-family: monospace">'
  postfix = '</span>'
  i = 0
  seq.each_char do |c|
    color = cs[c]
    prefix = %Q(<span style="background:\##{color};">)
    html += prefix + c + postfix
    html += br(i += 1)
  end
  html + '</p>'
end


# returns scheme wise html doc
def display_scheme(scheme, aaseq)
  html = ''
  cs = Bio::ColorScheme.const_get(scheme.intern)
  [aaseq].each do |seq|
    html += display(seq, cs)
  end
  return  ['<div>', "<h3>#{cs}</h3>", html, '</div>']
end



if fna = ARGV.shift
  aaseq = Bio::FlatFile.open(fna) { |ff| ff.next_entry.aaseq }
else
  aaseq = Bio::Sequence::AA.new('ARNDCQEGHILKMFPSTWYV' * 20).randomize
end

title =  'Bio::ColorScheme for amino acid sequences'
doc = ['<html>',
       '<header>', '<title>', title, '</title>', '</header>',
       '<body>',  '<h1>', title, '</h1>']

doc << ['<div>', '<h2>', 'Simple colors', '</h2>']

['Zappo', 'Taylor' ].each do |scheme|
  doc << display_scheme(scheme, aaseq)
end
doc << ['</div>']


doc << ['<div>', '<h2>', 'Score colors', '</h2>']
['Buried', 'Helix', 'Hydropathy', 'Strand', 'Turn'].each do |score|
  doc << display_scheme(score, aaseq)
end
doc << ['</div>']

puts doc + ['</body>','</html>']