hw6 solution

The problem was:

$seq = ‘GAGTATCTGGAAGACAGGCAGACTTTTCGCCACAGCGTGGTGGTACCTTATGAGCCACCCGAGGCCGGCT’;

use a loop and substr to print its “codons”, one per line.

Lets start considering that our task is to “split” the sequence in pieces of three chars.

The solution is available here at codepad, and below:

$seq = "GAGtatCTGGAAGACAGGCAGACTTTTCGCCACAGCGTGGTGGTACCTTATGAGCCACCCGAGGCCGGCT";
 
# Per fare il ciclo devo sapere quanto lunga è la stringa
$lunghSeq = length($seq);
 
# Adesso faccio un ciclo con una variabile "posizione"
# che faccio aumentare di tre (anziche il consueto +1)
 
for ($pos = 0; $pos < $lunghSeq; $pos+=3) {
   $codone = substr($seq, $pos, 3);
   print "$codone ";
}

You could also achieve the same with a while loop. Basically when we reach the end of the sequence the “substr” string will nothing, that is “FALSE”. Look in codepad or below:

$seq = "GAGtatCTGGAAGACAGGCAGACTTTTCGCCACAGCGTGGTGGTACCTTATGAGCCACCCGAGGCCGGCT";

$pos = 0;

while (substr($seq, $pos, 3)) {
    $codone =  substr($seq, $pos, 3);
    print "$codone ";
    $pos = $pos + 3;
}

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *