我正在尝试将字符串转换为基于空格分隔符的数组。
我的输入文件如下所示:
>Reference
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnctcACCATGGTGTCGACTC
TTCTATGGAAACAGCGTGGATGGCGTCTCCAGGCGATCTGACGGTTCACTAAACGAGCTC忽略以>开头的行,字符串的其余部分长度为360。
我正在尝试将其转换为数组。
到目前为止,我的代码如下:
#!/usr/bin/perl
use strict;
use warnings;
#### To to change bases with less than 10X coverage to N #####
#### Take depth file and consensus fasta file as input arguments ####
my ($in2) = @ARGV;
my $args = $#ARGV + 1;
if ( $args != 1 ) {
print "Error!!! Insufficient Number of Argumrnts\n";
print "Usage: $0 <consensus fasta file> \n";
}
#### Open a filehandle to read in consensus fasta file ####
my $FH2;
my $line;
my @consensus;
my $char;
open($FH2, '<', $in2) || die "Could not open file $in2\n";
while ( <$FH2> ) {
$line = $_;
chomp $line;
next if $line =~ />/; # skip header line
$line =~ s/\s+//g;
my $len = length($line);
print "$len\n";
#print "$line";
@consensus = split(// , $line);
print "$#consensus\n";
#print "@consensus\n";
#for $char (0 .. $#consensus){
# print "$char: $consensus[$char]\n";
# }
}问题是$len变量返回值60而不是360,$#consensus返回值59而不是字符串长度360。
我已经删除了代码为$line =~ s/\s+//g;的每一行后面的空格,但它仍然不起作用。
发布于 2018-06-08 04:53:17
看起来你的代码基本上是工作的。这只是你的检查逻辑没有任何意义。我会这样做:
use strict;
use warnings;
if (@ARGV != 1) {
print STDERR "Usage: $0 <consensus fasta file>\n";
exit 1;
}
open my $fh, '<', $ARGV[0] or die "$0: cannot open $ARGV[0]: $!\n";
my @consensus;
while (my $line = readline $fh) {
next if $line =~ /^>/;
$line =~ s/\s+//g;
push @consensus, split //, $line;
}
print "N = ", scalar @consensus, "\n";需要注意的主要事项:
如果你要删除所有的空格,
STDERR,而不是返回错误代码,程序应该退出,而不是继续运行。chomp的原因是多余的。@consensus的末尾。在循环结束时,它将累积所有lines.@consensus没有什么意义,因为它还没有完成构建。只有在循环之后,我们才有了我们感兴趣的所有字符。https://stackoverflow.com/questions/50746720
复制相似问题