In Perl, we have IO::ScalarArray
for treating the elements of an array like the lines of a file. In BioPerl, we have Bio::SeqIO
, which can produce a filehandle that reads and writes Bio::Seq
objects instead of strings representing lines of text. I would like to do a combination of the two: I would like to obtain a handle that reads successive Bio::Seq
objects from an array of such objects. Is there any way to do this? Would it be trivial for me to implement a module that does this?
My reason for wanting this is that I would like to be able to write a subroutine that accepts either a Bio::SeqIO
handle or an array of Bio::Seq
objects, and I'd like to avoid writing separate loops based on what kind of input I get. Perhaps the following would be better than writing my own IO module?
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)
}
}