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:
Help with key/value pairs

 



TheDragonReborn
Novice

Oct 19, 2008, 11:59 AM

Post #1 of 17 (2289 views)
Help with key/value pairs Can't Post

Hello to all,

I am a newbie to Perl and am having some problems with a program that uses key/value pairs. Below is my code.

Code
## A program to take an Interactome data set and convert to unicon format for JProNet 

# Example input
# YLR291C YNL229C CCSB-Y2H
# YLR291C YCR086W CCSB-Y2H
# YLR291C YPR062W CCSB-Y2H
# YJL085W YBR057C CCSB-Y2H
# YLR227C YDR208W CCSB-Y2H
# YER106W YCR086W CCSB-Y2H

# Example Output
# 1 1 2 1
# 2 1 3 1
# 3 1 4 1
# 4 5 6 1
# 5 7 3 1


print "Please type the file to be converted: " ;

$input = <STDIN> ;

chomp $input ;

# Does file exist?
unless( -e $input)
{
print "File \"$input\" does not seem to exist!!\n";
exit ;
}

# Can we open the file?
unless( open(INPUT, $input))
{
print "Cannot open file \"$input\"\n\n" ;
exit ;
}

#counter for nodes
$i = 1 ;

#counter for output file (iteration)
$count = 0 ;

#weight value for edge
$weight = 1 ;

#array for lines (collect so that we can print after header part of fiile
@lines ;

# $endvertex is the last node iterated ( not neccessarily the last node because of multiple links)
$endvertex = 0 ;

#hash for key/value pairs between nodes ( Y12345 => 1)
%KeyMap ;

