#This program counts the number of different bases in a given DNA sequence.
#Written by Ilkka Kronholm, March 2007
$seq = "atcgtgactgatgctgatcgatgatagtcatcga"; #Sequence of interest
#Initial values for some variables
$no_of_a = 0;
$no_of_c = 0;
$no_of_t = 0;
$no_of_g = 0;
$position = 0;
$length_of_seq = length( $seq );
print "The sequence is $length_of_seq base pairs long\n";
print "\n";
#Next for-loop counts the number of bases. Each base has its own if-statement
for( $position; $position < $length_of_seq; $position++ )
{
if (substr( $seq, $position, 1 ) eq "a" )
{
$no_of_a++;
}
if (substr( $seq, $position, 1 ) eq "c" )
{
$no_of_c++;
}
if (substr( $seq, $position, 1 ) eq "t" )
{
$no_of_t++;
}
if (substr( $seq, $position, 1 ) eq "g" )
{
$no_of_g++;
}
}
# Previous could have also been done with a while-loop. Alternative solution commented out.
#while ( $position < $length_of_seq )
#{
# if (substr( $seq, $position , 1 ) eq "a" )
# {
# $no_of_a++;
# }
# $position++;
#}
print "Number of A's is $no_of_a \n";
print "Number of T's is $no_of_t \n";
print "Number of C's is $no_of_c \n";
print "Number of G's is $no_of_g \n";
print "\n";
#We also want to count how many times some dimers and trimers appear in a given sequence
$position = 0; #Variable position is set again to zero.
#Here initial values for different dimers and trimers are set.
#Dimers
$no_of_at = 0;
$no_of_ct = 0;
$no_of_ag = 0;
$no_of_cc = 0;
#Trimers
$no_of_gat = 0;
$no_of_tga = 0;
$no_of_cat = 0;
for( $position; $position < $length_of_seq; $position++ )
{
if( substr( $seq, $position, 2 ) eq "at" )
{
$no_of_at++;
}
if( substr( $seq, $position, 2 ) eq "ct" )
{
$no_of_ct++;
}
if( substr( $seq, $position, 2 ) eq "ag" )
{
$no_of_ag++;
}
if( substr( $seq, $position, 2 ) eq "cc" )
{
$no_of_cc++;
}
}
$position = 0; #Again position must be reset to begin counting from the beginning.
for( $position; $position < $length_of_seq; $position++ )
{
if( substr( $seq, $position, 3 ) eq "gat" )
{
$no_of_gat++;
}
if( substr( $seq, $position, 3 ) eq "tga" )
{
$no_of_tga++;
}
if( substr( $seq, $position, 3 ) eq "cat" )
{
$no_of_cat++;
}
}
print "Dimer 'at' occurs $no_of_at times in the given sequence\n";
print "Dimer 'ct' $no_of_ct times\n";
print "Dimer 'ag' $no_of_ag times\n";
print "Dimer 'cc' $no_of_cc times\n";
print "\n";
print "Trimer 'gat' occurs $no_of_gat times in the given sequence\n";
print "Trimer 'tga' $no_of_tga times\n";
print "Trimer 'cat' $no_of_cat times\n";