Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with Perl

    Hello guys

    I have the following dataset

    ID Kind
    259 P0_P2
    260 P0_P2
    57 P2
    58 P2
    59 P2
    60 P2
    61 P2
    62 P2

    I am trying to write a Perl code to pick the kind ID of "P0_P2" and attach it to the IDs of the rest of the kinds. So the expected results will be:

    Anc Child
    259 57
    259 58
    259 59
    259 60
    259 61
    259 62
    259 57
    260 58
    260 59
    260 60
    260 61
    260 62

    I wrote the following Perl code, but not sure why it is not working. The code takes an external tab delimited file, which contains the data.

    #!/usr/bin/perl

    my $firFile = "$ARGV[0]";
    open (NGS, $firFile);
    my $Ancestor="";
    my $Childern="";
    my $starting_childern = 0;

    while (<NGS>) {
    chomp;

    my @fields = split ('\t', $_);

    if ($fields[1] eq "P0_P2") {

    $starting_childern = 1;
    $Ancestor = $fields[0];

    print "$Ancestor\n";
    }

    elsif ($starting_childern == 1) {

    $Childern = $fields[0];
    if ($fields[1] ne "P0_P2"){
    print "$Ancestor \t $Childern\n";

    }

    }

    }
    close (NGS);

    Could you please help me with this
    Many thanks

  • #2
    PS.
    The code is printing nothing upon execution!

    Comment


    • #3
      I'd add some informative print statements like print "fields[0] fields[1] in Ancestor if block\n" at different locations to see what your computer is reading in.

      I was going to say, "oh it is a typo that you spelled children childern" but you were consistent the whole time!".
      Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

      Comment


      • #4
        Thank you SNPsaurus. I am not sure what is going on with this code.
        Any suggestion anyone
        Many thanks

        Comment


        • #5
          Did you try to add some informative print statements to see what information is in your variables?
          Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

          Comment


          • #6
            Hi SNPsaurus

            Yes, I tried. The only way it printed something when I put

            print "fields[0] \n"

            Just after the split command. I tried to play with many other things with no positive results. For example, I tried to change the variable types from my $Ancestor=""; to my $Ancestor=0;. I also tried to move print "$Ancestor \t $Childern\n"; around the code, but nothing worked...

            Comment


            • #7
              What did it print? It is hard to help when you provide so little information. Did it print "259 P0_P2" or the proper "259"? After the split, add:
              print "f0 $fields[0]\tf1 $fields[1]\n";

              What is the output from that?
              Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

              Comment


              • #8
                Thank you very much SNPsaurus.

                I removed the column titles for now. So I removed the ID nad the Kind from the table. WhenI use print "f0 $fields[0]\tf1 $fields[1]\n"; It prints the following

                f0 259 f1 P0_P2
                f0 260 f1 P0_P2
                f0 57 f1 P2
                f0 58 f1 P2
                f0 59 f1 P2
                f0 60 f1 P2
                f0 61 f1 P2
                f0 62 f1 P2
                When I use:
                print "f0 $fields[0]\n"; it prints:

                f0 259
                f0 260
                f0 57
                f0 58
                f0 59
                f0 60
                f0 61
                f0 62
                In both cases it should be after the split line

                Many thanks

                Comment


                • #9
                  OK, so the split works. Let's see if your if statement works. After if ($fields[1] eq "P0_P2") {
                  put:
                  print "in if statement\n";
                  Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

                  Comment


                  • #10
                    Originally posted by SNPsaurus View Post
                    OK, so the split works. Let's see if your if statement works. After if ($fields[1] eq "P0_P2") {
                    put:
                    print "in if statement\n";
                    Nothing appeared, so that means the statement is not working?

                    Comment


                    • #11
                      Well, when I replaced the "eq" with "=~" things worked and it printed:
                      in if statement
                      in if statement

                      It seems that the eq is not working

                      Comment


                      • #12
                        Right. I wonder if your input file has DOS or other funny line breaks/ carriage returns at the end. That used to be the source of most of my woes.

                        You could try chop; chop; instead of chomp; I'd print field[0] and [1] again to see if it is trimming your actual data instead of getting rid of both line end characters.

                        You could also do

                        local $/ = "\n";

                        for my $line (<NGS>) {
                        $line =~ s/\r?\n$//;

                        Which should get rid end of line weirdness. You could re-type your if statement to make sure P0 is not PO or other random typos.
                        Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

                        Comment


                        • #13
                          Thank you

                          I really appreciate your help, I will play around with it and will let you know

                          Many thanks
                          Fadi

                          Comment

                          Latest Articles

                          Collapse

                          • seqadmin
                            Strategies for Sequencing Challenging Samples
                            by seqadmin


                            Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
                            03-22-2024, 06:39 AM
                          • seqadmin
                            Techniques and Challenges in Conservation Genomics
                            by seqadmin



                            The field of conservation genomics centers on applying genomics technologies in support of conservation efforts and the preservation of biodiversity. This article features interviews with two researchers who showcase their innovative work and highlight the current state and future of conservation genomics.

                            Avian Conservation
                            Matthew DeSaix, a recent doctoral graduate from Kristen Ruegg’s lab at The University of Colorado, shared that most of his research...
                            03-08-2024, 10:41 AM

                          ad_right_rmr

                          Collapse

                          News

                          Collapse

                          Topics Statistics Last Post
                          Started by seqadmin, Yesterday, 06:37 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post seqadmin  
                          Started by seqadmin, Yesterday, 06:07 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post seqadmin  
                          Started by seqadmin, 03-22-2024, 10:03 AM
                          0 responses
                          49 views
                          0 likes
                          Last Post seqadmin  
                          Started by seqadmin, 03-21-2024, 07:32 AM
                          0 responses
                          66 views
                          0 likes
                          Last Post seqadmin  
                          Working...
                          X