while(<INPUT>)
{
foreach $line (<INPUT>)
{
chomp($line) ;
@tokens = split('\s\t', $line) ;

$node1 = $token[0] ;
$node2 = $token[1] ;

# MAP THE NODES TO VALUES

if(exists $KeyMap{'$node1}')
{
}
else
{
$KeyMap('$node1' => '$i') ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{'$node1'} ;

++$i ;
}

if(exists $KeyMap{'$node2'})
{
}
else
{
$KeyMap('$node2' => '$i') ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{'$node2'} ;

++$i ;
}

# Collect output lines (exp. 1 1 2 1)
# Print nodes in ascending order (1 1 2 1) NOT (1 2 1 1) !!!
if($KeyMap[$node1] < $KeyMap[$node2])
{
$lines[$count] = "$count+1 $KeyMap{'$node1'} $KeyMap{'$node2'} $weight" ;
$count = $count + 1 ;
}
else
{
$lines[$count] = "$count+1 $KeyMap{'$node2'} $KeyMap{'$node1'} $weight" ;
$count = $count + 1 ;
}
}
}

# Print out header to .unicon file
print "Chain\n\n" ;

print "Vertexnumber= $endvertex\n" ; # The number for the last vertex will be total
print "Startvertex=1\n" ;
print "endvertex = $endvertex\n";
print "vertexadd=0\n" ;
print "sarig=0\n\n" ; # Extra line

print "chained\n" ;

print "contact\n\n" ; # Extra line

# Print out lines
# foreach($row @lines)
# {
# print "$row\n" ;
# }

print "contactend\n\n" ;

close INPUT ;


The input file would look something similar to this:


Code
YLR291C	YNL229C	CCSB-Y2H 
YLR291C YCR086W CCSB-Y2H
YLR291C YPR062W CCSB-Y2H
YJL085W YBR057C CCSB-Y2H
YLR227C YDR208W CCSB-Y2H
YER106W YCR086W CCSB-Y2H
YLR010C YDR082W CCSB-Y2H
YDR013W YPR135W CCSB-Y2H
YLR291C YGR267C CCSB-Y2H
YLR291C YOR111W CCSB-Y2H
YLR291C YLR377C CCSB-Y2H
YLR291C YHR112C CCSB-Y2H
YLR291C YJR057W CCSB-Y2H
YLR291C YCL028W CCSB-Y2H
YDR439W YCR086W CCSB-Y2H
YFR008W YMR029C CCSB-Y2H


The output should look something akin to this:


Chain

Vertexnumber= 17
Startvertex= 1
endvertex= 17
vertexadd=0
sarig=0
chainend

contact

1 1 2 1
2 1 3 1
3 1 4 1
4 5 6 1
contactend


I believe that my problem is with the way I am setting up my key/value pairs. Any suggestions would be great.

Thanks

Code



      
    


KevinR
Veteran


Oct 19, 2008, 12:31 PM

Post #2 of 17 (2286 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

Change this line:

@tokens = split('\s\t', $line) ;

change to:

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

and retry your code
-------------------------------------------------


TheDragonReborn
Novice

Oct 19, 2008, 12:49 PM

Post #3 of 17 (2285 views)
Re: [KevinR] Help with key/value pairs [In reply to] Can't Post

No help. My output is only printing

Chain

Vertexnumber= 1
Startvertex=1
endvertex = 0
vertexadd=0
sarig=0

chained
contact

contactend

any more suggestions?


eWish
User

Oct 19, 2008, 1:11 PM

Post #4 of 17 (2279 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

Are you using the strict pragma? If not then add this line of code to your script.


Code
use strict;


This is not correct.

Code
if(exists $KeyMap{'$node1}')



Should be:

Code
if(exists $KeyMap{$node1})


Also, this line looks wrong (which is done in more than one spot):

Code
$KeyMap('$node2' => '$i') ;


I believe that it should be more like this:

Code
$KeyMap{$node2} = $i;



(This post was edited by eWish on Oct 19, 2008, 1:19 PM)


eWish
User

Oct 19, 2008, 1:24 PM

Post #5 of 17 (2272 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

Also, I would delete the quotes around the variable $node1 and $node2.

Like so:

Code
$KeyMap{$node1}; 
$KeyMap{$node2};


By doing it the way you have it written you should be getting a literal key/value pair called $node1 and $node2 instead of the value that $node1 and $node2 hold.


KevinR
Veteran


Oct 19, 2008, 2:48 PM

Post #6 of 17 (2257 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

Maybe you understand the difference between single-quotes and double-quotes but your code does not reflect that. If not, you need to read up on single and double quoted strings and how the quotes affect variable interpolation/expansion.
-------------------------------------------------


(This post was edited by KevinR on Oct 19, 2008, 2:50 PM)


eWish
User

Oct 19, 2008, 3:52 PM

Post #7 of 17 (2252 views)
Re: [KevinR] Help with key/value pairs [In reply to] Can't Post

I will get to reading about single and double quotes. Thanks.


KevinR
Veteran


Oct 19, 2008, 4:05 PM

Post #8 of 17 (2250 views)
Re: [eWish] Help with key/value pairs [In reply to] Can't Post

Not you Kevin, I meant the OP. Sorry about the confusion.
-------------------------------------------------


TheDragonReborn
Novice

Oct 20, 2008, 5:27 AM

Post #9 of 17 (2240 views)
Re: [KevinR] Help with key/value pairs [In reply to] Can't Post

Thanks for the help. However, the program still does not seem to be acting on the input file. Here is my updated code:


Code
#!/usr/bin/perl 
## A program to take an Interactome data set and convert to unicon format for JProNet

use strict ;

# Example input
# YLR291C YNL229C CCSB-Y2H
# YLR291C YCR086W CCSB-Y2H
# YLR291C YPR062W CCSB-Y2H
# YJL085W YBR057C CCSB-Y2H
# YLR227C YDR208W CCSB-Y2H
# YER106W YCR086W CCSB-Y2H

# Example Output
# 1 1 2 1
# 2 1 3 1
# 3 1 4 1
# 4 5 6 1
# 5 7 3 1


print "Please type the file to be converted: " ;

$input = <STDIN> ;

chomp $input ;

# Does file exist?
unless( -e $input)
{
print "File \"$input\" does not seem to exist!!\n";
exit ;
}

# Can we open the file?
unless( open(INPUT, $input))
{
print "Cannot open file \"$input\"\n\n" ;
exit ;
}

#counter for nodes
$i = 1 ;

#counter for output file (iteration)
$count = 0 ;

#weight value for edge
$weight = 1 ;

#array for lines (collect so that we can print after header part of fiile
@lines ;

# $endvertex is the last node iterated ( not neccessarily the last node because of multiple links)
$endvertex = 0 ;

#hash for key/value pairs between nodes ( Y12345 => 1)
%KeyMap ;

while(INPUT)
{
foreach ($line (INPUT))
{
chomp($line) ;
@tokens = split(/\s+/, $line) ;

$node1 = $tokens[0] ;
$node2 = $tokens[1] ;

# MAP THE NODES TO VALUES

if(exists $KeyMap{$node1})
{
}
else
{
$KeyMap({$node1} = $i) ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node1} ;

++$i ;
}

if(exists $KeyMap{$node2})
{
}
else
{
$KeyMap({$node2} = $i) ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node2} ;

++$i ;
}

# Collect output lines (exp. 1 1 2 1)
# Print nodes in ascending order (1 1 2 1) NOT (1 2 1 1) !!!
if($KeyMap[$node1] < $KeyMap[$node2])
{
$lines[$count] = "$count+1 $KeyMap{$node1} $KeyMap{$node2} $weight" ;
$count = $count + 1 ;
}
else
{
$lines[$count] = "$count+1 $KeyMap{$node2} $KeyMap{$node1} $weight" ;
$count = $count + 1 ;
}
}
}

