Hi I have two files. One file contains all my interested uniprot id and second file contains uniprot ID with their corresponding family. My aim or I am trying to create new file.

So from file1, I want compare all Id with file2's Id. If equal then they should print id with their family name or not equal then they should print their id with 'None' word.

I uploaded, File1 which contains uniprot Id.
File2 which contains uniprot Id and family name.
File3 is my desired output file.
I tried to write script for it and that is
#!/usr/bin/perl -w

use strict;

my $outfile = 'file3.txt';
open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";

open (PM,"./file1.txt");
my @uniprot = <PM>;
close (PM);

my @col;


while (my $line = <>)
{
@col = split(/\s+/,$line);
print $col[1],"\n";
for (my $i=0; $i<@uniprot; $i++)
{
chomp $uniprot[$i];

print OUTFILE $uniprot[$i],"\t";
if ($uniprot[$i] eq $col[0]) {
print OUTFILE $col[1],"\n";
}
elsif ($uniprot[$i] ne $col[0]) {
print OUTFILE "None \n";
}
}
}

#print OUTFILE @uniprot;


close (OUTFILE);

Although its not working at all.

If anyone kindly help me I will be grateful to you.

Thank you in advance.

cheers
pallab

#!/usr/bin/perl;
use strict; 
use warnings; 

my $filename1 = 'file1.txt';
my $filename2 = 'file2.txt';
my $filename3 = 'file3.txt';

#Build a hash of arrays from file2
my %uniprot_families;
open my $fh, '<', $filename2 or die "Failed to open $filename2: $!";

while (my $rec = <$fh>){
    chomp($rec);
    my ($id, $family) = split(/\s+/, $rec);
    $uniprot_families{$id} = [] if not exists $uniprot_families{$id};
    push $uniprot_families{$id}, $family;
}
close $fh;

#open your other input file
open $fh, '<', $filename1 or die "Failed to open $filename1: $!";

#open your output file
open my $fh_out, '>', $filename3 or die "Failed to open $filename3: $!";

while (my $id = <$fh>){
    chomp($id);
    my $families;
    if (exists $uniprot_families{$id}){
        $families = join(',', @{$uniprot_families{$id}});
    }
    else{
        $families = 'None';
    }
    print $fh_out "$id\t$families\n";
}
close $fh;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.