Answers.Body: <p>I think the <code>.ungap()</code> is what I was looking for.</p>
  <pre><code>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.ungap().translate())} for rec in fasta_sequences])
      return sequences
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-26T20:56:14.323Z
Answers.LastActivityDate: 2020-11-26T20:56:14.323Z
Answers.UserDisplayName: Soerendip
Body: <p>I have a bunch of sequences created with <code>Roary</code> which I want to preprocess. When I read the sequences (genomics data) I want to translate them to protein sequences using:</p>
  <pre><code>import pandas as pd
  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
  </code></pre>
  <p>The problem is that <code>.translate()</code> fails with:</p>
  <blockquote>
  <p>TranslationError: Codon 'T--' is invalid</p>
  </blockquote>
  <p>Is there a build-in function to ignore the dashes?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-26T22:47:46.697Z
Comments.Text: That is some cool comprehension within a dataframe ... of course you need `import pandas as pd`
Comments.UserDisplayName: M__
Tags: <python><seqio>
Title: What is the best way to drop alignment artifacts in sequence objects in biopython?
ThreadText: # What is the best way to drop alignment artifacts in sequence objects in biopython?
  
  (Asked by: username_0 on 2020-11-26)
  
  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:
  
  ```
  import pandas as pd
  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?
  
  **Tags:** python, seqio
  
  > **Comments:**
  > **username_1**: That is some cool comprehension within a dataframe ... of course you need `import pandas as pd`  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-11-26. Score: 2
  
  I think the `.ungap()` is what I was looking for.
  
  ```
  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.ungap().translate())} for rec in fasta_sequences])
      return sequences
  
  ```
  
  
  ---
  
  