Hi,
EDIT: I had misunderstood that you actually had the nucleotide sequences in the exclusion file. I modified the code accordingly.
You can use the following script. Put it in a file named fasta_remove.py, make the file executable:
chmod +x fasta_remove.py
and use it by typing:
./fasta_remove.py <input_fasta> <remove_file> <output_fasta>
The remove_file file contains one sequence name per line that needs to be removed from the input_fasta file. The input fasta file is not limited in size. You will need to have Biopython installed and this works under Linux, obviously.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from Bio import SeqIO
fasta_file = sys.argv[1] # Input fasta file
remove_file = sys.argv[2] # Input wanted file, one gene name per line
result_file = sys.argv[3] # Output fasta file
remove = set()
with open(remove_file) as f:
for line in f:
line = line.strip()
if line != "":
remove.add(line)
fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')
with open(result_file, "w") as f:
for seq in fasta_sequences:
nuc = seq.seq.tostring()
if nuc not in remove and len(nuc) > 0:
SeqIO.write([seq], f, "fasta")
Cheers!
can you make a better example... you have a file with a lot of sequences, and a list of sequence ids to remove from it. Is it correct?
Yes, except that the list is not made up of IDs. They are nucleotide sequences.
Do I understand correctly that you have a list of nucleotide sequences, and you want to go through a FASTA file and remove all entries that contain an exact, full-length hit to one of the nucleotide sequences in your list? Are the nucleotide sequences in your list by any chance all of the same length?
I'm fairly sure this has been asked/answered before - check the "Related" box or search the archives.
Have a look at these two questions:
Also, make a search in the archives.