#!/usr/bin/perl -w

# Programme comptant toutes les occurences d'un motif exact
# dans une séquence lue dans un fichier au format FASTA
# Le programme utilise la fonction substr


# declare all variables
my ($seq_file, $seq_name, $sequence);
my ($line);
my ($motif, $count);

# file name
$seq_file = "p53_seq.txt";

# open file for reading
open (SEQ, $seq_file) || die "cannot open \"$seq_file\": $!";

# read sequence name and description from first line
$seq_name = <SEQ>;
chomp ($seq_name);

# initialize a variable to contain the entire sequence
$sequence = "";

# read sequence lines from file
while ($line = <SEQ>) {
   chomp ($line);
   $sequence .= $line;     #add line to $sequence
}

# close file
close (SEQ);


# motif exact
$motif = "AT";  # mettre ce qu'on veut

# traitement séquence
$count = 0;
foreach $i (0..length($sequence)-1-length($motif)) {
    if ( $motif eq substr($sequence, $i, length($motif))) {
	$count++;
    }
}

# affichage des résultats    
print "$motif: $count\n";

