in reply to Re: Compare three columns of one file with three columns of another file in perl
in thread Compare three columns of one file with three columns of another file in perl

Hi Aaron, Thanks. They have to match in order.Like all three chr,start,end of one file should match the chr,start,end of second file.

  • Comment on Re^2: Compare three columns of one file with three columns of another file in perl

Replies are listed 'Best First'.
Re^3: Compare three columns of one file with three columns of another file in perl
by anonym (Acolyte) on May 26, 2015 at 03:07 UTC

    Aaron File infile few lines are chr10 40095550 40096075 chr10 40102275 40102575

    second infile few lines are chr1 mm10_knownGene exon 3205904 3207317 0.000000 - . gene_id "uc007aet.1"; transcript_id "uc007aet.1"; chr1 mm10_knownGene exon 3213439 3215632 0.000000 - . gene_id "uc007aet.1"; transcript_id "uc007aet.1";

    output should be for matching chr,start,end of first file with that of second file.

      If you edit your post and put <code> tags around your data, as you did with your code in your first post in this thread, we'll be able to read it.

      Okay, it sounds like you have a fairly typical "match lines from fileA to lines in fileB" problem, with the added feature of needing to match three fields instead of just one. So the standard solution is to go through one file (usually the smaller one to keep memory use lower), creating a hash of keys with the important fields from that file, then go through the other file line-by-line, checking each line to see if its fields are found in the hash. In pseudo-code:

      create a %hash open fileA while get a line from fileA parse out the three important fields from the line concat those fields into a single string put that string in the hash as a key, with the line as its value close fileA open fileB while get a line from fileB parse out the three important fields from this line concat those fields into a single string if that string exists as a key in the hash print out this line and the value of the matching hash key close fileB

      One caveat: this only works if the keys created from fileA are unique. If it's possible for multiple lines to have the same key (the same first three column values), you'll have to get a bit more complicated.

      Aaron B.
      Available for small or large Perl jobs and *nix system administration; see my home node.

        Hi Thanks.Below is the code i tried but it does not print anything to the STDOUT.

        #!usr/bin/perl use strict; use warnings; my ($infile1,$infile2) = @ARGV; open (IFILE1, "<", $infile1) || die "Cannot open $infile1 +:$!\n"; open (IFILE2, "<", $infile2) || die "Cannot open $infile2:$!\n"; my @array1; my %hash1; my $str; while (<IFILE1>) { @array1 = split (" ", $_); $str = $array1[0]."\t".$array1[1]."\t".$array1[2]; $hash1{$str} = 1; } close IFILE1; my @array2; my $string; while (<IFILE2>) { @array2 = split (" ", $_); $string = $array2[0]."\t".$array2[3]."\t".$array2[4]; #print "$string\n"; if (exists($hash1{$string})) { print "$string\n"; } } close IFILE2;