By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Reg using hash of arrays in foreach loop

P: 23
Hi all,
I have defined some array variables a
*****************************************
Expand|Select|Wrap|Line Numbers
  1. @L=('CTT','CTC','CTA','CTG','TTA','TTG');
  2.  
  3. @S=('TCT','TCC','TCA','TCG','AGT','AGC');
  4.  
  5. @R=('CGT','CGC','CGA','CGG','AGA','AGG');
  6.  
  7. @V=('GTT','GTC','GTA','GTG');
  8.  
  9. @A=('GCT','GCC','GCA','GCG');
  10.  
  11. @G=('GGT','GGC','GGA','GGG');
  12.  
  13. @P=('CCT','CCC','CCA','CCG');
  14.  
  15. @T=('ACT','ACC','ACA','ACG');
  16.  
  17. @I=('ATT','ATC','ATA');
  18.  
  19. @F=('TTT','TTC');
  20.  
  21. @C=('TGT','TGC');
  22.  
  23. @Y=('TAT','TAC');
  24.  
  25. @Q=('CAA','CAG');
  26.  
  27. @N=('AAT','AAC');
  28.  
  29. @H=('CAT','CAC');
  30.  
  31. @E=('GAA','GAG');
  32.  
  33. @D=('GAT','GAC');
  34.  
  35. @K=('AAA','AAG');
  36.  
  37. @M=('ATG');
  38.  
  39. @W=('TGG');
  40.  
************************************************** *****
and also created a hash to store the arrays in it and finally I wanted to do a foreach loop over the desired input say for example input=VEFCDAMP and to create all possible permutation of the word taken from the definition from dictionary as defined in the array.

The following is the perl script that I conceived to do the job but I am confused here how to use the forloop here to iterate through this and make permutation,
************************************************** **********
Expand|Select|Wrap|Line Numbers
  1. print "\n\n\t\#################### AA 2 PROTEIN #################### \n\n";
  2.  
  3. print "This script will convert your amino acid sequence to DNA Sequence\n\n";
  4.  
  5. print "ENTER THE FILENAME OF THE Amino acid SEQUENCE:= ";
  6.  
  7. $aafilename = <STDIN>;
  8.  
  9. chomp $aafilename;
  10.  
  11. unless ( open(aaFILE, $aafilename) ) {
  12.  
  13. print "Cannot open file \"$aafilename\"\n\n";
  14.  
  15. }
  16.  
  17. @aa = <aaFILE>;
  18.  
  19. close aaFILE;
  20.  
  21. $aa = join( '', @aa);
  22.  
  23. print " \nThe original AA file is:\n$aa \n";
  24.  
  25. $aa =~ s/\s//g;
  26.  
  27. @aa=$aa;
  28.  
  29. my $protein='';
  30.  
  31. my $codon;
  32.  
  33. @L=('CTT','CTC','CTA','CTG','TTA','TTG');
  34.  
  35. @S=('TCT','TCC','TCA','TCG','AGT','AGC');
  36.  
  37. @R=('CGT','CGC','CGA','CGG','AGA','AGG');
  38.  
  39. @V=('GTT','GTC','GTA','GTG');
  40.  
  41. @A=('GCT','GCC','GCA','GCG');
  42.  
  43. @G=('GGT','GGC','GGA','GGG');
  44.  
  45. @P=('CCT','CCC','CCA','CCG');
  46.  
  47. @T=('ACT','ACC','ACA','ACG');
  48.  
  49. @I=('ATT','ATC','ATA');
  50.  
  51. @F=('TTT','TTC');
  52.  
  53. @C=('TGT','TGC');
  54.  
  55. @Y=('TAT','TAC');
  56.  
  57. @Q=('CAA','CAG');
  58.  
  59. @N=('AAT','AAC');
  60.  
  61. @H=('CAT','CAC');
  62.  
  63. @E=('GAA','GAG');
  64.  
  65. @D=('GAT','GAC');
  66.  
  67. @K=('AAA','AAG');
  68.  
  69. @M=('ATG');
  70.  
  71. @W=('TGG');
  72.  
  73.  
  74.  
  75. $L=\@L;
  76.  
  77. $S=\@S;
  78.  
  79. $R=\@R;
  80.  
  81. $V=\@V;
  82.  
  83. $A=\@A;
  84.  
  85. $G=\@G;
  86.  
  87. $P=\@P;
  88.  
  89. $T=\@T;
  90.  
  91. $I=\@I;
  92.  
  93. $F=\@F;
  94.  
  95. $C=\@C;
  96.  
  97. $Y=\@Y;
  98.  
  99. $Q=\@Q;
  100.  
  101. $N=\@N;
  102.  
  103. $H=\@H;
  104.  
  105. $E=\@E;
  106.  
  107. $D=\@D;
  108.  
  109. $K=\@K;
  110.  
  111. $M=\@M;
  112.  
  113. $W=\@W;
  114.  
  115.  
  116.  
  117. %hash = (L=>$L,S=>$S,R=>$R,V=>$V,A=>$A,G=>$G,P=>$P,T=>$T,I=>$I,F=>$F,C=>$C,Y=>$Y,Q=>$Q,N=>$N,H=>$H,E=>$E,D=>$D,K=>$K,M=>$M,W=>$W);
  118.  
  119.  
  120.  
  121. $hash_ref=\%hash;
  122.  
  123.  
  124. my @aa_split = split //, $aa;
  125.  
  126.  
  127.  
  128. foreach my $val (@{hash{@aa_split}})
  129.  
  130. {
  131. print "@{$val} ";
  132.  
  133. }
  134. print " \n";
  135.  
