300 likes | 453 Views
R 於微陣列數據分析 - Bioinformatics analysis in Perl -. 教師 : 黃宣誠 老師 助教 : 林振慶 E-mail: kdragongo@gmail.com. Outline. Perl Basics. Perl Installation. http://www.activestate.com/activeperl. Look of perl. Syntax. Variables. $ var scalar @ var array % var hash ; #.
E N D
R於微陣列數據分析-Bioinformatics analysis in Perl- 教師: 黃宣誠 老師 助教: 林振慶 E-mail: kdragongo@gmail.com
Perl Installation • http://www.activestate.com/activeperl
Look of perl Syntax
Variables • $var • scalar • @var • array • %var • hash • ; • #
Control Flow (Condition) • if(condition1) • elsif(condition2) • else • unless(condition1) • else • Ternary operator - ? • $a = ($b > 5) ? “yes” : “no”;
True or False • False • 0 • ‘0’、”0”、’’、”” • undef • Undefined value • True • others
Control Flow (Loop) • while (condition) • until (condition) • for(condition) • foreach(array) • last and next • last: exit this loop • next: ignore below statements
The pivot part of perl Data Structure
Scalar • $ • $A = 55; • $B = “55”; • $C = ‘Hello Perl!\n’; • $D = “Hello Perl!\n” • $E = \$A; • +, -, *, /, %, **, • <STDIN>
Array • @ • @Empty = (); • @A = (1, 2, 3); @B = (‘a’, ‘b’, ‘c’); • $A[0] == 1 • $A[0] = 4; • @A == (4, 2, 3) • @C = (@A, @B);
Array operator • pop @A:Pick up an element from the end of array • push @A:Add an element to the end of array • shift @A:Pick up an element from the top of array • unshift @A:Add an element to the top of array • @A = @B; • reverse @A • sort @A :follow the order of ASCII code • sort {$a <=> $b} @A:from small to big • sort {$b <=> $a} @A:from big to small
Hash (the soul of Perl) • % • key => value • %Empty = (); • %A = {“A” => 1, “B” => 2, “C” => 3}; • reverse: reverse key and value(unsafe) • keys:return keys of hash as an array • values: return values of hash as an array • exists: check if a key existsin hash(unsafe) • delete:delete one key of hash
Object-oriented Subroutine
Functions • Array • push, pop, reverse • Hash • keys, delete, exists, reverse • String • substr, index, length • rand, int
Subroutine sub subroutine_name { statements in subroutine; return something/nothing; } #call subroutine $return_value = &subroutine_name(parameters);
Subroutine sub ADD { ($lo, $ro) = @_; return $lo + $ro; } print &ADD(3, 5);
Subroutine $max_value = &Max(3, 5); sub Max { ($lo, $ro) = @_; my $bigger = ($lo >= $ro) ? $lo:$ro; return $bigger; }
Subroutine • Call by value • 傳值給subroutine,實際上變數並沒有被傳進subroutine • 在subroutine中是用另一個local variable來存傳進來的值 • Call by reference • 傳位址給subroutine,所以變數被以位址的方式傳進subroutine中 • 在subroutine中是用另一個local variable來存傳進來的位址
Subroutine (call by value) @initial = (3, 5); @answer = &exchange(@initial); sub exchange { reverse @_; return @_; }
Subroutine (call by reference) &exchange(\@initial); sub exchange { (my $add) = @_; reverse @$add; return; }
rand & int • rand • 亂數函式 • rand; #隨機回傳一個0~1之間的數 • rand(100); #隨機回傳一個0~100之間的數 • int • 整數函式 • int(15.44332521122323211554) #回傳15 • int(rand(50)) #隨機回傳一個0~50之間的整數
Recursive • Permutation • 0! = 1, N! = N * (N-1)! Sub Permutation { my $n = $_[0]; if($_[0] == 0) { return 1; } else { return $n * Permutation($n- 1); } }
PPM & CPAN Perl Package
PPM • Perl Package Manager
CPAN • Comprehensive Perl Archive Network
Debug Mode • perl -d test.pl • 小明媽有500元 • 小明有100元 • 媽媽給小明45元 • 媽媽跟小明分別剩多少錢? • 小明跟雜貨店老闆買了兩本20元的筆記本、三隻7元的鉛筆,一瓶15元的輕鬆小品 • 小明剩多少錢 • 老闆入賬多少錢 • 請輸出每一個步驟的金額以及其對應的記憶體位址
Homeworks • 要求使用者輸入一正整數,並做輸入檢查,然後判斷它是奇數或偶數。 • 分別印出1到100中的偶數跟奇數 • 先印全部的奇數再換行印全部的偶數,數字間用空白分隔 • 用iterative方式印出Fibonacci 數列的前 50 個數 • Fibonacci 數列:F(0) == F(1) == 1,之後,數列中每個數是前兩個數之和,F(n) = F(n-1) + F(n-2) • 寫出漢諾塔的程式 (recursive) • 若有四個銅環,請問需搬動幾次。 • 請將搬動過程print出來。