I have two files, each approximately 700,000 lines, that look like:
File1
1 rs58108140 0 10583 A G
1 rs189107123 0 10611 G C
1 rs180734498 0 13302 T C
1 rs144762171 0 13327 C G
22 rs1001008 0 44928369 G A
File2
hg19chrc snpid a1 a2 bp info or se p ngt
1 rs4951859 C G 729679 0.631 0.97853 0.0173 0.2083 0
1 rs142557973 T C 731718 0.665 1.01949 0.0198 0.3298 0
22 rs1001008 A G 44928369 0.969 0.98649 0.0107 0.2023 0
I have a script where I'm finding lines where fields 1 and 4 from File 1 and fields 1 and 5 from File 2 match. Then if fields 5 and 6 from File 1 match fields 3 and 4 from File 2, I'm simply printing out the line from File 2. However, if fields 3 and 4 (File2) are flipped with respect to fields 5 and 6 (File1), I'm taking the reciprocal of field 7 from File2, and printing the line with that adjusted field 7:
#! perl -w
use strict;
use warnings;
my @kgloci;
open( my $loci_in, "<", "File1" ) or die $!;
while (<$loci_in>) {
my ($chr, $snpID, $dist, $bp, $A1, $A2) = split;
next if m/Chromosome/;
push @kgloci, [$chr, $snpID, $dist, $bp, $A1, $A2];
}
close $loci_in;
my $filename = shift @ARGV;
open( my $input, "<", "File2" ) or die $!;
while (<$input>) {
next if m/hg19chrc/;
my ($chr, $snpID, $A1, $A2, $bp, $info, $or, $se, $p, $ngt) = split;
foreach my $kglocus (@kgloci) {
if ( $chr == $kglocus->[0]
and $bp == $kglocus->[3]
and $A1 eq $kglocus->[4] ){
print "$chr $snpID $A1 $A2 $bp $info $or $se $p $ngt\n";
next;
}
elsif ( $chr == $kglocus->[0]
and $bp == $kglocus->[3]
and $A1 eq $kglocus->[5]){
my $new_or = 1/$or;
print "$chr $snpID $A1 $A2 $bp $info $new_or $se $p $ngt\n";
next;
}
}
}
close($input);
As is, the script will run for days. Can someone point out a way to increase efficiency?