-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathref2spename.py
More file actions
31 lines (22 loc) · 783 Bytes
/
ref2spename.py
File metadata and controls
31 lines (22 loc) · 783 Bytes
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
30
31
# ref2spename.py
# Author: Juse with ChatGPT
from Bio import SeqIO
import re
import sys
input_file = sys.argv[1]
output_file = sys.argv[2]
def modify_header(record):
new_id = record.description.replace(' ', '_')
match = re.search(r".*\[([^]]*)\]", new_id)
if match:
species = match.group(1).replace(' ', '_')
rest = new_id[:match.start(1)-1]
new_id = species + '@' + rest
record.id = new_id.strip('_')
record.description = ""
return record
def process_fasta_file(input_file, output_file):
records = SeqIO.parse(input_file, "fasta")
new_records = (modify_header(record) for record in records)
SeqIO.write(new_records, output_file, "fasta")
process_fasta_file(input_file, output_file)