************************************************** ***************

to run this program please create and save a txt file with the content of word VEFCDAMP in it.

I want my program to perform simillar task what the following program does it for
************************************************** **************
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my @V=('GTT','GTC','GTA','GTG');
  4. my @A=('GCT','GCC','GCA','GCG');
  5. my @E=('GAA','GAG');
  6. my @F=('TTT','TTC');
  7. my @G=('GGT','GGC','GGA','GGG');
  8. my @H=('CAT','CAC');
  9. my $i = 1;
  10. foreach my $v (@V) {
  11.    foreach my $a (@A) {
  12.       foreach my $e (@E) {
  13.          foreach my $f (@F) {
  14.             foreach my $g (@G) {
  15.                foreach my $h (@H) {
  16.                   print "$i $v$a$e$f$g$h\n";
  17.                   $i++;
  18.                }
  19.             }
  20.          }
  21.       }
  22.    }
  23. }
  24.  
************************************************** ************

Thanks in advance,
B.Nataraj
Oct 28 '08 #1
Share this Question
Share on Google+
28 Replies


Icecrack
Expert 100+
P: 174
Please look in this thread and consider adding those items necessary to allow the Experts to give you an answer.
(POSTING GUIDELINES: Please read carefully before posting to a forum)

EXPERT
Oct 28 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
And PLEASE use code tags around any and all code you post in the forums. The link that my colleague provided show's their use as well.

Regards,

Moderator
Oct 28 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Assuming everything else is good with your code:

Expand|Select|Wrap|Line Numbers
  1. open (OUT, ">>" , 'path/to/outfile.txt') or die "$!";
  2. my @aa_split = split //, $aa;
  3. foreach my $val (@{hash{@aa_split}}){
  4.    print OUT "@{$val} ";
  5. }
  6. print " \n";
  7. close OUT;
  8.  
Oct 28 '08 #4

P: 23
Assuming everything else is good with your code:

Expand|Select|Wrap|Line Numbers
  1. open (OUT, ">>" , 'path/to/outfile.txt') or die "$!";
  2. my @aa_split = split //, $aa;
  3. foreach my $val (@{hash{@aa_split}}){
  4.    print OUT "@{$val} ";
  5. }
  6. print " \n";
  7. close OUT;
  8.  
