Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Fad2012
    Member
    • Sep 2012
    • 62

    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
  • Fad2012
    Member
    • Sep 2012
    • 62

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

    Comment

    • SNPsaurus
      Registered Vendor
      • May 2013
      • 525

      #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

      • Fad2012
        Member
        • Sep 2012
        • 62

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

        Comment

        • SNPsaurus
          Registered Vendor
          • May 2013
          • 525

          #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

          • Fad2012
            Member
            • Sep 2012
            • 62

            #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

            • SNPsaurus
              Registered Vendor
              • May 2013
              • 525

              #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

              • Fad2012
                Member
                • Sep 2012
                • 62

                #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

                • SNPsaurus
                  Registered Vendor
                  • May 2013
                  • 525

                  #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

                  • Fad2012
                    Member
                    • Sep 2012
                    • 62

                    #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

                    • Fad2012
                      Member
                      • Sep 2012
                      • 62

                      #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

                      • SNPsaurus
                        Registered Vendor
                        • May 2013
                        • 525

                        #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

                        • Fad2012
                          Member
                          • Sep 2012
                          • 62

                          #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

                          • mylaser
                            Reply to Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                            by mylaser
                            Kheloyar – Everything You Need to Know About Kheloyaar Login and Kheoyar Id
                            If you are looking for an online gaming platform that offers a user-friendly experience, Kheloyar has become a name that many users search for. Whether you're interested in creating a new account, accessing your dashboard through Kheloyaar Login, or learning how to obtain a Kheoyar Id, understanding the platform's features and account process is essential.
                            This guide explains everything you need to know about...
                            07-11-2026, 01:13 AM
                          • SEQadmin2
                            Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                            by SEQadmin2



                            Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                            ...
                            07-09-2026, 11:10 AM
                          • SEQadmin2
                            Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                            by SEQadmin2



                            Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                            There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                            07-08-2026, 05:17 AM

                          ad_right_rmr

                          Collapse

                          News

                          Collapse

                          Topics Statistics Last Post
                          Started by SEQadmin2, 07-09-2026, 10:04 AM
                          0 responses
                          22 views
                          0 reactions
                          Last Post SEQadmin2  
                          Started by SEQadmin2, 07-08-2026, 10:08 AM
                          0 responses
                          14 views
                          0 reactions
                          Last Post SEQadmin2  
                          Started by SEQadmin2, 07-07-2026, 11:05 AM
                          0 responses
                          33 views
                          0 reactions
                          Last Post SEQadmin2  
                          Started by SEQadmin2, 07-02-2026, 11:08 AM
                          0 responses
                          31 views
                          0 reactions
                          Last Post SEQadmin2  
                          Working...