22
3.1 Ifs and Loops

3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

3.1

Ifs and Loops

3.2Revision: variables

Scalar variables can store scalar values:

Variable declaration my ($priority);

Numerical assignment $priority = 1;

String assignment $priority = 'high';

3.3Revision: arrays

Array List value variable @a = (1,2,3,"hi");

Array element: print $a[1];

2

$a[0] = "*";

Array size: print scalar(@b);

4

Reading a list of lines: @a = <STDIN>;

3.4Revision: arrays

Push & pop

push(@a,6);

print @a;

123hi6

my $x = pop(@a);

print @a;

123hi

print $x;

6

Unshift & shift

unshift(@a,0);print @a;

0123hi

my $x = shift(@a);

print @a;

123hi

print $x;

0

@a=(1,2,3,"hi");

012 3

123"hi"

3.5Revision: arrays

$str = "So-long-and-thanks-for-all-the-fish";

@a = split("-", $str);

$str = join("!! ", @a );

print "$str\n";

So!! Long!! and!! thanks!! for!! all!! the!! fish

reverse(1,2,3); sort("b","a","d","c",);

3.6

Controls:Ifs and Loops

3.7Controls: if ?

Controls allow non-sequential execution of commands, and responding to

different conditions.

else {

print "Are you doing anything tomorrow night?\n";

}

print "How old are you?\n";

my $age = <STDIN>;

if ($age < 18) {

print "Sorry, I’m not allowed to chat with minors\n";

}

3.8if, elsif, else

It’s convenient to test several conditions in one if structure:

if ($age < 18) {

print "Sorry, I’m not allowed to chat with minors\n";

} elsif ($age < 25) {

print "Are you doing anything tomorrow night?\n";

} elsif ($age < 35) {

print "Are you married?\n";

} else {

print "Do you need help crossing the street?\n";

}

3.9Comparison operators

ComparisonNumericString

Equal==eq

Not equal!=ne

Less than<lt

Greater than>gt

Less than or equal to

<=le

Greater than or equal to

>=ge

if ($age == 18){

...

}

if ($name eq "Yossi")...

if ($name ne "Yossi")...

if ($name lt "n")...

ComparisonNumeric

Equal==

Not equal!=

Less than<

Greater than>

Less than or equal to

<=

Greater than or equal to

>=

Found = in conditional, should be == at

3.10Boolean operators

if (($age==18) || ($name eq "Yossi")){

...

}

if (($age==18) && ($name eq "Yossi")){

...

}

if (!($name eq "Yossi")){

...

}

And &&

Or ||

Not !

3.11Controls: Loops

Commands inside a loop are executed repeatedly (iteratively):

while ($name ne "Yossi") {

chomp($name = <STDIN>);

print "Hello $name!\n";

}

@names = <STDIN>;

foreach $name (@names) {

print "Hello $name!\n";

}

* There are also until, do-while and do-until loops

3.12Loops

$i=0;while ($i<scalar(@names)) { $name = $names[$i]; print "Hello $name!\n"; $i++;}

for ($i=0; $i<scalar(@names); $i++) {

$name = $names[$i];

print "Hello $name!\n";

}

A for loop is controlled by three statements:

• 1st is executed before the first iteration

• 2nd is the stop condition

• 3rd is executed before every re-iteration

These are equivalent

foreach $name (@names) {

print "Hello $name!\n"

}

3.13Breaking out of loops

next – skip to the next iteration last – skip out of the loop

my @lines = <STDIN>;

foreach $line (@lines) {

if (substr($line,0,1) ne ">") { next; }

print(substr($line,1));

if (substr($line,0,4) eq ">ehd") { last; }

}

3.14Breaking out of loops

die – end the program and print an error message to the standard error <STDERR>

if ($score < 0) { die "score must be positive"; }

score must be positive at test.pl line 8.

Note: if you end the string with a "\n" then only your message will be printed

* warn does the same thing as die without ending the program

3.15

“The Programming Process”

3.16The programming process

It pays to plan ahead before writing a computer program:

1. Define the purpose of the program

2. Identify the required inputs

3. Decide how to present the outputs

