CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
hash referencing. need urgent help

 



ap88
Novice

Nov 1, 2013, 4:57 PM

Post #1 of 14 (1729 views)
hash referencing. need urgent help Can't Post

hey folks,
i have two hashes and one array.
hash1 consists of key1 and value1,value2
hash2 consists of key2 and value2.

now i have an array containing some elements which are also the keys(key2) in hash2(not all but few).

key1 and value2 are same.

i need value1 and value2 of hash1 based on those array elements which are keys for hash2.



Code
open my $file, '<', $knowngene; 
#$line = <$file>;
while($line = <$file>) #alwaz remember to assign the file handle to a variable in while statement
{
chomp ($line);
@splitarray = split('\s+',$line); #file is splitted with white space. tab doesnt work here.
$knowngenehash{$splitarray[0]} = [$splitarray[3],$splitarray[4]];
#print Dumper \%knowngenehash;
}
close $file;

open my $file2, '<', $kgxref;
#$line = <$file2>;
while($line = <$file2>) #alwaz remember to assign the file handle to a variable in while statement
{
chomp ($line);
@splitarray = split('\s+',$line); #file is splitted with white space. tab doesnt work here.
$kgxrefhash{$splitarray[4]} = $splitarray[0];
#print Dumper \%kgxrefhash;
}
close $file2;



#my $file = "junk";
open (file3, "< $geneset") or die "Can't open $geneset for read: $!";
$line = <file3>;
while ($line = <file3>)
{

push (@genesetarray,$line);
chomp($line);

}

close file3 or die "Cannot close $file: $!";

#print @genesetarray;

my $genes = \@genesetarray;
my $test = \%kgxrefhash;
$finalhash1{$genes} = $kgxrefhash{genes};
print Dumper \%finalhash1;


please help how can i get the desired value1 and value2


Kenosis
User

Nov 2, 2013, 12:27 PM

Post #2 of 14 (1711 views)
Re: [ap88] hash referencing. need urgent help [In reply to] Can't Post

You have:

Code
push (@genesetarray,$line);  
chomp($line);

but I believe you meant:

Code
chomp($line);  
push (@genesetarray,$line);

Try the following (untested) to obtain value1 and value2 of hash1 based on those array elements which are keys for hash2:

Code
print "@{ $knowngenehash{ $kgxrefhash{$_} } }\n" for grep exists $kgxrefhash{$_}, @genesetarray;

Explained:

Code
print "@{ $knowngenehash{ $kgxrefhash{$_} } }\n" for grep exists $kgxrefhash{$_}, @genesetarray; 
^^ ^ ^ ^ ^ ^
|| | | | | |
|| | | | | + - Is the element of @genesetarray a key of %kgxrefhash?
|| | | | + - Filter elements of @genesetarray
|| | | + - Elements of @genesetarray that are keys of %kgxrefhash
|| | + - Get hash2 values
|| + - Hash1
|+ - Dereference the value as list reference
+ - Interpolate the array to print the elements

Hope this helps!


ap88
Novice

Nov 2, 2013, 3:41 PM

Post #3 of 14 (1699 views)
Re: [Kenosis] hash referencing. need urgent help [In reply to] Can't Post

@Kenosis

i tried your piece of code but it just does not print anything.

here are the output for both hash and array elements:

knowngenehash:

