in reply to Re^3: Converting Filehandle to Array call in Object Creation
in thread Converting Filehandle to Array call in Object Creation

The handle used by Bio::AlignIO (when 'fasta' is specified) must return ">Seq1\n", "AAAAAAAAAAAAAAA\n", ">Seq2\n", "AAAAAAAGGAAACCA\n" on subsequent calls to a read line, but yours returns "AAAAAAAAAAAAAAA" and "AAAAAAAGGAAACCA".

I suppose you could change:

my $fh = Tie::Handle::FromArray->new(\@array);

to

my $fh = Tie::Handle::FromArray->new([ map { ">Seq".$seq_id++."\n", "$_\n" } @array ]);

but it would be nice if the tied class did that for you instead of creating a new array twice the size of the original one.

Replies are listed 'Best First'.
Re^5: Converting Filehandle to Array call in Object Creation
by holli (Abbot) on Apr 27, 2005 at 19:03 UTC
    Patch: use the last element of the array for a counter.
    sub TIEHANDLE($;$) { my $pkg = shift; my $ref = shift || []; push @{$ref}, 0; bless( $ref, $pkg ); } sub READLINE { my $ref = shift; return @{$ref} > 1 ? ">Seq" . ++$ref->[-1] . "\n" . shift (@{$ref} +) . "\n" : undef; }
    so
    use Tie::Handle::FromArray; my $fh = new Tie::Handle::FromArray ( ["AAAAAAAAAAAAAAA", "AAAAAAAGGAA +ACCA"] ); while (<$fh>) { print; }
    prints
    >Seq1 AAAAAAAAAAAAAAA >Seq2 AAAAAAAGGAAACCA


    holli, /regexed monk/