|
Hello to all,
I am a newbie to Perl and am having problems with the use of key/value pairs. Below is my code. What am I doing wrong with my assignment of key/value pairs?
My output is empty (no matrix).
################################ CODE #######################################
#!/usr/bin/perl
use strict ;
use warnings ;
## 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
# 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 ;
########################### END CODE ######################################
Any help would be great.
Thanks.
|
|
|
run it in your debugger and then you can narrow the problem down.
|
|
|
|
|
|
|
// |