Perl 为什么我只能写输出中的最后一行?
我有这段代码,但在我的输出文件中,它只写最后的结果。我错过了什么Perl 为什么我只能写输出中的最后一行?,perl,Perl,我有这段代码,但在我的输出文件中,它只写最后的结果。我错过了什么 #!/usr/bin/perl use strict; use warnings; print "type in the name of a file\n"; my $fasta_file = <STDIN>; chomp ($fasta_file); open (FASTA, $fasta_file) or die "error $!"; my $fasta = ""; while (my $line =
#!/usr/bin/perl
use strict;
use warnings;
print "type in the name of a file\n";
my $fasta_file = <STDIN>;
chomp ($fasta_file);
open (FASTA, $fasta_file) or die "error $!";
my $fasta = "";
while (my $line = <FASTA>) {
$fasta .= $line; }
my @seq = split (/\>/s, $fasta);
for (my $i=0; $i<@seq; $i++) {
my $sequence = $seq[$i];
next if $sequence eq '';
..
my $perc_GC = ((($G + $C) / $size) *100);
print "$perc_GC% GC\n";
open(OUTFILE, "> $fasta_file") or die "\nCould not create file ;/";
print OUTFILE "$perc_GC% GC\n";
};
};
close (FASTA);
close(OUTFILE);
exit;
输出
cel-mir-39 MI0000010 50%隐杆线虫GC
另外,它没有在我的输出文件上写入头。问题是,您正在为循环打开内部的
输出文件,这是一个在基上迭代的循环。这会删除任何已经写入的内容,每次都会打开一个新的空文件,这不是您想要的。在开始处理输入数据之前,只需打开一次输出文件
您的头不会打印到文件中,因为您只使用print“$header\n”
将其打印到标准输出。即使输出文件工作正常,重复打开输出文件也会将其删除
您还需要将基础计算移到内部循环之外。这个循环所要做的就是扫描碱基并计算Gs和Cs。在计算总数之前,不应输出任何内容
您可能更喜欢重写您的程序,它更简洁,更好地遵循最佳实践。它还解决了将序列头打印到输出的问题
#!/usr/bin/perl
use strict;
use warnings 'all';
print 'Enter the name of the FASTA file: ';
my $fasta_file = <STDIN>;
chomp $fasta_file;
print "\n";
(my $out_file = $fasta_file) =~ s/\.fasta$/.gc/ or die "File name must end in .fasta";
open my $out_fh, '>', $out_file or die qq{Unable to open "$out_file" for output: $!};
my $fasta = do {
open my $fh, '<', $fasta_file or die qq{Unable to open "$fasta_file" for input: $!};
local $/;
<$fh>;
};
for my $sequence ( split />/, $fasta ) {
next unless $sequence =~ /\S/;
my ( $header, $dna ) = split /\n/, $sequence, 2;
$dna =~ s/\s//g;
my %bases;
++$bases{$_} for split //, $dna;
for my $fh ( \*STDOUT, $out_fh ) {
printf $fh "%s %.1f%% GC\n",
$header,
( $bases{G} + $bases{C} ) / length($dna) * 100;
}
}
问题在于,您正在内部for
循环中打开输出文件,该循环在基上进行迭代。这会删除任何已经写入的内容,每次都会打开一个新的空文件,这不是您想要的。在开始处理输入数据之前,只需打开一次输出文件
您的头不会打印到文件中,因为您只使用print“$header\n”
将其打印到标准输出。即使输出文件工作正常,重复打开输出文件也会将其删除
您还需要将基础计算移到内部循环之外。这个循环所要做的就是扫描碱基并计算Gs和Cs。在计算总数之前,不应输出任何内容
您可能更喜欢重写您的程序,它更简洁,更好地遵循最佳实践。它还解决了将序列头打印到输出的问题
#!/usr/bin/perl
use strict;
use warnings 'all';
print 'Enter the name of the FASTA file: ';
my $fasta_file = <STDIN>;
chomp $fasta_file;
print "\n";
(my $out_file = $fasta_file) =~ s/\.fasta$/.gc/ or die "File name must end in .fasta";
open my $out_fh, '>', $out_file or die qq{Unable to open "$out_file" for output: $!};
my $fasta = do {
open my $fh, '<', $fasta_file or die qq{Unable to open "$fasta_file" for input: $!};
local $/;
<$fh>;
};
for my $sequence ( split />/, $fasta ) {
next unless $sequence =~ /\S/;
my ( $header, $dna ) = split /\n/, $sequence, 2;
$dna =~ s/\s//g;
my %bases;
++$bases{$_} for split //, $dna;
for my $fh ( \*STDOUT, $out_fh ) {
printf $fh "%s %.1f%% GC\n",
$header,
( $bases{G} + $bases{C} ) / length($dna) * 100;
}
}
解释OP为什么不能按预期工作可能很有用。@123:是的。本来应该是在池上的帖子之外解释的,但他现在删除了。我已经写了更多的文字。这可能有助于解释为什么OP没有按预期工作。@123:是的。本来应该是在池上的帖子之外解释的,但他现在删除了。我还写了一些字