String Processing
Introduction
In this lesson the student will learn how to:
- Use a variety of loop constructs
- Compare the various loop constructs
- Create and use embedded loops
By the end of this lesson the student will be able to:
Write a script utilizing embedded loops which
prints out all 64 possible codons.
Anticodons
The physical mechanism responsible for translating an mRNA into an amino acid
sequence is very interesting. We will go over a very simplified over view
here. A structure called a ribosome is where an mRNA strand is used as
a template for construction of the amino acid sequence. Another kind of RNA
called tRNA (t for transfer) actually contains a complementary RNA sequence
called the anticodon which matches to the next three mRNAs encountered by
the ribosome. There are different tRNAs for each amino acid, but not
necessarily one for each possible codon. This is because of a phenomenon
known as wobble in which an anticodon only needs to match the first two
amino acids in a codon in order to recognize the codon and add the
corresponding amino acid to the growing protein sequence. You will notice
that certain amino acids possess a set of codons which all begin with the
same first two letters. For these amino acids wobble works, but this is not
the case for all amino acids and codon combinations. In fact, there are
actually 56 tRNAs.
Here is a small collection of loops:
#!/usr/bin/perl
print "FOREACH LOOP: ";
foreach $n (1..9){
print "$n ";
}
print "\n";
$c = 1;
print "UNTIL LOOP: ";
until($c==10){
print "$c ";
$c++;
}
print "\n";
$a = 1;
print "WHILE LOOP: ";
while($a<10){
print "$a ";
$a++;
}
print "\n";
$b = 1;
print "DO-WHILE LOOP: ";
do{
print "$b ";
$b++;
}while($b<10);
print "\n";
print "FOR LOOP: ";
for($x = 1; $x<10; $x++){
print "$x ";
}
print "\n";
The while and until constructs can come after a line of executable code.
#!/usr/bin/perl
$a = 0;
print "WHILE: ";
print "$a " while($a++<10);
print "\nUNTIL: ";
print "$a " until($a--<2);
Foreach loops can also be used to iterate through all the items stored in an
array.
#!/usr/bin/perl
@x = ( "bunny", 2, "Fred", "c", "cabbage", 10, "doggy", 555 );
foreach $i (@x){
print "$i\n"
}
Embedded loops: Loops can be placed within loops. Following the execution of
such loops can sometimes be a little difficult.
#!/usr/bin/perl
print "EMBEDDED WHILE LOOPS:\n";
$a = 1;
while($a<5){
$b=$a;
while($b>0){
print "$a $b . ";
$b--;
}
$a++;
}
print "\n";
print "TRIPLE EMBEDDED FOR LOOP:\n";
for($x=3; $x>0; $x--){
for($y=93; $y>90; $y--){
for($z=0; $z<3; $z++){
print "$x $y $z . ";
}
}
}
print "\n";
ASSIGNMENT:
Write a script which generates all 64 possible codons. Create an array which
contains the basic nucleotides (A, G, T, C) and then create a triply embedded
loop which generates each of the possible combinations of three nucleotides.
The key line should look something like this:
print "$nuc[$a]$nuc[$b]$nuc[$c]"
Your final output should look more or less like this:
AAA AAC AAG AAT
ACA ACC ACG ACT
AGA AGC AGG AGT
ATA ATC ATG ATT
CAA CAC CAG CAT
CCA CCC CCG CCT
CGA CGC CGG CGT
CTA CTC CTG CTT
GAA GAC GAG GAT
GCA GCC GCG GCT
GGA GGC GGG GGT
GTA GTC GTG GTT
TAA TAC TAG TAT
TCA TCC TCG TCT
TGA TGC TGG TGT
TTA TTC TTG TTT
Actually, all the T's in this list should be U's since codons are more of an
RNA sort of thing than a DNA kind of thing.