#Julia Reimer 16.3.2007
#This is the exercise I have done in the fouth part of PERL courses.
#Today we try to count bases in a given sequence.

#Here comes my given Sequence.

$seq = "atcgtgactcgcatgatcgcggatcg";

#Here comes a variable to count different bases.

$noa = 0;
$not = 0;
$noc = 0;
$nog = 0;

#Here we define the length of the sequence.

$lseq = length($seq);

#We need a running variable

$position = 0;

#I will have an array with the four bases to play a bit.

@base = ("a", "t", "c", "g");

#let«s start

print "The sequence has $lseq bases. \n";

while ($position < $lseq)
	{
	if (substr($seq, $position, 1) eq "a")
		{
		$noa++;
		}
	if (substr($seq, $position, 1) eq "t")
		{
		$not++;
		}
	if (substr($seq, $position, 1) eq "g")
		{
		$nog++;
		}
	if (substr($seq, $position, 1) eq "c")
		{
		$noc++;
		}
	$position++;
	}

print "The number of A within the sequence is $noa. \n";
print "The number of T within the sequence is $not. \n";
print "The number of C within the sequence is $noc. \n";
print "The number of G within the sequence is $nog. \n";

#I want to know, how many dimers starting with a specific base exisist. I will count every possible dimer in an open reading frame.
#We need therefore some counting variables for dimers.

$noda = 0;
$nodt = 0;
$nodc = 0;
$nodg = 0;

#The number of the variable $position has to be set again to 0.

$position = 0;

#I will use a for-loop to start at different positions in the given sequence.
#You can also use the substr($seq, $position, 2), when you will have a given dimer sequence.

for ($m = 0; $m < 2; $m++)
	{
	while ($position < $lseq)
		{
		if ( $lseq - $position > 1)
			{
			if (substr($seq, $position, 1) eq "a")
				{
				$noda++;
				}
			if (substr($seq, $position, 1) eq "t")
				{
				$nodt++;
				}
			if (substr($seq, $position, 1) eq "g")
				{
				$nodg++;
				}
			if (substr($seq, $position, 1) eq "c")
				{
				$nodc++;
				}
			}
		$position = $position + 2;
		}
	$position = $m + 1;	
	}

print "There are $noda dimers beginning with A. \n";
print "There are $nodt dimers beginning with T. \n";
print "There are $nodc dimers beginning with C. \n";
print "There are $nodg dimers beginning with G. \n";

#I want to know, how many trimers starting with a specific base exisist. I will count every possible trimer in an open reading frame.
#We need some counting variables for trimers.

$nota = 0;
$nott = 0;
$notc = 0;
$notg = 0;

#The number of the variable $position has to be set again to 0.

$position = 0;

#I will use a for-loop to start at different positions in the given sequence.
#You can also use the substr($seq, $position, 3), when you will have a given trimer sequence.

for ($m = 0; $m < 3; $m++)
	{
	while ($position < $lseq)
		{
		if ( $lseq - $position > 2)
			{
			if (substr($seq, $position, 1) eq "a")
				{
				$nota++;
				}
			if (substr($seq, $position, 1) eq "t")
				{
				$nott++;
				}
			if (substr($seq, $position, 1) eq "g")
				{
				$notg++;
				}
			if (substr($seq, $position, 1) eq "c")
				{
				$notc++;
				}
			}
		$position = $position + 3;
		}
	$position = $m + 1;	
	}

print "There are $nota trimers beginning with A. \n";
print "There are $nott trimers beginning with T. \n";
print "There are $notc trimers beginning with C. \n";
print "There are $notg trimers beginning with G. \n";
