从Perl中包含少于15个字符的字符串中获取15个字符
我有一个序列和一个数字,代表一个残基(字符)的位置。我想从残留物的每一面取7个残留物。这是执行此操作的代码:从Perl中包含少于15个字符的字符串中获取15个字符,perl,Perl,我有一个序列和一个数字,代表一个残基(字符)的位置。我想从残留物的每一面取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 su
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这是一个非常详细的答案,但或多或少能满足您的需求:
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;
这是一个非常冗长的答案,但或多或少能满足您的需求:
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;
这是一个非常冗长的答案,但或多或少能满足您的需求:
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;
这是一个非常冗长的答案,但或多或少能满足您的需求:
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;
在我以上评论的基础上进行扩展。我将创建一个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
在我以上评论的基础上进行扩展。我将创建一个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
在我以上评论的基础上进行扩展。我将创建一个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
在我以上评论的基础上进行扩展。我将创建一个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
如果有多个D,你会怎么做?我想你会想在这个答案中使用
sprintf
:。我认为最简单的方法是在搜索/substr之前,在序列的两侧填充“xxxxxx”(如果需要调整$location)。如果有多个D,你会怎么做?我想你会想在这个答案中使用sprintf
:。我认为最简单的方法是在搜索/substr之前,在序列的两侧填充“xxxxxx”(如果需要调整$location)。如果有多个D,你会怎么做?我想你会想在这个答案中使用sprintf
:。我认为最简单的方法是在搜索/substr之前,在序列的两侧填充“xxxxxx”(如果需要调整$location)。如果有多个D,你会怎么做?我想你会想在这个答案中使用sprintf
。我认为最简单的方法是在搜索/substr之前,在序列的两侧填充“XXXXXXX”(如果需要调整$location)。好主意,@ikegami。编辑以改变这一点。好主意,@ikegami。编辑以改变这一点。好主意,@ikegami。编辑以改变这一点。好主意,@ikegami。编辑以改变这一点。