Skip to main content
1 of 3
Soerendip
  • 1.3k
  • 12
  • 24

What is the best way to drop alignment artifacts in sequence objects in biopython?

I have a bunch of sequences created with Roary which I want to preprocess. When I read the sequences (genomics data) I want to translate them to protein sequences using:

from Bio.Seq import Seq
from Bio import SeqIO

def read_fasta_file(fn):
    with open(fn,'r') as file:
        fasta_sequences = SeqIO.parse(file, 'fasta')
        sequences = pd.DataFrame([{
            'filename': basename(fn),
            'id': rec.id,
            'seq': str(rec.seq.translate())} for rec in fasta_sequences])
    return sequences

The problem is that .translate() fails with:

TranslationError: Codon 'T--' is invalid

Is there a build-in function to ignore the dashes?

Soerendip
  • 1.3k
  • 12
  • 24