#! /local/bin/perl use strict; use warnings; #Declare hash to pull IDs and corresponding degrees into from file my %degree; my %newdegree; my @geneID_1; my @geneID_2; my $filename = "edges.txt"; #Set a variable for our file name open(my $fh, "<", $filename) or die "Can't open file $filename."; #Open the file edges.txt while (<$fh>) { if ($_ =~ m/(\S+)\t(\S+)/) { #Match the IDs in the file to $1 and $2 $degree{$1}++; #Count the appearance of each ID, and store this as $degree{$2}++; #the value for that key (this will be the degree) push (@geneID_1, $1); push (@geneID_2, $2); }} close $fh; #Close the file edges.txt #Calling the following subroutines DegreeDistribution(); RandomSequence(); DegreeRan(); #Subroutines can be found below sub DegreeDistribution{ #Determines the degree distribution (i.e. frequency of each degree) my %degree_distribution; $degree_distribution{$_}++ for values %degree; #Creates a hash with the keys as the degrees and the values as the frequencies for my $id (keys %degree) { #This corresponds each below to it's key my $d = $degree{$id}; #This is the degree value corresponding to its gene ID my $freq = $degree_distribution{$d}; #This is the frequency value corresponding to its gene ID??? print "$id has degree:\t$d\t(freq: $freq)\n"; }} sub RandomSequence{ #Generates a hash of random ID interactions and each IDs degree my $length = scalar (@geneID_1); for (my $i=0; $i<$length; $i++){ my $ID = int(rand($length)); my $new_id1 = $geneID_1[$ID]; my $new_id2 = $geneID_2[$ID]; $newdegree{$new_id1}++; $newdegree{$new_id2}++; }} sub DegreeRan{ #same as sub DegreeDistribution but for the random sequence my %degree_distribution; $degree_distribution{$_}++ for values %newdegree; #Creates a hash with the keys as the degrees and the values as the frequencies for my $id (keys %degree) { #This corresponds each below to it's key my $d = $degree{$id}; #This is the degree value corresponding to its gene ID my $freq = $degree_distribution{$d}; #This is the frequency value corresponding to its gene ID??? print "$id has degree:\t$d\t(freq: $freq)\n"; }} exit;