1. 程式人生 > >perl應用:DNA序列翻譯(下):從fasta格式中讀取序列,然後輸出蛋白質序列,以及fasta格式的介紹

perl應用:DNA序列翻譯(下):從fasta格式中讀取序列,然後輸出蛋白質序列,以及fasta格式的介紹

use strict;  
use warnings;  
  
  
my $dna      ='';  
my $protein  ='';  
my @file_data=( );  
my @filedata;


  
@filedata  = get_file_data();
$dna       = extract_sequence_from_fasta_data(@filedata);  
$protein   = dna2peptide($dna);  
print_sequence($protein,25);  
  
sub get_file_data
{  
	# A subroutine to get data from a file given its filename
	#讀取檔案的子序列
    my $dna_filename;
	my @filedata;
    print "please input the Path just like this f:\\\\perl\\\\data.txt\n";   
    chomp($dna_filename=<STDIN>); 
	open(DNAFILENAME,$dna_filename)||die("can not open the file!");    
	@filedata     = <DNAFILENAME>;  
	close DNAFILENAME;  
	return @filedata;#子函式的返回值一定要記住寫
}

sub extract_sequence_from_fasta_data  
{  
    #*******************************************************************  
    # A subroutine to extract FASTA sequence data from an array  
    # 得到其中的序列  
    # fasta格式介紹:  
    # 包括三個部分  
    # 1.第一行中以>開頭的註釋行,後面是名稱和序列的來源  
    # 2.標準單字母符號的序列  
    # 3.*表示結尾  
    #*******************************************************************  
  
    my (@fasta_file_data) =@_;  
    my $sequence =' ';  
    foreach my $line (@fasta_file_data)  
    {  
        #這裡忽略空白行  
        if ($line=~/^\s*$/)  
        {  
            next;  
        }  
        #忽略註釋行  
        elsif($line=~/^\s*#/)  
        {  
            next;  
        }  
        #忽略fasta的第一行  
        elsif($line=~/^>/)  
        {  
            next;  
        }  
        else  
        {  
            $sequence .=$line;  
        }  
    }  
    $sequence=~s/\s//g;  
    return $sequence;  
}  
  
sub print_sequence  
{  
    # A subroutine to format and print sequence data  
    my ($sequence, $length) = @_;  
    for (my $pos =0; $pos<length($sequence);$pos+=$length)  
    {  
        print substr($sequence,$pos,$length),"\n";  
    }  
}  
  
     
  
sub codon2aa     
{     
  
    #第三種方法    
    #也就是運用雜湊    
    #我們將所有的密碼子作為hash的key,然後將代表的氨基酸作為hash的value    
    #然後進行匹配    
    # codon2aa     
    # A subroutine to translate a DNA 3-character codon to an amino acid     
    # Version 3, using hash lookup     
    my($codon) = @_;     
     
    $codon = uc $codon;#uc=uppercase;lc=lowercase    
                   #也就是大小寫轉換,uc表示將所有的小寫 轉換為大寫    
               #lc將所有的大寫轉換為小寫    
      
    my(%genetic_code) = (     
         
    'TCA' => 'S',    # Serine     
    'TCC' => 'S',    # Serine     
    'TCG' => 'S',    # Serine     
    'TCT' => 'S',    # Serine     
    'TTC' => 'F',    # Phenylalanine     
    'TTT' => 'F',    # Phenylalanine     
    'TTA' => 'L',    # Leucine     
    'TTG' => 'L',    # Leucine     
    'TAC' => 'Y',    # Tyrosine      
    'TAT' => 'Y',    # Tyrosine     
    'TAA' => '_',    # Stop     
    'TAG' => '_',    # Stop     
    'TGC' => 'C',    # Cysteine     
    'TGT' => 'C',    # Cysteine     
    'TGA' => '_',    # Stop     
    'TGG' => 'W',    # Tryptophan     
    'CTA' => 'L',    # Leucine     
    'CTC' => 'L',    # Leucine     
    'CTG' => 'L',    # Leucine     
    'CTT' => 'L',    # Leucine     
    'CCA' => 'P',    # Proline     
    'CCC' => 'P',    # Proline     
    'CCG' => 'P',    # Proline     
    'CCT' => 'P',    # Proline     
    'CAC' => 'H',    # Histidine     
    'CAT' => 'H',    # Histidine     
    'CAA' => 'Q',    # Glutamine     
    'CAG' => 'Q',    # Glutamine     
    'CGA' => 'R',    # Arginine     
    'CGC' => 'R',    # Arginine     
    'CGG' => 'R',    # Arginine     
    'CGT' => 'R',    # Arginine     
    'ATA' => 'I',    # Isoleucine     
    'ATC' => 'I',    # Isoleucine     
    'ATT' => 'I',    # Isoleucine     
    'ATG' => 'M',    # Methionine     
    'ACA' => 'T',    # Threonine     
    'ACC' => 'T',    # Threonine     
    'ACG' => 'T',    # Threonine     
    'ACT' => 'T',    # Threonine     
    'AAC' => 'N',    # Asparagine     
    'AAT' => 'N',    # Asparagine     
    'AAA' => 'K',    # Lysine     
    'AAG' => 'K',    # Lysine     
    'AGC' => 'S',    # Serine     
    'AGT' => 'S',    # Serine     
    'AGA' => 'R',    # Arginine     
    'AGG' => 'R',    # Arginine     
    'GTA' => 'V',    # Valine     
    'GTC' => 'V',    # Valine     
    'GTG' => 'V',    # Valine     
    'GTT' => 'V',    # Valine     
    'GCA' => 'A',    # Alanine     
    'GCC' => 'A',    # Alanine     
    'GCG' => 'A',    # Alanine     
    'GCT' => 'A',    # Alanine         
    'GAC' => 'D',    # Aspartic Acid     
    'GAT' => 'D',    # Aspartic Acid     
    'GAA' => 'E',    # Glutamic Acid     
    'GAG' => 'E',    # Glutamic Acid     
    'GGA' => 'G',    # Glycine     
    'GGC' => 'G',    # Glycine     
    'GGG' => 'G',    # Glycine     
    'GGT' => 'G',    # Glycine     
    );     
     
    if(exists $genetic_code{$codon})     
    {     
        return $genetic_code{$codon};     
    }    
    else    
    {     
     
            print STDERR "Bad codon \"$codon\"!!\n";     
            exit;     
    }     
}     
  
sub dna2peptide  
{  
    my ($dna)=@_;  
    my $protein ='';  
    for (my $i=0; $i<(length($dna)-2);$i+=3)  
    {  
        $protein .=codon2aa(substr($dna,$i,3));  
    }  
    return $protein;#這個詞錯誤找了一晚上,沒有返回值,所以結果總是沒有內容,以後要引以為戒,子程式一定要有返回值  
}  





結果如下:

F:\>f:perl\a.pl
RWRR_GVLGALGRPPTGLQRRRRMG
PAQ_EYAAWEA_LEAEVVVGAFATA
WDAAEWSVQVRGSLAGVVRECAGSG
DMEGDGSDPEPPDAGEDSKSENGEN
APIYCICRKPDINCFMIGCDNCNEW
FHGDCIRITEKMAKAIREWYCRECR
EKDPKLEIRYRHKKSRERDGNERDS
SEPRDEGGGRKRPVPDPDLQRRAGS
GTGVGAMLARGSASPHKSSPQPLVA
TPSQHHQQQQQQIKRSARMCGECEA
CRRTEDCGHCDFCRDMKKFGGPNKI
RQKCRLRQCQLRARESYKYFPSSLS
PVTPSESLPRPRRPLPTQQQPQPSQ
KLGRIREDEGAVASSTVKEPPEATA
TPEPLSDEDL

F:\>