0

我无法确定我正在为分析酵母基因组而编写的这个子模块中的错误。帮助任何人?

sub 应该使用散列键作为正则表达式来检查某对大写字母。如果我们有匹配项,则返回关联的值。但是,它似乎只匹配“TT”并返回值 5。我不知道为什么它会跳转到 TT 哈希元素;不同元素之间的其他一切似乎都是相同的。我认为它会返回每个值,或者不返回任何值,但它只返回 TT。

完整代码在 github 上:https ://github.com/bsima/yeast-TRX

以下是相关代码(script.pl 的第 159 行):

sub trxScore {

    my ( $dinucleotide ) = @_;

    my %trxScores = (
        qr/(CG)/ => 43,
        qr/(CA)/ => 42,
        qr/(TG)/ => 42,
        qr/(GG)/ => 42,
        qr/(CC)/ => 42,
        qr/(GC)/ => 25,
        qr/(GA)/ => 22,
        qr/(TC)/ => 22,
        qr/(TA)/ => 14,
        qr/(AG)/ =>  9,
        qr/(CT)/ =>  9,
        qr/(AA)/ =>  5,
        qr/(TT)/ =>  5,
        qr/(AC)/ =>  4,
        qr/(GT)/ =>  4,
        qr/(AT)/ =>  0
    );

    foreach my $re (keys %trxScores) {
        if ( match($re,$dinucleotide) ) {
            return $trxScores{$re};
        } else {
            return "null";
        }
    }
}

输出在bayanus-TRXscore.csv 中:

Saccharomyces bayanus
gene,gene pair,position,trx score
...
eYAL001C,TA,23,null
eYAL001C,AT,24,null
eYAL001C,TT,25,5
eYAL001C,TT,26,5
eYAL001C,TT,27,5
eYAL001C,TA,28,null
4

2 回答 2

3

您总是在第一次尝试匹配时返回,这将是散列中的第一个键(这通常是一个未定义的顺序,但考虑到完全相同的散列结构和相同版本的 Perl,您将倾向于访问相同的顺序)。如果不匹配,则代码返回“null”,并且不会尝试另一个循环。该return命令将退出 trxScore sub,为调用它的任何 Perl 代码提供值 - areturn将立即(通常是干净地)结束 a 中的所有循环和代码块sub

改变你的循环如下:

foreach my $re (keys %trxScores) {
    if ( match($re,$dinucleotide) ) {
        return $trxScores{$re};
    }
}
return "null";

这将return "null";仅在代码尝试与哈希中的所有键匹配后发生。

于 2013-09-14T19:05:08.593 回答
2

你为什么还要使用正则表达式?在我看来,您只是在进行字符串匹配。另外,为什么你有一个哈希?看起来您想按特定顺序匹配您的字符串,并且哈希键没有按任何顺序保存。

$ cat foo.pl
#!/usr/bin/perl

use strict;
use warnings;

# Note that this is an ordered array of matcher and scores, not a hash.
my @trxScores = (
    CG => 43,
    CA => 42,
    TG => 42,
    GG => 42,
    CC => 42,
    GC => 25,
    GA => 22,
    TC => 22,
    TA => 14,
    AG =>  9,
    CT =>  9,
    AA =>  5,
    TT =>  5,
    AC =>  4,
    GT =>  4,
    AT =>  0
);

my $dinucleotide = 'GTACTTAAGCTATTGGAGC';

my $found;
while ( my ($matcher,$score) = splice( @trxScores, 0, 2 ) ) {
    print "Trying to match $matcher for a score of $score\n";

    if ( index( $dinucleotide, $matcher ) > -1 ) {
        print "Found $matcher for a score of $score\n";
        $found = 1;
        last;
    }
}

if ( !$found ) {
    print "Couldn't find any matches\n";
}


$ perl foo.pl
Trying to match CG for a score of 43
Trying to match CA for a score of 42
Trying to match TG for a score of 42
Found TG for a score of 42
于 2013-09-14T19:24:42.290 回答