next up previous
Next: Flow control Up: perl1st Previous: First encouter

Subsections

Quick elements of perl programming

Character String

Exercises:

Let's see the differences between double- and single-quotes.

  1. Make a small perl scripts which prints out the following strings. Make sure to print "\n" so that one string per line is printed.

    "Pick'n Grin", 'Pick\'n Grin', "ATGC\n", 'ATGC\n', '"\t"\n', "\"\t\"\n", '"'

  2. How do you make a text string containing backslash (\)? Modify the script to print this out.

Note that single quotes are more literal, and it does not ``interprete'' many special or escape characters. Inside of single quotes, only two special characters can be used. What are these two?

Variable assignment

Example of printing character strings

Concatenate two DNA sequences:

#!/usr/bin/perl -w
  
$seqA = "ATATA";
$seqB = "GCGCG";

# concatenating 2 seqs
print "1st method:\t", $seqA, $seqB, "\n";

$joinedSeq = "$seqA$seqB";
print "2nd method:\t$joinedSeq\n";

$joinedSeq = $seqA . $seqB;   # using concatenate operator (.)
print "3rd method:\t", $joinedSeq,"\n";

exit;

Basic operators

+, -, *, / Arithmetic operators
$a % $b Modulus
$a ** $b Raises $a to the power of $b
$a . $b Concatenates two strings.
(some operation) The operation in the parentheses get executed first
   
$a = $b Assignment
$a += $b same as $a = $a + $b.
-=, *=, /=, .= Similar to above
$a++, $a-- Incremeny, decrement by one.
"abc" x 4 String multiplication (results in "abcabcabcabc")

Exercises:

Make the following perl script. Pay attention to what the opertors are doing to the variables (containers).

  $a = 16;
  print "$a\n";

  $a += 4;
  print "Added 4: $a\n";

  $b = $a;
  $mod = $b % 4;
  print "$b mod 4 = $mod\n";

  $a--;
  print "decremented: $a\n";

  $codon = "ATG";
  $codon .= " CCC"; 
  print "$codon\n";

Perl variable types

Perl has three main variable types:

Scalars

  $popSize = 1000;      # integer
  $probability = 0.9;   # floating point
  $smallVal = 2.11E-2;  # floating point with engineering notation
  $name = "Bob";        # character string

Arrays

Hashes

Similar to arrays because a hash can contain multiple data (values).

Different from array because hash values are looked up (indexed) by names (keys), instead of numerical indecies in arrays.

Extremely useful, and central to Perl programming.

Hash Exercise/Example

Type in the following program, and think what it is doing.

#!/usr/bin/perl -w

my %recognitionSite = (
  "EcoRI" => "G^AATTC",
  "HindIII" => "GG^CC"
);

$recognitionSite{"AatII"} = "GACGI^C";  # adding more key/value combos to
$recognitionSite{"XbaI"} = "T^CTAGA";   # the hash

my @knownEnzymes = keys(%recognitionSite);
print "Known enzymes: ", join(",", @knownEnzymes), "\n";

for( ; ; ) {
  print STDERR "Restriction Enzyme?: ";
  my $enzyme =  <STDIN>;
  chomp ($enzyme);  # remove the newline;

  print "Recognition Site = $recognitionSite{$enzyme}\n\n";
}

exit;

Note: join function: join($glue, @list) takes a list of values, and ``glue'' them with $glue string and return a scalar (string).

Variable Scopes

Homework

Create a directory with your first name inside of /scratch/compbio/hw. Then create a directory called hash in there (e.g. /scratch/compbin/hw/naoki/hash for me). After completing the following homework, please copy your 3 scripts into this directory.

  1. Make an array with 4 elements: ("AAA","AAT","AAG", "AAC"). Then print out the last element.

  2. Make a hash, whose keys are the name of species and values are their DNA sequences. You can use the following fake sequence data:
    species DNA sequence
    camel ATGCCTATTGGA
    llama ATGACTACCGGC
    owl ATGAAGAGGGTC
    Then print out the sequence of llama.
    Then concatenate the three sequences (store it in a new scalar variable), and print out the concatenated sequence.

  3. Can you make a program which will ask for 3 nucleotides, and print out the amino acid corresponding to the codon? You can basically modify the example program (restriction site) used in the hashes section.


next up previous
Next: Flow control Up: perl1st Previous: First encouter
Naoki Takebayashi 2011-10-06