Hi Kevin,
The code that you added, does not give the desired output of all permutation of the word "VEFCDAMP". This particular code is the one that I wrote as an extention of your previous code given in the thread ( http://bytes.com/forum/thread847179.html ) for fixed input like "VEFCDAMP" (your code is also pasted in this topic) . I tried here to extend the code for all possible input word (generalization) and the number of possible letter is only 20 (for my case) and the maximum size of any word never going to exceed 10 letters, thats why I thought of iterating only the desired letter in foreach loop to save computational time, otherwise I tried for all possible foreach loop for 20 letters and it goes on and on in my system.

Hope you can understand my problem by reading the previous thread and also this thread.

Thanks in advance,
B.Nataraj
Oct 29 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I'm sorry but your current requirements are out of my range of experience. Trying to make a list of all possible permutations from an arbitrarily deep list of arrays is something I have no experience with.
Oct 29 '08 #6

nithinpes
Expert 100+
P: 410
Nataraj,

When you are taking input dynamically, it would be difficult to get all permutations if the length of input string varies. But if it is fixed at 8 as in VEFCDAMP, then you should be able to do it using 8 foreach loops as you have done in the second script.
Oct 29 '08 #7

P: 23
Nataraj,

When you are taking input dynamically, it would be difficult to get all permutations if the length of input string varies. But if it is fixed at 8 as in VEFCDAMP, then you should be able to do it using 8 foreach loops as you have done in the second script.
Hi nithin & Kevin,
Ok then, Is there any other way to address this problem ? I mean otherthan using foreach loop.

Thanks ,
B.Nataraj
Oct 29 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
This is what I came up with although I am not sure its what you want and it means you will have to install the List::Permutor module and any of its dependencies.

This is a script with all extraneous code removed just so we can get the permutations given a list of letters.

Expand|Select|Wrap|Line Numbers
  1. use List::Permutor;
  2. my $seq = 'VF';
  3. my %hash = ( 
  4.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'],
  5.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'],
  6.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'],
  7.    V => ['GTT','GTC','GTA','GTG'],
  8.    A => ['GCT','GCC','GCA','GCG'],
  9.    G => ['GGT','GGC','GGA','GGG'],
  10.    P => ['CCT','CCC','CCA','CCG'],
  11.    T => ['ACT','ACC','ACA','ACG'],
  12.    I => ['ATT','ATC','ATA'],
  13.    F => ['TTT','TTC'],
  14.    C => ['TGT','TGC'],
  15.    Y => ['TAT','TAC'],
  16.    Q => ['CAA','CAG'],
  17.    N => ['AAT','AAC'],
  18.    H => ['CAT','CAC'],
  19.    E => ['GAA','GAG'],
  20.    D => ['GAT','GAC'],
  21.    K => ['AAA','AAG'],
  22.    M => ['ATG'],
  23.    W => ['TGG'],
  24. );
  25.  
  26. my @loops = split '', $seq;
  27. my @AoA;
  28. foreach my $letter (@loops) {
  29.    push @AoA, @{$hash{$letter}};
  30. }
  31. my $p = List::Permutor->new(@AoA);
  32. {
  33.    local $";
  34.    while(my @set = $p->next) {
  35.       print "@set\n";
  36.    }
  37. }
  38.  
I used a very small list of letters (VF) because for a long string of letters all the possible premutations is a large number and takes a little while to run. Basically 'VF' has 6 unique codons, so there is a possible 720 unique permutations (UP) just for those:

UP = 1*2*3*4*5*6 = 720

if you add one more unique codon the number obviously grows exponentially to 5,040. You will probably be OK until you get into the range of 10 unique codons in which case the UP is 3,628,800. A fast computer could figure out the permutations quickly, but writing them all to a file is what will take time.
Oct 29 '08 #9

P: 23
This is what I came up with although I am not sure its what you want and it means you will have to install the List::Permutor module and any of its dependencies.

This is a script with all extraneous code removed just so we can get the permutations given a list of letters.

Expand|Select|Wrap|Line Numbers
  1. use List::Permutor;
  2. my $seq = 'VF';
  3. my %hash = ( 
  4.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'],
  5.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'],
  6.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'],
  7.    V => ['GTT','GTC','GTA','GTG'],
  8.    A => ['GCT','GCC','GCA','GCG'],
  9.    G => ['GGT','GGC','GGA','GGG'],
  10.    P => ['CCT','CCC','CCA','CCG'],
  11.    T => ['ACT','ACC','ACA','ACG'],
  12.    I => ['ATT','ATC','ATA'],
  13.    F => ['TTT','TTC'],
  14.    C => ['TGT','TGC'],
  15.    Y => ['TAT','TAC'],
  16.    Q => ['CAA','CAG'],
  17.    N => ['AAT','AAC'],
  18.    H => ['CAT','CAC'],
  19.    E => ['GAA','GAG'],
  20.    D => ['GAT','GAC'],
  21.    K => ['AAA','AAG'],
  22.    M => ['ATG'],
  23.    W => ['TGG'],
  24. );
  25.  
  26. my @loops = split '', $seq;
  27. my @AoA;
  28. foreach my $letter (@loops) {
  29.    push @AoA, @{$hash{$letter}};
  30. }
  31. my $p = List::Permutor->new(@AoA);
  32. {
  33.    local $";
  34.    while(my @set = $p->next) {
  35.       print "@set\n";
  36.    }
  37. }
  38.  
I used a very small list of letters (VF) because for a long string of letters all the possible premutations is a large number and takes a little while to run. Basically 'VF' has 6 unique codons, so there is a possible 720 unique permutations (UP) just for those:

UP = 1*2*3*4*5*6 = 720

if you add one more unique codon the number obviously grows exponentially to 5,040. You will probably be OK until you get into the range of 10 unique codons in which case the UP is 3,628,800. A fast computer could figure out the permutations quickly, but writing them all to a file is what will take time.
Hi Kevin,
Thanks once again for your help. It took some time for me to install the List::Permutor module into my system and so a delay in replying you, now your code is working fine but as you said it goes on exponential time for a larger size input. I have not much gone in detail of your code, Hope I soon understand it and will come back to you.

With Thanks,
B.Nataraj
Oct 30 '08 #10

P: 23
Hi Kevin,
Thanks once again for your help. It took some time for me to install the List::Permutor module into my system and so a delay in replying you, now your code is working fine but as you said it goes on exponential time for a larger size input. I have not much gone in detail of your code, Hope I soon understand it and will come back to you.

With Thanks,
B.Nataraj
Hi Kevin,
Sorry about my last reply, I just carried away by seeing new code and implementing new module and running the code. Infact your new code is not doing the right job, To say it short to you , I wanted the output for the word "VF" (you took this word as sample in your code) like the one come out of the following code (This is also your code) , The total permutation would be 512.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my @V=('GTT','GTC','GTA','GTG');
  4. my @A=('GCT','GCC','GCA','GCG');
  5. my @E=('GAA','GAG');
  6. my @F=('TTT','TTC');
  7. my @G=('GGT','GGC','GGA','GGG');
  8. my @H=('CAT','CAC');
  9. my $i = 1;
  10. foreach my $v (@V) {
  11.    foreach my $a (@A) {
  12.       foreach my $e (@E) {
  13.          foreach my $f (@F) {
  14.             foreach my $g (@G) {
  15.                foreach my $h (@H) {
  16.                   print "$i $v$f\n";
  17.                   $i++;
  18.                }
  19.             }
  20.          }
  21.       }
  22.    }
  23. }
  24.  
  25.  
Hope you can now better understand the problem. In mean time I am trying to change your code the way I wanted it is to be.

Thanks for your all help,
B.Nataraj
Oct 31 '08 #11

nithinpes
Expert 100+
P: 410
Hi Kevin,
Sorry about my last reply, I just carried away by seeing new code and implementing new module and running the code. Infact your new code is not doing the right job, To say it short to you , I wanted the output for the word "VF" (you took this word as sample in your code) like the one come out of the following code (This is also your code) , The total permutation would be 512.
Thanks for your all help,
B.Nataraj
The total permutation for 'VF' should not be 512. It will be 8. The script you have shown will just create multiple duplicates to produce 512 results, because of usage of unnecessary foreach loops.
Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3. my @V=('GTT','GTC','GTA','GTG'); 
  4. my @A=('GCT','GCC','GCA','GCG'); 
  5. my @E=('GAA','GAG'); 
  6. my @F=('TTT','TTC'); 
  7. my @G=('GGT','GGC','GGA','GGG'); 
  8. my @H=('CAT','CAC'); 
  9. my $i = 1; 
  10.  
  11.          foreach my $v (@V ){           
  12.                foreach my $f (@F) { 
  13.                   print "$i $v$f\n"; 
  14.                   $i++; 
  15.                } 
  16.             } 
  17.  
  18.  
However, I haven't tested the output using List::Permutor. I will check it in my free-time and compare the output.

-Nithin
Oct 31 '08 #12

P: 23
The total permutation for 'VF' should not be 512. It will be 8. The script you have shown will just create multiple duplicates to produce 512 results, because of usage of unnecessary foreach loops.
Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3. my @V=('GTT','GTC','GTA','GTG'); 
  4. my @A=('GCT','GCC','GCA','GCG'); 
  5. my @E=('GAA','GAG'); 
  6. my @F=('TTT','TTC'); 
  7. my @G=('GGT','GGC','GGA','GGG'); 
  8. my @H=('CAT','CAC'); 
  9. my $i = 1; 
  10.  
  11.          foreach my $v (@V ){           
  12.                foreach my $f (@F) { 
  13.                   print "$i $v$f\n"; 
  14.                   $i++; 
  15.                } 
  16.             } 
  17.  
  18.  
However, I haven't tested the output using List::Permutor. I will check it in my free-time and compare the output.

-Nithin
Hi Nithin,

Sorry about my careless mistake and thanks for correcting it. Looking forward your next reply.

B.Nataraj
Oct 31 '08 #13

KevinADC
Expert 2.5K+
P: 4,059
The total permutation for 'VF' should not be 512. It will be 8. The script you have shown will just create multiple duplicates to produce 512 results, because of usage of unnecessary foreach loops.
Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3. my @V=('GTT','GTC','GTA','GTG'); 
  4. my @A=('GCT','GCC','GCA','GCG'); 
  5. my @E=('GAA','GAG'); 
  6. my @F=('TTT','TTC'); 
  7. my @G=('GGT','GGC','GGA','GGG'); 
  8. my @H=('CAT','CAC'); 
  9. my $i = 1; 
  10.  
  11.          foreach my $v (@V ){           
  12.                foreach my $f (@F) { 
  13.                   print "$i $v$f\n"; 
  14.                   $i++; 
  15.                } 
  16.             } 
  17.  
  18.  
However, I haven't tested the output using List::Permutor. I will check it in my free-time and compare the output.

-Nithin
8? Then you are not talking about permutations. VF will produce 512 unique permutations with no duplicates assuming all 6 codons can be in all six positions of each permutation.

Using VF as an example, I think your requirements are:

V (first position)
F (second position)

So all the codons in @V can only ever occupy the first position of the permutations you desire. All codons in @F can only occupy the second position. There are only two positions because that is the number of letters/arrays. Using VFas the example, there can not be a permutation with TTT or TTC in the first position, for example:

TTTGTT
TTCGTT

Is that correct?
Oct 31 '08 #14

P: 23
8? Then you are not talking about permutations. VF will produce 512 unique permutations with no duplicates assuming all 6 codons can be in all six positions of each permutation.

Using VF as an example, I think your requirements are:

V (first position)
F (second position)

So all the codons in @V can only ever occupy the first position of the permutations you desire. All codons in @F can only occupy the second position. There are only two positions because that is the number of letters/arrays. Using VFas the example, there can not be a permutation with TTT or TTC in the first position, for example:

TTTGTT
TTCGTT

Is that correct?

Hi Kevin,

Exactly... yea the position should not be interchanged.

Thanks,
B.Nataraj
Oct 31 '08 #15

nithinpes
Expert 100+
P: 410
Hi Kevin,

Exactly... yea the position should not be interchanged.

Thanks,
B.Nataraj
List::Permutor is not the module of choice then. It will produce all possible combinations/ orders of list elements. E.g for a list with elements - 'A', 'B', 'C'.,
the possible combinations are:
A, B, C
A, C, B
B, A, C
B, C, A
C, A, B
C, B, A

But, what you are looking for is something like A can be constituted by three possible sets of codons, B by two etc.
I believe there are Bio-Perl modules which do the job that you are looking for.
Oct 31 '08 #16

P: 23
List::Permutor is not the module of choice then. It will produce all possible combinations/ orders of list elements. E.g for a list with elements - 'A', 'B', 'C'.,
the possible combinations are:
A, B, C
A, C, B
B, A, C
B, C, A
C, A, B
C, B, A

But, what you are looking for is something like A can be constituted by three possible sets of codons, B by two etc.
I believe there are Bio-Perl modules which do the job that you are looking for.

Hi Nithin,

Before coming to this forum, I tried possible code for the same in bioperl module and all possible code depository for any snippet to do my objective but my search ended in vein. Technically the approach is called "Back-translation"(bioinformatics term). More the Bioinformatics problem that I try to address here is so unique and rare and it may not be a regular requirement for many , thatís why I suppose that this is not there to find in bioperl. thatís how I started to develop my own code and sought the help from this forum. Anyhow I would try again there then.

Thanks ,
B.Nataraj
Oct 31 '08 #17

nithinpes
Expert 100+
P: 410
Hi Nithin,

Before coming to this forum, I tried possible code for the same in bioperl module and all possible code depository for any snippet to do my objective but my search ended in vein. Technically the approach is called "Back-translation"(bioinformatics term). More the Bioinformatics problem that I try to address here is so unique and rare and it may not be a regular requirement for many , thatís why I suppose that this is not there to find in bioperl. thatís how I started to develop my own code and sought the help from this forum. Anyhow I would try again there then.

Thanks ,
B.Nataraj

Here is one way of doing it. This code dynamically generates required foreach loops and executes the command.

Expand|Select|Wrap|Line Numbers
  1.  my $seq = 'VF'; 
  2. my %hash = (  
  3.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'], 
  4.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'], 
  5.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'], 
  6.    V => ['GTT','GTC','GTA','GTG'], 
  7.    A => ['GCT','GCC','GCA','GCG'], 
  8.    G => ['GGT','GGC','GGA','GGG'], 
  9.    P => ['CCT','CCC','CCA','CCG'], 
  10.    T => ['ACT','ACC','ACA','ACG'], 
  11.    I => ['ATT','ATC','ATA'], 
  12.    F => ['TTT','TTC'], 
  13.    C => ['TGT','TGC'], 
  14.    Y => ['TAT','TAC'], 
  15.    Q => ['CAA','CAG'], 
  16.    N => ['AAT','AAC'], 
  17.    H => ['CAT','CAC'], 
  18.    E => ['GAA','GAG'], 
  19.    D => ['GAT','GAC'], 
  20.    K => ['AAA','AAG'], 
  21.    M => ['ATG'], 
  22.    W => ['TGG'], 
  23. ); 
  24.  
  25. my @loops = split '', $seq; 
  26. my $cmd;
  27. local $i=0;
  28. my $vars;
  29.  
  30. foreach my $letter (@loops) {
  31.     my ($str,$v)= createloop($letter);
  32.     $vars.=$v;
  33.     $cmd.=$str;
  34. $cmd=$cmd.'print'. " \"$vars\\n\";";  ## adding print statement
  35. $cmd.= '}'x$i;       ### ending statement blocks
  36.  
  37. eval {eval $cmd; };   ## first eval returns statement block
  38.  
  39. ##creating foreach loops 
  40. sub createloop {
  41. $i++;
  42. my $var ='$a'.$i;
  43. return ("foreach $var (qw(@{$hash{$_[0]}})) {",$var) ;
  44.  
  45. }
  46.  
Oct 31 '08 #18

P: 23
Here is one way of doing it. This code dynamically generates required foreach loops and executes the command.

Expand|Select|Wrap|Line Numbers
  1.  my $seq = 'VF'; 
  2. my %hash = (  
  3.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'], 
  4.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'], 
  5.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'], 
  6.    V => ['GTT','GTC','GTA','GTG'], 
  7.    A => ['GCT','GCC','GCA','GCG'], 
  8.    G => ['GGT','GGC','GGA','GGG'], 
  9.    P => ['CCT','CCC','CCA','CCG'], 
  10.    T => ['ACT','ACC','ACA','ACG'], 
  11.    I => ['ATT','ATC','ATA'], 
  12.    F => ['TTT','TTC'], 
  13.    C => ['TGT','TGC'], 
  14.    Y => ['TAT','TAC'], 
  15.    Q => ['CAA','CAG'], 
  16.    N => ['AAT','AAC'], 
  17.    H => ['CAT','CAC'], 
  18.    E => ['GAA','GAG'], 
  19.    D => ['GAT','GAC'], 
  20.    K => ['AAA','AAG'], 
  21.    M => ['ATG'], 
  22.    W => ['TGG'], 
  23. ); 
  24.  
  25. my @loops = split '', $seq; 
  26. my $cmd;
  27. local $i=0;
  28. my $vars;
  29.  
  30. foreach my $letter (@loops) {
  31.     my ($str,$v)= createloop($letter);
  32.     $vars.=$v;
  33.     $cmd.=$str;
  34. $cmd=$cmd.'print'. " \"$vars\\n\";";  ## adding print statement
  35. $cmd.= '}'x$i;       ### ending statement blocks
  36.  
  37. eval {eval $cmd; };   ## first eval returns statement block
  38.  
  39. ##creating foreach loops 
  40. sub createloop {
  41. $i++;
  42. my $var ='$a'.$i;
  43. return ("foreach $var (qw(@{$hash{$_[0]}})) {",$var) ;
  44.  
  45. }
  46.  
Hi Nithin,

Thank you very much for the code, it does great and meet my objective.
I really thankful for you and Kevin for extending your kind help to me.

You guys are doing really a fantastic job.

Thanks once again,
B.Nataraj
Oct 31 '08 #19

KevinADC
Expert 2.5K+
P: 4,059
nithinpes has posted a very interesting solution, but I urge caution using it since eval will run any code it evaluates. Just make sure you don't accidently introduce something unwise into the eval block. Its fine as its written, but read up on eval before making any changes that might have serious side effects.
Oct 31 '08 #20

KevinADC
Expert 2.5K+
P: 4,059
Heres a solution that does not use eval, but I would benchmark my code and nithinpes code when you get a chance and see if one is better than the other.

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict; 
  3. my $seq = 'VF'; 
  4. my %hash = (  
  5.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'], 
  6.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'], 
  7.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'], 
  8.    V => ['GTT','GTC','GTA','GTG'], 
  9.    A => ['GCT','GCC','GCA','GCG'], 
  10.    G => ['GGT','GGC','GGA','GGG'], 
  11.    P => ['CCT','CCC','CCA','CCG'], 
  12.    T => ['ACT','ACC','ACA','ACG'], 
  13.    I => ['ATT','ATC','ATA'], 
  14.    F => ['TTT','TTC'], 
  15.    C => ['TGT','TGC'], 
  16.    Y => ['TAT','TAC'], 
  17.    Q => ['CAA','CAG'], 
  18.    N => ['AAT','AAC'], 
  19.    H => ['CAT','CAC'], 
  20.    E => ['GAA','GAG'], 
  21.    D => ['GAT','GAC'], 
  22.    K => ['AAA','AAG'], 
  23.    M => ['ATG'], 
  24.    W => ['TGG'], 
  25. ); 
  26.  
  27. my @letters = split '', $seq; 
  28. my $loops = 1;
  29. foreach my $L (@letters) {
  30.    $loops *= @{$hash{$L}};
  31. }
  32. my @perms;
  33. foreach my $S (@letters) {
  34.    my @t = @{$hash{$S}};
  35.    for my $i (0..$loops-1) {
  36.       my $c = shift @t;
  37.       $perms[$i] .= $c;
  38.       push @t, $c;
  39.    }
  40. }
  41. my $i = 1;
  42. foreach my $s (@perms) {
  43.    print $i++, " $s\n";
  44. }
  45.  
I'm not sure how efficient my use of "shift" and "push" is to loop through the arrays over and over the number of times necessary.
Oct 31 '08 #21

P: 23
Heres a solution that does not use eval, but I would benchmark my code and nithinpes code when you get a chance and see if one is better than the other.

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict; 
  3. my $seq = 'VF'; 
  4. my %hash = (  
  5.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'], 
  6.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'], 
  7.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'], 
  8.    V => ['GTT','GTC','GTA','GTG'], 
  9.    A => ['GCT','GCC','GCA','GCG'], 
  10.    G => ['GGT','GGC','GGA','GGG'], 
  11.    P => ['CCT','CCC','CCA','CCG'], 
  12.    T => ['ACT','ACC','ACA','ACG'], 
  13.    I => ['ATT','ATC','ATA'], 
  14.    F => ['TTT','TTC'], 
  15.    C => ['TGT','TGC'], 
  16.    Y => ['TAT','TAC'], 
  17.    Q => ['CAA','CAG'], 
  18.    N => ['AAT','AAC'], 
  19.    H => ['CAT','CAC'], 
  20.    E => ['GAA','GAG'], 
  21.    D => ['GAT','GAC'], 
  22.    K => ['AAA','AAG'], 
  23.    M => ['ATG'], 
  24.    W => ['TGG'], 
  25. ); 
  26.  
  27. my @letters = split '', $seq; 
  28. my $loops = 1;
  29. foreach my $L (@letters) {
  30.    $loops *= @{$hash{$L}};
  31. }
  32. my @perms;
  33. foreach my $S (@letters) {
  34.    my @t = @{$hash{$S}};
  35.    for my $i (0..$loops-1) {
  36.       my $c = shift @t;
  37.       $perms[$i] .= $c;
  38.       push @t, $c;
  39.    }
  40. }
  41. my $i = 1;
  42. foreach my $s (@perms) {
  43.    print $i++, " $s\n";
  44. }
  45.  
I'm not sure how efficient my use of "shift" and "push" is to loop through the arrays over and over the number of times necessary.
Hi Kevin,

Thanks for your new code and pointing out possible pitfall. In fact I am half way down my real work and another half way to pass on. I am making the remaining code and hopefully that wont be so difficult logic to make it out and all about string manipulation and once entire code is ready and I will be in a position to check yours and nithin for a real time problem and I hope can give you my feedback very soon.


B.Nataraj
Nov 1 '08 #22

P: 23
Hi Kevin,

The last code you have posted, is in fact repeating the first four sequence as output but it make up to the desired 512 (for a input seqnence of VAEFGH) sequence as per rule. But the alternative code by Nithin is working just fine for creating all possible combination and there is no repetition.

I just happened to compare this bug in your code at the last stage of implementing in my entire code using your code as a starting point. I could not fix in it and possibly you can do it quickly so I am posting it to you once again.

Thanks,
B.Nataraj
Nov 5 '08 #23

P: 23
(I tried to delete this message, but I could not do it)

Hi Kevin,
Sorry I did not mention my input sequence in my previous message, here it is "VAEFGH".

Thanks,
B.Nataraj
Nov 5 '08 #24

KevinADC
Expert 2.5K+
P: 4,059
(I tried to delete this message, but I could not do it)

Hi Kevin,
Sorry I did not mention my input sequence in my previous message, here it is "VAEFGH".

Thanks,
B.Nataraj
hmmm...guess I made a logical error in the code I wrote. I'll look at it later today and see if I can find the error.
Nov 5 '08 #25

KevinADC
Expert 2.5K+
P: 4,059
well, I can't get it to work. I think you are will need to use nithinpes code or wait and see if someone else can help or post on www.perlmonks.com and see if one of the members there has a suggestion.
Nov 6 '08 #26

P: 23
Hi Kevin,
Ok, I will use nithin's code then.

Thanks,
B.Nataraj
Nov 6 '08 #27

P: 23
Here is one way of doing it. This code dynamically generates required foreach loops and executes the command.

Expand|Select|Wrap|Line Numbers
  1.  my $seq = 'VF'; 
  2. my %hash = (  
  3.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'], 
  4.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'], 
  5.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'], 
  6.    V => ['GTT','GTC','GTA','GTG'], 
  7.    A => ['GCT','GCC','GCA','GCG'], 
  8.    G => ['GGT','GGC','GGA','GGG'], 
  9.    P => ['CCT','CCC','CCA','CCG'], 
  10.    T => ['ACT','ACC','ACA','ACG'], 
  11.    I => ['ATT','ATC','ATA'], 
  12.    F => ['TTT','TTC'], 
  13.    C => ['TGT','TGC'], 
  14.    Y => ['TAT','TAC'], 
  15.    Q => ['CAA','CAG'], 
  16.    N => ['AAT','AAC'], 
  17.    H => ['CAT','CAC'], 
  18.    E => ['GAA','GAG'], 
  19.    D => ['GAT','GAC'], 
  20.    K => ['AAA','AAG'], 
  21.    M => ['ATG'], 
  22.    W => ['TGG'], 
  23. ); 
  24.  
  25. my @loops = split '', $seq; 
  26. my $cmd;
  27. local $i=0;
  28. my $vars;
  29.  
  30. foreach my $letter (@loops) {
  31.     my ($str,$v)= createloop($letter);
  32.     $vars.=$v;
  33.     $cmd.=$str;
  34. $cmd=$cmd.'print'. " \"$vars\\n\";";  ## adding print statement
  35. $cmd.= '}'x$i;       ### ending statement blocks
  36.  
  37. eval {eval $cmd; };   ## first eval returns statement block
  38.  
  39. ##creating foreach loops 
  40. sub createloop {
  41. $i++;
  42. my $var ='$a'.$i;
  43. return ("foreach $var (qw(@{$hash{$_[0]}})) {",$var) ;
  44.  
  45. }
  46.  
Hi Nithinpes,
I am using your code posted by you in this thread for my work. Now I had a little problem in using your code to direct the output to a file. I could not do it since I could not understand the code properly, especially the eval statement and its contribution. Kindly help me to add an extra code to direct the generated sequence to save in a text file.

Thanks,
B.Nataraj
Nov 7 '08 #28

P: 23
Hi Nithinpes,
I am using your code posted by you in this thread for my work. Now I had a little problem in using your code to direct the output to a file. I could not do it since I could not understand the code properly, especially the eval statement and its contribution. Kindly help me to add an extra code to direct the generated sequence to save in a text file.

Thanks,
B.Nataraj
Hi Nithinpes,

Yes, I done it myself by following way. Please igonre my post.


Expand|Select|Wrap|Line Numbers
  1. print "\n\n\t\#################### AA 2 PROTEIN #################### \n\n";
  2. print "This script will convert your amino acid sequence to DNA Sequence\n\n";
  3. print "ENTER THE FILENAME OF THE Amino acid SEQUENCE:= ";
  4. $aafilename = <STDIN>;
  5. chomp $aafilename;
  6.  
  7. unless ( open(aaFILE, $aafilename) ) {
  8.     print "Cannot open file \"$aafilename\"\n\n";
  9. }
  10. @aa = <aaFILE>;
  11. close aaFILE;
  12. $aa = join( '', @aa);
  13.  
  14. my $seq = "$aa";  
  15. my %hash = (   
  16.    L => ['CTT','CTC','CTA','CTG','TTA','TTG'],  
  17.    S => ['TCT','TCC','TCA','TCG','AGT','AGC'],  
  18.    R => ['CGT','CGC','CGA','CGG','AGA','AGG'],  
  19.    V => ['GTT','GTC','GTA','GTG'],  
  20.    A => ['GCT','GCC','GCA','GCG'],  
  21.    G => ['GGT','GGC','GGA','GGG'],  
  22.    P => ['CCT','CCC','CCA','CCG'],  
  23.    T => ['ACT','ACC','ACA','ACG'],  
  24.    I => ['ATT','ATC','ATA'],  
  25.    F => ['TTT','TTC'],  
  26.    C => ['TGT','TGC'],  
  27.    Y => ['TAT','TAC'],  
  28.    Q => ['CAA','CAG'],  
  29.    N => ['AAT','AAC'],  
  30.    H => ['CAT','CAC'],  
  31.    E => ['GAA','GAG'],  
  32.    D => ['GAT','GAC'],  
  33.    K => ['AAA','AAG'],  
  34.    M => ['ATG'],  
  35.    W => ['TGG'],  
  36. );  
  37.   open(OUT, ">>dna_outfile.txt"); 
  38.  
  39. my @loops = split '', $seq;  
  40. my $cmd; 
  41. local $i=0; 
  42. my $vars; 
  43. foreach my $letter (@loops) { 
  44.     my ($str,$v)= createloop($letter); 
  45.     $vars.=$v; 
  46.     $cmd.=$str; 
  47. }  
  48. $cmd=$cmd.'print OUT'. " \"$vars\\n\";";  ## adding print statement 
  49. #$cmd=$cmd.'print'. " \"$vars\\n\";";  ## adding print statement 
  50. $cmd.= '}'x$i;  ;     ### ending statement blocks 
  51.  
  52. eval {eval $cmd; };   ## first eval returns statement block 
  53.  
  54. ##creating foreach loops  
  55. sub createloop { 
  56. $i++; 
  57. my $var ='$a'.$i; 
  58. return ("foreach $var (qw(@{$hash{$_[0]}})) {",$var) ; 
  59.  
  60.  
  61.  
  62.  
  63. close OUT;
Nov 7 '08 #29

Post your reply

Sign in to post your reply or Sign up for a free account.