4. Make an overall design of the program

5. Refine the design, specify more details

6. Write the code – one stage at a time and test each stage

7. Debug…

3.17An example: SAGE libraries

1. Double-stranded cDNA is generated from cell extracts

2. The cDNA is cleaved with a restriction enzyme (NlaIII)

3. The most 3'-end of the cDNA is then collected by their poly-A

4. The fragments are ligated to linkers containing a recognition site for a type IIS restriction enzyme and a PCR primer site

5. This restriction enzyme cuts 20bp away from its recognition site

6. Ligation, PCR, cleavage, concatenation, cloning, sequencing… A 10bp tag sequence from each mRNA

7. 10bp sequences are searched in an mRNA database and the corresponding genes are identified

SAGE (Serial Analysis of Gene Expression) is used to identify all transcripts that are expressed in a tissue:

(1)

(2&3)

(4&5)

3.18

An example: SAGE

libraries

SAGE (Serial Analysis of

Gene Expression) is used to

identify all transcripts that

are expressed in a tissue:

3.19Predicting the SAGE tag of an mRNA

It would be useful to know what tag to expect for each mRNA in the database.

So lets write a script:

1. Purpose: To predict the 10bp sequence of the SAGE tag of a given mRNA

2. Inputs: A list of mRNA sequences in FASTA format

>gi|24646380|ref|NM_079608.2| Mus musculus EH-domain containing 4 (EHD4), mRNA GTGGTATTTCTTCGTTGTCTCTGGCGTGGTCACGTTGATTGGTCCGCTATCTGGACCGAAAAAAGTCGTA......GTCGACGGCGATGGGTTCCTGGACTCTGACGAGTTCGCGCTGGCCTTGCACTTAATCAACGTCAAGCTGGAAGGCTGCGAGCTGCCCACCGTGCTGCCGGAGCACTTAGTACCGCCGTCGAAGCGCTATGACTAGTGTCCTGTAGCATACGCATACGCACACTAGATCACACAGCCTCACAATTCCCAAAAAAAAAAAAAAAA

>gi|71895640|ref|NM_001031040.1| Mus musculus EH-domain containing 3 (EHD3), mRNAGGTAGGGCGCTACCGCCTCCGCCCGCCTCTCGCGCTGTTCCTCCGCGGTATGCCCGCGCCGGCAGCCGGC......TATTATATAGAGAAATATATTGTGTATGTAGGATGTGCTTATTGCATTACATTTATCACTTGTCTTAACTAGAATGCATTAACCTTTTTTGTACCCTGGTCCTAAAACATTATTAAAAAGAAAGGCTAAAAAAAAAAAAAAAAA

>gi|55742710|ref|NM_153068.2| Mus musculus EH-domain containing 2 (Ehd2), mRNATGAGGGGGCCTGGGGCCCGCCCTGCTCGCCGCTCCTAGCGCACGCGGCCCCACCCGTCTCACTCCACTGC......

3.20Predicting the SAGE tag of an mRNA

3. Decide how to present the results

Simply print the header line of each mRNA and then it’s predicted 10bp tag, like so:

> gi|24646380|ref|NM_079608.2| Mus musculus EH-domain containing 4 (EHD4), mRNAATCACACAGC

>gi|71895640|ref|NM_001031040.1| Mus musculus EH-domain containing 3 (EHD3), mRNAAATGCATTAA

...

...

3.21Predicting the SAGE tag of an mRNA

4. Overall design:

1. For each mRNA in the input:

1. Read the sequence

2. Find the most downstream recognition site of NlaII (CTAG)

3. Get the 10bp tag after that site

4. Print it

3.22Predicting the SAGE tag of an mRNA

5. Refine the design, specify more details:

1. For each mRNA in the input (use a loop):

1. Read the sequence

1. Store its header line in one string variable

2. Concatenate all lines of the sequence and store it in another string variable

2. Find the most downstream recognition site of NlaII (CTAG)

1. Go over the sequence with a loop, starting from the 3’ tail, and going back until the first CTAG is found

3. Get the 10bp tag after that site

1. Take a substr of length 10

4. Print it

6. Write the code