user1633337
user1633337

Reputation: 11

Combining duplicated lines in txt file with perl

I am trying to combine duplicate lines using Perl with little luck. My tab-delimited text file is structured as follows (spaces added for readability):

Pentamer  Probability Observed Length
ATGCA    0.008        1        16
TGTAC    0.021        1        16
GGCAT    0.008        1        16
CAGTG    0.004        1        16
ATGCA    0.016        2        23
TGTAC    0.007        1        23

I would like to be combine duplicated lines by adding the three numeric columns, therefor the line containing "ATGCA" would now look like this:

ATGCA  0.024 3 39

Any ideas/help/suggestions would be greatly appreciated! Thanks!

Upvotes: 1

Views: 237

Answers (2)

Kenosis
Kenosis

Reputation: 6204

Here's another option:

use Modern::Perl;

my %hash;

while ( my $line = <DATA> ) {
    my @vals = split /\s+/, $line;
    $hash{ $vals[0] }->[$_] += $vals[ $_ + 1 ] for 0 .. 2;
}

say join "\t", $_, @{ $hash{$_} } for sort keys %hash;

__DATA__
ATGCA   0.008   1   16
TGTAC   0.021   1   16
GGCAT   0.008   1   16
CAGTG   0.004   1   16
ATGCA   0.016   2   23
TGTAC   0.007   1   23

Output:

ATGCA   0.024   3   39
CAGTG   0.004   1   16
GGCAT   0.008   1   16
TGTAC   0.028   2   39

Upvotes: 1

perreal
perreal

Reputation: 97948

#!/usr/bin/perl

use warnings;
use strict;

my %hash;
while(<>) {
  my @v = split(/\s+/);
  if (defined $hash{$v[0]}) {
    my $arr = $hash{$v[0]};
    $hash{$v[0]} = [$v[0], $arr->[1] + $v[1], 
                   $arr->[2] + $v[2], $arr->[3] + $v[3]];
  } else {
    $hash{$v[0]} = [@v];
  }
}
foreach my $key (keys %hash) {
  print join(" ", @{$hash{$key}}), "\n";
}

Upvotes: 1

Related Questions