# How can I remove FASTA sequences with an asterisk inside the sequence?

I have several protein sequences with an asterisk in the middle of the sequence, and I'd like to remove those sequences.

In the example below, I have three sequences:

From this example, I would like to obtain only the following output:

• Why do you want to remove sequences with * in them? Jul 15 at 16:47

It’s good you brought up the question.

### * are stop codons

Don't simply parse out the * symbol. If done incorrectly, it could constitute 'tampering' and that’s a red line in biology.

The * means a stop codon in the middle of a protein sequence. There are quite a number of reasons why there is an *, i.e., a stop codon in the sequence, for example, in no particular order:

• the open reading frame is out of frame which is part of the natural molecular biology, i.e., there can be multiple reading frames in a gene
• the open reading frame is out of frame due to a sequencing error and poor annotation (which is common);
• the wrong reading frame was used (very, very common);
• the locus isn't translated, e.g., it’s an intron or UTR (untranslated region)

The list goes on, e.g., it could be expression regulation ...

1. If it is not an intron/UTR the way you might 'remove' * is by changing the reading frame, e.g., try using the second codon position (then the third) as the opening reading frame: if the * 'disappear' it was very likely just a reading frame issue. How to do that is a separate question. The resulting amino acid sequence will of course be very different.

2. There's a second possibility if the reading frame is correct, which is to treat each locus between the * as a separate protein. That way the * is 'removed' because their a multiple proteins within the locus. In that case, a \n would replace the * (Mac and Linux), i.e., carriage return.

• I don't think they're looking to tamper, but they probably don't understand the significance of the *. Your point should help them avoid an XY problem if they're in one - I hope they read your answer before proceeding in whatever direction they're headed. Jul 15 at 16:47

Try seqkit grep

seqkit grep --by-seq --invert-match --pattern '*' seq.fa


--by-seq search by seq

It will output sequences without an asterisk.

• This technically answers the question, if we assume OP is not in an XY problem and removing valid sequences that they don't understand (which is highly probable given that they call them "sequences with an asterisk" and not "sequenced containing stop codons". Jul 15 at 16:45

You could use Biopython for this. It makes FASTA parsing really easy.

'''
Python version: 3.10.0
Biopython version: 1.79
'''
from Bio import SeqIO
for seq_record in SeqIO.parse("input.fasta", "fasta"):
if "*" not in (seq_record.seq):
print(">",seq_record.id)
print(seq_record.seq)