How to get a while loop to recognize an extracted number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sessmurda
    New Member
    • Jul 2008
    • 14

    #1

    How to get a while loop to recognize an extracted number

    I am writing a fairly simple script that is supposed to print out lines from a txt file and group them based on where I want them. The general format of the files I am using the script on is as follows:

    662376 [S] |GCGG | | |
    662375 [S] |CGCC | | |
    662374 [S] |GCGG | | |
    662373 [H] |CATC | | |
    662371 [Y] |TCCC | | |
    662369 [M] |CACCC| | |
    662367 [Y] |TCTTT| | |
    662365 [S] |GCGGG| | |
    662364 [R] |AGAAA| | |
    662360 [M] |CACCC| | |
    662359 [W] |ATAAA| | |
    662358 [S] |CGCCC| | |
    662357 [M] |ACAAA| | |
    662356 [R] |GAAAG| | |
    662354 [S] |GCGGG| | |
    662353 [R] |AGAAA| | |
    662352 [R] |GAGGG| | |
    662350 [K] |TGTTT| | |
    662349 [Y] |CTCCC| | |
    662348 [M] |ACAAA| | |
    662347 [R] |GAGGG| | |
    662376 [S] |GCGG | | |
    662375 [S] |CGCC | | |
    662374 [S] |GCGG | | |
    662373 [H] |CATC | | |
    662371 [Y] |TCCC | | |
    662369 [M] |CACCC| | |
    662367 [Y] |TCTTT| | |
    662365 [S] |GCGGG| | |
    662364 [R] |AGAAA| | |
    662360 [M] |CACCC| | |
    662359 [W] |ATAAA| | |
    662358 [S] |CGCCC| | |
    662357 [M] |ACAAA| | |
    662356 [R] |GAAAG| | |
    662354 [S] |GCGGG| | |
    662353 [R] |AGAAA| | |
    662352 [R] |GAGGG| | |
    662350 [K] |TGTTT| | |
    662349 [Y] |CTCCC| | |
    662348 [M] |ACAAA| | |
    662347 [R] |GAGGG| | |


    and here is the code:

    Code:
    print "What is the start of the ORF?\n";
    $orf_beg = <STDIN>;
    
    open (IN, '/home/asession/pracperl/bin/SNPYNR+-1kb.txt') || die "nope\n";
    
    while (<IN>) {
    
        chomp;
    
        $position = substr $_, 1, 11;               # extract the position
        $type_of_polymorphism = substr $_, 13, 3;   # extract the polymorphism type, same as ref if no polymorphism
        $ref_seq = substr $_, 17, 1;                # extract the cerevisiae ref sequence
        $para_seq = substr $_, 18, 1;               # extract the paradoxus sequence
        $mika_seq = substr $_, 19, 1;               # extract the mika sequence
        $kudr_seq = substr $_, 20, 1;               # extract the kudr sequence
        $baya_seq = substr $_, 21, 1;               # extract the bayanus sequence
    
    
        $baya_seq =~ s/\s+/x/g;
        #$position =~ tr/ //d;
    
        foreach ($position) {
            while ($postion > $orf_beg) {
            print "$position $ref_seq $para_seq $baya_seq\n";
    }}
    
    }
    close (IN);
    
        print "\n";
    Basically the problem I am having is that the while loop withing the foreach loop is not recognizing the position variable as a number, I tried to remove the white space, thinking that was an issue, but it did not work. Any suggestions?
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    If this is supposed to be input to the perl program, remove the newline from the end:

    Code:
    print "What is the start of the ORF?\n";

    should be:

    Code:
    print "Enter the start of the ORF: ";
    chomp($orf_beg = <STDIN>);

    Comment

    • sessmurda
      New Member
      • Jul 2008
      • 14

      #3
      Thats not the variable that perl isnt recognizing, the $position is the problem, I defined orf beg before opening the file handle so that during the loop it wouldnt ask me to input the start of the ORF before every iteration.

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        Are you certain that $position has a number in it? Have you checked the value of $position?

        --Kevin

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by eWish
          Are you certain that $position has a number in it? Have you checked the value of $position?

          --Kevin
          To find out, you could run your script in the Perl debugger and check all the values to make sure. To start the debugger, use:

          Code:
          perl -d <scriptname>
          THe debugger is a little daunting at first, but its not bad once you read a tutorial, even the one at Perldoc is doable.

          Regards,

          Jeff

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Originally posted by eWish
            Are you certain that $position has a number in it? Have you checked the value of $position?

            --Kevin

            sessmurda,

            I was wondering about the logic in this loop:
            Code:
                foreach ($position) {
                    while ($postion > $orf_beg) {
                    print "$position $ref_seq $para_seq $baya_seq\n";
            }}
            Inside while ($postion > $orf_beg) {} loop, you are not modifying the value of $position. So, if the condition returns true, this will result in an infinite loop.

            You should be using if() loop instead of while(). However, check if $position has indeed a number in it by printing out the value.

            - Nithin

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              this line:

              Code:
              $position = substr $_, 1, 11;
              with this input fromthe file:

              Code:
              662376 [S] |GCGG | | |
              will result in:

              62376 [S] |

              Is that really what you want?

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Originally posted by nithinpes
                sessmurda,

                I was wondering about the logic in this loop:
                Code:
                    foreach ($position) {
                        while ($postion > $orf_beg) {
                        print "$position $ref_seq $para_seq $baya_seq\n";
                }}
                Inside while ($postion > $orf_beg) {} loop, you are not modifying the value of $position. So, if the condition returns true, this will result in an infinite loop.

                You should be using if() loop instead of while(). However, check if $position has indeed a number in it by printing out the value.

                - Nithin
                Also, correct me if I am wrong, but I don't see that $position was defined as an array, so doing a foreach will result in one time through.

                Regards,

                Jeff

                Comment

                • nithinpes
                  Recognized Expert Contributor
                  • Dec 2007
                  • 410

                  #9
                  Originally posted by numberwhun
                  Also, correct me if I am wrong, but I don't see that $position was defined as an array, so doing a foreach will result in one time through.

                  Regards,

                  Jeff
                  Thanks for the addition, Jeff. I missed it totally :)

                  The foreach loop will be of no specific purpose in that section of script.

                  Comment

                  • sessmurda
                    New Member
                    • Jul 2008
                    • 14

                    #10
                    I am currently working on the foreach and while/if suggestions you guys provided, thanks! To answer some questions: the position is a number, it doesnt show when I copy and pasted the text, but there is a lot of white space between the last number of the position and the sequence character to allow for the fact that on occasion there will need to be decimals due to the format of the file. I checked all of the substring column numbers before posting, they are all correct. I originally had the if statement inside the loop but it kept asking me for the polymorphism on every iteration, I incorrectly thought while was what I should be using. Also when I tried pushing the $position variable into an array it would not move past the first position, even when I tried the shift function. When I use the script I posted without the while/if line, and have the $position being acted on by the foreach loop, it works, its just that I want to catalogue the positions based on where I specify it, then output those to each their own file, which I will work on after I get the script working.

                    Thanks again for the help.

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      use the code tags around your data to preserve the spacing.

                      Comment

                      • sessmurda
                        New Member
                        • Jul 2008
                        • 14

                        #12
                        Sorry for the late response, had a conference away in LA, but after playing with the script and using warnings I have figured out that even though when I print out only the $position, do not use STDIN for orf_beg, or redefine $position not to include decimals, the script tells me this..

                        Use of uninitialized value in string gt at gamma.pl line 25, <IN> line 1.
                        Use of uninitialized value in string gt at gamma.pl line 25, <IN> line 2.

                        etc etc. So I figure that even though I see only numbers whenever I print out the position variable, that it is not reading the position as a number because of the way I created the substr maybe. That is my only guess, and I was wondering how would I go about making sure that the gt statement works. I tried reformatting $position with sprint f so that all of the values turn to numbers that are rounded to nearest thousandth, but that did not work out. I also took out the foreach statement as suggested, the new script is below:

                        Code:
                        #!/usr/bin/perl -w
                        
                        
                        print "What is the start of the ORF?";
                        $orf_beg = <STDIN>;
                        
                        open (IN, '/home/asession/pracperl/bin/SNPYNR+-1kb.txt') || die "nope\n";
                        
                        while (<IN>) {
                        
                            chomp;
                        
                            $position = substr $_, 1, 10;               # extract the position
                            $type_of_polymorphism = substr $_, 13, 3;   # extract the polymorphism type, same as ref if no polymorphism
                            $ref_seq = substr $_, 17, 1;                # extract the cerevisiae ref sequence
                            $para_seq = substr $_, 18, 1;               # extract the paradoxus sequence
                            $mika_seq = substr $_, 19, 1;               # extract the mika sequence
                            $kudr_seq = substr $_, 20, 1;               # extract the kudr sequence
                            $baya_seq = substr $_, 21, 1;               # extract the bayanus sequence
                        
                        
                            $baya_seq =~ s/\s+/x/g;                     # replace all blanks with 'x'
                            $position =~ tr/ //d;                       # remove white space
                        
                            if ($postion gt $orf_beg) {
                                print "$position $ref_seq $para_seq $baya_seq\n";
                            #print "$position\n";
                        
                        }}
                        close (IN);
                        
                            print "\n";

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          need to see your data, the formatted data. Post it and put code tags around it.

                          Comment

                          • sessmurda
                            New Member
                            • Jul 2008
                            • 14

                            #14
                            My bad, here is the data:

                            Code:
                             662376     [S] |GCGG | |     |
                             662375     [S] |CGCC | |     |
                             662374     [S] |GCGG | |     |
                             662373     [H] |CATC | |     |
                             662371     [Y] |TCCC | |     |
                             662369     [M] |CACCC| |     |
                             662367     [Y] |TCTTT| |     |
                             662365     [S] |GCGGG| |     |
                             662364     [R] |AGAAA| |     |
                             662360     [M] |CACCC| |     |
                             662359     [W] |ATAAA| |     |
                             662358     [S] |CGCCC| |     |
                             662357     [M] |ACAAA| |     |
                             662356     [R] |GAAAG| |     |
                             662354     [S] |GCGGG| |     |
                             662353     [R] |AGAAA| |     |
                             662352     [R] |GAGGG| |     |
                             662350     [K] |TGTTT| |     |
                             662349     [Y] |CTCCC| |     |
                             662348     [M] |ACAAA| |     |
                             662347     [R] |GAGGG| |     |
                             662346     [B] |GCCTC| |     |
                             662345     [M] |CACCC| |     |
                             662344     [B] |CGTCC| |     |
                             662343.002 [c] |---C-| |     |
                             662343.001 [a] |---A-| |     |
                             662343     [K] |GTTGG| |     |

                            Comment

                            • nithinpes
                              Recognized Expert Contributor
                              • Dec 2007
                              • 410

                              #15
                              Originally posted by sessmurda
                              Sorry for the late response, had a conference away in LA, but after playing with the script and using warnings I have figured out that even though when I print out only the $position, do not use STDIN for orf_beg, or redefine $position not to include decimals, the script tells me this..

                              Use of uninitialized value in string gt at gamma.pl line 25, <IN> line 1.
                              Use of uninitialized value in string gt at gamma.pl line 25, <IN> line 2.

                              The error you are getting is because of a typo in this line:
                              Code:
                              if ($postion gt $orf_beg) {
                              Look out for the missing 'i' in $position.

                              Comment

                              Working...