我正在寻找一种方法来替换所有出现的'A'为1,'T'用2,'C'用8替换,'G'用16替换为字节数组。怎么办呢?
答案 0 :(得分: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 ]
答案 1 :(得分: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 }