Question: How to remove duplicate sequences in fasta file using python?
1
gravatar for horsedog
2.1 years ago by
horsedog50
horsedog50 wrote:

I have a two fasta files, file 1 and file 2 ,they have a lot of overlapped sequences but not all of them, here I want to merge these two files into one file file 3 and remove the duplicate part, just keeping the unique one, is there code example for python use? Well the duplicate here means the exact same query name and sequences, like these two:

>YP_204112.2
MEHYISLLVKSIFIENMALSFFLGMCTFLAVSKKVKTSFGLGVAVVVVLTIAVPVNNLVYTYLLKENALV
AGVDLTFLSFITFIGVIAALVQILEMILDRFFPPLYNALGIFLPLITVNCAIFGGVSFMVQRDYNFAESV
VYGFGSGIGWMLAIVALAGIREKMKYSDVPPGLRGLGITFITVGLMALGFMSFSGVQL

>YP_204112.2
MEHYISLLVKSIFIENMALSFFLGMCTFLAVSKKVKTSFGLGVAVVVVLTIAVPVNNLVYTYLLKENALV
AGVDLTFLSFITFIGVIAALVQILEMILDRFFPPLYNALGIFLPLITVNCAIFGGVSFMVQRDYNFAESV
VYGFGSGIGWMLAIVALAGIREKMKYSDVPPGLRGLGITFITVGLMALGFMSFSGVQL

Many thanks!

python • 5.0k views
ADD COMMENTlink modified 15 months ago by michau30 • written 2.1 years ago by horsedog50
1

Is there a reason you seem to want a python solution everytime? CD-HIT is meant for this sort of application.

ADD REPLYlink written 2.1 years ago by genomax78k

Well thank you very much! I'll take a look into CDHIT! no reason just I'm practising python recently so I would like to see how people solve problem by python.

ADD REPLYlink written 2.1 years ago by horsedog50
3
gravatar for Alex Reynolds
2.1 years ago by
Alex Reynolds29k
Seattle, WA USA
Alex Reynolds29k wrote:

Shorter option via awk:

$ cat one.fa two.fa | awk -vRS=">" '!a[$0]++ { print ">"$0; }' - > answer.fa

If you want to replicate this with Python, you could look at using a dictionary to store unique keys.

ADD COMMENTlink modified 2.1 years ago • written 2.1 years ago by Alex Reynolds29k

hi , it said "invalid -v"

ADD REPLYlink written 2.1 years ago by horsedog50

Are you using GNU awk? If you're using OS X, install GNU awk via Homebrew: brew install gawk

ADD REPLYlink written 2.1 years ago by Alex Reynolds29k

Hi again, well you're right it works now, but it's so weird cuz in the "answer.fa" it contains even more lines than file1 plus file2, which means that seems it didn't 'remove' but 'add'? (I used wc -l to count lines)

ADD REPLYlink written 2.1 years ago by horsedog50

Maybe grab the first few lines of both files and try it out on those test files. I'm not sure why you get that result; this is a pretty common use of awk.

ADD REPLYlink written 2.1 years ago by Alex Reynolds29k

I like the simplicity! This method almost worked for me, but was printing empty lines and one line with only '>'. I'm not an expert in awk, so filtering results by using grep:

cat one.fa two.fa | awk -v RS=">" '!a[$0]++ { print ">"$0; }' - | grep -Ev '^\s*$|^>\s*$' > answer.fa
ADD REPLYlink modified 20 months ago • written 20 months ago by souzademedeiros0
2
gravatar for tiago211287
2.1 years ago by
tiago2112871.1k
USA
tiago2112871.1k wrote:

This task can be accomplished with FASTA/Q Collapser quickly.

ADD COMMENTlink modified 2.1 years ago • written 2.1 years ago by tiago2112871.1k
2
gravatar for lakhujanivijay
2.1 years ago by
lakhujanivijay4.7k
India
lakhujanivijay4.7k wrote:

One liner using seqkit

zcat fasta.fa.gz | seqkit rmdup -s -i -m -o clean.fa.gz -d duplicated.fa.gz -D duplicated.detail.txt
ADD COMMENTlink written 2.1 years ago by lakhujanivijay4.7k

Fast and do the job nicely. The inspection of the duplicated is very helpful. Best option for me so far.

ADD REPLYlink written 6 months ago by gildas.lepennetier10
1
gravatar for pauley-p
22 months ago by
pauley-p10
UNAM
pauley-p10 wrote:

This is a python based alternative to your issue that uses BioPy

https://github.com/MJChimal/BiologPy/blob/master/drop_unique_records.py

Hope it still helps! :)

ADD COMMENTlink written 22 months ago by pauley-p10
0
gravatar for shoujun.gu
2.1 years ago by
shoujun.gu370
Rockville/MD
shoujun.gu370 wrote:

This python code will combine all the input fasta files (any number of files), and output 1 file with duplicated sequences removed. Note: if your input fasta files are too big to load into memory, this code will fail.

input_files=[list of input file names]
output_file='output_file_name'

holder=[]
for file in input_files:
    with open(file,'r') as file:
        rec=file.read().split('>')[1:]
        rec=['>'+i.strip()+'\n' for i in rec]
    holder.extend(rec)

total='\n'.join(list(set(holder)))

with open(output_file,'w') as out:
    out.write(total)
ADD COMMENTlink modified 2.1 years ago • written 2.1 years ago by shoujun.gu370
0
gravatar for michau
15 months ago by
michau30
Germany/Berlin/MPIMG
michau30 wrote:

Learn to use Biopython library. It's handy as hell. You can use any format as in/out

from Bio import SeqIO

with open('output.fasta', 'a') as outFile:
    record_ids = list()
    for record in SeqIO.parse('input.fasta', 'fasta'):
        if record.id not in record_ids:
            record_ids.append( record.id)
            SeqIO.write(record, outFile, 'fasta')
ADD COMMENTlink modified 15 months ago • written 15 months ago by michau30
1

It should be noted this only checks for duplicates based on their IDs, which is not always going to be the most robust way to do it. It would probably be best to check for duplicated sequences which is what most of the other solutions are doing.

ADD REPLYlink written 15 months ago by Joe16k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1232 users visited in the last hour