【问题标题】:Perl: Using regular expressions with hashesPerl:使用带有散列的正则表达式
【发布时间】:2013-09-19 06:17:43
【问题描述】:

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

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

【问题讨论】:

  • 旁注;为什么match() 函数只需要普通的正则表达式测试,为什么%trxScores 哈希当你迭代它? (想到数组数组)

标签: regex perl hash bioinformatics


【解决方案1】:

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

如下改变你的循环:

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

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

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多