This code will cluster your "genes".. I do not know how far it goes to allow you to solve the second part of your problem.

#!/usr/bin/perl use warnings; use strict; my %adjacent; while (<DATA>) { my @component = split /\,/, $_, 3; my $parent; if ( defined($adjacent{$component[0]})) { $adjacent{$component[0]}{$component[1]} = $component[2]; } elsif ( defined($adjacent{$component[1]})) { $adjacent{$component[1]}{$component[0]} = $component[2]; } elsif ( $parent = has_parent($component[0]) ) { $adjacent{$parent}{$component[1]} = $component[2]; } elsif ( $parent = has_parent($component[1]) ) { $adjacent{$parent}{$component[0]} = $component[2]; } else { $adjacent{$component[0]}{$component[1]} = $component[2]; } } print "\n\n*----------------------------------------------*\n"; for my $inner ( sort keys %adjacent ) { print "$inner "; for my $outer ( sort keys %{$adjacent{$inner}} ) { print ", $outer"; } print "\n"; } sub has_parent { my $candidate = shift; for my $inner ( sort keys %adjacent ) { for my $outer ( sort keys %{$adjacent{$inner}} ) { if ( $candidate eq $outer ) { return $inner; } } } return 0; } __DATA__ Gene1,Gene2,spc1,spc2 Gene3,Gene1,spc1,spc2,spc4 Gene4,Gene1,spc1,spc2,spc5,spc3,spc1 Gene2,Gene3,spc1,spc2 Gene2,Gene4,spc2,spc3 Gene3,Gene4,spc1,spc2 GeneA,GeneB,spc4,spc5 GeneB,GeneC,spc1,spc2 GeneC,GeneD,spc1,spc2 GeneD,GeneE,spc4,spc2 GeneE,GeneF,spc3,spc1 GeneX,GeneY,spc6,spc8 GeneX,GeneP,spc6,spc7
And the results are:

C:\Code>perl adjacent.pl *----------------------------------------------* Gene1 , Gene2, Gene3, Gene4 GeneA , GeneB, GeneC, GeneD, GeneE, GeneF GeneX , GeneP, GeneY
Update: newer code solves it completely.

#!/usr/bin/perl use warnings; use strict; # # %adjacent - each key contains a cluster identified by the key (root +). # %sequences - contains sequences of "spcs" for each cluster. # my %adjacent; my %sequences; while (<DATA>) { chomp; my @component = split /\,/, $_, 3; my $parent; $component[2] =~ s/\s+$//; if ( $parent = has_root($component[0]) ) { push @{$adjacent{$parent}}, $component[1] unless ( has_root($component[1])); push @{$sequences{$parent}}, $component[2]; } elsif ( $parent = has_root($component[1]) ) { push @{$adjacent{$parent}}, $component[0] unless ( has_root($component[0])); push @{$sequences{$parent}}, $component[2]; } else { push @{$adjacent{$component[0]}}, $component[1]; push @{$sequences{$component[0]}}, $component[2]; } } print "*----------------------------------------------*\n"; for my $inner ( sort keys %adjacent ) { print "$inner"; for my $outer ( sort @{$adjacent{$inner}} ) { print ",$outer"; } for (@{$sequences{$inner}}) { print ",$_"; } print "\n"; } sub has_root { my $candidate = shift; return 0 if ( keys %adjacent < 1); for my $inner ( sort keys %adjacent ) { if ( $candidate eq $inner ) { return $inner; } for my $outer ( sort @{$adjacent{$inner}} ) { if ( $candidate eq $outer ) { return $inner; } } } return 0; } __DATA__ Gene1,Gene2,spc1,spc2 Gene3,Gene1,spc1,spc2,spc4 Gene4,Gene1,spc1,spc2,spc5,spc3,spc1 Gene2,Gene3,spc1,spc2 Gene2,Gene4,spc2,spc3 Gene3,Gene4,spc1,spc2 GeneA,GeneB,spc4,spc5 GeneB,GeneC,spc1,spc2 GeneC,GeneD,spc1,spc2 GeneD,GeneE,spc4,spc2 GeneE,GeneF,spc3,spc1 GeneX,GeneY,spc6,spc8 GeneX,GeneP,spc6,spc7

In reply to Re: Clustering with Perl by dwm042
in thread Clustering with Perl by nerve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.