for 循环只运行一次 PERL,参数 " " 在 substr 中不是数字



我有一个 for 循环,里面嵌套了 if、else 和 elsif 语句。for 循环运行正常,但由于某种原因只运行一次。我希望按顺序计算 A、C、G 和 T,但我想将它们分为两组 - 主题组和背景组。主题组计数需要特定于位置,而背景计数则不需要。

这是我的.dna文件中包含的内容(.txt可以正常工作): AGGCT

这是我到目前为止所拥有的:

use strict;
use warnings;
#Upload sequence
print "Please enter the filename of the first sequence data: ";
my $filename1 = <STDIN>;
#Remove newline from file
chomp $filename1;
#Open the file and ignore comment lines
open (FILE, '<', $filename1) or die "Cannot open $filename1.",$!;
my $dna;
for (<FILE>)
{
next if /^#/;
next if /^>/;
$dna .= $_;
}
close FILE;
#Remove white spaces 
$dna =~ s/[sd]//g;
$dna =~ /./g;
#User specifies motif width
print "Please enter the motif width:n";
my $width = <STDIN>;
#Remove newline from file
chomp $width;
#Omitting code for non-negative widths to keep this shorter
#Initialize counts and arrays for motif positions
my @motA;
my @motC;
my @motG;
my @motT;
#Define length of motif arrays per width
for(0..($width-1))
{
$motA[$_] = 0;
$motC[$_] = 0;
$motG[$_] = 0;
$motT[$_] = 0;
}
#Initialize background counts
my $bgA = 0;
my $bgC = 0;
my $bgG = 0;
my $bgT = 0;
#Generate random start site in the sequence
#for motif to start from
my $ms = int(rand(((length($dna)+1)-$width)));
#Within a motif, count the bases at the positions
for (my $pos = 0..(length($dna)-1))
{
my $base = substr($dna, $pos, 1);
if ($pos = $ms..($ms + $width))
{
#Add to motif counts    
if($base eq 'A')
{
$motA[$pos-$ms] = $motA[$pos-$ms] + 1;
}
elsif($base eq 'C')
{
$motC[$pos-$ms] = $motC[$pos-$ms] + 1;
}
elsif($base eq 'G')
{
$motG[$pos-$ms] = $motG[$pos-$ms] + 1;
}
elsif($base eq 'T')
{
$motT[$pos-$ms] = $motT[$pos-$ms] + 1;
}
}
else
{
#Create background counts
if ($base eq 'A')
{
$bgA = $bgA + 1;
}
elsif ($base eq 'C')
{
$bgC = $bgC + 1;
}
elsif ($base eq 'G')
{
$bgG = $bgG + 1;
}
elsif ($base eq 'T')
{
$bgT = $bgT + 1;
}
}
}
print "A @motAnC @motCnG @motGnT @motTnn";
print "bgA = $bgAn
bgC = $bgCn
bgG = $bgGn
bgT = $bgT";

输出如下所示:

Please enter the filename of the first sequence data: sample.dna
Please enter the motif width:
3
Argument "" isn't numeric in substr at line 62, <STDIN> line2.
A 0 1 0
C 0 0 0
G 0 0 0
T 0 0 0
bgA = 0
bgC = 0
bgG = 0
bgT = 0

我知道这很可能是因为我在带有 substr 的行中的$dna或$pos包含"(空字符串?),但我不确定如何解决这个问题。我认为$pos的初始化解决了这个问题,但这就是为什么我想问大师们看看该怎么做。我认为这也将解决 for 循环问题。与往常一样,任何和所有帮助都是有用的。我提前感谢它!

这个:

for (my $pos = 0..length($dna))
{
my $base = substr($dna, $pos, 1);

可能是为了0..length($dna)-1

当长度$pos时,子字符串将是一个空字符串。

这不是在某个范围内迭代的 for 循环的正确语法。 它应该是

for my $pos (0..length($dna)-1)

这:

if ($pos = $ms..($ms + $width))

如果我理解正确应该是

if ($pos >= $ms && $pos < $ms + $width)

您所拥有的是分配给$pos触发器操作的结果,这不会有任何有用。

它看起来像这样:

my $ms = int(rand(((length($dna)+1)-$width)));

应该是

my $ms = int(rand(((length($dna))-$width)));

例如,如果$dna长度为 10,宽度为 3,则希望可能的起始偏移量为 0 到 7,而不是 0 到 8。

看起来你在图案内的计数应该使用图案内的位置,而不是$pos; 这个:

$motA[$pos] = $motA[$pos] + 1;

应该是

$motA[$pos-$ms] = $motA[$pos-$ms] + 1;

最新更新