Reg using hash of arrays in foreach loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • natarajmtech
    New Member
    • Oct 2008
    • 23

    Reg using hash of arrays in foreach loop

    Hi all,
    I have defined some array variables a
    *************** *************** ***********
    Code:
    @L=('CTT','CTC','CTA','CTG','TTA','TTG');
    
    @S=('TCT','TCC','TCA','TCG','AGT','AGC');
    
    @R=('CGT','CGC','CGA','CGG','AGA','AGG');
    
    @V=('GTT','GTC','GTA','GTG');
    
    @A=('GCT','GCC','GCA','GCG');
    
    @G=('GGT','GGC','GGA','GGG');
    
    @P=('CCT','CCC','CCA','CCG');
    
    @T=('ACT','ACC','ACA','ACG');
    
    @I=('ATT','ATC','ATA');
    
    @F=('TTT','TTC');
    
    @C=('TGT','TGC');
    
    @Y=('TAT','TAC');
    
    @Q=('CAA','CAG');
    
    @N=('AAT','AAC');
    
    @H=('CAT','CAC');
    
    @E=('GAA','GAG');
    
    @D=('GAT','GAC');
    
    @K=('AAA','AAG');
    
    @M=('ATG');
    
    @W=('TGG');
    *************** *************** *************** **********
    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,
    *************** *************** *************** ***************
    Code:
    print "\n\n\t\#################### AA 2 PROTEIN #################### \n\n";
    
    print "This script will convert your amino acid sequence to DNA Sequence\n\n";
    
    print "ENTER THE FILENAME OF THE Amino acid SEQUENCE:= ";
    
    $aafilename = <STDIN>;
    
    chomp $aafilename;
    
    unless ( open(aaFILE, $aafilename) ) {
    
    print "Cannot open file \"$aafilename\"\n\n";
    
    }
    
    @aa = <aaFILE>;
    
    close aaFILE;
    
    $aa = join( '', @aa);
    
    print " \nThe original AA file is:\n$aa \n";
    
    $aa =~ s/\s//g;
    
    @aa=$aa;
    
    my $protein='';
    
    my $codon;
    
    @L=('CTT','CTC','CTA','CTG','TTA','TTG');
    
    @S=('TCT','TCC','TCA','TCG','AGT','AGC');
    
    @R=('CGT','CGC','CGA','CGG','AGA','AGG');
    
    @V=('GTT','GTC','GTA','GTG');
    
    @A=('GCT','GCC','GCA','GCG');
    
    @G=('GGT','GGC','GGA','GGG');
    
    @P=('CCT','CCC','CCA','CCG');
    
    @T=('ACT','ACC','ACA','ACG');
    
    @I=('ATT','ATC','ATA');
    
    @F=('TTT','TTC');
    
    @C=('TGT','TGC');
    
    @Y=('TAT','TAC');
    
    @Q=('CAA','CAG');
    
    @N=('AAT','AAC');
    
    @H=('CAT','CAC');
    
    @E=('GAA','GAG');
    
    @D=('GAT','GAC');
    
    @K=('AAA','AAG');
    
    @M=('ATG');
    
    @W=('TGG');
    
    
    
    $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;
    
    
    
    %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);
    
    
    
    $hash_ref=\%hash;
    
    
    my @aa_split = split //, $aa;
    
    
    
    foreach my $val (@{hash{@aa_split}})
    
    {
    print "@{$val} ";
    
    }
    print " \n";
    *************** *************** *************** *************** *****

    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
    *************** *************** *************** *************** ****
    Code:
    use strict;
    use warnings;
    my @V=('GTT','GTC','GTA','GTG');
    my @A=('GCT','GCC','GCA','GCG');
    my @E=('GAA','GAG');
    my @F=('TTT','TTC');
    my @G=('GGT','GGC','GGA','GGG');
    my @H=('CAT','CAC');
    my $i = 1;
    foreach my $v (@V) {
       foreach my $a (@A) {
          foreach my $e (@E) {
             foreach my $f (@F) {
                foreach my $g (@G) {
                   foreach my $h (@H) {
                      print "$i $v$a$e$f$g$h\n";
                      $i++;
                   }
                }
             }
          }
       }
    }
    *************** *************** *************** *************** **

    Thanks in advance,
    B.Nataraj
    Last edited by numberwhun; Oct 28 '08, 12:16 PM. Reason: Please use code tags
  • Icecrack
    Recognized Expert New Member
    • Sep 2008
    • 174

    #2
    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

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Assuming everything else is good with your code:

        Code:
        open (OUT, ">>" , 'path/to/outfile.txt') or die "$!";
        my @aa_split = split //, $aa;
        foreach my $val (@{hash{@aa_split}}){
           print OUT "@{$val} ";
        }
        print " \n";
        close OUT;

        Comment

        • natarajmtech
          New Member
          • Oct 2008
          • 23

          #5
          Originally posted by KevinADC
          Assuming everything else is good with your code:

          Code:
          open (OUT, ">>" , 'path/to/outfile.txt') or die "$!";
          my @aa_split = split //, $aa;
          foreach my $val (@{hash{@aa_split}}){
             print OUT "@{$val} ";
          }
          print " \n";
          close OUT;
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

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

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

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

              Comment

              • natarajmtech
                New Member
                • Oct 2008
                • 23

                #8
                Originally posted by nithinpes
                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

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

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

                  Code:
                  use List::Permutor;
                  my $seq = 'VF';
                  my %hash = ( 
                     L => ['CTT','CTC','CTA','CTG','TTA','TTG'],
                     S => ['TCT','TCC','TCA','TCG','AGT','AGC'],
                     R => ['CGT','CGC','CGA','CGG','AGA','AGG'],
                     V => ['GTT','GTC','GTA','GTG'],
                     A => ['GCT','GCC','GCA','GCG'],
                     G => ['GGT','GGC','GGA','GGG'],
                     P => ['CCT','CCC','CCA','CCG'],
                     T => ['ACT','ACC','ACA','ACG'],
                     I => ['ATT','ATC','ATA'],
                     F => ['TTT','TTC'],
                     C => ['TGT','TGC'],
                     Y => ['TAT','TAC'],
                     Q => ['CAA','CAG'],
                     N => ['AAT','AAC'],
                     H => ['CAT','CAC'],
                     E => ['GAA','GAG'],
                     D => ['GAT','GAC'],
                     K => ['AAA','AAG'],
                     M => ['ATG'],
                     W => ['TGG'],
                  );
                  
                  my @loops = split '', $seq;
                  my @AoA;
                  foreach my $letter (@loops) {
                     push @AoA, @{$hash{$letter}};
                  }
                  my $p = List::Permutor->new(@AoA);
                  {
                     local $";
                     while(my @set = $p->next) {
                        print "@set\n";
                     }
                  }
                  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.

                  Comment

                  • natarajmtech
                    New Member
                    • Oct 2008
                    • 23

                    #10
                    Originally posted by KevinADC
                    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.

                    Code:
                    use List::Permutor;
                    my $seq = 'VF';
                    my %hash = ( 
                       L => ['CTT','CTC','CTA','CTG','TTA','TTG'],
                       S => ['TCT','TCC','TCA','TCG','AGT','AGC'],
                       R => ['CGT','CGC','CGA','CGG','AGA','AGG'],
                       V => ['GTT','GTC','GTA','GTG'],
                       A => ['GCT','GCC','GCA','GCG'],
                       G => ['GGT','GGC','GGA','GGG'],
                       P => ['CCT','CCC','CCA','CCG'],
                       T => ['ACT','ACC','ACA','ACG'],
                       I => ['ATT','ATC','ATA'],
                       F => ['TTT','TTC'],
                       C => ['TGT','TGC'],
                       Y => ['TAT','TAC'],
                       Q => ['CAA','CAG'],
                       N => ['AAT','AAC'],
                       H => ['CAT','CAC'],
                       E => ['GAA','GAG'],
                       D => ['GAT','GAC'],
                       K => ['AAA','AAG'],
                       M => ['ATG'],
                       W => ['TGG'],
                    );
                    
                    my @loops = split '', $seq;
                    my @AoA;
                    foreach my $letter (@loops) {
                       push @AoA, @{$hash{$letter}};
                    }
                    my $p = List::Permutor->new(@AoA);
                    {
                       local $";
                       while(my @set = $p->next) {
                          print "@set\n";
                       }
                    }
                    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

                    Comment

                    • natarajmtech
                      New Member
                      • Oct 2008
                      • 23

                      #11
                      Originally posted by natarajmtech
                      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.
                      Code:
                      use strict;
                      use warnings;
                      my @V=('GTT','GTC','GTA','GTG');
                      my @A=('GCT','GCC','GCA','GCG');
                      my @E=('GAA','GAG');
                      my @F=('TTT','TTC');
                      my @G=('GGT','GGC','GGA','GGG');
                      my @H=('CAT','CAC');
                      my $i = 1;
                      foreach my $v (@V) {
                         foreach my $a (@A) {
                            foreach my $e (@E) {
                               foreach my $f (@F) {
                                  foreach my $g (@G) {
                                     foreach my $h (@H) {
                                        print "$i $v$f\n";
                                        $i++;
                                     }
                                  }
                               }
                            }
                         }
                      }
                      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

                      Comment

                      • nithinpes
                        Recognized Expert Contributor
                        • Dec 2007
                        • 410

                        #12
                        Originally posted by natarajmtech
                        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.
                        Code:
                        use strict; 
                        use warnings; 
                        my @V=('GTT','GTC','GTA','GTG'); 
                        my @A=('GCT','GCC','GCA','GCG'); 
                        my @E=('GAA','GAG'); 
                        my @F=('TTT','TTC'); 
                        my @G=('GGT','GGC','GGA','GGG'); 
                        my @H=('CAT','CAC'); 
                        my $i = 1; 
                        
                                 foreach my $v (@V ){           
                                       foreach my $f (@F) { 
                                          print "$i $v$f\n"; 
                                          $i++; 
                                       } 
                                    }
                        However, I haven't tested the output using List::Permutor. I will check it in my free-time and compare the output.

                        -Nithin
                        Last edited by nithinpes; Oct 31 '08, 06:23 AM. Reason: text

                        Comment

                        • natarajmtech
                          New Member
                          • Oct 2008
                          • 23

                          #13
                          Originally posted by nithinpes
                          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.
                          Code:
                          use strict; 
                          use warnings; 
                          my @V=('GTT','GTC','GTA','GTG'); 
                          my @A=('GCT','GCC','GCA','GCG'); 
                          my @E=('GAA','GAG'); 
                          my @F=('TTT','TTC'); 
                          my @G=('GGT','GGC','GGA','GGG'); 
                          my @H=('CAT','CAC'); 
                          my $i = 1; 
                          
                                   foreach my $v (@V ){           
                                         foreach my $f (@F) { 
                                            print "$i $v$f\n"; 
                                            $i++; 
                                         } 
                                      }
                          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

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            Originally posted by nithinpes
                            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.
                            Code:
                            use strict; 
                            use warnings; 
                            my @V=('GTT','GTC','GTA','GTG'); 
                            my @A=('GCT','GCC','GCA','GCG'); 
                            my @E=('GAA','GAG'); 
                            my @F=('TTT','TTC'); 
                            my @G=('GGT','GGC','GGA','GGG'); 
                            my @H=('CAT','CAC'); 
                            my $i = 1; 
                            
                                     foreach my $v (@V ){           
                                           foreach my $f (@F) { 
                                              print "$i $v$f\n"; 
                                              $i++; 
                                           } 
                                        }
                            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?

                            Comment

                            • natarajmtech
                              New Member
                              • Oct 2008
                              • 23

                              #15
                              Originally posted by KevinADC
                              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

                              Comment

                              Working...