sub translate { # Translate the input file from dna to protein, replacing the # original file. Sequences that are already protein remain # untouched. my ( $filename, $fullpath ) = @_; # Input sequence stream. my $seqI = Bio::SeqIO->new( -file => $fullpath ); # Output sequence stream. my $tempfile = File::Temp->new( SUFFIX => '.fasta' )->filename; my $seqO = Bio::SeqIO->new( -file => ">$tempfile", -format => 'fasta' ); # Translate dna sequences and write to temp file. while ( my $seq = $seqI->next_seq ) { if ( $seq->alphabet eq 'dna' ) { $seq = $seq->translate; } $seqO->write_seq($seq); } $_->close for ( $seqI, $seqO ); # Replace the original file with the temporary one with the # translated sequences. move( $tempfile, $fullpath ); }