Unconfigured Ad

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

    #1

    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

                          • SEQadmin2
                            Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                            by SEQadmin2



                            CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                            Despite this, “CRISPR helped turn genome editing from a specialized technique into
                            ...
                            07-31-2026, 11:01 AM
                          • SEQadmin2
                            Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                            by SEQadmin2


                            Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                            The systematic characterization of the human proteome has
                            ...
                            07-20-2026, 11:48 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

                          ad_right_rmr

                          Collapse

                          News

                          Collapse

                          Topics Statistics Last Post
                          Started by SEQadmin2, 07-31-2026, 02:55 AM
                          0 responses
                          15 views
                          0 reactions
                          Last Post SEQadmin2  
                          Started by SEQadmin2, 07-24-2026, 12:17 PM
                          0 responses
                          15 views
                          0 reactions
                          Last Post SEQadmin2  
                          Started by SEQadmin2, 07-23-2026, 11:41 AM
                          0 responses
                          13 views
                          0 reactions
                          Last Post SEQadmin2  
                          Started by SEQadmin2, 07-20-2026, 11:10 AM
                          0 responses
                          24 views
                          0 reactions
                          Last Post SEQadmin2  
                          Working...