regex - 为什么将此语句视为字符串而不是其结果?

标签 regex perl string

我正在尝试对大量字符串(蛋白质序列)执行一些基于组合的过滤。
我写了一组三个子程序来处理它,但我在两种方式上遇到了麻烦——一个是次要的,一个是主要的。小麻烦是当我使用 List::MoreUtils 'pairwise' 时我收到有关使用 $a 的警告和 $b只有一次并且它们未初始化。但我相信我正确地调用了这个方法(基于 CPAN 的条目和网络上的一些例子)。
主要问题是错误"Can't use string ("17/32") as HASH ref while "strict refs" in use..."
似乎只有在 foreach 时才会发生这种情况。在 &comp 中循环将哈希值作为字符串给出,而不是评估除法运算。我确定我犯了一个菜鸟错误,但在网上找不到答案。我第一次看 perl 代码是在上周三...

use List::Util;
use List::MoreUtils;
my @alphabet = (
 'A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I',
 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V'
);
my $gapchr = '-';
# Takes a sequence and returns letter => occurrence count pairs as hash.
sub getcounts {
 my %counts = ();
 foreach my $chr (@alphabet) {
  $counts{$chr} = ( $_[0] =~ tr/$chr/$chr/ );
 }
 $counts{'gap'} = ( $_[0] =~ tr/$gapchr/$gapchr/ );
 return %counts;
}

# Takes a sequence and returns letter => fractional composition pairs as a hash.
sub comp {
 my %comp = getcounts( $_[0] );
 foreach my $chr (@alphabet) {
  $comp{$chr} = $comp{$chr} / ( length( $_[0] ) - $comp{'gap'} );
 }
 return %comp;
}

# Takes two sequences and returns a measure of the composition difference between them, as a scalar.
# Originally all on one line but it was unreadable.

sub dcomp {
 my @dcomp = pairwise { $a - $b } @{ values( %{ comp( $_[0] ) } ) }, @{ values( %{ comp( $_[1] ) } ) };
 @dcomp = apply { $_ ** 2 } @dcomp;
 my $dcomp = sqrt( sum( 0, @dcomp ) ) / 20;
 return $dcomp;
}

非常感谢任何答案或建议!

最佳答案

您的代码中有一些错误。首先,请注意来自 perldoc perlop :

Because the transliteration table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote interpolation.



所以你的计数方法是错误的。我也相信你在滥用 pairwise .很难评估什么构成了正确的用法,因为您没有举例说明通过一些简单的输入应该得到什么样的输出。

在任何情况下,我都会将此脚本重写为(其中包含一些调试语句):
#!/usr/bin/perl

use List::AllUtils qw( sum );
use YAML;

our ($a, $b);
my @alphabet = ('A' .. 'Z');
my $gap = '-';

my $seq1 = 'ABCD-EFGH--MNOP';
my $seq2 = 'EFGH-ZZZH-KLMN';

print composition_difference($seq1, $seq2);

sub getcounts {
    my ($seq) = @_;
    my %counts;
    my $pattern = join '|', @alphabet, $gap;
    $counts{$1} ++ while $seq =~ /($pattern)/g;
    warn Dump \%counts;
    return \%counts;
}

sub fractional_composition_pairs {
    my ($seq) = @_;
    my $comp = getcounts( $seq );
    my $denom = length $seq - $comp->{$gap};
    $comp->{$_} /= $denom for @alphabet;
    warn Dump $comp;
    return $comp;
}

sub composition_difference {
    # I think your use of pairwise in the original script
    # is very buggy unless every sequence always contains
    # all the letters in the alphabet and the gap character.
    # Is the gap character supposed to factor in the computations here?

    my ($comp1, $comp2) = map { fractional_composition_pairs($_) } @_;
    my %union;
    ++ $union{$_} for (keys %$comp1, keys %$comp2);

    my $dcomp;
    {
        no warnings 'uninitialized';
        $dcomp = sum map {
            ($comp1->{$_} - $comp2->{$_}) ** 2
        } keys %union;
    }

    return sqrt( $dcomp ) / 20; # where did 20 come from?
}

关于regex - 为什么将此语句视为字符串而不是其结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2583968/

相关文章:

regex - 如何在正则表达式末尾转义大括号

javascript - 正则表达式 - 替换字符串 - charAt(0) 处没有空格

regex - Delphi 正则表达式的最大模式 "separation"?

perl - 使用 Plack 和 Middlewares 时将默认 perl 的 IO 切换为 utf-8 是否正确?

Java:尝试运行字符串函数时出现字符串索引越界错误

javascript - 如何在 JavaScript(和正则表达式?)中将字符串转换为连字符连接的单词?

html - Perl: 不能使用 'defined(%hash)'

regex - Perl m//运算符莫名其妙地无法匹配简单的正则表达式

string - 当我在单个函数或仅在主函数中使用时,字符串文字的 Rust 引用表现不同

python - 从反斜杠到斜杠(创建以数字命名的文件夹)