Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^5: Find duplicate based on specific fields while allowing 2 mismatch

by tybalt89 (Monsignor)
on Aug 29, 2017 at 01:52 UTC ( [id://1198230]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Find duplicate based on specific fields while allowing 2 mismatch
in thread Find duplicate based on specific fields while allowing 2 mismatch

Next time be explicit that you wanted it sorted by the number AFTER the colon.

#!/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 chr1:1257 1 TGGAAATC 129 chr1:1262 0 CGGGAAGC 129 chr1:1262 0 AGGGAAGG 129 chr1:1603 0 GTATCGGA 130 chr1:1603 1 GTATCGGA 130
  • Comment on Re^5: Find duplicate based on specific fields while allowing 2 mismatch
  • Download Code

Replies are listed 'Best First'.
Re^6: Find duplicate based on specific fields while allowing 2 mismatch
by amitgsir (Novice) on Aug 29, 2017 at 02:05 UTC

    Thanks! This version make the ourput in perfect way!

    I will try to make comment/requirement more specific while making an question

    Thanks for your effort! I need to learn more about indexing and hash in perl. This post will be a good starting point.

    Amit

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198230]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found