“使用未初始化的值”的错误

时间:2017-10-23 02:54:16

标签: perl

之前我问过关于我的作业计算两个蛋白质序列的比对分数的问题: how to pass a variable to different if statement 但是,我错过了问题中的大量信息,所以我必须重写代码。

现在我的代码已经完成,但我收到警告Use of uninitialized value $sequence2_new in split at alignment_sequence.pl line 52.发出警告后,有很多警告Use of uninitialized value in string eq at alignment_sequence.pl line 56.我理解它,因为我有一个未初始化的值,但我无法理解它是如何未初始化的。我的代码如下:

#!/usr/in/perl

use warnings;
use strict;
use feature qw(say);

my $infile1 = "cystic_fibrosis.fasta";
my $inFH1;
unless (open($inFH1, "<", $infile1)){
    die join (' ', "Can't open", $infile1, $!)
}
my @seq = <$inFH1>;
close $inFH1;
shift @seq;
my $sequence1 = join("", @seq);
$sequence1 =~ s/\n//g;
#parse the query sequence

my $infile2 = "sequence_collection.fasta";
my $inFH2;
unless (open($inFH2, "<", $infile2)){
    die join (' ', "Can't open", $infile2, $!)
}
my $beginning = 1;
my @sequence2;
my $sequence2 = "";
while (my $line = <$inFH2>){
    chomp $line;
    my $chr = substr($line, 0, 1);
    if ($chr ne ">"){
        $sequence2 = $sequence2.$line;
    }else {
        if (! $beginning){
            push @sequence2, $sequence2;
            $sequence2 = "";
        }elsif ($beginning){
            $beginning = 0;
        }
    }
}
close $inFH2;
#parse multiple sequence

my $element = scalar(@sequence2);
for ($a = 0; $a < $element; $a++ ){
    my $sequence2_new;
    if (length $sequence1 < length $sequence2[$a]){
        $sequence2_new = substr ($sequence2[$a], 0, length $sequence1);
    }

    my @sequence1_new = split('', $sequence1);
    my @sequence2_new = split('', $sequence2_new);
    my $element_new = scalar(@sequence1_new);

    my $num = 0;
    for ($b = 0; $b < $element_new; $b++){
        if ($sequence1_new[$b] eq $sequence2_new[$b]){
            $num++;
        }
    }

    my $score = $num / length $sequence1;
    say $sequence1;
    say $sequence2[$a];
    say "\n";
    print "The alignment score is: ";
    printf("%.2f", $score);
    say "\n\n";
}

文件cystic_fibrosis.fasta包含:

>gi|90421313|ref|NP_000483.3| cystic fibrosis transmembrane conductance regulator [Homo sapiens]
MQRSPLEKASVVSKLFFSWTRPILRKGYRQRLELSDIYQIPSVDSADNLSEKLEREWDRELASKKNPKLI
NALRRCFFWRFMFYGIFLYLGEVTKAVQPLLLGRIIASYDPDNKEERSIAIYLGIGLCLLFIVRTLLLHP
AIFGLHHIGMQMRIAMFSLIYKKTLKLSSRVLDKISIGQLVSLLSNNLNKFDEGLALAHFVWIAPLQVAL
LMGLIWELLQASAFCGLGFLIVLALFQAGLGRMMMKYRDQRAGKISERLVITSEMIENIQSVKAYCWEEA
MEKMIENLRQTELKLTRKAAYVRYFNSSAFFFSGFFVVFLSVLPYALIKGIILRKIFTTISFCIVLRMAV
TRQFPWAVQTWYDSLGAINKIQDFLQKQEYKTLEYNLTTTEVVMENVTAFWEEGFGELFEKAKQNNNNRK
TSNGDDSLFFSNFSLLGTPVLKDINFKIERGQLLAVAGSTGAGKTSLLMVIMGELEPSEGKIKHSGRISF
CSQFSWIMPGTIKENIIFGVSYDEYRYRSVIKACQLEEDISKFAEKDNIVLGEGGITLSGGQRARISLAR
AVYKDADLYLLDSPFGYLDVLTEKEIFESCVCKLMANKTRILVTSKMEHLKKADKILILHEGSSYFYGTF
SELQNLQPDFSSKLMGCDSFDQFSAERRNSILTETLHRFSLEGDAPVSWTETKKQSFKQTGEFGEKRKNS
ILNPINSIRKFSIVQKTPLQMNGIEEDSDEPLERRLSLVPDSEQGEAILPRISVISTGPTLQARRRQSVL
NLMTHSVNQGQNIHRKTTASTRKVSLAPQANLTELDIYSRRLSQETGLEISEEINEEDLKECFFDDMESI
PAVTTWNTYLRYITVHKSLIFVLIWCLVIFLAEVAASLVVLWLLGNTPLQDKGNSTHSRNNSYAVIITST
SSYYVFYIYVGVADTLLAMGFFRGLPLVHTLITVSKILHHKMLHSVLQAPMSTLNTLKAGGILNRFSKDI
AILDDLLPLTIFDFIQLLLIVIGAIAVVAVLQPYIFVATVPVIVAFIMLRAYFLQTSQQLKQLESEGRSP
IFTHLVTSLKGLWTLRAFGRQPYFETLFHKALNLHTANWFLYLSTLRWFQMRIEMIFVIFFIAVTFISIL
TTGEGEGRVGIILTLAMNIMSTLQWAVNSSIDVDSLMRSVSRVFKFIDMPTEGKPTKSTKPYKNGQLSKV
MIIENSHVKKDDIWPSGGQMTVKDLTAKYTEGGNAILENISFSISPGQRVGLLGRTGSGKSTLLSAFLRL
LNTEGEIQIDGVSWDSITLQQWRKAFGVIPQKVFIFSGTFRKNLDPYEQWSDQEIWKVADEVGLRSVIEQ
FPGKLDFVLVDGGCVLSHGHKQLMCLARSVLSKAKILLLDEPSAHLDPVTYQIIRRTLKQAFADCTVILC
EHRIEAMLECQQFLVIEENKVRQYDSIQKLLNERSLFRQAISPSDRVKLFPHRNSSKCKSKPQIAALKEE
TEEEVQDTRL

