gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:41:37 26 4
gpt4 key购买 nike

我正在尝试对大量字符串(蛋白质序列)执行一些基于组合的过滤。
我写了一组三个子程序来处理它,但我在两种方式上遇到了麻烦——一个是次要的,一个是主要的。小麻烦是当我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com