-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNP.py
494 lines (396 loc) · 19 KB
/
SNP.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
"""
Created on Thu Jun 23 21:00:00 2022
@author: Hugo Leblond - Master 2 AVR / Telecom Nancy / Université de Lorraine
@contact: hugo.leblond@telecomnancy.eu - hugo.leblond7@gmail.com
"""
################################## Main goal ##################################
# The purpose of these functions is to identify the parental origin of chromosomal
# regions in a recombinant. The algorithm first assigns each SNP to one of the
# two parents (P1 or P2). It then defines regions assigned to P1 or P2 by
# applying a threshold of minimum number of SNPs (typically 2), then calculates
# their size relative to the parental genomes.
###############################################################################
import pandas as pd
import xlsxwriter
from datetime import datetime
def SNP_parent(nom_fichier_excel):
"""
Assign each SNP to a parent with its position in P1, P2 and R.
Parameters
----------
nom_fichier_excel : String,
SNP file name from MAUVE converted to Excel format.
Returns
-------
liste_appartenance_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
liste_position_SNP_P1 : list,
List of position of each SNP related to the P1 genome.
liste_position_SNP_P2 : list,
List of position of each SNP related to the P2 genome.
liste_position_SNP_R : list,
List of position of each SNP related to the Recombinant consensus genome (see material and methods in the related article).
"""
mon_fichier = pd.read_excel(nom_fichier_excel)
SNP_pattern = list(mon_fichier['SNP pattern'])
positions_SNP_P1 = list(mon_fichier['sequence_1_PosInContg'])
positions_SNP_P2 = list(mon_fichier['sequence_2_PosInContg'])
positions_SNP_R = list(mon_fichier['sequence_3_PosInContg'])
liste_appartenance_SNP = []
liste_position_SNP_P1 = []
liste_position_SNP_P2 = []
liste_position_SNP_R = []
compt = 0
liste_SNP_pattern = []
for snp_pattern in SNP_pattern :
liste_SNP_pattern.append(str(snp_pattern))
for snp in liste_SNP_pattern :
if snp[2].lower() == 'n' or snp[1].lower() == 'n' or snp[0].lower() == 'n' :
compt+=1
elif snp[2].lower() == snp[0].lower():
if positions_SNP_P1[compt] != 0 and positions_SNP_P2[compt] != 0 :
liste_appartenance_SNP.append("P1")
liste_position_SNP_P1.append(positions_SNP_P1[compt])
liste_position_SNP_P2.append(positions_SNP_P2[compt])
liste_position_SNP_R.append(positions_SNP_R[compt])
compt+=1
elif snp[2].lower() == snp[1].lower() :
if positions_SNP_P1[compt] != 0 and positions_SNP_P2[compt] != 0 :
liste_appartenance_SNP.append("P2")
liste_position_SNP_P1.append(positions_SNP_P1[compt])
liste_position_SNP_P2.append(positions_SNP_P2[compt])
liste_position_SNP_R.append(positions_SNP_R[compt])
compt+=1
else :
compt+=1
return liste_appartenance_SNP, liste_position_SNP_P1, liste_position_SNP_P2, liste_position_SNP_R
def SNP_chaine(liste_appartenance_SNP, base=1, nombre_suite_P1=2, nombre_suite_P2=2):
"""
Define regions of identical successive SNP labels.
Parameters
----------
liste_appartenance_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
base : int, by default the receptor genome (1 or 2 according to P1 and P2 parents), automatically selected in the main file.
nombre_suite_P1 : int, optional
Minimum number of successive SNP belonging to the same parent P1. The default is 2.
nombre_suite_P2 : int, optional
Minimum number of successive SNP belonging to the same parent P2. The default is 2.
Returns
-------
chaine_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format. Filter positions under the threshold defined by nombre_suite_P1 and nombre_suite_P2.
"""
chaine_SNP = []
init_boucle = 0
compt_suite = 0
switch1 = "P1"
switch2 = "P2"
switch = ""
base = "P"+str(base)
if base == "P1":
switch = switch2
else :
switch = switch1
while init_boucle <= len(liste_appartenance_SNP)-1 :
if switch == switch1 :
if liste_appartenance_SNP[init_boucle] == switch2 :
compt_suite += 1
chaine_SNP.append(1)
init_boucle+=1
elif liste_appartenance_SNP[init_boucle]==switch1 :
compt_suite = 0
chaine_SNP.append(1)
init_boucle+=1
if compt_suite == nombre_suite_P2 :
for i in range(nombre_suite_P2):
chaine_SNP[init_boucle - i - 1]=2
compt_suite = 0
switch = switch2
else :
if liste_appartenance_SNP[init_boucle] == switch1 :
compt_suite += 1
chaine_SNP.append(2)
init_boucle+=1
elif liste_appartenance_SNP[init_boucle]==switch2 :
compt_suite = 0
chaine_SNP.append(2)
init_boucle+=1
if compt_suite == nombre_suite_P1 :
for i in range(nombre_suite_P1):
chaine_SNP[init_boucle - i - 1]=1
compt_suite = 0
switch = switch1
return chaine_SNP
def compteur_snp(chaine_snp):
"""
Counts the number of SNPs in each segment.
Parameters:
- chaine_snp (list): List representing the SNP sequence.
Returns:
- liste_taille_snp (list): List containing the number of SNPs in each segment.
"""
compt_p1 = 0
compt_p2 = 0
liste_taille_snp = []
for i in range(len(chaine_snp)-1):
if i+1 == len(chaine_snp)-1:
if compt_p1 != 0:
compt_p1 += 1
liste_taille_snp.append(compt_p1)
else:
compt_p2 += 1
liste_taille_snp.append(compt_p2)
elif chaine_snp[i] == 2 and chaine_snp[i+1] == 1:
compt_p2 += 1
liste_taille_snp.append(compt_p2)
compt_p2 = 0
elif chaine_snp[i] == 1 and chaine_snp[i+1] == 2:
compt_p1+=1
liste_taille_snp.append(compt_p1)
compt_p1 = 0
elif chaine_snp[i] == 1:
compt_p1 += 1
elif chaine_snp[i] == 2:
compt_p2 += 1
return liste_taille_snp
#Convertit les chaines du type [P1,P1,P1 ... P2,P2,P2... P1,P1,P1] avec leur position en segments (ex : P1, debutP1,finP1)
def chaine_borne(chaine_SNP, liste_P1, liste_P2, liste_R, donneur=2):
"""
Take a SNP list and lists of position of each SNP related to the P1, P2 and R genome.
Return lists of positions (start/end) of stretches of SNP related to P1, P2 and R.
Parameters
----------
chaine_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
liste_P1 : list,
List of position of each SNP related to the P1 genome.
liste_P2 : list,
List of position of each SNP related to the P2 genome.
liste_R : list,
List of position of each SNP related to the R genome.
donneur : int, optional
by default the donor genome (1 or 2 according to P1 and P2 parents), automatically selected in the main file.
Returns
-------
chaine_borne_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
chaine_borne_P1_start : list,
List of start positions of SNP stretches relative to P1.
chaine_borne_P2_start : list,
List of start positions of SNP stretches relative to P2.
chaine_borne_R_start : list,
List of start positions of SNP stretches relative to R.
chaine_borne_P1_end : list,
List of end positions of SNP stretches relative to P1.
chaine_borne_P2_end : list,
List of end positions of SNP stretches relative to P2.
chaine_borne_R_end : list,
List of end positions of SNP stretches relative to R.
"""
chaine_borne_SNP = []
chaine_borne_P1_start = []
chaine_borne_P2_start = []
chaine_borne_R_start = []
chaine_borne_P1_end = []
chaine_borne_P2_end = []
chaine_borne_R_end = []
init_boucle = 0
while init_boucle <= len(chaine_SNP)-1 :
if init_boucle == 0 :
chaine_borne_SNP.append(chaine_SNP[init_boucle])
chaine_borne_P1_start.append(liste_P1[init_boucle])
chaine_borne_P2_start.append(liste_P2[init_boucle])
chaine_borne_R_start.append(liste_R[init_boucle])
init_boucle+=1
elif init_boucle == len(chaine_SNP)-1 :
chaine_borne_P1_end.append(liste_P1[init_boucle])
chaine_borne_P2_end.append(liste_P2[init_boucle])
chaine_borne_R_end.append(liste_R[init_boucle])
init_boucle+=1
elif chaine_SNP[init_boucle]!=chaine_SNP[init_boucle+1] and init_boucle+1 != len(chaine_SNP)-1:
if chaine_SNP[init_boucle] == donneur :
chaine_borne_P1_end.append(liste_P1[init_boucle])
chaine_borne_P2_end.append(liste_P2[init_boucle])
chaine_borne_R_end.append(liste_R[init_boucle])
chaine_borne_SNP.append(chaine_SNP[init_boucle+1])
chaine_borne_P1_start.append(liste_P1[init_boucle]+1)
chaine_borne_P2_start.append(liste_P2[init_boucle]+1)
chaine_borne_R_start.append(liste_R[init_boucle]+1)
else :
chaine_borne_P1_end.append(liste_P1[init_boucle+1]-1)
chaine_borne_P2_end.append(liste_P2[init_boucle+1]-1)
chaine_borne_R_end.append(liste_R[init_boucle+1]-1)
chaine_borne_SNP.append(chaine_SNP[init_boucle+1])
chaine_borne_P1_start.append(liste_P1[init_boucle+1])
chaine_borne_P2_start.append(liste_P2[init_boucle+1])
chaine_borne_R_start.append(liste_R[init_boucle+1])
init_boucle+=1
else :
init_boucle+=1
return chaine_borne_SNP, chaine_borne_P1_start, chaine_borne_P2_start, chaine_borne_R_start, chaine_borne_P1_end, chaine_borne_P2_end, chaine_borne_R_end
def calcul_des_sommes_rapport_recombinant(chaine_borne_SNP, chaine_borne_Receptor_start, chaine_borne_Receptor_end, borne_sup =1000):
"""
Parameters
----------
chaine_borne_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
chaine_borne_Receptor_start : list,
DESCRIPTION.
chaine_borne_Receptor_end : list,
List of start positions of SNP stretches relative to the receptor (P1 or P2). Automatically selected in the main file.
borne_sup : int, optional
Compute the sum of regions of length exceeding the threshold. The default is 1000. Not displayed in the excel output.
Returns
-------
somme_P1_liste : list,
List containing the total length of P1 regions.
somme_P2_liste : list,
List containing the total length of P2 regions.
min_P1_P2_liste : String,
Label of the parent with the minimum total length.
somme_sup_liste : list,
DESCRIPTION.
compt : list,
List containing the number of fragments over the threshold length borne_sup. Not displayed in the output excel file.
compt_P1 : list,
List containing the number of fragments of label P1.
compt_P2 : list,
List containing the number of fragments of label P2.
"""
somme_P1 = 0
somme_P2 = 0
somme_sup = 0
compt_P1 = [0]
compt_P2 = [0]
compt = [0]
somme_P1_liste = []
somme_P2_liste = []
min_P1_P2_liste = []
somme_sup_liste = []
for i in range(len(chaine_borne_SNP)):
if chaine_borne_SNP[i] == 1 :
compt_P1[0] +=1
somme_P1 += chaine_borne_Receptor_end[i] - chaine_borne_Receptor_start[i]
elif chaine_borne_SNP[i] == 2 :
compt_P2[0] +=1
somme_P2 += chaine_borne_Receptor_end[i] - chaine_borne_Receptor_start[i]
somme_P1_liste.append(somme_P1)
somme_P2_liste.append(somme_P2)
# Partie pour le calcul des sommes bornées
if min(somme_P1, somme_P2) == somme_P1 :
min_P1_P2_liste.append("P1")
for i in range(len(chaine_borne_SNP)):
if chaine_borne_SNP[i] == 1 and chaine_borne_Receptor_end[i] - chaine_borne_Receptor_start[i] > borne_sup :
compt[0]+=1
somme_sup += chaine_borne_Receptor_end[i] - chaine_borne_Receptor_start[i]
elif min(somme_P1, somme_P2) == somme_P2 :
min_P1_P2_liste.append("P2")
for i in range(len(chaine_borne_SNP)):
if chaine_borne_SNP[i] == 2 and chaine_borne_Receptor_end[i] - chaine_borne_Receptor_start[i] > borne_sup :
somme_sup += chaine_borne_Receptor_end[i] - chaine_borne_Receptor_start[i]
compt[0]+=1
somme_sup_liste.append(somme_sup)
return somme_P1_liste, somme_P2_liste, min_P1_P2_liste, somme_sup_liste, compt, compt_P1, compt_P2
def calcul_des_sommes_rapport_donneur(chaine_borne_SNP, chaine_borne_donneur_start, chaine_borne_donneur_end):
"""
Parameters
----------
chaine_borne_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
chaine_borne_donneur_start : list,
List of start positions of SNP stretches relative to the donor (P1 or P2). Automatically selected in the main file.
chaine_borne_donneur_end : list,
List of end positions of SNP stretches relative to the donor (P1 or P2). Automatically selected in the main file.
Returns
-------
somme_P1_liste : list,
List containing the total length of P1 regions.
somme_P2_liste : list,
List containing the total length of P2 regions.
"""
somme_P1 = 0
somme_P2 = 0
somme_P1_liste = []
somme_P2_liste = []
for i in range(len(chaine_borne_SNP)):
if chaine_borne_SNP[i] == 1 :
somme_P1 += chaine_borne_donneur_end[i] - chaine_borne_donneur_start[i]
elif chaine_borne_SNP[i] == 2 :
somme_P2 += chaine_borne_donneur_end[i] - chaine_borne_donneur_start[i]
somme_P1_liste.append(somme_P1)
somme_P2_liste.append(somme_P2)
return somme_P1_liste, somme_P2_liste
def creer_excel(nom_fichier_entree, chaine_borne_SNP, liste_debut, liste_fin, liste_P1_debut, liste_P1_fin, liste_P2_debut, liste_P2_fin,chaine_borne_cote_donneur,liste_cote_donneur_start, liste_cote_donneur_end, somme_P1, somme_P2, min_P1_P2, compt_P1, compt_P2, somme_donneur_indices_donneur, liste_somme):
"""
Parameters
----------
nom_fichier_entree : String,
SNP file name from MAUVE converted to Excel format.
chaine_borne_SNP : list,
List containing the labels of the SNPs (P1 or P2) in string format.
liste_debut : list,
List of start positions of SNP stretches relative to R.
liste_fin : list,
List of end positions of SNP stretches relative to R.
liste_P1_debut : list,
List of start positions of SNP stretches relative to P1.
liste_P1_fin : list,
List of end positions of SNP stretches relative to P1.
liste_P2_debut : list,
List of start positions of SNP stretches relative to P2.
liste_P2_fin : list,
List of end positions of SNP stretches relative to P2.
chaine_borne_cote_donneur : list,
List containing the labels of the SNPs (P1 or P2) in string format sorted relative to the donor.
liste_cote_donneur_start : list,
List containing the start positions of the SNP regions relative to the donor.
liste_cote_donneur_end : list,
List containing the end positions of the SNP regions relative to the donor.
somme_P1 : list,
List containing the total length of P1 regions.
somme_P2 : list,
List containing the total length of P2 regions.
min_P1_P2 : String,
Label of the parent with the minimum total length.
compt_P1 : list,
List containing the number of fragments of label P1.
compt_P2 : list,
List containing the number of fragments of label P2.
somme_donneur_indices_donneur : list,
Total length of donor fragments relative to the donor sorted list.
Returns
-------
Excel file containing all the results.
"""
now = datetime.now()
nom_fichier = nom_fichier_entree[: (len(nom_fichier_entree) - 5)] +"_sortie" + now.strftime("_%d_%m_%Y %H_%M") + ".xlsx"
xlsxwriter.Workbook(nom_fichier)
donnee = pd.DataFrame({"Pattern" : chaine_borne_SNP,
"Start" : liste_debut,
"End" : liste_fin})
donnee_P1_P2_cote_recepteur = pd.DataFrame({"Pattern" : chaine_borne_SNP,
"Nombre de SNP" : liste_somme,
"Start P1" : liste_P1_debut,
"End P1" : liste_P1_fin,
"Start P2" : liste_P2_debut,
"End P2" : liste_P2_fin,
})
sommes_cote_recepteur = pd.DataFrame({
"somme P1" : somme_P1,
"somme P2" : somme_P2,
"min P1 P2" : min_P1_P2,
"nombre de fragments P1" : compt_P1,
"nombre de fragments P2" : compt_P2,})
donnee_P2_cote_donneur = pd.DataFrame({"Pattern" : chaine_borne_cote_donneur,
"Start P2" : liste_cote_donneur_start,
"End P2" : liste_cote_donneur_end,})
somme_cote_donneur = pd.DataFrame({
"Somme du donneur" : somme_donneur_indices_donneur,})
with pd.ExcelWriter(nom_fichier) as writer:
donnee.to_excel(writer, sheet_name='donnees R')
donnee_P1_P2_cote_recepteur.to_excel(writer, sheet_name="Cotes recepteur")
sommes_cote_recepteur.to_excel(writer, sheet_name="Somme recepteur")
donnee_P2_cote_donneur.to_excel(writer, sheet_name='Cotes donneur')
somme_cote_donneur.to_excel(writer, sheet_name='Somme donneur')
return 0