Reputation: 5
I have data and perl code as following. I am trying to find letter combination with maximum score (last column) under each header (>dna1, >dna2) and print it. I wrote code in Perl as following:
use strict;
use warnings;
my ($line);
my $max = 0;
my @prediction;
foreach my $line (<DATA>) {
chomp($line);
if ($line =~ /^>/) {
print "$line";
}
else {
my @split = split(/\s+/,$line);
my $score = pop @split;
if ($max < $score) {
$max = $score;
@prediction = @split;
push @prediction, $max;
}
#print "$domain\n";
}
print "@prediction\n";
}
__DATA__
>dna1
D 124.6
D T 137.5
G -3.4
G T 9.5
T 12.9
>dna2
A 196.3
A K 186.5
A K H 192.7
A H M 206.2
A M 200
The output is
>dna1
D 124.6
D T 137.5
D T 137.5
D T 137.5
D T 137.5
>dna2D T 137.5
A 196.3
A 196.3
A 196.3
A H M 206.2
A H M 206.2
Could you please help me to figure out how I can get output just final combination with max score as following:
>dna1
D T 137.5
>dna2
A H M 206.2
Thanks!
Upvotes: 0
Views: 67
Reputation: 241988
The following produces the expected output:
#!/usr/bin/perl
use warnings;
use strict;
my ($max_score, $max_combination);
while (my $line = <DATA>) {
if ($line =~ /^>/) {
print "$max_combination $max_score\n" if $max_combination;
print $line;
$max_score = 0;
} elsif (my ($combination, $score) = $line =~ /(.*)\s+([0-9.]+)/) {
if ($score > $max_score) {
$max_score = $score;
$max_combination = $combination;
}
}
}
print "$max_combination $max_score\n";
The main difference is you need to print the result only when a new group starts, or at the very end.
Upvotes: 1