Someone_1313
Someone_1313

Reputation: 432

How merge multiple files using Perl?

I’m trying to merge 3 different files in only one. These files have 3 columns, the first 2 columns are the same between the three files.

The files (3 columns * 263.900 rows):

FILE 1
ID_1        ID_2        dN
QJY77946    NP_073551   0.0293
QJY77954    NP_073551   0.0273
QJY77954    QJY77946    0.0041
QJY77962    NP_073551   0.0273
QJY77962    QJY77946    0.0041   
...
FILE 2
ID_1        ID_2        dS
QJY77946    NP_073551   0.0757
QJY77954    NP_073551   0.0745
QJY77954    QJY77946    0.0085
QJY77962    NP_073551   0.0745
QJY77962    QJY77946    0.0109
...
FILE 3
ID_1        ID_2        Omega (dN/dS)
QJY77946    NP_073551   0.3872
QJY77954    NP_073551   0.3668
QJY77954    QJY77946    0.4851
QJY77962    NP_073551   0.3668
QJY77962    QJY77946    0.3767
...

For doing this, I’m using a Perl script to merge the files.

This script works when I merge two files (and create one file with 4 columns) but if I try to add one more, doesn’t work!

The script:

use strict;
use warnings;

# Variables
my $file01 = "M8_S.dNdS.tsv" ;
my $file02 = "M8_dN-dS.tsv" ;
# Define hash
my %content02 = () ;
open (B, $file02) or die;
while (my $l1 = <B>) {
  $l1 =~ s/\n//g;
  $l1 =~ s/\r//g;
  my @cont_linea = split ("\t", $l1);
  my $valor_1 = $cont_linea[0];
  my $valor_2 = $cont_linea[1];
  my $valor_3 = $cont_linea[2];
  my $ids = "$valor_1\_$valor_2";
  $content02{$ids} = $valor_3;  ### <- STORE DATA IN HASH
}
close B;

#### Open second file
open (A, $file01) or die;
LOOPFILE:
while (my $l2 = <A>) {
  $l2 =~ s/\n//g;
  $l2 =~ s/\r//g;
  my @cont_linea_2 = split ("\t", $l2);
  my $valor_21 = $cont_linea_2[0];
  my $valor_22 = $cont_linea_2[1];
  my $valor_23 = $cont_linea_2[2];
  my $ids_2 = "$valor_21\_$valor_22";
  if (exists $content02{$ids_2}) {
    my $newval = $content02{$ids_2};
    print "$l2\t$newval\n";
    next LOOPFILE;
  }
}
close A;
exit;

Any suggestion of what I have to do or add in the script to merge the 3 files and generate something like this:

ID_1        ID_2        dN      dS      Omega 
QJY77946    NP_073551   0.0293  0.0757  0.3872
QJY77954    NP_073551   0.0273  0.0745  0.3668
QJY77954    QJY77946    0.0041  0.0085  0.4851
QJY77962    NP_073551   0.0273  0.0745  0.3668
QJY77962    QJY77946    0.0041  0.0109  0.3767 
...

Upvotes: 2

Views: 230

Answers (2)

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use cut and paste in the shell instead of Perl:

paste file1.tsv <(cut -f3 file2.tsv) <(cut -f3 file3.tsv) > out.tsv

Upvotes: 0

choroba
choroba

Reputation: 241828

I abstracted the loading to a subroutine so the code is not repeated. The $phase parameter tells it whether it should initialise the hash, accumulate the columns, or even print the result. Works with 3 files or more.

#! /usr/bin/perl
use strict;
use warnings;
use feature qw{ say };

sub load {
    my ($file, $table, $phase) = @_;
    open my $in, '<', $file or die "$file: $!";
    while (<$in>) {
        chomp;
        my @columns = split /\t/;
        my $id = join '_', @columns[0, 1];
        die "Duplicate $id."
            if 'first' eq $phase && exists $table->{$id};

        push @{ $table->{$id} }, $columns[2];
        say join "\t", @columns[0, 1], @{ $table->{$id} }
            if 'print' eq $phase;
    }
}

my %table;
my $phase = 'first';
while (my $file = shift @ARGV) {
    load($file, \%table, $phase);
    $phase = 1 == @ARGV ? 'print' : '';
}

Upvotes: 2

Related Questions