N-mer profiling perl script
From Biocourse
N-mer profiling is basic but useful tool for bioinformatics.
Below perl script is a simple source which shows the n-mer counts in your seqeunce with an user-defined length(N).
Save it with name.pl and run the script with 'perl name.pl' in the prompt.
# =========================================================================================
#
# File Description:
# Script to make profiles for n-mer
#
# ========================================================================================
# Author: Areum Han (arhan@ucla.edu)
#
##############################################################
# Main
##############################################################
$inputseqeunce = "UUUUUUUAaaaaaaaaaaaaaaaaaaatttttttttttttttccccccccccc";
$N = 6;
&generateNmerProfile();
##############################################################
# GenerateNmerProfile
##############################################################
sub generateNmerProfile()
{
$inputseqeunce = "UUUUUUUAaaaaaaaaaaaaaaaaaaatttttttttttttttccccccccccc";
##############################################################
# ClearInputSequences
##############################################################
$inputseqeunce =~ tr/[A-Z]/[a-z]/;
$inputseqeunce =~ tr/[u]/[t]/;
#print $inputseqeunce;
##############################################################
# MakeMatrixOfNmer
##############################################################
for($i= 0; $i<$N;$i++){
$start = $start.'0';
}
#print $start;
$end = $start;
$end =~ tr/[0]/[3]/;
#print $end;
my %hash = ();
$count = 0;
for($key = $start;$key<=$end;$key++){
if(&hasNumberGreaterThanThree($key)eq("0")){
if(length($key)!=$N){
$key = &attachZero($key, $N-length($key));
}
$key1 = &changeToDNA($key);
$count = $count +1;
#print $key1."\n";
$hash{ $key1 } = 0;
}
}
#print $count;
##############################################################
# CountNmer
##############################################################
for($j=0;$j<=length($inputseqeunce)-$N;$j++){
$nmer = substr($inputseqeunce, $j, $N);
$value = $hash{$nmer};
$hash{$nmer} = $value + 1;
}
# print $hash{tttttt};
##############################################################
# OutputResult
##############################################################
for my $key ( keys %hash ) {
my $value = $hash{$key};
print "$key\t$value\n";
}
}
sub hasNumberGreaterThanThree(){
my $number = shift(@_);
if($number =~ m/[4-9]/){return "1";}
else{return "0";}
}
sub attachZero(){
my $string = shift(@_);
my $number = shift(@_);
for($i=0;$i<$number;$i++){ $string = "0".$string;}
return $string;
}
sub changeToDNA(){
my $string = shift(@_);
$string =~ tr/[0]/[a]/;
$string =~ tr/[1]/[t]/;
$string =~ tr/[2]/[g]/;
$string =~ tr/[3]/[c]/;
return $string;
}
