473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

help to print data with their corresponding value

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
Attached Files
File Type: txt file1.txt (28 Bytes, 341 views)
File Type: txt file2.txt (127 Bytes, 331 views)
File Type: txt file3.txt (90 Bytes, 339 views)
Feb 2 '12 #1
6 1897
RonB
589 Expert Mod 512MB
Use a HoA (hash of arrays) instead of individual arrays.

Process file1 first and use its content as the hash keys. As you process file2, split the line into its 2 fields and push field 2 onto the HoA, using field1 for the key.

Once the data is loaded into the HoA, loop over it and test if the key has a value, if it does, then output the data. If it doesn't have a value, then output the "None" line.
Feb 3 '12 #2
@RonB
Thank you so much for your quick reply. I am not sure about this hash of array because I didn't use it before !! :( But if you kindly give some out structure of your idea in code language(perl) may be I will try to crack that.
Feb 3 '12 #3
@RonB
I tried to write programme for HOA and its working for file2.txt only because I didn't get the idea about 2nd part of your suggestion, how to compare file1.txt and file2.txt.

#!/usr/bin/perl -w

use strict;




my @col;



my %HoA = (
$col[0] => [ $col[1] ],
);

while (my $line = <>)
{

#print OUTFILE $fam[$j],"\n";
@col = split(/\s+/,$line);

push @{ $HoA{$col[0]} },$col[1];


}

for my $family ( keys %HoA ) {
print "$family: @{ $HoA{$family} }\n";
}

----output-----

Q9ULV8: CBL
Use of uninitialized value in join or string at compare.pl line 28, <> line 10.
:
P22681: CBL GOLIATH IAP TRAF TRIM/RBCC
Q13191: CBL GOLIATH IAP PRAJA
---------------------
of course there is error which I am trying to fix it still now!!!

Thank you in advance for you further suggestion. :)
Feb 3 '12 #4
@RonB
I wrote new programme and its working perfectly what I want but there is some error still now

#!/usr/bin/perl -w

use strict;



my %H;
my @col;
my %HoA = (
$col[0] => [ $col[1] ],
);


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

for (my $i=0; $i<@fam; $i++) {
chomp $fam[$i];
%H = (
$fam[$i] => 'None',
);

}
while (my $line = <>)
{

@col = split(/\s+/,$line);

push @{ $HoA{$col[0]} },$col[1];

}

for my $family ( keys %HoA ) {
print "$family \t @{ $HoA{$family} }\n";
}



for ( keys %H ) {
unless ( exists $HoA{$_} ) {
print "$_ \t None \n";
next;
}
}


-----OUTPUT------
Use of uninitialized value in list assignment at compare.pl line 9.
Q9ULV8 CBL
Use of uninitialized value in join or string at compare.pl line 35, <> line 10.

P22681 CBL GOLIATH IAP TRAF TRIM/RBCC
Q13191 CBL GOLIATH IAP PRAJA
O00463 None

----------

Thank You again :)
Feb 3 '12 #5
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %data;
  7.  
  8. # initialize the hash keys with data in file1
  9. my $file1 = 'file1.txt';
  10. open my $fh1, '<', $file1 or die "failed to open '$file1' $!";
  11. while ( my $id = <$fh1> ) {
  12.     chomp $id;
  13.     $data{$id} = undef;
  14. }
  15. close $fh1;
  16.  
  17. # assign hash values with data in file2
  18. my $file2 = 'file2.txt';
  19. open my $fh2, '<', $file2 or die "failed to open '$file2' $!";
  20. while ( my $line = <$fh2> ) {
  21.     chomp $line;
  22.     my ($id, $family) = split /\s+/, $line;
  23.     next unless exists $data{$id};
  24.     push @{$data{$id}}, $family;
  25. }
  26. close $fh2;
  27.  
  28. # output the data to file3
  29. my $file3 = 'file3.txt';
  30. open my $fh3, '>', $file3 or die "failed to open '$file3' $!";
  31. foreach my $id ( sort keys %data ) {
  32.     print $fh3 "$id ";
  33.     if ( $data{$id} ) {
  34.         print $fh3 join(',', @{$data{$id}}), "\n";
  35.     }
  36.     else {
  37.         print $fh3 "None\n";
  38.     }
  39. }
  40. close $fh3;
  41.  
Feb 3 '12 #6
@RonB
Thanks for your help and specially giving idea how to use HoA and which is really good for me and I will use it in future. :)
Feb 3 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: spar | last post by:
I'm converting a Perl script to Python and have run into something I'm not sure how to do in Python. In Perl, I am running through a couple loops and inserting values directly into a complex...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
0
by: RDI | last post by:
I need to watch a certain print spooler and if a print job appears, extract the print data to a PRN file on the hard disk and delete it from the spooler. Can VB.Net do this? If so, can someone...
2
by: Mitul | last post by:
Hello friends, I am getting stuck at a point in my project. I would like to give functionality of print data from site. but there is a problem is that if there are lots of data which takes...
4
by: Christian Havel | last post by:
Hi, I have the problem, that my controls change their width value when the form is visible in the IDE and I compile the solution. I use .NET 1.1. Does anybody have a tip how I can prevent...
2
by: technocraze | last post by:
Hi community experts, I am encountering a problem to display the corresponding values of the combo box into the textboxes. I have chose option 1 & 3 for this implementation but neither is working....
0
by: fariba123 | last post by:
hi i have designed 2 web pages using smarty. in the first page a list of records has been shown with a link in the invoice no. whenever one clicks the link the details according to the invoice no...
2
by: wish | last post by:
my problem is after asort($array) then i want to print out the first value of the array? Like this array( => april =>free =>ghost =>key) I notice that the first index number is not a zero...and...
12
by: waterdriven | last post by:
Hello; I am a newbie. A homework assignment was assigned and I am having trouble getting started. The assignment reads: Write a program to print out the binary value of a 16 bit number.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.