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
                    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

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, Yesterday, 10:05 AM
                  0 responses
                  8 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 08-13-2026, 12:22 PM
                  0 responses
                  33 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 08-11-2026, 10:35 AM
                  0 responses
                  27 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 08-06-2026, 07:41 AM
                  0 responses
                  38 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...