Is there a Bioperl equivalent of IO :: ScalarArray for an array of Seq objects?

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];

    # 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)
    }
}
+3
source share
1 answer

Your solution looks like it should work. If you really do not want to spend a lot of time solving this problem, go with him until you like it no longer. Perhaps I wrote this so as not to print the variable name several times:

my $nextseq = do {
     if (ref $input eq ref [] ) {
         my $pos = 0;  #maybe a state variable if you have Perl 5.10
         sub { return $input->[$pos++] if $pos < @$input} }
         }
     else {
         sub { $input->getline() }
     }
 }

, , Mark Jason Dominus " Perl" , .

+1

Source: https://habr.com/ru/post/1742302/


All Articles