文件sequence_collection.fasta包含100个类似的块:

>gi|1100985|gb|AAC48608.1| CFTR chloride channel [Oryctolagus cuniculus]
MQRSPLEKAGVLSKLFFSWTRPILRKGYRQRLELSDIYQIPSADSADNLSEKLEREWDRELASKKNPKLI
NALRRCFFWRFMFYGIFLYLGEVTKAVQPLLLGRIIASYDPDNKEERSIAIYLGIGLCLLFVVRTLLLHP
AIFGLHHIGMQMRIAMFSLIYKKGLALAHFVWISPLQVTLLMGLLWELLQASAFCGLAFLIVLALVQAGL
GRMMMKYRDQRAGKINERLVITSEMIENIQSVKAYCWEEAMEKMIENLRQTELKLTRKAAYVRYFNSSAF
FFSGFFVVFLSVLPYALTKGIILRKIFTTISFCIVLRMAVTRQFPWAVQTWYDSLGAINKIQDFLQKQEY
KTLEYNLTTTEVVMDNVTAFWEEGFGELFEKAKQNNSDRKISNGDNNLFFSNFSLLGAPVLEDISFKIER
GQLLAVAGSTGAGKTSLLMMITGELEPSEGKIKHSGRISFCSQFSWIMPGTIKENIIFGVSYDEYRYRSV
IKACQLEEDISKFTEKDNTVLGEGGITLSGGQRARISLARAVYKDADLYLLDSPFGYLDVLTEKEIFESC
VCKLMANKTRIMVTSKMEHLKKADKILILHEGSSYFYGTFSELQSLRPDFSSKLMGYDSFDQFSAERRNS
ILTETLRRFSLEGDASVSWNDTRKQSFKQNGELGEKRKNSILNPVNSMRKFSIVLKTPLQMNGIEEDSDA
TIERRLSLVPDSEQGEAILPRSNMINTGPMLQGCRRQSVLNLMTHSVSQGPSIYRRTTTSTRKMSLAPQT
NLTEMDIYSRRLSQESGLEISEEINEEDLKECFIDDVDSIPTVTTWNTYLRYITVHRSLIFVLIWCIVIF
LAEVAASLVVLWLFGNTAPQDKENSTKSGNSSYAVIITNTSSYYFFYIYVGVADTLLALGLFRGLPLVHT
LITVSKILHHKMLHSVLQAPMSTLNTLKAGGILNRFSKDIAILDDLLPLTIFDFIQLLLIVVGAIAVVSV
LQPYIFLATVPVIAAFILLRAYFLHTSQQLKQLESEGRSPIFTHLVTSLKGLWTLRAFGRQPYFETLFHK
ALNLHTANWFLYLSTLRWFQMRIEMIFVLFFIAVAFISILTTGEGEGRVGIILTLAMNIMSTLQWAVNSS
IDVDSLMQSVSRVFMFIDMPTEAKSTKSIKPSSNCQLSKVMIIENQHVKKDDVWPSGGQMTVKGLTAKYI
DSGNAILENISFSISPGQRVGLLGRTGSGKSTLLSAFLRLLSTEGEIQIDGVSWDSITLQQWRKAFGVIP
QKVFIFSGTFRKNLDPYEQWSDQEIWKVADEVGLRSVIEQFPGKLDFVLVDGGYVLSHGHKQLMCLARSV
LSKAKILLLDEPSAHLDPITYQIIRRTLKQAFADCTVILCEHRIEAMLECQRFLVIEENTVRQYESIQKL
LSEKSLFRQAISSSDRAKLFPHRNSSKHKSRPQITALKEEAEEEVQGTRL

抱歉,我知道这非常多余。我将非常感谢任何建议。

1 个答案:

答案 0 :(得分:3)

在此代码中,当$sequence2_new条件为false时,您不会为if分配值。这意味着它仍然是undef

my $sequence2_new;
if (length $sequence1 < length $sequence2[$a]){
    $sequence2_new = substr ($sequence2[$a], 0, length $sequence1);
}

尝试为其指定值:

my $sequence2_new = '';
if (length $sequence1 < length $sequence2[$a]){
    $sequence2_new = substr ($sequence2[$a], 0, length $sequence1);
}