loop

N

Nina

My file looks like this,
A1 0 550 -235.6974
0 650 -185.2378
0 750 -193.6359
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
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.
 
A

Anno Siegel

Nina said:
My file looks like this,
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,
1 450 -139.76

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

1 250 -137.9132
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__
0 550 -235.6974
0 650 -185.2378
0 750 -193.6359
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
4 250 -170.9308
4 350 -140.8445

Anno
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top