I wrote a simple code to match and select/discard elements across two files. This code works on the $file_in which contains pairs of Id:
Id1 Id2
Id1 Id3
Id1 Id4
Id3 Id4
Id3 Id5
Id3 Id6
Id4 Id5
Id4 Id6
and on the $file_list which contains a list of Id:
Id1
Id2
Id4
Id6
Id7
Id8
What I want is to obtain an output in which are reported only the pairs from $file_in which both Id are included in the $file_list. Thus, my desired output looks like:
Id1 Id2
Id1 Id4
Id4 Id6
The code I'm using overloops across $file_in and gives me this output:
Id1 Id2
Id1 Id2
Id1 Id4
Id1 Id2
Id1 Id4
Id4 Id6
Id1 Id2
Id1 Id4
Id4 Id6
Id1 Id2
Id1 Id4
Id4 Id6
The code follows:
use List::MoreUtils qw(any);
$file_in = "gold_standards.txt";
$file_list = "list.txt";
open (HAN, "< $file_in") || die "Impossible open the in-file...";
my @r = <HAN>;
close (HAN);
open (PEW, "< $file_list") || die "Impossible open list_file...";
@l = <PEW>;
close (PEW);
for ($p=0; $p<=$#l; $p++){
chomp ($l[$p]);
for ($i=0; $i<=$#r; $i++){
chomp ($r[$i]);
@v = split (/\t/, $r[$i],2);
$id_protein1 = $v[0];
$id_protein2 = $v[1];
if ((any {$id_protein1 eq $_} @l) && (any {$id_protein2 eq $_} @l)) {
print "$r[$i]\n";
}
}
}
I'm sure it is a basic issue, but in this moment I cannot fix it. Any help is appreciated.