From: Maurizio Cirilli Date: 2010-10-28T17:45:35+09:00 Subject: Re: Extraction of single subarrays from multidimensional array Dear Jesus, YES this this exactly what I wanted to do. Reading your dense end elegant code, I understood how I was trying to use a wrong approach in writing my own program although some parts of your syntax is still obscure to me. I will study them. I wish to thank all the people who replied to my question offering their so valuable help. -- Maurizio On Oct 27, 6:07 pm, Jesús Gabriel y Galán wrote: > On Wed, Oct 27, 2010 at 2:05 PM, Maurizio Cirilli wrote: > > Dear Robert, > > >   this is I want to do: > > > (1) extract subarrays from a md-arrays (but the number of subarrays > >     can vary from case to case) > > (2) use separated subarrays to make all possible nucleotide sequences > >     is possible to build from them permutating the codons at each > > position > >     (row) i.e. all the 6*2*6 sequences 9 nucleotide long in my > > example > > (3) convert them to string and put in a single array ( this is > > required for > >     compatibility with BioRuby classes and methods that deal withDNA > >     sequences as strings) > > (4) make further analysis on these sequences by BioRuby. > > Looking around for permutations in Ruby, I came acrosshttp://snippets.dzone.com/posts/show/3332 > and adapting it a little bit for your use case, I came up with: > > class Array >   def sequence(i = 0, *a) >     return a.join if i == size >     self[i].map {|x| sequence(i+1, *(a + [x]))} >   end > end > > ss = [["tcg", "agt", "tct", "agc", "tca", "tcc"], >          ["aaa", "aag"], >          ["ctg", "tta", "ctt", "cta", "ctc", "ttg"]] > > p ss.sequence.flatten > > $ ruby permutations.rb > ["tcgaaactg", "tcgaaatta", "tcgaaactt", "tcgaaacta", "tcgaaactc", > "tcgaaattg", "tcgaagctg", "tcgaagtta", "tcgaagctt", "tcgaagcta", > "tcgaagctc", "tcgaagttg", "agtaaactg", "agtaaatta", "agtaaactt", > "agtaaacta", "agtaaactc", "agtaaattg", "agtaagctg", "agtaagtta", > "agtaagctt", "agtaagcta", "agtaagctc", "agtaagttg", "tctaaactg", > "tctaaatta", "tctaaactt", "tctaaacta", "tctaaactc", "tctaaattg", > "tctaagctg", "tctaagtta", "tctaagctt", "tctaagcta", "tctaagctc", > "tctaagttg", "agcaaactg", "agcaaatta", "agcaaactt", "agcaaacta", > "agcaaactc", "agcaaattg", "agcaagctg", "agcaagtta", "agcaagctt", > "agcaagcta", "agcaagctc", "agcaagttg", "tcaaaactg", "tcaaaatta", > "tcaaaactt", "tcaaaacta", "tcaaaactc", "tcaaaattg", "tcaaagctg", > "tcaaagtta", "tcaaagctt", "tcaaagcta", "tcaaagctc", "tcaaagttg", > "tccaaactg", "tccaaatta", "tccaaactt", "tccaaacta", "tccaaactc", > "tccaaattg", "tccaagctg", "tccaagtta", "tccaagctt", "tccaagcta", > "tccaagctc", "tccaagttg"] > > Which if I understood correctly solves your steps 1 to 3. > > Hope this helps, > > Jesus.