-5

我有这个代码

use warnings;
use Getopt::Long;
use Bio::SeqIO;

GetOptions("in=s" => \$file) or die("Error in command line arguments\n");
open $new3, ">", "sequences_tmp.tab";
$seqin = Bio::SeqIO->new(-file => $file, -format => "Fasta");
$seqout = Bio::SeqIO->new(-file => ">$new3", -format => "tab");

while ($seq = $seqin->next_seq()) {
    $seqout->width($seq->length);
    $obj = $seq->id ."\t".$seq->seq()."\n";
    $seqout->write_seq($obj);
}

close $new3;

期望以这种方式打印序列seq_id TAB sequence。但是,此代码打印一个空文件。你知道发生了什么事吗?

4

2 回答 2

0

您打开一个文件句柄$new3,然后将其字符串化并将其用作-file参数中的文件名。这是一个错误。

open $new3, ">", "sequences_tmp.tab";
$seqout = Bio::SeqIO->new(
    -file    => ">$new3",      # <--- Not what you want
    -format  => "tab",
);

Bio::SeqIO->new可以接受文件句柄-fh或文件名-file作为初始化参数。因此,以下任何一项都可能适合您:

my $seqout = Bio::SeqIO->new(
    -fh      => $new3,
    -format  => "tab",
);

      #or#

my $seqout = Bio::SeqIO->new(
    -file    => '>sequences_tmp.tab',
    -format  => "tab",
);

您的代码也可以使用进一步清理:

应用这些并从代码中删除可能的调试工件会将其减少为:

use strict;
use warnings;
use autodie;

use Getopt::Long;
use Bio::SeqIO;

GetOptions(
    "in=s"    => \my $infile,
) or die "Error in command line arguments\n";

my $outfile = "sequences_tmp.tab";

my $seqin  = Bio::SeqIO->new(-file => $infile,     -format => "Fasta");
my $seqout = Bio::SeqIO->new(-file => ">$outfile", -format => "tab");

while (my $seq = $seqin->next_seq()) {
    $seqout->write_seq($seq);
}
于 2014-06-12T17:58:00.957 回答
0

这个$obj变量对我来说看起来没用。它是一个字符串,而不是一个序列对象。由于您只想重新格式化序列,因此可以简单地传递$seq给该write_seq()方法。

所以我想知道你是否正在执行循环体。您可以打印调试输出以进行验证。如果未执行循环体,请确保您的输入文件确实包含 FASTA 格式的序列。

还请use strict;在您的脚本之上声明。它将帮助您避免许多陷阱。

于 2014-06-12T16:18:26.507 回答