gpt4 book ai didi

perl - Perl 中未定义的子例程和主要错误

转载 作者:行者123 更新时间:2023-12-02 00:25:55 24 4
gpt4 key购买 nike

我正在尝试从该 FASTA 文件中提取 DNA 序列,使其达到每行指定的碱基长度,例如 40。

> sample dna  (This is a typical fasta header.)
agatggcggcgctgaggggtcttgggggctctaggccggccacctactgg
tttgcagcggagacgacgcatggggcctgcgcaataggagtacgctgcct
gggaggcgtgactagaagcggaagtagttgtgggcgcctttgcaaccgcc
tgggacgccgccgagtggtctgtgcaggttcgcgggtcgctggcgggggt

使用此 Perl 模块 (fasta.pm):

package fasta;
use strict;

sub read_fasta ($filename) {
my $filename = @_;
open (my $FH_IN, "<", $filename) or die "Can't open file: $filename $!";
my @lines = <$FH_IN>;
chomp @lines;
return @lines;
}

sub read_seq (\@lines) {
my $linesRef = @_;
my @lines = @{$linesRef};
my @seq;
foreach my $line (@lines) {
if ($line!~ /^>/) {
print "$line\n";
push (@seq, $line);
}
}
return @seq;
}

sub print_seq_40 (\@seq) {
my $linesRef = @_;
my @lines = @{$linesRef};
my $seq;
foreach my $line (@lines) {
$seq = $seq.$line;
}

my $i= 0;
my $seq_line;
while (($i+1)*40 < length ($seq)) {
my $seq_line = substr ($seq, $i*40, 40);
print "$seq_line\n";
$i++;
}
$seq_line = substr ($seq, $i*40);
print "$seq_line\n";
}
1;

主要脚本是

use strict;
use warnings;
use fasta;

print "What is your filename: ";
my $filename = <STDIN>;
chomp $filename;

my @lines = read_fasta ($filename);
my @seq = read_seq (\@lines);
print_seq_40 (\@seq);
exit;

这是我得到的错误

Undefined subroutine &main::read_fasta called at q2.pl line 13, <STDIN> line 1.

谁能告诉我我哪里做错了?

最佳答案

看起来你在这方面毫无进展。

我认为您选择使用模块和子例程有点奇怪,因为您只调用每个子例程一次并且确实对应很少的代码。

你的程序和模块都需要以use strictuse warnings开始,并且你不能使用像Perl子例程中那样的原型(prototype)。包括许多其他错误,这更接近您需要的代码。

package Fasta;

use strict;
use warnings;
use 5.010;
use autodie;

use base 'Exporter';

our @EXPORT = qw/ read_fasta read_seq print_seq_40 /;

sub read_fasta {
my ($filename) = @_;

open my $fh_in, '<', $filename;
chomp(my @lines = <$fh_in>);

@lines;
}

sub read_seq {
my ($lines_ref) = $_[0];

grep { not /^>/ } @$lines_ref;
}

sub print_seq_40 {
my ($lines_ref) = @_;

print "$_\n" for unpack '(A40)*', join '', @$lines_ref;
}

1;

q2.pl

use strict;
use warnings;

use Fasta qw/ read_fasta read_seq print_seq_40 /;

print "What is your filename: ";
my $filename = <STDIN>;
chomp $filename;

my @lines = read_fasta($filename);
my @seq = read_seq(\@lines);
print_seq_40(\@seq);

关于perl - Perl 中未定义的子例程和主要错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26947503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com