#############################################################################
##################### OUTPUT FOR PROGRAM ##################################
#############################################################################

# Print out header to .unicon file
print "Chain\n\n" ;

print "Vertexnumber= $endvertex\n" ; # The number for the last vertex will be total
print "Startvertex=1\n" ;
print "endvertex = $endvertex\n";
print "vertexadd=0\n" ;
print "sarig=0\n\n" ; # Extra line

print "chained\n" ;

print "contact\n\n" ; # Extra line

# Print out lines
foreach($row @lines)
{
print "$row\n" ;
}

print "contactend\n\n" ;

close INPUT ;


Here is my output


Quote
Chain

Vertexnumber= 1
Startvertex=1
endvertex = 0
vertexadd=0
sarig=0

chained
contact

contactend


Any suggestions ?


KevinR
Veteran


Oct 20, 2008, 11:46 AM

Post #10 of 17 (2229 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

I still see in your program:

$KeyMap({$node1} = $i) ;

That is wrong as has been previoulsy noted, it should be:

$KeyMap{$node1} = $i;
-------------------------------------------------


TheDragonReborn
Novice

Oct 20, 2008, 11:55 AM

Post #11 of 17 (2228 views)
Re: [KevinR] Help with key/value pairs [In reply to] Can't Post

Thanks for the reply. Sorry I missed that edit. However, still not processing the key/value pairs. I added some lines to help illustrate what I am trying to do in my new code:


Code
 
## A program to take an Interactome data set and convert to unicon format for JProNet

# Example input (Disregard the 3rd and 4th columns)
# YLR291C YNL229C CCSB-Y2H
# YLR291C YCR086W CCSB-Y2H
# YLR291C YPR062W CCSB-Y2H
# YJL085W YBR057C CCSB-Y2H


# Example assignment of nodes to integer values
# YLR291C => 1
# YNL229C => 2
# YCR086W => 3
# YPR062W => 4
# YJL085W => 5
# YBR057C => 6


# Example Output for matrix part

# Counter Node Node Weight (This line will not be printed)
# 1 1 2 1
# 2 1 3 1
# 3 1 4 1
# 4 5 6 1

print "Please type the file to be converted: " ;

$input = <STDIN> ;

chomp $input ;

# Does file exist?
unless( -e $input)
{
print "File \"$input\" does not seem to exist!!\n";
exit ;
}

# Can we open the file?
unless( open(INPUT, $input))
{
print "Cannot open file \"$input\"\n\n" ;
exit ;
}

#counter for nodes
$i = 1 ;

#counter for output file (iteration)
$count = 0 ;

#weight value for edge
$weight = 1 ;

#array for lines (collect so that we can print after header part of fiile
@lines ;

# $endvertex is the last node iterated ( not neccessarily the last node because of multiple links)
$endvertex = 0 ;

#hash for key/value pairs between nodes ( Y12345 => 1)
%KeyMap ;

while(INPUT)
{
foreach ($line (INPUT))
{
chomp($line) ;
@tokens = split(/\s+/, $line) ;

$node1 = $tokens[0] ;
$node2 = $tokens[1] ;

# MAP THE NODES TO VALUES

if(exists $KeyMap{$node1})
{
}
else
{
$KeyMap{$node1} = $i ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node1} ;

++$i ;
}

if(exists $KeyMap{$node2})
{
}
else
{
$KeyMap{$node2} = $i ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node2} ;

++$i ;
}

# MATRIX PART !!!
# Collect output lines (exp. 1 1 2 1)
# Print nodes in ascending order (1 1 2 1) NOT (1 2 1 1) !!!
if($KeyMap{$node1} < $KeyMap{$node2})
{
$lines[$count] = "$count+1 $KeyMap{$node1} $KeyMap{$node2} $weight" ;
$count = $count + 1 ;
}
else
{
$lines[$count] = "$count+1 $KeyMap{$node2} $KeyMap{$node1} $weight" ;
$count = $count + 1 ;
}
}
}

#############################################################################
##################### OUTPUT FOR PROGRAM ##################################
#############################################################################

# Print out header to .unicon file
print "Chain\n\n" ;

print "Vertexnumber= $endvertex\n" ; # The number for the last vertex will be total
print "Startvertex=1\n" ;
print "endvertex = $endvertex\n";
print "vertexadd=0\n" ;
print "sarig=0\n\n" ; # Extra line

