In Perl, we have IO::ScalarArrayto handle array elements similar to file lines. In BioPerl, we have Bio::SeqIOone that can create a file descriptor that reads and writes objects Bio::Seqinstead of lines representing lines of text. I would like to make a combination of two: I would like to get a descriptor that reads sequential objects Bio::Seqfrom 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 this is that I would like to be able to write a routine that takes either a handle Bio::SeqIOor an array of objects Bio::Seq, and I would like to avoid writing separate loops based on what type of input I get. Perhaps the following would be better than writing my own I / O module?
sub process_sequences {
my $input = $_[0];
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)
}
}
source
share