在 Perl 中,我们IO::ScalarArray
将数组的元素视为文件的行。在 BioPerl 中,我们有Bio::SeqIO
,它可以生成一个文件句柄来读写Bio::Seq
对象而不是代表文本行的字符串。我想做两者的结合:我想获得一个句柄,它Bio::Seq
从这样的对象数组中读取连续的对象。有没有办法做到这一点?实现一个模块对我来说是微不足道的吗?
我想要这个的原因是我希望能够编写一个接受Bio::SeqIO
句柄或对象数组的子例程Bio::Seq
,并且我想避免根据我得到的输入类型编写单独的循环。也许以下会比编写我自己的 IO 模块更好?
sub process_sequences {
my $input = $_[0];
# read either from array of Bio::Seq or from Bio::SeqIO
my $nextseq;
if (ref $input eq 'ARRAY') {
my $pos = 0
$nextseq = sub { return $input->[$pos++] if $pos < @$input}; }
}
else {
$nextseq = sub { $input->getline(); }
}
while (my $seq = $nextseq->()) {
do_cool_stuff_with($seq)
}
}