Amy Ellison
Amy Ellison

Reputation: 89

Parse data from one tab delimited file into another with perl

I have a tab delimited file like this (in my script DIVERGE):

contig04730 contigK02622 0.3515
contig04733 contigK02622 0.3636
contig14757 contigK03055 0.4

And I have a second tab delimited file like this (DATA):

contig04730 F GO:0000228 nuclear GO:0000783 telomere_cap
contig04730 F GO:0005528 reproduction GO:0001113 eggs
contig14757 P GO:0123456 immune GO:0003456 cells
contig14757 P GO:0000782 nuclear GO:0001891 DNA_binding
contig14757 C GO:0000001 immune GO:00066669 more_cells

I am trying to add the 2nd and 3rd column from first file into second so that I would have (OUT):

contig04730 F GO:0000228 nuclear GO:0000783 telomere_cap contigK02622 0.3515
contig04730 F GO:0005528 reproduction GO:0001113 eggs contigK02622 0.3515
contig14757 P GO:0123456 immune GO:0003456 cells contigK03055 0.4
contig14757 P GO:0000782 nuclear GO:0001891 DNA_binding contigK03055 0.4
contig14757 C GO:0000001 immune GO:00066669 more_cells contigK03055 0.4

This is the perl script I am trying to use (trying to adapt ones I have found on here - very new to perl):

#!/usr/bin/env/perl

use strict;
use warnings;

#open the ortholog contig list
open (DIVERGE, "$ARGV[0]") or die "Error opening the input file with contig pairs";

#hash to store contig IDs
my ($espr, $liya, $divergence) = split("\t", $_);

#read through the ortho contig list and read into memory
while(<DIVERGE>){
    chomp $_;   #get rid of ending whitepace
    ($espr, $liya, $divergence)->{$_} = 1;
}
close(DIVERGE);

#open output file
open(OUT, ">$ARGV[2]") or die "Error opening the output file";

#open data file
open(DATA, "$ARGV[1]") or die "Error opening the sequence pairs file\n";

while(<DATA>){
    chomp $_;

    my ($contigs, $FPC, $GOslim, $slimdesc, $GOterm, $GOdesc) = split("\t", $_);
    if (defined $espr->{$contigs}) {
        print OUT "$_", "\t$liya\t$divergence", "\n";
    }
}
close(DATA);
close(OUT);

But I'm getting an error about useless use of private variable at line 15 and unitialized value _$ in split a line 10. I only have a very basic grasp of perl terms/variables. So if anyone could point out where I'm going wrong and how to fix, it would be much appreciated.

Upvotes: 2

Views: 876

Answers (4)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185871

What I would do :

#!/usr/bin/env perl

use strict; use warnings;

open my $fh1, "<", "file1" or die $!;
open my $fh2, "<", "file2" or die $!;

my %hash;

while (<$fh1>) {
    chomp;
    my @F = split;
    $hash{$F[0]} = join "\t", @F[1..2];
}

while (<$fh2>) {
    chomp;
    my @F = split;
    print join("\t", $_, $hash{$F[0]}), "\n";
}

close $fh1;
close $fh2;

Output

contig04730 F GO:0000228 nuclear GO:0000783 telomere_cap        contigK02622    0.3515
contig04730 F GO:0005528 reproduction GO:0001113 eggs   contigK02622    0.3515
contig14757 P GO:0123456 immune GO:0003456 cells        contigK03055    0.4
contig14757 P GO:0000782 nuclear GO:0001891 DNA_binding contigK03055    0.4
contig14757 C GO:0000001 immune GO:00066669 more_cells  contigK03055    0.4

Upvotes: 2

Kenosis
Kenosis

Reputation: 6204

Here's another option:

use strict;
use warnings;

my $data = pop;
my %diverge = map { /(\S+)\t+(.+)/; $1 => $2 } <>;
push @ARGV, $data;

while (<>) {
    chomp;
    $_ .= "\t$diverge{$1}\n" if /(\S+)/ and $diverge{$1};
    print;
}

Usage: perl DIVERGE_File DATA_File [>outFile]

Output on your datasets:

contig04730 F GO:0000228 nuclear GO:0000783 telomere_cap    contigK02622 0.3515
contig04730 F GO:0005528 reproduction GO:0001113 eggs   contigK02622 0.3515
contig14757 P GO:0123456 immune GO:0003456 cells    contigK03055 0.4
contig14757 P GO:0000782 nuclear GO:0001891 DNA_binding contigK03055 0.4
contig14757 C GO:0000001 immune GO:00066669 more_cells  contigK03055 0.4

Upvotes: 1

TLP
TLP

Reputation: 67940

This is an opportunity to use the Text::CSV module. The benefit of using a proper parser for the csv data is, of course, to avoid edge cases breaking your data.

use strict;
use warnings;
use Text::CSV;

my $div     = "diverge.txt";   # you can also assign dynamical names, e.g.
my $data    = "data.txt";      # my ($div, $data) = @ARGV
my $csv     = Text::CSV->new({
            binary      => 1,
            eol     => $/,
            sep_char    => "\t",
        });
my %div;

open my $fh, "<", $div or die $!;

while (my $row = $csv->getline($fh)) {
    my $key = shift @$row;              # first col is key
    $div{$key} = $row;                  # store row entries 
}
close $fh;

open $fh, "<", $data or die $!;

while (my $row = $csv->getline($fh)) {
    my $key = $row->[0];                # first col is key (again)
    push @$row, @{ $div{$key} };        # add stored values to $row
    $csv->print(*STDOUT, $row);         # print using Text::CSV's method
}

Output:

contig04730     F       GO:0000228      nuclear GO:0000783      telomere_cap contigK02622    0.3515
contig04730     F       GO:0005528      reproduction    GO:0001113      eggs    contigK02622    0.3515
contig14757     P       GO:0123456      immune  GO:0003456      cells   contigK03055    0.4
contig14757     P       GO:0000782      nuclear GO:0001891      DNA_binding    contigK03055    0.4
contig14757     C       GO:0000001      immune  GO:00066669     more_cells    contigK03055    0.4

Note that the output looks different because it is tab-delimited, whereas in the question it was space delimited.

Upvotes: 3

leonbloy
leonbloy

Reputation: 76026

This (if I understood your intent right) can be done in one line (in Linux, at least) by the command join:

 $ cat DATA
contig04730 F GO:0000228 nuclear GO:0000783 telomere_cap
contig04730 F GO:0005528 reproduction GO:0001113 eggs
contig14757 P GO:0123456 immune GO:0003456 cells
contig14757 P GO:0000782 nuclear GO:0001891 DNA_binding
contig14757 C GO:0000001 immune GO:00066669 more_cells

 $ cat DIVERGE
contig04730 contigK02622 0.3515
contig04733 contigK02622 0.3636
contig14757 contigK03055 0.4

 $ join DATA DIVERGE
contig04730 F GO:0000228 nuclear GO:0000783 telomere_cap contigK02622 0.3515
contig04730 F GO:0005528 reproduction GO:0001113 eggs contigK02622 0.3515
contig14757 P GO:0123456 immune GO:0003456 cells contigK03055 0.4
contig14757 P GO:0000782 nuclear GO:0001891 DNA_binding contigK03055 0.4
contig14757 C GO:0000001 immune GO:00066669 more_cells contigK03055 0.4

Upvotes: 2

Related Questions