Extraction of single subarrays from multidimensional array

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 626 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.

That’s it.

While is clear to me how to do points 2,3 and 4 I really struggled
how
to accomplish point 1 , which is the subject of this thread.

– Maurizio

On Wed, Oct 27, 2010 at 2:05 PM, Maurizio C. [email protected]
wrote:

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 across
http://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.

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:07pm, Jess Gabriel y Galn [email protected]

On 10/27/2010 05:05 AM, Maurizio C. wrote:

(1) extract subarrays from a md-arrays (but the number of subarrays
can vary from case to case)

I’m not sure I’ve understood what you’re looking for, but it might be
useful to look into the NArray gem. NArrays can store multidimensional
data (not just numeric data) and slice-and-dice that data in a boggling
variety of ways.

An example:

a = NArray.object(2,3,4).indgen
=> NArray.object(2,3,4):
[ [ [ 0, 1 ],
[ 2, 3 ],
[ 4, 5 ] ],
[ [ 6, 7 ],
[ 8, 9 ],
[ 10, 11 ] ],
[ [ 12, 13 ],
[ 14, 15 ],
[ 16, 17 ] ],
[ [ 18, 19 ],

a[0,1,2] = “hello”
=> “hello”

a
=> NArray.object(2,3,4):
[ [ [ 0, 1 ],
[ 2, 3 ],
[ 4, 5 ] ],
[ [ 6, 7 ],
[ 8, 9 ],
[ 10, 11 ] ],
[ [ 12, 13 ],
[ “hello”, 15 ],
[ 16, 17 ] ],
[ [ 18, 19 ],

a[0,1,1…2]
=> NArray.object(2):
[ 8, “hello” ]

a[0,1,true]
=> NArray.object(4):
[ 2, 8, “hello”, 20 ]

a[0,true,2]
=> NArray.object(3):
[ 12, “hello”, 16 ]

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:07pm, Jess Gabriel y Galn [email protected]