loop

Discussion in 'Perl Misc' started by Nina, Oct 10, 2005.

  1. Nina

    Nina Guest

    My file looks like this,
    >A1

    0 550 -235.6974
    0 650 -185.2378
    0 750 -193.6359
    >A2

    1 250 -137.9132
    1 350 -182.881
    1 450 -139.7555
    1 550 -170.2239
    >A3

    2 250 6.5954
    >A4

    3 250 -162.1777
    3 350 -129.308
    3 450 -105.5604
    >A5

    4 250 -170.9308
    4 350 -140.8445
    What I want is pick the third column's biggest score, then get output
    like this,
    >A1

    0 650 -185.24
    >A2

    1 450 -139.76
    >A3

    2 250 6.6
    >A4

    3 450 -105.56
    >A5

    4 350 -140.84

    I made the script as,
    #!/usr/bin/perl -w


    my $file=$ARGV[0];
    open (FILE, "$file") or die "Cannot open file.\n";
    open (OUT, "> $file.position_bigscore") or die "Cannot open file.\n";
    while ($line=<FILE>)
    {
    if ($line=~/>.+/)
    {
    $current_seq=$line;
    print OUT $current_seq, "\n";
    }
    else
    {
    my @columns=split(/\t/,$line);
    $current_score=$columns[2];
    if ($max_score>$current_score)
    {
    $max_score=$current_score;
    $max_line=$line;
    }
    print OUT $max_line, "\n";
    }
    }
    close OUT;
    close FILE;

    I CANNOT get the expected result, because the loop has problems.
    if ($max_score>$current_score)
    {
    $max_score=$current_score;
    $max_line=$line;
    }


    Is there anybody very kind can help me out?

    Thanks.
     
    Nina, Oct 10, 2005
    #1
    1. Advertising

  2. Nina

    Anno Siegel Guest

    Nina <> wrote in comp.lang.perl.misc:
    > My file looks like this,
    > >A1

    > 0 550 -235.6974
    > 0 650 -185.2378
    > 0 750 -193.6359
    > >A2

    > 1 250 -137.9132
    > 1 350 -182.881
    > 1 450 -139.7555
    > 1 550 -170.2239
    > >A3

    > 2 250 6.5954
    > >A4

    > 3 250 -162.1777
    > 3 350 -129.308
    > 3 450 -105.5604
    > >A5

    > 4 250 -170.9308
    > 4 350 -140.8445
    > What I want is pick the third column's biggest score, then get output


    What if there are two or more lines with maximal score?

    > like this,
    > >A1

    > 0 650 -185.24
    > >A2

    > 1 450 -139.76


    This isn't the line with the biggest score for A2, should be

    1 250 -137.9132

    > >A3

    > 2 250 6.6
    > >A4

    > 3 450 -105.56
    > >A5

    > 4 350 -140.84


    Use ">" as the record delimiter. Then you'll read the lines for each
    seq in one go:

    local $/ = '>';
    <DATA>; # junk one record
    while ( <DATA> ) {
    chomp;
    my ( $seq, @lines) = split /\n/;
    my $top_line = shift @lines;
    my ( $top_score) = $top_line =~ /(-?[\d.]+)$/ or die;
    for ( @lines ) {
    my ( $score) = /(-?[\d.]+)$/ or die;
    ( $top_line, $top_score) = ( $_, $score) if $score > $top_score;
    }
    $top_line =~ s/(-?[\d.]+)$/sprintf "%.2f", $1/e;
    print ">$seq\n", "$top_line\n";
    }

    __DATA__
    >A1

    0 550 -235.6974
    0 650 -185.2378
    0 750 -193.6359
    >A2

    1 250 -137.9132
    1 350 -182.881
    1 450 -139.7555
    1 550 -170.2239
    >A3

    2 250 6.5954
    >A4

    3 250 -162.1777
    3 350 -129.308
    3 450 -105.5604
    >A5

    4 250 -170.9308
    4 350 -140.8445

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Oct 10, 2005
    #2
    1. Advertising

  3. Nina

    Nina Guest

    Thanks for learn me the professional scritp!
     
    Nina, Oct 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,420
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,295
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    719
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    439
  5. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page