{
'uc001abl.3' => [
'671915',
'671947'
],
'uc001aaq.2' => [
'321083',
'321115'
],
'uc021oeg.2' => [
'134772',
'140566'
}

kgxrefhash:

'LCE1D' => [
'uc021ozh.1'
],
'BRF2' => [
'uc003xkk.3'
],
'SIGLEC11' => [
'uc010ybi.2'
],
'RF00189' => [
'uc021vqx.1'


genesetarray:
WASL
TRAF6
TYK2
VCAM1
VCL


i am badly stuck here and new to hash and referencing.
i will appreciate any type of help.


In Reply To



Zhris
Enthusiast

Nov 2, 2013, 4:41 PM

Post #4 of 14 (1692 views)
Re: [ap88] hash referencing. need urgent help [In reply to] Can't Post

Hi,

Below I have taken the output you provided and produced the following test script:


Code
use Data::Dumper; 

my %knowngenehash = (
'uc001abl.3' => [
'671915',
'671947'
],
'uc001aaq.2' => [
'321083',
'321115'
],
'uc021oeg.2' => [
'134772',
'140566'
]
);

my %kgxrefhash = (
'LCE1D' => [
'uc021ozh.1'
],
'BRF2' => [
'uc003xkk.3'
],
'SIGLEC11' => [
'uc010ybi.2'
],
'RF00189' => [
'uc021vqx.1'
]
);

my @genesetarray = qw(
WASL
TRAF6
TYK2
VCAM1
VCL
);

print Dumper(\%knowngenehash, \%kgxrefhash, \@genesetarray);

my $genes = \@genesetarray;
my $test = \%kgxrefhash;
$finalhash1{$genes} = $kgxrefhash{genes};
print Dumper \%finalhash1;


Which outputs:


Code
$VAR1 = { 
'uc021oeg.2' => [
'134772',
'140566'
],
'uc001aaq.2' => [
'321083',
'321115'
],
'uc001abl.3' => [
'671915',
'671947'
]
};
$VAR2 = {
'LCE1D' => [
'uc021ozh.1'
],
'SIGLEC11' => [
'uc010ybi.2'
],
'BRF2' => [
'uc003xkk.3'
],
'RF00189' => [
'uc021vqx.1'
]
};
$VAR3 = [
'WASL',
'TRAF6',
'TYK2',
'VCAM1',
'VCL'
];
$VAR1 = {
'ARRAY(0x813db34)' => undef
};


Obvously $VAR1 = { 'ARRAY(0x813db34)' => undef }; isn't your desired output. Can you provide us with what you expect %finalhash1 to look like (your desired output).

Kenosis provided a method of processing the data, but there is nothing for it to print based on your sample data. Also, here is a possible correction, but I'm uncertain of what you expect:


Code
print "@{$knowngenehash{$kgxrefhash{$_}->[0]}}\n" for grep exists $kgxrefhash{$_} && exists $knowngenehash{$kgxrefhash{$_}->[0]}, @genesetarray;


Chris


(This post was edited by Zhris on Nov 2, 2013, 7:52 PM)


Kenosis
User

Nov 2, 2013, 4:45 PM

Post #5 of 14 (1691 views)
Re: [ap88] hash referencing. need urgent help [In reply to] Can't Post

Well, id does help to see some data, since I didn't realize that the values of %kgxrefhash were references to lists. Also, there needs (of course) to be data that common to all three data structures as you mentioned. Give this, try the following (tested):

Code
use warnings; 
use strict;

my %knowngenehash = (
'uc021ozh.1' => [ '671915', '671947' ],
'uc001aaq.2' => [ '321083', '321115' ],
'uc021vqx.1' => [ '134772', '140566' ]
);

my %kgxrefhash = (
'LCE1D' => ['uc021ozh.1'],
'BRF2' => ['uc003xkk.3'],
'SIGLEC11' => ['uc010ybi.2'],
'RF00189' => ['uc021vqx.1']
);

my @genesetarray = qw/WASL TRAF6 TYK2 VCAM1 VCL LCE1D RF00189/;

for ( grep exists $kgxrefhash{$_}, @genesetarray ) {
print "@{ $knowngenehash{ $kgxrefhash{$_}->[0] } }\n"
if exists $knowngenehash{ $kgxrefhash{$_}->[0] };
}

Output:

Code
671915 671947 
134772 140566

Note the dereferencing arrow (->) used to get a list items of %kgxrefhash;

Hope this helps!


Zhris
Enthusiast

Nov 2, 2013, 4:59 PM

Post #6 of 14 (1687 views)
Re: [Kenosis] hash referencing. need urgent help [In reply to] Can't Post

Ahh, you have it covered Laugh!

Chris


Kenosis
User

Nov 2, 2013, 5:28 PM

Post #7 of 14 (1682 views)
Re: [Zhris] hash referencing. need urgent help [In reply to] Can't Post

And you do, too... Smile


(This post was edited by Kenosis on Nov 2, 2013, 6:27 PM)


ap88
Novice

Nov 2, 2013, 6:53 PM

Post #8 of 14 (1674 views)
Re: [Kenosis] hash referencing. need urgent help [In reply to] Can't Post

hey folks, thanks for your help.


i tried the code for my files but unable to print the desired value i.e

genename value1 value2

i am attaching my code file and files along with this reply. i am trying to debug it but couldn't find out the exact cause of it. may be because my head got stuck :(

perl fourth.pl -knowngene kg.txt -kgxref kgXref.txt -geneset geneset1

please use the above command.
waiting for your reply.
Attachments: fourth.pl (2.13 KB)
  knowngene.txt (0.62 KB)
  kgxref.txt (28.9 KB)
  geneset (2.13 KB)


Zhris
Enthusiast

Nov 2, 2013, 7:51 PM

Post #9 of 14 (1666 views)
Re: [ap88] hash referencing. need urgent help [In reply to] Can't Post

Hi,

knowngene.txt appears to be a Perl script and not data.

Chris


(This post was edited by Zhris on Nov 2, 2013, 7:51 PM)


ap88
Novice

Nov 2, 2013, 8:27 PM

Post #10 of 14 (1658 views)
Re: [Zhris] hash referencing. need urgent help [In reply to] Can't Post

ohh i am sorry...


(This post was edited by ap88 on Nov 3, 2013, 5:22 AM)


Kenosis
User

Nov 2, 2013, 10:30 PM

Post #11 of 14 (1642 views)
Re: [ap88] hash referencing. need urgent help [In reply to] Can't Post

This is also a Perl script.


ap88
Novice

Nov 3, 2013, 5:23 AM

Post #12 of 14 (1617 views)
Re: [Kenosis] hash referencing. need urgent help [In reply to] Can't Post

 
Attachments: knowngene.txt (23.3 KB)


Zhris
Enthusiast

Nov 3, 2013, 8:52 AM

Post #13 of 14 (1604 views)
Re: [ap88] hash referencing. need urgent help [In reply to] Can't Post

Hi,

After executing your code with your sample data it looks as though there aren't any elements in @genesetarray that exist in %kgxrefhash etc, therefore there is nothing to print. I also made this correction:


Code
push (@genesetarray,@test); 
push (@genesetarray,$_);


Chris


ap88
Novice

Nov 3, 2013, 10:11 AM

Post #14 of 14 (1599 views)
Re: [Zhris] hash referencing. need urgent help [In reply to] Can't Post

thank you very much chris. i will check the data file once again and be back with the solution.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives