-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConvertFasta_KeepAllFrames.py
72 lines (56 loc) · 2.56 KB
/
ConvertFasta_KeepAllFrames.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#####
## This script will translate all three or all six frames
## For three frames, set translate_direction to "forward" or "reverse"
## For six frames, set translate_direction to "both"
#####
from Bio import SeqIO
from Bio.Alphabet import IUPAC, ProteinAlphabet
from Bio.Seq import Seq
from Bio.Seq import translate
from Bio.SeqRecord import SeqRecord
src_filename = "SourceFile.fasta"
faa_filename = "SourceFile_converted.fasta"
translate_direction = "both"
minimum_peptide_length = 10
src_filename = "BP_Sediment_Genomes_Jansson.fasta"
faa_filename = "BP_Sediment_Genomes_Jansson_converted_KeepAllFrames.fasta"
input_handle = open(src_filename, "r")
output_handle = open(faa_filename, "w")
#codonTableID = 1 # Use for eukaryotes
codonTableID = 11 # Use for bacteria
def translate_and_write_DNA_frames(seq, directionsToConsider="forward", tranlationTable=1):
"""
directionToConsider:
forward - normal DNA direction
reverse - reverse complement
both - both of the abover
"""
allPossibilities = []
if directionsToConsider in ("forward","both"):
# start translation from 1, 2 and 3 nucleotide
for frame in range(3):
trans = str(seq[frame:].translate(tranlationTable))
allPossibilities.append(trans)
if directionsToConsider in ("reverse","both"):
# consider reverse complement DNA sequence as well
# start translation from 1, 2 and 3 nucleotide
for frame in range(3):
trans = str(seq.reverse_complement()[frame:].translate(tranlationTable))
allPossibilities.append(trans)
i = 0
for currentFrame in allPossibilities:
i = i + 1
currentProtein = Seq(currentFrame, alphabet=ProteinAlphabet)
if len(currentProtein) >= minimum_peptide_length:
currentProteinRecord = SeqRecord(currentProtein, seq_record.name)
currentProteinRecord.id = currentProteinRecord.id + "." + str(i)
currentProteinRecord.description = seq_record.description + "; frame " + str(i)
SeqIO.write(currentProteinRecord, output_handle, "fasta")
return
for seq_record in SeqIO.parse(input_handle, "fasta", alphabet=IUPAC.ambiguous_dna):
try:
translate_and_write_DNA_frames(seq_record.seq, translate_direction, codonTableID)
except Exception as inst:
print("Error translating {0}, {1}".format(seq_record.name, inst.args[0]))
input_handle.close()
output_handle.close()