#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0]; my %GFs = get_gene_families($file); foreach my $key (keys(%GFs)){ print $key.$GFs{$key}."\n"; } exit; sub get_gene_families{ my $fileName = $_[0]; my %hash = (); open(IFILE, "$fileName") or die "Couldn't open: $fileName\n"; while(my $line = ){ my @genes = split(/\,/, $line); my $new = 1; foreach my $key (keys(%hash)){ my @split = split(/\,/, $hash{$key}); push(@split, $key); if(contains($genes[0], \@split) && contains($genes[1], \@split)){ $new = 0; } if(contains($genes[0], \@split) && !contains($genes[1], \@split)){ $hash{$key} .= ",".$genes[1]; $new = 0; } if(!contains($genes[0], \@split) && contains($genes[1], \@split)){ $hash{$key} .= ",".$genes[0]; $new = 0; } } if($new){ $hash{$genes[0]} .= ",".$genes[1]; } } close IFILE; return %hash; } sub contains{ my $target = $_[0]; my @array = @{$_[1]}; foreach my $element (@array){ if($element eq $target){ return 1; } else } return 0; }