I am looking for a way to replace all occurrences of 'A' with 1, 'T' with 2, 'C' with 8, and 'G' with 16 in a byte array. How can this be done?
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 ]
Is it what you are looking for?
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 }
Copyright © 2021 Jogjafile Inc.