0
$\begingroup$

I'm attempting to remove redundant sequences from a fasta file (from NCBI). When I execute this code, it returns the number of spots, not the number of sequences. (Number of spots: 408,293, Number of sequences: 40,830)

Please let me know if you have run a code that can remove the duplicate ideas or have any ideas/suggestions.

Thank you SO much.

from Bio import SeqIO
import time

start = time.time() 

seen = []
records = []

for record in SeqIO.parse("b4r2.fasta", "fasta"):  
    if str(record.seq) not in seen:
        seen.append(str(record.seq))
        records.append(record)


#writing to a fasta file
SeqIO.write(records, "no_dupes_b4r2.fasta", "fasta")
end = time.time()

print(f"Run time is {(end- start)/60}") 
$\endgroup$
2
  • $\begingroup$ Hi, your code was not formatting and it became a bit of a headache to format. Please invest some more effort into presenting your questions better. $\endgroup$ – Ram RS Jul 1 '20 at 23:46
  • $\begingroup$ Thank you so much, I'm just learning to use Stack Exchange! I appreciate it $\endgroup$ – User Jul 2 '20 at 18:41
4
$\begingroup$

Your code (as currently formatted) should generate the correct output, but it is not efficient. Make seen a set to improve these O(n) in calls to O(1) time complexity- this will speed it up a lot

seen = set()
records = []

for record in SeqIO.parse("b4r2.fasta", "fasta"):  
    if record.seq not in seen:
        seen.add(record.seq)
        records.append(record)
$\endgroup$
1
  • $\begingroup$ Thank you for your input! $\endgroup$ – User Jul 3 '20 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.