-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_fa_headers.py
More file actions
20 lines (17 loc) · 898 Bytes
/
Copy pathclean_fa_headers.py
File metadata and controls
20 lines (17 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Replaces space, semicolon, colon, or parenthesis with underscore in fasta headers
from Bio import SeqIO
INFILE = "testncbi.fasta"
OUTFILE = "testncbi_underscores.fas"
EXCLUDED = [" ", ";", ":", "(", ")"] # Characters to be replaced with underscores
# Read the input FASTA file and replace specified characters in headers
# Replace __ with _ in headers
# Write the modified sequences to the output file
with open(OUTFILE, "w") as outputs:
for seqrecord in SeqIO.parse(INFILE, "fasta"):
for char in EXCLUDED:
seqrecord.id = seqrecord.id.replace(char, "_")
seqrecord.description = seqrecord.description.replace(char, "_")
# Ensure no double underscores remain in the headers
seqrecord.id = seqrecord.id.replace("__", "_")
seqrecord.description = seqrecord.description.replace("__", "_")
SeqIO.write(seqrecord, outputs, "fasta")