-
Notifications
You must be signed in to change notification settings - Fork 2
/
viruses.rb
69 lines (61 loc) · 1.13 KB
/
viruses.rb
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
# ----------------------------------------------------------------------------
class Viruses
def initialize(file)
@a = []
File.new(file).each_line { |l|
l.strip!
next if l.empty?
next if l =~ /^\#/
parts = l.split(':')
# parts[2].gsub!('xxx','AGG')
# parts[2].gsub!('yyy','CTG')
parts[2].gsub!(/(xxx[^y]+yyy)/) { |m|
'x' * $1.size
}
@a.push({:name => parts[0], :data => parts[2]})
}
end
def nucl_stat
t = {}
@a.each { |vir|
val = vir[:data]
(val.size / 3).times { |i|
triplet = val[i * 3, 3]
if t.has_key? triplet then
t[triplet] += 1
else
t[triplet] = 1
end
}
}
t.keys.sort.each { |k|
puts "#{k} #{t[k]}"
}
end
def convert(s)
s.split(//).collect { |chr|
case chr
when 'A' then 1
when 'C' then 2
when 'G' then 4
when 'T' then 8
else
0
end
}
end
def show
@a.each { |p|
puts "#{p[:name]} #{p[:data]}"
}
end
def numbers
@a.collect { |p|
num = convert(p[:data]).join('')
num.gsub!(/^0+/,'')
num.gsub!(/0+$/,'')
num.hex
}
end
end
# ----------------------------------------------------------------------------