KSH HW2 Translation of DNA sequences into Amino acids

From Biolecture.org

I tried to make a code to translate DNA sequences into amino acids using Perl program.

Foreach loop1)

First, I learned about Perl foreach loop.

Perl foreach loop iterates over a list value and sets the control variable to be each element of the list in turn

The basic form is like this

foreach var (list) {

 

}

Example

<span style="color:rgb(136,0,0)">#!/usr/local/bin/perl</span>
 
<span style="color:rgb(0,102,102)">@list</span> <span style="color:rgb(102,102,0)">=</span> <span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,102,102)">2</span><span style="color:rgb(102,102,0)">,</span> <span style="color:rgb(0,102,102)">20</span><span style="color:rgb(102,102,0)">,</span> <span style="color:rgb(0,102,102)">30</span><span style="color:rgb(102,102,0)">,</span> <span style="color:rgb(0,102,102)">40</span><span style="color:rgb(102,102,0)">,</span> <span style="color:rgb(0,102,102)">50</span><span style="color:rgb(102,102,0)">);</span>

<span style="color:rgb(136,0,0)"># foreach loop execution</span>
<span style="color:rgb(0,0,136)">foreach</span> $a <span style="color:rgb(102,102,0)">(</span><span style="color:rgb(0,102,102)">@list</span><span style="color:rgb(102,102,0)">){</span>
    <span style="color:rgb(0,0,136)">print</span> <span style="color:rgb(0,136,0)">"value of a: $a\n"</span><span style="color:rgb(102,102,0)">;</span>
<span style="color:rgb(102,102,0)">}</span>

The result is

value of a: 2
value of a: 20
value of a: 30
value of a: 40
value of a: 50

Perl Script to convert DNA to Protein

To make a new object and save to a file, write down

1. use Bio::Seq;

   use Bio::SeqIO;

When we make a new object, we always have to use this method (method is a function)

2. new()

3. -> : Method call sign

We have to use these arguments to read codon and save it (Argument is a variable to use function or command)

4. -seq, -alphabet, -file, -format

5. => : To assign argument value
 

6. -alphabet argument has one value among dna, rna, and protein.

   -format argument has 'fasta' or 'genbank' value

7. Translation method:

   $argument name = $object name->translate;

8. Print method:

   Print $argument name ->seq, "\n";

Example2)

use Bio::Seq;

use Bio::SeqIO;

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

   foreach $first(@bases){

     foreach $second(@bases){

          foreach $third(@bases){

               $codon = $first.$second.$third;

               $seq_codon = Bio::Seq -> new(-seq => $codon, -alphabet => 'dna');

               $aa_letter = $seq_codon -> translate;

               print "$codon \t", $aa_letter->seq, "\t";

         }

       print "\n";

   }

}

$seqio_aa = Bio::SeqIO->new(-file => 'aa.fas', -format => 'fasta');

$seqio_aa = write_seq($aa_letter);

exit;

Reference

1) http://www.tutorialspoint.com/perl/perl_foreach_loop.htm

2) This code was originally written by Myeong Seo Yoon and I made a little modification