1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#!/usr/bin/env perl
use strict;
use warnings;
my $usage = "\n\nusage: $0 weldmer\n\n";
my $weldmer = $ARGV[0] or die $usage;
main: {
if (-f $weldmer) {
open (my $fh, $weldmer) or die $!;
while (<$fh>) {
my $weldmer = $_;
chomp $weldmer;
&check_weldmer($weldmer);
}
close $fh;
}
else {
&check_weldmer($weldmer);
}
exit(0);
}
####
sub check_weldmer {
my ($weldmer) = @_;
my $weldmer_len = length($weldmer);
my $left_substr = substr($weldmer, 0, int($weldmer_len/2));
my $right_substr = substr($weldmer, int($weldmer_len/2));
# &check_per_id($weldmer);
&check_per_id($left_substr);
&check_per_id($right_substr);
return;
}
####
sub check_per_id {
my ($weldmer) = @_;
my $weldmer_len = length($weldmer);
my $half_len = int($weldmer_len/2);
#print "Half_len of " . length($weldmer_len) . " = $half_len\n";
my @chars = split(//, $weldmer);
my $max_ratio = 0;
my $best_left = "";
my $best_right = "";
for (my $i = 0; $i < $half_len; $i++) {
for (my $j = $i + 1; $j <= $half_len; $j++) {
my $ref_pos = $i;
my $other_pos = $j;
my $count_same = 0;
my $count_chars = 0;
my $left_sub = "";
my $right_sub = "";
while ($other_pos <= $j + $half_len - 1) {
$count_chars++;
if ($chars[$ref_pos] eq $chars[$other_pos]) {
$count_same++;
}
$left_sub .= $chars[$ref_pos];
$right_sub .= $chars[$other_pos];
$ref_pos++;
$other_pos++;
}
my $ratio = $count_same/$count_chars;
if ($ratio > $max_ratio) {
$max_ratio = $ratio;
$best_left = $left_sub;
$best_right = $right_sub;
}
}
}
$max_ratio = sprintf("%.3f", $max_ratio);
print "$weldmer\t$best_left\t$best_right\t$max_ratio\n";
return;
}
|