Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wobble Hypothesis - Combination or Permutation of Array of strings in ruby

I have a ruby array with 4 strings

nucleotides = ['A', 'G', 'C', 'T']

I need 4^3 combinations of strings.

new_array = ['AGC', 'AGT' .. 'TTT']

This is the wobble hypothesis in genetics. Could someone explain how I could go about achieving this using ruby.

like image 604
Stacked-for-life Avatar asked Feb 12 '23 04:02

Stacked-for-life


2 Answers

use Array#repeated_permutation:

nucleotides = %w(A G C T)
nucleotides.repeated_permutation(3).map(&:join)
#=> ["AAA", "AAG", "AAC", "AAT", "AGA", "AGG", "AGC", "AGT", "ACA", "ACG", "ACC", "ACT", "ATA", "ATG", "ATC", "ATT", "GAA", "GAG", "GAC", "GAT", "GGA", "GGG", "GGC", "GGT", "GCA", "GCG", "GCC", "GCT", "GTA", "GTG", "GTC", "GTT", "CAA", "CAG", "CAC", "CAT", "CGA", "CGG", "CGC", "CGT", "CCA", "CCG", "CCC", "CCT", "CTA", "CTG", "CTC", "CTT", "TAA", "TAG", "TAC", "TAT", "TGA", "TGG", "TGC", "TGT", "TCA", "TCG", "TCC", "TCT", "TTA", "TTG", "TTC", "TTT"]
like image 122
Stefan Avatar answered Feb 15 '23 09:02

Stefan


nucleotides = ['A', 'G', 'C', 'T'] 
(nucleotides*3).permutation(3).to_a.map(&:join).uniq
#=> ["AGC", "AGT", "AGA", "AGG", "ACG", "ACT", "ACA", "ACC", "ATG", "ATC", "ATA", "ATT", "AAG", "AAC", "AAT", "AAA", "GAC", "GAT", "GAA", "GAG", "GCA", "GCT", "GCG", "GCC", "GTA", "GTC", "GTG", "GTT", "GGA", "GGC", "GGT", "GGG", "CAG", "CAT", "CAA", "CAC", "CGA", "CGT", "CGG", "CGC", "CTA", "CTG", "CTC", "CTT", "CCA", "CCG", "CCT", "CCC", "TAG", "TAC", "TAA", "TAT", "TGA", "TGC", "TGG", "TGT", "TCA", "TCG", "TCC", "TCT", "TTA", "TTG", "TTC", "TTT"]
like image 21
fl00r Avatar answered Feb 15 '23 10:02

fl00r