- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
30 lines (26 loc) · 995 Bytes
/
utils.py
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
defread_fasta(file_path: str) ->dict:
"""Read FASTA file and return dictionary of sequences."""
sequences= {}
current_id=None
current_seq= []
withopen(file_path, 'r') asf:
forlineinf:
line=line.strip()
ifline.startswith('>'):
ifcurrent_id:
sequences[current_id] =''.join(current_seq)
current_id=line[1:]
current_seq= []
else:
current_seq.append(line)
ifcurrent_id:
sequences[current_id] =''.join(current_seq)
returnsequences
defwrite_fasta(sequences: dict, file_path: str):
"""Write sequences to FASTA file."""
withopen(file_path, 'w') asf:
forseq_id, sequenceinsequences.items():
f.write(f'>{seq_id}\n')
# Write sequence in lines of 60 characters
foriinrange(0, len(sequence), 60):
f.write(f'{sequence[i:i+60]}\n')