我有一个序列和一个表示残差(字符)位置的数字。我想从残留物的两边各取7个残留物。下面是实现这一点的代码:
my $seq = substr($sequence, $location-8, 14);这从残留物的两边各抓取7个。然而,有些序列两边的残基都少于7个。因此,当发生这种情况时,我会收到一个错误消息:
substr outside of string at test9.pl line 52 (#1) (W substr)(F) You tried to reference a substr() that pointed outside of a string. That is, the absolute value of the offset was larger than the length of the string.如何更改空白处,并用另一个字母(例如X)替换它们?
例如,如果有一个序列ABCDEFGH,并且$location指向D,那么我需要在两边各有7个,因此结果将是: XXXXABCDEFGHXXX
发布于 2014-03-16 06:32:05
对我上面的评论进行扩展。我将创建一个my_substr函数来封装填充和位置移位。
my $sequence = "ABCDEFGH";
my $location = 3;
sub my_substr {
my ($seq, $location, $pad_length) = @_;
my $pad = "X"x$pad_length;
return substr("$pad$seq$pad", $location, (2*$pad_length+1));
}
print my_substr($sequence, $location, 7) . "\n";收益率
XXXXABCDEFGHXXX发布于 2014-03-16 06:20:08
这是一个非常冗长的答案,但或多或少会得到你想要的:
use strict;
use warnings;
my $sequence = 'ABCDEFGH';
my $wings = 7;
my $location = index $sequence, 'D';
die "D not found" if $location == -1;
my $start = $location - $wings;
my $length = 1 + 2 * $wings;
my $leftpad = 0;
if ($start < 0) {
$leftpad = -1 * $start;
$start = 0;
}
my $seq = substr($sequence, $start, $length);
$seq = ('X' x $leftpad) . $seq if $leftpad;
my $rightpad = $length - length ($seq);
$seq .= 'X' x $rightpad if $rightpad > 0;
print $seq;或者为了避免所有额外的工作,可以只创建一个包含padding的新$sequence变量:
my $sequence = 'ABCDEFGH';
my $wings = 7;
my $location = index $sequence, 'D';
die "D not found" if $location == -1;
my $paddedseq = ('X' x $wings) . $sequence . ('X' x $wings);
my $seq = substr($paddedseq, $location, 1 + 2 * $wings);
print $seq;https://stackoverflow.com/questions/22430347
复制相似问题