-
Notifications
You must be signed in to change notification settings - Fork 0
/
myProject.py
498 lines (391 loc) · 17.9 KB
/
myProject.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import statistics
from typing import *
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib_venn import venn2
class ORF:
"""Open Reading Frame defined by start and stop nucleotide"""
def __init__(self, start: int, stop: int, frame: int, protein: str = "xxx", product: str = "xxx",
name: str = "unknown"):
self.start = start
self.stop = stop
self.frame = frame
self.protein = protein
self.length = abs(self.stop - self.start)
self.product = product
self.name = name
def __len__(self):
return self.length
def __repr__(self):
return "{:2} :{:9}..{:<9}. {:7} --> {}".format(self.frame, self.start, self.stop, self.name, self.product)
@property
def comparable_attributes(self):
return self.start, self.stop, self.frame, self.protein
class GenBank:
"""GenBank data structure (More interesting than dictionaries)"""
def __init__(self, filename: str):
"""Parse a GenBank file
This function is written by Kévin Merchadou.
Args:
filename: .gb file to parse
Returns:
list of ORF (either as ORF or as dict)
"""
text = self.read_flat_file(filename)
list_lines = text.split('\n')
features = self.get_features(text)
for line in list_lines:
if line[:12] == "DEFINITION ":
self.description = line[12:]
elif line[:12] == "VERSION ":
self.id = line[12:]
elif line[:12] == "SOURCE ":
self.organism = line[12:]
elif line[:21] == " source ":
self.length = int(line.split('..')[1])
elif line[:31] == ' /mol_type=':
self.gbtype = line.split('"')[1]
if "DNA" in self.gbtype:
self.type = "dna"
elif "RNA" in self.gbtype:
self.type = "rna"
# Problème pour les protéines : pas de mol_type dans le cas de protéines
elif line[:34] == " /transl_table":
self.code_table_id = int(line.split("=")[-1])
break
self.genes = self.get_genes(features)
for i in range(len(list_lines) - 1, -1, -1):
if list_lines[i][:6] == "ORIGIN":
self.data = ''.join([''.join(line.split(' ')[-6:]) for line in list_lines[i + 1: -3]])
break
elif i == 0:
self.data = "xxx"
def show_genes(self):
for orf in self.genes:
print(orf)
@staticmethod
def read_flat_file(filename: str) -> str:
"""Load a file in memory by returning a string
This function is written by Théo Gauvrit.
Args:
filename: file to open
Returns:
string of the whole file (with \n)
"""
with open(filename, 'r') as file:
return file.read()
@staticmethod
def get_features(txt: str) -> str:
"""Extract features lines from flat text and return them
This function is written by Kévin Merchadou.
Args:
txt: flat text with features to extract
Returns:
string of features
"""
ls = txt.split('\n')
for i in range(len(ls)):
if ls[i][:8] == "FEATURES":
return '\n'.join([x[5:] for x in ls[i + 1:]])
def get_genes(self, features: str) -> [ORF]:
"""Extract gene and CDS data from their section inside features table
This function is written by Kévin Merchadou.
Args:
features: text with gene to extract
Returns:
list of ORF (either as ORF or as dict)
"""
ls = features.split('\n')
cds_begins, cds_begins, cds_ends, cds_list, orf_list = [], [], [], [], []
cds = False
for i in range(len(ls)): # Récupération des positions des différentes entrées CDS
classe = ls[i][:3]
if classe == "CDS":
cds = True
cds_begins.append(i)
elif classe != " " and cds:
cds = False
cds_ends.append(i)
for i in range(len(cds_begins)): # Récupération des CDS depuis leurs positions dans le fichier
cds_list.append(ls[cds_begins[i]: cds_ends[i]])
for cds in cds_list: # Création de la liste d'ORF à partir de la liste de CDS du fichier gb
if cds[0][16:26] == "complement":
pos = cds[0][27:].split('..')
start = int(pos[0])
stop = int(pos[1].strip(')'))
frame = -((self.length - start) % 3 + 1)
else:
pos = [int(x) for x in cds[0][16:].split('..')]
start = int(pos[0])
stop = int(pos[1])
frame = (start % 3) + 1
name = "unknown"
protein = "xxx"
product = "xxx"
for i in range(1, len(cds)):
line = cds[i][16:]
if "/gene" == line[:5]:
name = ''.join([x[16:] for x in cds[i:]]).split('"')[1]
elif "/product" == line[:8]:
product = ''.join([x[16:] for x in cds[i:]]).split('"')[1]
elif "/translation" == line[:12]:
protein = ''.join([x[16:] for x in cds[i:]]).split('"')[1]
break
orf_list.append(ORF(start, stop, frame, protein, product, name))
return orf_list
@staticmethod
def read(filename):
"""Parse a GenBank file
This function is written by Kévin Merchadou.
Args:
filename: .gb file to parse
Returns:
list of ORF (either as ORF or as dict)
"""
return GenBank(filename)
def __repr__(self):
return "{} : {} of {}".format(self.id, self.gbtype, self.organism)
# -------- FUNCTIONS --------- #
def read_fasta(filename: str) -> str:
"""Parse a simple FASTA file (only one sequence)
This function is written by Eliot Ragueneau.
Args:
filename: .fasta file to parse
Returns:
sequence contained in the fasta
"""
dna = ""
with open(filename, 'r') as fasta:
for fasta_line in fasta:
if fasta_line[0] != ">":
dna += fasta_line.strip()
return dna
def get_genetic_code(ncbi_id: int) -> Tuple[dict, dict]:
"""Give the initiation codons and the translation table by its id
This function is written by Eliot Ragueneau.
Args:
ncbi_id: ncbi translation table identifier
Returns:
transl_table: translating dictionary
start_table: list of start codons
"""
with open("Translation_tables/{}.txt".format(ncbi_id), "r") as file:
lines = [line.strip() for line in file.readlines()]
transl_table = {lines[2][i] + lines[3][i] + lines[4][i]: lines[0][i]
for i in range(len(lines[0]))}
start_table = {lines[2][i] + lines[3][i] + lines[4][i]: lines[1][i]
for i in range(len(lines[0])) if lines[1][i] == "M"}
return transl_table, start_table
def reversed_complement(dna_seq: str):
"""Return the reversed complement of the given DNA sequence
This function is written by Théo Gauvrit.
Args:
dna_seq: DNA sequence to be reversed.
Returns:
reversed complement DNA sequence
"""
complement_dict = {"A": "T", "T": "A", "C": "G", "G": "C"}
return "".join([complement_dict[i] for i in dna_seq[::-1]])
def find_orf_all(seq: str, threshold: int, code_table_id: int) -> [ORF]:
"""Give a list of all ORF in the sequence if they are grater than the threshold
This function is written by Théo Gauvrit.
Args:
seq: Sequence to analyse
threshold: Minimum size of the ORF in the list
code_table_id: NCBI identifier of the translation table used on this sequence
Returns:
list of ORF
"""
transl_table, start_table = get_genetic_code(code_table_id)
length = len(seq)
strands = {1: seq, -1: reversed_complement(seq)}
orf_list = []
for strand in strands:
inits = []
for i in range(length):
if strands[strand][i:i + 3] in start_table:
inits.append(i)
for init in inits:
prot = "M"
for i in range(init + 3, length, 3):
codon = strands[strand][i: i + 3]
if len(codon) == 3:
aa = transl_table[codon]
if aa == "*":
if i - init >= threshold:
if strand > 0:
init += 1
orf_list.append(ORF(start=init,
stop=i + 3,
frame=init % 3 + 1,
protein=prot))
else:
orf_list.append(ORF(start=length - (i + 2),
stop=length - init,
frame=-1 * ((init - 1) % 3 + 1),
protein=prot))
break
else:
break
prot += aa
return orf_list
def find_orf_max(seq: str, threshold: int, code_table_id: int) -> [ORF]:
"""Give a list of all ORF in the sequence if they are grater than the threshold.
NCBI version so only gives biggest ORFs.
This function is written by Théo Gauvrit.
Args:
seq: Sequence to analyse
threshold: Minimum size of the ORF in the list
code_table_id: NCBI identifier of the translation table used on this sequence
Returns:
list of ORF
"""
transl_table, start_table = get_genetic_code(code_table_id)
length = len(seq)
strands = {1: seq, -1: reversed_complement(seq)}
orf_list = []
for strand in strands:
inits = []
for i in range(length):
if strands[strand][i:i + 3] in start_table:
inits.append(i)
list_stop = []
for init in inits:
prot = "M"
for i in range(init + 3, length, 3):
codon = strands[strand][i: i + 3]
if len(codon) == 3:
aa = transl_table[codon]
if aa == "*":
if i - init >= threshold and i not in list_stop:
list_stop.append(i)
if strand > 0:
init += 1
orf_list.append(ORF(start=init,
stop=i + 3,
frame=init % 3 + 1,
protein=prot))
else:
orf_list.append(ORF(start=length - (i + 2),
stop=length - init,
frame=-1 * ((init - 1) % 3 + 1),
protein=prot))
break
else:
break
prot += aa
return orf_list
def get_lengths(orf_list: list) -> [int]:
"""Give the list of orf lengths from a list of orf
This function is written by Mélissa Sadouki.
Args:
orf_list: list of ORF.
Returns:
list of lengths of ORF
"""
return [len(orf) for orf in orf_list]
def get_longest_orf(orf_list: List[ORF]) -> ORF:
"""Give the longest orf from a list of orf
This function is written by Mélissa Sadouki.
Args:
orf_list: list of ORF.
Returns:
longest ORF
"""
return max(orf_list, key=lambda orf: len(orf))
def get_top_longest_orf(orf_list: List[ORF], value: float) -> [ORF]:
"""Return the value% top longest orfs from a list of orf
This function is written by Mélissa Sadouki.
Args:
orf_list: list of ORF.
value: 0 > float > 1 : % of the top longest orf to show
Returns:
list of top ORF
"""
orf_list.sort(key=lambda orf: len(orf))
return orf_list[int(- value * len(orf_list)) - 1:]
def compare(orf_list_1: Iterable[ORF], orf_list_2: Iterable[ORF]) -> {ORF}:
"""Compare two iterables containing ORFs.
Two ORFs are considered as identical if they have the same frame,
the same starting position, the same stop position, and the same protein sequence produced.
This function is written by Eliot Ragueneau.
Args:
orf_list_1: list or any iterable containing ORF objects
orf_list_2: list or any iterable containing ORF objects
Returns:
intersections of the two sets"""
orf_set_1 = {orf.comparable_attributes for orf in orf_list_1}
orf_set_2 = {orf.comparable_attributes for orf in orf_list_2}
return [ORF(*orf) for orf in orf_set_1.intersection(orf_set_2)]
def read_csv(filename: str, separator: str = ";") -> [dict]:
"""Read a csv file delimited by separator
This function is written by Eliot Ragueneau.
Args:
filename: .csv file to read
separator: separator between data in the csv file
Returns:
list of dictionary of features:
[
{colonne 1: element 1, colonne 2: element 2}
{colonne 1: element 3, colonne 2: element 4}
{colonne 1: element 5, colonne 2: element 6}
{colonne 1: element 7, colonne 2: element 8}
]
"""
with open(filename, 'r') as file:
list_lines = [line.strip() for line in file.readlines()]
keys = list_lines[0].split(separator)
return [{key: element for key in keys for element in line.split(separator)} for line in list_lines[1:]]
def write_csv(filename: str, data: list, separator: str = ";"):
"""Write a csv file delimited by separator from a list of dictionary
This function is written by Eliot Ragueneau.
Args:
filename: .csv file to read
data: list of dict where each dict is a line in the product file,
and their keys the first row of the file
separator: separator between data in the csv file
Returns:
None
"""
if isinstance(data[0], ORF):
data = [orf.as_dict() for orf in data]
filename += ".csv" if filename[-4:] != ".csv" else ""
with open(filename, "w") as file:
file.write("{}\n".format(separator.join(data[0]))) # Write only the keys
for dictionary in data:
file.write("{}\n".format(separator.join([str(dictionary[key]) for key in dictionary])))
if __name__ == '__main__':
influenza = GenBank("sequence.gb")
genes_lengths = get_lengths(influenza.genes)
sns.kdeplot(get_lengths(influenza.genes), shade=True, cut=0,
label="min : {}\nmax : {}\nmean : {:.2f}\nmedian : {}".format(min(genes_lengths),
max(genes_lengths),
statistics.mean(genes_lengths),
statistics.median(genes_lengths)))
plt.title("Distribution des tailles des CDS de CP007470.1")
plt.xlim(0, 5000)
plt.xlabel("Tailles des ORFs (pb)")
frame1 = plt.gca()
frame1.axes.get_yaxis().set_ticks([])
plt.savefig("distribCDS")
plt.show()
influenza_gene_set = {orf.comparable_attributes for orf in influenza.genes}
length_genes = len(influenza_gene_set)
genome = read_fasta("influenza.fasta")
for threshold in (0, 90, 210, 300, 420)[::-1]:
list_orf = find_orf_all(genome, threshold, 11)
set_orf = {orf.comparable_attributes for orf in list_orf}
length_intersection = len(set_orf.intersection(influenza_gene_set))
venn2(subsets=(len(list_orf) - length_intersection, length_genes - length_intersection, length_intersection),
set_labels=('ORFs trouvés', 'CDS GenBank'))
plt.title("Diagramme de Venn des résultats de l'algorithme \"ALL\"\n avec un seuil de {}".format(threshold))
plt.savefig("venn_{}_all".format(threshold))
plt.show()
list_orf = find_orf_max(genome, threshold, 11)
set_orf = {orf.comparable_attributes for orf in list_orf}
length_intersection = len(set_orf.intersection(influenza_gene_set))
venn2(subsets=(len(list_orf) - length_intersection, length_genes - length_intersection, length_intersection),
set_labels=('ORFs trouvés', 'CDS GenBank'))
plt.title("Diagramme de Venn des résultats de l'algorithme \"MAX\"\n avec un seuil de {}".format(threshold))
plt.savefig("venn_{}_max".format(threshold))
plt.show()