View
214
Download
1
Category
Preview:
DESCRIPTION
completo
Citation preview
Programao PERL
Israel Tojal da Silvaitojal@usp.br
Sumrio
Introduo viso geral
Variveis e Estrutura de Dados
Referncias
Expresses Regulares
Funes
Manipulao de Arquivos
Ordenao
Introduo
Perl : Guia de Consulta RpidaDecio Jr. - Ed. Novatec
Learning Perl (4th ed)by Randal L. Schwartz, Tom Phoenix, brian d foy. Ed. O'Reilly
Programming Perl (2nd Edition) by Larry Wall, Tom Christiansen, Randal L. Schwartz,
Stephen Potter Ed. O'Reilly
Advanced Perl Programming (2th ed)by Simon Cozens. Ed. O'Reilly
Introduo
O que PERL?
Perl is a language for getting your job done
Larry Wall
Introduo
PERL ( Practical Extraction and Report Language ) uma linguagem de programaocriada por Larry Wall em 1987 e que pode ser usada em uma grande variedade detarefas :
Processamento de texto;
Desenvolvimento Web;
Administrao de sistemas;
Acesso a Banco de Dados;
Caractersticas da PERL
Linguagem simples;
Alto nvel;
Suporte a vrias plataformas (Unix, Mac OS, Windows, ...);
Modular e Orientadas a Objetos;
Desenvolvimento rpido;
Introduo
Perl x Bioinformtica
http://www.bioperl.org/
Perl x Bioinformtica
Perl x Bioinformtica
Perl x Bioinformtica
Primeiro programa
#!/usr/bin/perl
print Imprimindo uma mensagem\n;
$ perl hello.pl
hello.pl
Variveis
O que so ?
Posies na memria do computador, que so usadas para guardarem valores
$var
...
E0E1E2
En
E3E4
6
...
E0E1E2
En
E3E4
#!/usr/bin/perl
$var = 6;
Sintaxe das variveis
Tipo Caracter Exemplo um nome para:
Escalar $ $dna
Array @ @genes Uma lista de valores
Hash % %codons
Um valor individual ( nmeros ou string )
Um grupo de valores, indexado por string
Escalares :
$dna = 'ATGCTTATTGTT';
$hits = 5;
$evalue = 5e-120;
$directory = `pwd`;
Sintaxe das variveis
Array:
@genes = ('BRCA1', 'NAT2' , 'MMP9' , 'MYC');@cromossomos = (1,3,5,7,9,11,13,15,17,21,'X','Y);
Os valores so acessados pelos seus ndices:print $gene[0]; # imprime BRCA1;print $cromossomos[3]; # imprime 7;
Tamanho do arrayprint $#gene; # imprime 3;
Acessando uma fatia do arrayprint join(\t,@cromossomos[3..5]) , \n; imprime 7 9 11
Sintaxe das variveis
Hash:
%codons = ( 'ATG'=>'M','CTT'=>'L','ATT'=>'I',
);
Acessando o valor de uma chave do hash
print $codons{'CTT'}\n; # imprime: L
Acessando os valores de todas as chaves do hash
foreach my $key ( keys %codons ){ # imprime:# CTT L
print $key\t$codon{$key}\ n; # ATT I} # ATG M
Sintaxe das variveis
Controlando avisos e variveis
use warnings;use strict;
my $var = 10;
Variveis em Perl so globais por default
Operadores
Operador Tipo
+ , - , * , / , % , ** , ++ , -- Aritmtico
< , = , > , != Comparao Numrica
&& , || , ! , and , or , not Lgicos
lt , gt , le , ge , eq , ne
Atribuio
Comparao de String= , += , -= ,*= , .=
Exemplos :
$x = 8;$y = 2;$z = $x * $y$z == 16
$codonA = 'ACG';$codonB = 'ACG';$codonA ne $codonB
Operadores
If : A declarao ser executada se a expresso lgica for verdadeira:
unless: A declarao no ser executada se a expresso for verdadeira:
Instrues if e unless
my $varA = 3;my $varB = 5;if ( $varA < $varB ) {
....}
my $varA = A;unless ( $varA eq 'B' ) {
....}
Instrues Interativas (looping)
my $i = 0;while ( $i
Formas especiais da instruo do
my $i = 0;do{
print $genes[$i]\n;$i++;
}while ( $i $#genes );
do bloco until expr;
@genes = ('BRCA1', 'NAT2' , 'MMP9' , 'MYC');
Desviando do caminho : next e last
next;
last;
@genes = ('BRCA1', 'NAT2' , 'MMP9' , 'MYC');
my $i = 0;while ( $i
Um programa Perl
#!/usr/bin/perl
use strict;use warnings;
my $dna;my @array = ('a','c','g','g','a','g','c','t','c','g');for (my $i =0; $i
Referncias
Referncias so ponteiros para tipos de dados previamente definidos :
my $var = 6;my $ref_var = \$var;
$var
$ref_var
...
E0E1E2
En
E3E4
6
E1
...
E0E1E2
En
E3E4
print $ref_var; # imprime E1
print $$ref_var; # imprime 6
Referncias
Referncia Definio Valor
Escalar $ref_var = \$var; $$ref_var
Array $ref_array = \@genes; $ref_array->[0]Hash $ref_hash = \%hash; $ref_hash->{'ATG'}
Expresses Regulares
Pesquisa A procura por grandes quantidades de dados de modo conveniente e eficaz
realizada atravs da combinao de padres
expr =~ [ m ] /padro/[g][i][m][o][v][x]
my $dna = 'ACGTTCCAACA';if ( $dna =~ /aac/i ){
...}
my $dna = 'ACGTTCCAACA';if ( $dna =~ /aac/ ){
...}
Expresses Regulares
Substituio Pesquisa uma string procurando um padro e, se encontrado, troca aquele
padro pelo texto de substituio
$var =~ s/padro/substituto/[e][g][i][m][o][s][x]
my $dna = 'acgtccatc--------------ccccatta';$dna =~ s/\-//g;print $dna; #imprime acgtccatcccccatta
my $dna = 'TCCTCCATCCG';$dna =~ s/C/c/;print $dna; #imprime TcCTCCATCCG
Sub-rotinas
A sub-rotina agrupa uma sequncia de declaraes e funes quepodero ser reutilizadas pelo programa.
#!/usr/bin/perl
use strict;use warnings;
my ($x,$y,$sum) = (5,9,0);
$sum= &sum ($x,$y);
print $x + $x = $sum\n;
sub sum{
my ($var1,$var2) = @_;
my $sum = $var1 + $var2;
return $sum;}
#!/usr/bin/perl
use strict;use warnings;
my ($x,$y,$sum) = (5,9,0);
&sum ($x,$y,\$sum);
print $x + $x = $sum\n;
sub sum{
my ($var1,$var2,$rS_sum) = @_;
$$rS_sum = $var1 + $var2;
return;}
Manipulao de Arquivos
...
open (IN,'arquivo.txt') or die $!;while (){
print $_;}close (IN);
...
...
open (OUT,'>arquivo.txt') or die $!;
print OUT $var\n;
close (OUT);
...
Estruturas Avanadas
1 BRCA12 IKZF21 FOXD34 WFS14 RUFY33 COLQ4 NSUN7
1MMP9 FOXD3
2IKZF2
4WFS1 RUFY3 NSUN7
3COLQ
#!/usr/bin/perl
use strict;use warnings;
my %HoA;
my $fileIn = shift or die $!;my @array;open(IN,$fileIn) or die $!;while(){
chomp;@array = split(\t,$_);push ( @ { $HoA{$array[0]} }, $array[1] );
}close(IN);
foreach my $key ( keys %HoA ){
print $key\n\t;print join ( \t,@{$HoA{$key}} ) , \n;
}
Hash de Array
Estruturas Avanadas
1BRCA1 NM_11 NM_32FOXD3 NM_82
4WFS1 NM_54RUFY3 NM_88 NM_89NSUN7 NM_52
3COLQ NM_92 NM_93
2IKZF2 NM_13 NM_14
#!/usr/bin/perl
use strict;use warnings;
my %HoHoA;
my $fileIn = shift or die $!;my @array;open(IN,$fileIn) or die $!;while(){
chomp;@array = split(\t,$_);push ( @ { $HoHoA{$array[0]}{$array[1]} }, $array[2] );
}close(IN);
foreach my $ka ( keys %HoHoA ){
print $ka\n;foreach my $kb ( keys %{$HoHoA{$ka}} ){
print $kb\t;print join ( ,@{$HoHoA{$ka}{$kb}} ) , \n;
}}
Hash de Hash de Array 1 BRCA1 NM_111 BRCA1 NM_322 IKZF2 NM_132 IKZF2 NM_141 FOXD3 NM_824 WFS1 NM_544 RUFY3 NM_884 RUFY3 NM_893 COLQ NM_923 COLQ NM_934 NSUN7 NM_52
Ordenao
BRCA1 15 10MMP9 10 2NAT2 3 4NAD1 9 25HOX 44 87TP53 60 3
TP53 60 3HOX 44 87BRCA1 15 10MMP9 10 2NAD1 9 25NAT2 3 4
#!/usr/bin/perl
use strict;use warnings;
my %HoA;
my $fileIn = shift or die $!;my @array;open(IN,$fileIn) or die $!;while(){
chomp;@array = split(\t,$_);push ( @ { $HoA{$array[0]} }, @array[1..2] );
}close(IN);
foreach my $key ( sort { $HoA{$b}[0] $HoA{$a}[0] } keys %HoA ){
print $key\n;print join ( ,@{$HoA{$key}} ) , \n;
}
Funes PERL
split join sort substr length push pop chomp sqrt glob reverse
http://geekandpoke.typepad.com/geekandpoke/perl/
Vamos praticar ?
Faa um script para traduzir uma sequncia de nucleotdeos para uma ORF.Dica :
perldoc split join reverse substr hash rotina
http://xavante.fmrp.usp.br/verao
Recommended