Extraction of single subarrays from multidimensional array

J

Jesús Gabriel y Galán

Sorry for my bad explanation, in my case I actually know how many
columns are in md-array
i.e how many sub-arrays are to be extracted because I read them as
backtraslated aminocids
from a gene database but this number can change from case to case. So
to make my code of
general use I have to take in consideration this "variable" otherways
I have to change by hand
this number every time I run the program. In the example I provided
this number is 3 but this
number of subarrays can vary. In this respect I wrote "unknown"
dimension.
Sorry again for misunderstanding.

The question is: what do you do with those subarrays? What's common in
both cases?
When you take a look at that answer you can come up with a way of
making a generalized algorithm that can work for different sizes, and
I bet that you don't need to generate a local variable for each
subarray.
For example, say that you have to take each subarray, join the strings
together comma separated and pass it to a method for further
processing:

ss = [ .... your array of arrays ...]

ss.each {|subarray| process_subarray(subarray.join(",")) }

See? You don't need to know there were 2, 3 or 20 subarrays, or even
the length of each subarray.

So, take a look at the requirement from a higher perspective and you
will get to a generalized algorithm.

Jesus.
 
J

Jesús Gabriel y Galán

Dear Robert,

=A0 this is I want to do:

(1) extract subarrays from a md-arrays (but the number of subarrays
=A0 =A0 can vary from case to case)
(2) use separated subarrays to make all possible nucleotide sequences
=A0 =A0 is possible to build from them permutating the codons at each
position
=A0 =A0 (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
=A0 =A0 compatibility with BioRuby classes and methods that deal withDNA
=A0 =A0 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 =3D 0, *a)
return a.join if i =3D=3D size
self.map {|x| sequence(i+1, *(a + [x]))}
end
end

ss =3D [["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.
 
J

Joel VanderWerf

(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:
=> 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 ],
...
=> NArray.object(2):
[ 8, "hello" ]
=> NArray.object(4):
[ 2, 8, "hello", 20 ]
=> NArray.object(3):
[ 12, "hello", 16 ]
 
M

Maurizio Cirilli

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


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

Maurizio Cirilli

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


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top