-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
207 lines (166 loc) · 6.09 KB
/
Copy pathclasses.py
File metadata and controls
207 lines (166 loc) · 6.09 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
205
206
from pandas import *
import matplotlib
matplotlib.use('Agg') # problem with matplotlib UI, only works with this for me (Nick)
import matplotlib.pyplot as plt
import base64
from io import BytesIO
#import requests
class Sequence:
def __init__(self, sequence):
self.sequence = sequence
def get_sequence(self):
return self.sequence
def __len__(self):
return len(self.sequence)
def __del__(self):
return 0
class DNA(Sequence):
def __init__(self, sequence):
super().__init__(sequence)
def view_statistics(self):
dic_nucleotides = {}
for i, j in self.sequence.value_counts().items():
dic_nucleotides[i] = j
statistics = {
'freq': dic_nucleotides,
'Count': self.sequence.count(),
'MaxFreqValue': self.sequence.value_counts().idxmax(),
'MinFreqValue': self.sequence.value_counts().idxmin()
}
return statistics
def transcription(self):
trs = self.sequence.replace("T", "U")
return trs
def get_complement(self):
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
return ''.join([complement[base] for base in self.sequence])
def bar_chart(self, xlabel, ylabel, title):
fig, ax = plt.subplots()
ax.bar(self.sequence.value_counts().index, self.sequence.value_counts(),
color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728'], width=0.5)
ax.set(xlabel=xlabel, ylabel=ylabel, title=title)
# Ottieni i dati dell'immagine come base64 senza salvarla
img = BytesIO()
fig.savefig(img, format='png')
img.seek(0)
img_base64 = base64.b64encode(img.getvalue()).decode()
plt.close(fig)
return img_base64
def pie_chart(self, title):
fig, ax = plt.subplots()
ax.pie(self.sequence.value_counts(), labels=self.sequence.value_counts().index, autopct='%1.1f%%')
ax.set(title=title)
# Ottieni i dati dell'immagine come base64 senza salvarla
img = BytesIO()
fig.savefig(img, format='png')
img.seek(0)
img_base64 = base64.b64encode(img.getvalue()).decode()
plt.close(fig)
return img_base64
class mRNA(Sequence):
codon = {'UUU': 'F', 'UUC': 'F', 'UUA': 'L', 'UUG': 'L', 'CUU': 'L',
'CUC': 'L', 'CUA': 'L', 'CUG': 'L', 'AUU': 'I', 'AUC': 'I',
'AUA': 'I', 'AUG': 'M', 'GUU': 'V', 'GUC': 'V', 'GUA': 'V',
'GUG': 'V', 'UCU': 'S', 'UCC': 'S', 'UCA': 'S', 'UCG': 'S',
'CCU': 'P', 'CCC': 'P', 'CCA': 'P', 'CCG': 'P', 'ACU': 'T',
'ACC': 'T', 'ACA': 'T', 'ACG': 'T', 'GCU': 'A', 'GCC': 'A',
'GCA': 'A', 'GCG': 'A', 'UAU': 'Y', 'UAC': 'Y', 'CAU': 'H',
'CAC': 'H', 'CAA': 'Q', 'CAG': 'Q', 'AAU': 'N', 'AAC': 'N',
'AAA': 'K', 'AAG': 'K', 'GAU': 'D', 'GAC': 'D', 'GAA': 'E',
'GAG': 'E', 'UGU': 'C', 'UGC': 'C', 'UGG': 'W', 'CGU': 'R',
'CGC': 'R', 'CGA': 'R', 'CGG': 'R', 'AGU': 'S', 'AGC': 'S',
'AGA': 'R', 'AGG': 'R', 'GGU': 'G', 'GGC': 'G', 'GGA': 'G',
'GGG': 'G'}
def __init__(self, sequence):
super().__init__(sequence)
@staticmethod
def convert_codon(codon: Series) -> str:
out = ""
for base in codon:
out += base
return out
def find_ORFs(self):
start_codon = ['A', 'U', 'G']
stop_codons = ["UAA", "UGA", "UAG"]
orfs = []
seq_array = self.sequence.values.tolist()
i = 0
while i < len(seq_array):
if seq_array[i:i+3] == start_codon:
orf = ['AUG']
i += 3
codon = self.convert_codon(seq_array[i:i + 3])
valid_codon = True
while codon not in stop_codons:
orf.append(codon)
i += 3
codon = self.convert_codon(seq_array[i:i + 3])
if i > len(seq_array):
valid_codon = False
break
if valid_codon:
orf.append(codon)
orfs.append(orf)
i += 1
return orfs
def translation(self):
aa = []
ORFs = self.find_ORFs()
for i in range(len(ORFs)):
prov = []
for j in ORFs[i]:
if j in self.codon.keys():
prov.append(self.codon[j])
aa.append(prov)
return aa
#TODO: subclass one o the other
class Protein(Sequence):
list_protein = []
count = 0
def __init__(self, sequence):
super().__init__(sequence)
Protein.count += 1
name = "Protein " + str(Protein.count)
self.name = name
self.list_protein.append(self)
def get_sequence(self):
return ''.join(self.sequence)
def get_name(self):
return self.name
@staticmethod
def empty_queue():
for protein in Protein.list_protein:
del protein
Protein.count = 0
Protein.list_protein = []
@staticmethod
def get_list_protein():
return Protein.list_protein
@staticmethod
def show_proteins(ascending=False):
return sorted(Protein.list_protein, key=len, reverse=ascending)
class OligoPeptide(Sequence):
list_oligo = []
count = 0
def __init__(self, sequence):
super().__init__(sequence)
OligoPeptide.count += 1
name = "OligoPeptide " + str(OligoPeptide.count)
self.name = name
self.list_oligo.append(self)
def get_sequence(self):
return ''.join(self.sequence)
def get_name(self):
return self.name
@staticmethod
def empty_queue():
for oligo in OligoPeptide.list_oligo:
del oligo
OligoPeptide.count = 0
OligoPeptide.list_oligo = []
@staticmethod
def get_list_oligo():
return OligoPeptide.list_oligo
@staticmethod
def show_oligos(ascending=False):
return sorted(OligoPeptide.list_oligo, key=len, reverse=ascending)