-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastaconvert.py
38 lines (31 loc) · 1.49 KB
/
fastaconvert.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
31
32
33
34
35
36
37
38
def reverse_complement(sequence):
complement = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C', 'N': 'N', 'R':'Y', 'Y':'R', 'M':'K', 'K':'M', 'S':'W', 'W':'S'}
return "".join(complement[base] for base in reversed(sequence.upper()))
def convert_fasta_to_antisense(fasta_file):
with open(fasta_file, 'r') as file:
lines = file.readlines()
antisense_fasta = ""
current_sequence = ""
header = ""
for line in lines:
if line.startswith('>'):
if current_sequence:
antisense_sequence = reverse_complement(current_sequence)
antisense_fasta += f"{header}_antisense\n"
antisense_fasta += '\n'.join([antisense_sequence[i:i+60] for i in range(0, len(antisense_sequence), 60)]) + '\n'
header = line.strip()
current_sequence = ""
else:
current_sequence += line.strip()
if current_sequence: # Add the last sequence
antisense_sequence = reverse_complement(current_sequence)
antisense_fasta += f"{header}_antisense\n"
antisense_fasta += '\n'.join([antisense_sequence[i:i+60] for i in range(0, len(antisense_sequence), 60)]) + '\n'
output_file = fasta_file.replace('.fasta', '_antisense.fasta')
with open(output_file, 'w') as file:
file.write(antisense_fasta)
return output_file
# Example usage
input_fasta = 'path to fasta'
antisense_fasta = convert_fasta_to_antisense(input_fasta)
print(f"Antisense FASTA saved to: {antisense_fasta}")