Like this? (Just a few minor tweaks :)
BTW, it was sorting, just not numerically.
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1198131
use strict;
use warnings;
my %one;
while(<DATA>)
{
my ($one, $two, $three, $four) = split;
/(\d+)/; # get number for later sort
push @{ $one{$1} }, [ $two, $three, $four, $_ ];
}
for my $one (sort { $a <=> $b } keys %one)
{
my %groups;
LOOP:
for ( sort {
$a->[1] cmp $b->[1] or
$b->[2] <=> $a->[2] or
$a->[0] <=> $b->[0] }
@{ $one{$one} } )
{
my ( $two, $three, $four, $all ) = @$_;
if( keys %groups )
{
for ( sort keys %groups )
{
if( ( $_ ^ "$three" ) =~ tr/\0//c <= 2 )
{
push @{ $groups{$_} }, [ $two, $three, $four, $all ];
next LOOP;
}
}
push @{ $groups{$three} }, [ $two, $three, $four, $all ];
}
else
{
push @{ $groups{$three} }, [ $two, $three, $four, $all ];
}
}
for ( sort keys %groups )
{
print "CLUSTER\n";
print $_->[3] for values @{ $groups{$_} };
}
}
__DATA__
chrM:307 0 TCAGGGTG 115
chrM:307 0 TCAGGGTG 107
chrM:307 0 TCAGGGTG 115
chrM:307 0 TCAGGGTG 130
chrM:307 0 TCAGGGTG 114
chrM:307 1 TCAGGGTG 106
chrM:310 0 TCAGGGTG 99
chrM:392 2 CCTCTTAT 130
chrM:396 2 AGTTACTA 129
chrM:443 0 ATTATCAA 130 extra columns
chrM:542 2 AATCCAAA 129
chrM:542 0 AATCCAAA 129
chrM:934 0 CATTCGCT 129
chrM:934 1 CATTCGCT 129
chrM:1001 0 CGTGACAT 129
chrM:1127 0 GATACTAA 130
chrM:1257 1 TGGAAATC 129
chrM:1262 0 CGGGAAGC 129
chrM:1262 0 AGGGAAGG 129
chrM:1603 0 GTATCGGA 130
chrM:1603 1 GTATCGGA 130
Outputs:
CLUSTER
chrM:307 0 TCAGGGTG 130
chrM:307 0 TCAGGGTG 115
chrM:307 0 TCAGGGTG 115
chrM:307 0 TCAGGGTG 114
chrM:307 0 TCAGGGTG 107
chrM:307 1 TCAGGGTG 106
CLUSTER
chrM:310 0 TCAGGGTG 99
CLUSTER
chrM:392 2 CCTCTTAT 130
CLUSTER
chrM:396 2 AGTTACTA 129
CLUSTER
chrM:443 0 ATTATCAA 130 extra columns
CLUSTER
chrM:542 0 AATCCAAA 129
chrM:542 2 AATCCAAA 129
CLUSTER
chrM:934 0 CATTCGCT 129
chrM:934 1 CATTCGCT 129
CLUSTER
chrM:1001 0 CGTGACAT 129
CLUSTER
chrM:1127 0 GATACTAA 130
CLUSTER
chrM:1257 1 TGGAAATC 129
CLUSTER
chrM:1262 0 AGGGAAGG 129
chrM:1262 0 CGGGAAGC 129
CLUSTER
chrM:1603 0 GTATCGGA 130
chrM:1603 1 GTATCGGA 130
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.