Perl::Script to find repeats

From Biocourse

Jump to: navigation, search

# =============================================================
#
# Author:  Areum Han (arhan@ucla.edu)
#
# File Description:
#   Script to find repeats
#
# ============================================================
#assume no insertion and deletions

$text = "VEALEKKVEALESKVEALEKKVEALEHG";
%hash = ();
$key = "";
$value = 0;

#build 2d similarity matrix with the text, similarity = 0 or 1
for($i=0;$i<length($text);$i++){
             for($j=$i+1;$j<length($text);$j++){

     $key = "";
     $value = 0;

     $key = $i."-".$j;
     if(substr($text, $i, 1) eq substr($text, $j, 1)){
      $value = 1;
     }
     else{
      $value = 0;
     }
     $hash{$key} = $value;
    }
}

#tracing back : find repeats
for($i=0;$i<length($text);$i++){

      for($j=$i+1;$j<length($text);$j++){

     $currentKey = $i."-".$j;
     $currentValue = $hash{$currentKey};
     $currenti = $i;
     $currentj = $j;

     if ($currentValue==1 and &getPreValue($currenti, $currentj)==1) {

      $from = $currentKey;
      $repeat = substr($text, $currenti, 1);
      $currenti--;
      $currentj--;

      while(&getPreValue($currenti, $currentj)==1){
       $repeat =  $repeat.substr($text, $currenti, 1);
       $currenti--;
       $currentj--;
      }

      $to = $currenti."-".$currentj;
      $repeat = $repeat.substr($text, $currenti, 1);
      $repeat = reverse($repeat);
      print $repeat." was repeated : ".$from." and ".$to."\n";
     }
    }
}

sub getPreValue{

  $x = $_[0]-1;
  $y = $_[1]-1;

  $preKey = $x."-".$y;

  if($x <0){
   $preValue = -1;
  }else{
   $preValue = $hash{$preKey};
  }
  return $preValue;
}