print "chained\n" ;

print "contact\n\n" ; # Extra line

# Print out lines (Matrix part)
foreach($row @lines)
{
print "$row\n" ;
}

print "contactend\n\n" ;

close INPUT ;

exit ;


I am still unsure why the keys are not being assigned to values.
Any suggestions...


KevinR
Veteran


Oct 20, 2008, 12:06 PM

Post #12 of 17 (2226 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

add this line to your program:


Code
use warnings;


it will help with syntax problems, like here:


Code
while(INPUT)  
{
foreach ($line (INPUT))
{


those four lines should be replaced with


Code
while(my $line = <INPUT>)  
{


Fix that and retry your code.
-------------------------------------------------


(This post was edited by KevinR on Oct 20, 2008, 12:08 PM)


TheDragonReborn
Novice

Oct 23, 2008, 6:45 PM

Post #13 of 17 (2191 views)
Re: [KevinR] Help with key/value pairs [In reply to] Can't Post

Hello to all,

Still having problems with my code.

I am trying to point a string to an integer value. This is my input file

YLR291C YNL229C CCSB-Y2H
YLR291C YCR086W CCSB-Y2H
YLR291C YPR062W CCSB-Y2H
YJL085W YBR057C CCSB-Y2H
YLR227C YDR208W CCSB-Y2H

This is the desired output

YLR291C YNL229C
1 2
YLR291C YCR086W
1 3
YLR291C YPR062W
1 4
YJL085W YBR057C
5 6
YLR227C YDR208W
7 8

I am a novice to Perl and unsure what I am doing wrong. Here is
the code




Code
$dataFile = 'test.txt' ; 

unless(open(DATAFILE, $dataFile))
{
print "Error !! Did not read file.\n" ;
exit ;
}

$x = 1 ;
$i = 1 ;

while($line = <DATAFILE>)
{
# Test file
#print $line ;

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

$node1 = $tokens[0] ;
$node2 = $tokens[1] ;

if($KeyMap{"$node1"})
{
return $KeyMap{$node1} ;
}
else
{
%KeyMap = ($node1 => $i) ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node1} ;
++$i ;
}

if($KeyMap{"$node2"})
{
return $KeyMap{$node2} ;
}
else
{
%KeyMap = ($node2 => $i) ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node2} ;
++$i ;
}

print $node1 , " ", $node2 , "\n" ;
print $KeyMap{$node1}, " ", $KeyMap{$node2},"\n" ;
++$x ;
}

close DATAFILE ;
print "\nendvertex = $endvertex\n" ;

exit ;


Any help would be great.


KevinR
Veteran


Oct 23, 2008, 7:15 PM

Post #14 of 17 (2189 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

Whats the significance of the digits betwwen the lines?

1 2
1 3
1 4
etc
etc

How do they relate to the data?
-------------------------------------------------


KevinR
Veteran


Oct 23, 2008, 7:23 PM

Post #15 of 17 (2188 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

Fixing a couple of things in your code produces the output you desire but I don't understand what those numbers signify.


Code
$dataFile = 'test.txt' ;  

unless(open(DATAFILE, $dataFile))
{
print "Error !! Did not read file.\n" ;
exit ;
}

$x = 1 ;
$i = 1 ;

while($line = <DATA>)
{
# Test file
#print $line ;

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

$node1 = $tokens[0] ;
$node2 = $tokens[1] ;

if($KeyMap{"$node1"})
{
# return $KeyMap{$node1} ;
}
else
{
$KeyMap{$node1} = $i;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node1} ;
++$i ;
}

if($KeyMap{"$node2"})
{
# return $KeyMap{$node2} ;
}
else
{
$KeyMap{$node2} = $i ;

# Record the highest value node (Last increment will be highest node)
$endvertex = $KeyMap{$node2} ;
++$i ;
}

print $node1 , " ", $node2 , "\n" ;
print $KeyMap{$node1}, " ", $KeyMap{$node2},"\n" ;
++$x ;
}

close DATAFILE ;
print "\nendvertex = $endvertex\n" ;

exit ;


Your use of the "return" function is not correct, but I am not sure what you are trying to do in those lines.
-------------------------------------------------


TheDragonReborn
Novice

Oct 24, 2008, 8:52 AM

Post #16 of 17 (2182 views)
Re: [KevinR] Help with key/value pairs [In reply to] Can't Post

Thank you so much. Program is finally working.


KevinR
Veteran


Oct 24, 2008, 9:46 AM

Post #17 of 17 (2181 views)
Re: [TheDragonReborn] Help with key/value pairs [In reply to] Can't Post

You're welcome.
-------------------------------------------------

 
 


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

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