Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semna
    Member
    • Apr 2010
    • 55

    #1

    perl?

    Hi,
    I have a file like this:
    ID : 741158
    PARENT ID : 9605
    RANK : species
    GC ID : 1
    MGC ID : 2
    SCIENTIFIC NAME : Homo sp. Altai
    GENBANK COMMON NAME : Denisova hominin
    //
    ID : 756884
    PARENT ID : 9598
    RANK : subspecies
    GC ID : 1
    MGC ID : 2
    SCIENTIFIC NAME : Pan troglodytes ellioti
    //
    I need just ID number if rank :species. so for this example the uotput should be :741158.
    my perl script is like this:
    #!/usr/bin/perl -w
    use strict;
    use warnings;


    open (FILE, 'm.txt');
    while (my $p = <FILE>){
    if ($p =~ /^\/\/\n/){
    last;
    }elsif ($p =~ /GC ID : 1/){
    next;
    }elsif ($p =~ /MGC ID : 2/){
    next;
    }elsif ($p =~ /SCIENTIFIC NAME :\D/){
    next;
    }elsif ($p =~ /\bspecies$/){
    print "ID number";?????
    }
    }

    Any sugeestion? Thanks.
  • thurisaz
    Member
    • Jun 2011
    • 24

    #2
    This should work if the file is formatted exactly as you've shown:

    Code:
    open (FILE, 'm.txt');
    while(<FILE>) {
      if ($_ =~ m/^ID :/ ) {
        @id = split(/ : /,$_);
      }
      if ($_ =~ m/\bspecies$/) {
        print $id[1];
      }
    }
    Note that it's pretty fragile -- it doesn't do any checking and assumes the input is just as you've shown. In particular: (1) Every record must have an ID; (2) the ID must always come before the rank; (3) the ID line must have a space, colon, space and then the ID number; (4) Any line that ends with the word "species" will cause the ID to be printed, so "subspecies" (etc) must always be one word and "species" shouldn't appear as the final word of any other line.

    Comment

    • semna
      Member
      • Apr 2010
      • 55

      #3
      Hi thurisaz,
      Thanks for your answer. All your assumptions are true but your code is not working.

      Comment

      • thurisaz
        Member
        • Jun 2011
        • 24

        #4
        Hi,

        Just to be clear: I omitted the initial "#!/usr/bin/perl -w" line. If you want to copy & paste into a file, you will need to include that, like this:

        Code:
        #!/usr/bin/perl -w
        
        open (FILE, 'm.txt');
        while(<FILE>) {
          if ($_ =~ m/^ID :/ ) {
            @id = split(/ : /,$_);
          }
          if ($_ =~ m/\bspecies$/) {
            print $id[1];
          }
        }
        If that code isn't working for you, then please let me know what exactly is going wrong. I just copied & pasted to be sure and it seems to work fine.

        Comment

        • semna
          Member
          • Apr 2010
          • 55

          #5
          I just copy your code but the error is :
          Use of uninitialized value in print at 110.pl line 13, <FILE> line 3
          for that I defined array and variable ID (my) but noting change.
          Thanks.

          Comment

          • thurisaz
            Member
            • Jun 2011
            • 24

            #6
            An entry at around line 13 in your file (m.txt) is breaking the script; it seems like it's because there is a line that ends in "species" _before_ an ID has been provided. Like I said, it's a fragile script that assumes everything is well-behaved. A few changes will make sure that ID has been assigned before printing and also clears the ID at the end of each record:

            Code:
            #!/usr/bin/perl -w
            
            open (FILE, 'm.txt');
            while(<FILE>) {
                if ($_ =~ m/^ID :/ ) {
                  @id = split(/ : /,$_);
                }
                if ($_ =~ m/\bspecies$/ && $id[1]) {
                  print $id[1];
                }
                if ($_ =~ m?^//$?) {
                  $id[1]=0;
                }
            }
            It sounds like you have something unexpected going on with your input file, though, so I strongly recommend having a good look at it, especially since the script makes so many assumptions.
            Last edited by thurisaz; 07-18-2011, 05:02 AM.

            Comment

            • semna
              Member
              • Apr 2010
              • 55

              #7
              Thanks but it is still not working. Each time I just used exactly that file posted on this page.It is really strange. But anyway thanks so much for your help.

              Comment

              • labunit
                Member
                • Sep 2010
                • 10

                #8
                Originally posted by semna View Post
                I just copy your code but the error is :
                Use of uninitialized value in print at 110.pl line 13, <FILE> line 3
                for that I defined array and variable ID (my) but noting change.
                Thanks.
                Make sure you define your id-array with
                Code:
                my @id;
                and not
                Code:
                my $id;
                If this is not it, just copy & paste your code here.

                Comment

                • severin
                  Genome Informatics Facility
                  • Sep 2009
                  • 105

                  #9
                  unix script

                  ID : 741158
                  PARENT ID : 9605
                  RANK : species
                  GC ID : 1
                  MGC ID : 2
                  SCIENTIFIC NAME : Homo sp. Altai
                  GENBANK COMMON NAME : Denisova hominin
                  //

                  At the command prompt you can

                  more filename.txt | egrep ID | awk '{print :$3}' | more
                  that will give you your list. of IDs and the colon in front.

                  Comment

                  Latest Articles

                  Collapse

                  • SEQadmin2
                    Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                    by SEQadmin2



                    Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                    Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                    ...
                    10-14-2026, 09:55 AM
                  • SEQadmin2
                    New Genomics Technologies Take Aim at Long-Standing Limits
                    by SEQadmin2


                    Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                    We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                    ...
                    09-28-2026, 10:25 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, 10-22-2026, 11:13 AM
                  0 responses
                  30 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-22-2026, 10:48 AM
                  0 responses
                  22 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-15-2026, 10:52 AM
                  0 responses
                  39 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-14-2026, 05:39 AM
                  0 responses
                  45 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...