This repository was archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence_analizer.py
More file actions
204 lines (180 loc) · 7.98 KB
/
sequence_analizer.py
File metadata and controls
204 lines (180 loc) · 7.98 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Author Byunghyun Ban
Email halfbottle@sangsang.farm
"""
RNA_dict = {
"UUU" : "Phe", "UUC" : "Phe", "UUA" : "Leu", "UUG" : "Leu",
"CUU" : "Leu", "CUC" : "Leu", "CUA" : "Leu", "CUG" : "Leu",
"AUU" : "Ile", "AUC" : "Ile", "AUA" : "Ile", "AUG" : "Met",
"GUU" : "Val", "GUC" : "Val", "GUA" : "Val", "GUG" : "Val",
"UCU" : "Ser", "UCC" : "Ser", "UCA" : "Ser", "UCG" : "Ser",
"CCU" : "Pro", "CCC" : "Pro", "CCA" : "Pro", "CCG" : "Pro",
"ACU" : "Thr", "ACC" : "Thr", "ACA" : "Thr", "ACG" : "Thr",
"GCU" : "Ala", "GCC" : "Ala", "GCA" : "Ala", "GCG" : "Ala",
"UAU" : "Tyr", "UAC" : "Tyr", "UAA" : "Stop", "UAG" : "Stop",
"CAU" : "His", "CAC" : "His", "CAA" : "Gln", "CAG" : "Gln",
"AAU" : "Asn", "AAC" : "Asn", "AAA" : "Lys", "AAG" : "Lys",
"GAU" : "Asp", "GAC" : "Asp", "GAA" : "Glu", "GAG" : "Glu",
"UGU" : "Cys", "UGC" : "Cys", "UGA" : "Stop", "UGG" : "Trp",
"CGU" : "Arg", "CGC" : "Arg", "CGA" : "Arg", "CGG" : "Arg",
"AGU" : "Ser", "AGC" : "Ser", "AGA" : "Arg", "AGG" : "Arg",
"GGU" : "Gly", "GGC" : "Gly", "GGA" : "Gly", "GGG" : "Gly"
}
DNA_dict = {
"TTT" : "Phe", "TTC" : "Phe", "TTA" : "Leu", "TTG" : "Leu",
"CTT" : "Leu", "CTC" : "Leu", "CTA" : "Leu", "CTG" : "Leu",
"ATT" : "Ile", "ATC" : "Ile", "ATA" : "Ile", "ATG" : "Met",
"GTT" : "Val", "GTC" : "Val", "GTA" : "Val", "GTG" : "Val",
"TCT" : "Ser", "TCC" : "Ser", "TCA" : "Ser", "TCG" : "Ser",
"CCT" : "Pro", "CCC" : "Pro", "CCA" : "Pro", "CCG" : "Pro",
"ACT" : "Thr", "ACC" : "Thr", "ACA" : "Thr", "ACG" : "Thr",
"GCT" : "Ala", "GCC" : "Ala", "GCA" : "Ala", "GCG" : "Ala",
"TAT" : "Tyr", "TAC" : "Tyr", "TAA" : "Stop", "TAG" : "Stop",
"CAT" : "His", "CAC" : "His", "CAA" : "Gln", "CAG" : "Gln",
"AAT" : "Asn", "AAC" : "Asn", "AAA" : "Lys", "AAG" : "Lys",
"GAT" : "Asp", "GAC" : "Asp", "GAA" : "Glu", "GAG" : "Glu",
"TGT" : "Cys", "TGC" : "Cys", "TGA" : "Stop", "TGG" : "Trp",
"CGT" : "Arg", "CGC" : "Arg", "CGA" : "Arg", "CGG" : "Arg",
"AGT" : "Ser", "AGC" : "Ser", "AGA" : "Arg", "AGG" : "Arg",
"GGT" : "Gly", "GGC" : "Gly", "GGA" : "Gly", "GGG" : "Gly"
}
Amino_dict = {
"Ala" : "A", "Asp" : "D", "Arg" : "R", "His" : "H", "Lys" : "K",
"Glu" : "E", "Ser" : "S", "Thr" : "T", "Asn" : "N", "Gln" : "Q",
"Cys" : "C", "Gly" : "G", "Pro" : "P", "Val" : "V", "Ile" : "I",
"Leu" : "L", "Met" : "M", "Phe" : "F", "Tyr" : "Y", "Trp" : "W"
}
Start_codon_DNA = "ATG"
Start_codon_RNA = "AUG"
Stop_codons_DNA = ["TAA", "TAG", "TGA"]
Stop_codons_RNA = ["UAA", "UAG", "UGA"]
class Sequence_analizer():
def __init__(self, sequence_filename, mode="DNA"):
self.mode = mode
self.filename = sequence_filename
self.sequence = ""
self.make_sequence(sequence_filename)
self.transcriptable_sequences = []
self.protein = []
def make_sequence(self, sequence_filename):
file = open(sequence_filename)
for line in file:
line = line.strip()
if not line:
continue
if ">" in line:
continue
self.sequence += line
file.close()
def find_all_possible_proteins(self):
if not self.transcriptable_sequences:
self.find_genes()
for seq in self.transcriptable_sequences:
protein = self.translation(seq)
if protein not in self.protein:
self.protein.append(protein)
return self.protein
def translation(self, refined_sequence):
if self.mode == "DNA":
dict = DNA_dict
else:
dict = RNA_dict
Protein_sequence = []
Gene_sequence = ""
while refined_sequence:
codon = refined_sequence[:3]
refined_sequence = refined_sequence[3:]
if codon not in dict:
return Protein_sequence, Gene_sequence
amino_acid = dict[codon]
if amino_acid == "Stop":
return Protein_sequence, Gene_sequence
Protein_sequence.append(amino_acid)
Gene_sequence += codon
return Protein_sequence, Gene_sequence
@staticmethod
def transcription(self, sequence):
return "U".join(sequence.split("T"))
@staticmethod
def reverse_transcription(self, sequence):
return "T".join(sequence.split("U"))
def find_genes(self):
if self.mode == "DNA":
start_codon = Start_codon_DNA
stop_codon1, stop_codon2, stop_codon3 = Stop_codons_DNA
else:
start_codon = Start_codon_RNA
stop_codon1, stop_codon2, stop_codon3 = Stop_codons_RNA
num_start_codons = self.sequence.count(start_codon)
template_sequence = self.sequence
for num in range(num_start_codons):
sequence = template_sequence[template_sequence.index(start_codon):]
while start_codon in sequence:
sequence = sequence[sequence.index(start_codon):]
stop_codon_idx = [sequence.find(stop_codon1), sequence.find(stop_codon2), sequence.find(stop_codon3)]
for i in range(3):
if stop_codon_idx[i]%3:
stop_codon_idx[i] = -1
if sum(stop_codon_idx) == -3:
if sequence[:int(len(sequence)/3)*3] not in self.transcriptable_sequences:
self.transcriptable_sequences.append(sequence[:int(len(sequence)/3)*3])
break
stop_site = len(self.sequence)
for idx in stop_codon_idx:
if idx == -1:
continue
stop_site = min(idx, stop_site)
if sequence[:stop_site] not in self.transcriptable_sequences:
self.transcriptable_sequences.append(sequence[:stop_site])
sequence = sequence[stop_site+3:]
if len(sequence) < 3:
break
template_sequence = template_sequence[3:]
def save_result_as_FASTA_format(self):
self.find_all_possible_transcription()
new_file_for_gene = open(self.filename.split(".")[0] + "_GENE.fasta", 'w')
new_file_for_protein = open(self.filename.split(".")[0] + "_PROTEIN.fasta", 'w')
for i, el in enumerate(self.transcriptable_sequences):
new_file_for_gene.write(">Sequence " + str(i) + "\n")
new_file_for_gene.write(self.gene_sequence_into_fasta(el) + "\n\n")
for i, el in enumerate(self.protein):
new_file_for_protein.write(">Sequence " + str(i) + "\n")
new_file_for_protein.write(self.protein_sequence_into_fasta(el) + "\n\n")
new_file_for_gene.close()
new_file_for_protein.close()
def protein_sequence_into_fasta(self, sequence):
fasta = ""
for el in sequence:
fasta += Amino_dict[el]
retval = ""
while len(fasta) > 70:
retval += fasta[:70]
retval += "\n"
fasta = fasta[70:]
retval += fasta
return retval
def gene_sequence_into_fasta(self, sequence):
retval = ""
while len(sequence) > 70:
retval += sequence[:70]
retval += "\n"
sequence = sequence[70:]
retval += sequence
return retval
def find_all_possible_transcription(self):
if self.mode == "DNA":
start_codon = Start_codon_DNA
else:
start_codon = Start_codon_RNA
num_start_codons = self.sequence.count(start_codon)
template_sequence = self.sequence
for i in range(num_start_codons):
template_sequence = template_sequence[template_sequence.index(start_codon):]
protein, gene = self.translation(template_sequence)
if gene not in self.transcriptable_sequences:
self.protein.append(protein)
self.transcriptable_sequences.append(gene)
if len(template_sequence) > 3:
template_sequence = template_sequence[3:]
else:
return