-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremove_duplicates_fasta.py
More file actions
29 lines (22 loc) · 1.08 KB
/
remove_duplicates_fasta.py
File metadata and controls
29 lines (22 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#######################################################################
# find duplicated sequences in a fasta file, #
# keep only one copy and combine the name of the duplicated scaffolds #
#######################################################################
## Usage:
# python remove_duplicates_fasta.py -f <file>
from Bio import SeqIO
from collections import defaultdict
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--File", help = "Fasta file to have duplicated sequences removed")
parser.add_argument("-o", "--Output", help = "Output file", default="deduplicated.fasta")
args = parser.parse_args()
dedup_records = defaultdict(list)
for record in SeqIO.parse(args.File, "fasta"):
# Use the sequence as the key and then have a list of id's as the value
dedup_records[str(record.seq)].append(record.id)
with open(args.Output, 'w') as output:
for seq, ids in dedup_records.items():
# Join the ids and write them out as the fasta
output.write(">{}\n".format('|'.join(ids)))
output.write(seq + "\n")