2

我正在寻找一种方法,将字节数组中所有出现的“A”替换为 1,将“T”替换为 2,将“C”替换为 8,将“G”替换为 16。如何才能做到这一点?

4

2 回答 2

1
require "narray"

class NArray
  def cast(type)
    a = NArray.new(type,*self.shape)
    a[] = self
    a
  end
end

conv = NArray.int(256)
atcg = NArray.to_na('ATCG', NArray::BYTE).cast(NArray::LINT)
conv[atcg] = [1,2,8,16]

seq_str = 'ABCDAGDE'
seq_ary = NArray.to_na(seq_str, NArray::BYTE).cast(NArray::LINT)

p conv[seq_ary]
#=> NArray.int(8):
#   [ 1, 0, 8, 0, 1, 16, 0, 0 ]
于 2012-01-06T03:47:04.590 回答
0

是你要找的吗?

h = {'A' => 1, 'T' => 2, 'C' => 8, 'G' => 16}
a = ['A', 'B', 'C', 'D', 'A', 'G', 'D', 'E']

result = a.map {|c| h.include?(c) ? h[c] : c }
于 2012-01-05T12:32:25.873 回答