Perl 子例程的输出返回0
我写了一个脚本,它使用一个子程序来调用给定序列中核苷酸的百分比。当我运行脚本时,每个核苷酸百分比的输出总是显示为零 这是我的密码Perl 子例程的输出返回0,perl,Perl,我写了一个脚本,它使用一个子程序来调用给定序列中核苷酸的百分比。当我运行脚本时,每个核苷酸百分比的输出总是显示为零 这是我的密码 #!/usr/bin/perl use strict; use warnings; #### Subroutine to report percentage of each nucleotide in DNA sequence #### my $input = $ARGV[0]; my $nt = $ARGV[1]; my $args = $#ARGV +1;
#!/usr/bin/perl
use strict;
use warnings;
#### Subroutine to report percentage of each nucleotide in DNA sequence ####
my $input = $ARGV[0];
my $nt = $ARGV[1];
my $args = $#ARGV +1;
if($args != 2){
print "Error!!! Insufficient number of arguments\n";
print "Usage: $0 <input fasta file>\n";
}
my($FH, $line);
open($FH, '<', $input) || die "Could\'nt open file: $input\n";
$line = do{
local $/;
<$FH>;
};
$line =~ s/>(.*)//g;
$line =~ s/\s+//g;
my $perc = perc_nucleotide($line , $nt);
printf("The percentage of $nt nucleotide in given sequence is %.0f", $perc);
print "\n";
sub perc_nucleotide {
my($line, $nt) = @_;
print "$nt\n";
my $count = 0;
if( $nt eq "A" || $nt eq "T" || $nt eq "G" || $nt eq "C"){
$count++;
}
my $total_len = length($line);
my $perc = ($count/$total_len)*100;
}
问题在于:
my $perc = perc_nucleotide($line , $nt);
printf("The percentage of $nt nucleotide in given sequence is %.0f", $perc);
perc_Nucleutice返回0.18018,但格式%.0f表示打印时不带小数点。所以它被截断为0。您可能应该使用更像%.2f的东西
值得注意的是,perc_核苷酸没有回报。它仍然有效,但原因可能并不明显
perc_设置我的$perc=$count/$total_len*100;但永远不要使用$perc。主程序中的$perc是另一个变量
perc_核苷酸确实返回了一些东西,没有显式返回的每个Perl子例程都返回最后一个计算的表达式。在本例中,它是my$perc=$count/$total_len*100;但最后计算的表达式规则可能会变得有点棘手
它更容易阅读,而且显式返回更安全。返回$count/$total_len*100 问题在于:
my $perc = perc_nucleotide($line , $nt);
printf("The percentage of $nt nucleotide in given sequence is %.0f", $perc);
perc_Nucleutice返回0.18018,但格式%.0f表示打印时不带小数点。所以它被截断为0。您可能应该使用更像%.2f的东西
值得注意的是,perc_核苷酸没有回报。它仍然有效,但原因可能并不明显
perc_设置我的$perc=$count/$total_len*100;但永远不要使用$perc。主程序中的$perc是另一个变量
perc_核苷酸确实返回了一些东西,没有显式返回的每个Perl子例程都返回最后一个计算的表达式。在本例中,它是my$perc=$count/$total_len*100;但最后计算的表达式规则可能会变得有点棘手
它更容易阅读,而且显式返回更安全。返回$count/$total_len*100 我更正了脚本,它给了我正确的答案
#!/usr/bin/perl
use strict;
use warnings;
##### Subroutine to calculate percentage of all nucleotides in a DNA sequence #####
my $input = $ARGV[0];
my $nt = $ARGV[1];
my $args = $#ARGV + 1;
if($args != 2){
print "Error!!! Insufficient number of arguments\n";
print "Usage: $0 <input_fasta_file> <nucleotide>\n";
}
my($FH, $line);
open($FH, '<', $input) || die "Couldn\'t open input file: $input\n";
$line = do{
local $/;
<$FH>;
};
chomp $line;
#print $line;
$line =~ s/>(.*)//g;
$line =~ s/\s+//g;
#print "$line\n";
my $total_len = length($line);
my $perc_of_nt = perc($line, $nt);
**printf("The percentage of nucleotide $nt in a given sequence is %.2f%%", $perc_of_nt);
print "\n";**
#print "$total_len\n";
sub perc{
my($line, $nt) = @_;
my $char; my $count = 0;
**foreach $char (split //, $line){
if($char eq $nt){
$count += 1;
}
}**
**return (($count/$total_len)*100)**
}
我所做的改动是粗体的
谢谢你的洞察力 我更正了脚本,它给了我正确的答案
#!/usr/bin/perl
use strict;
use warnings;
##### Subroutine to calculate percentage of all nucleotides in a DNA sequence #####
my $input = $ARGV[0];
my $nt = $ARGV[1];
my $args = $#ARGV + 1;
if($args != 2){
print "Error!!! Insufficient number of arguments\n";
print "Usage: $0 <input_fasta_file> <nucleotide>\n";
}
my($FH, $line);
open($FH, '<', $input) || die "Couldn\'t open input file: $input\n";
$line = do{
local $/;
<$FH>;
};
chomp $line;
#print $line;
$line =~ s/>(.*)//g;
$line =~ s/\s+//g;
#print "$line\n";
my $total_len = length($line);
my $perc_of_nt = perc($line, $nt);
**printf("The percentage of nucleotide $nt in a given sequence is %.2f%%", $perc_of_nt);
print "\n";**
#print "$total_len\n";
sub perc{
my($line, $nt) = @_;
my $char; my $count = 0;
**foreach $char (split //, $line){
if($char eq $nt){
$count += 1;
}
}**
**return (($count/$total_len)*100)**
}
我所做的改动是粗体的
谢谢你的洞察力 你是如何执行脚本的?您正在传递哪些参数?/script.pl.fasta a您是如何执行脚本的?您正在传递哪些参数?/script.pl.fasta