-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecsalmo.py
1260 lines (1207 loc) · 52 KB
/
recsalmo.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyrights 2023: Nuttachat Wisittipanit
import os
import itertools
import util
import time
from openpyxl import (Workbook, load_workbook, )
from openpyxl.utils import get_column_letter
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from Bio import Phylo
from Bio import SeqIO
from Bio import Align
from Bio.SeqRecord import SeqRecord
from Bio.Phylo.TreeConstruction import (DistanceMatrix, DistanceTreeConstructor, )
from const import (File, ResultKey, Record, )
from structobj import (RES_ALL, RecordRES, AssemblyAnnotation, STAnnotation, SeroAnnotation, AssemblyAnnXcel)
from main import WGSSal
from binfutil import PerformListAlignment
import warnings
warnings.filterwarnings("ignore")
class BatchRun:
def __init__(self, input_path, output_path, mainName=None):
# self.useInputFolder = File.inputFolder
# self.useOutputFolder = File.outputFolder
self.useInputFolder = input_path
self.useOutputFolder = output_path
self.mainName = mainName
self.fileCls = None # File ins
self.wgsFileList = []
self.numGenomes = 0
self.resultHash = {} # {'BK_SAL1': RES_ALL ins,'BK_SAL4': RES_ALL ins, ..}
self.genomeNameList = [] # list of genome names run in order
self.headLabels = Record.headLabels
self.recordTitle = Record.recordTitle
self.recordRESList = [] # RecordRES ins
self.spProfileList = [] # list of SpacerProfile ins
def setupFolderPath(self):
"""
Setup all folder paths for the program
:return:
"""
self.fileCls = File(self.useInputFolder, self.useOutputFolder, self.mainName)
self.fileCls.setupAllPaths()
def databaseInitialize(self):
"""
Initialize some databases
:return:
"""
# copy 'sptemp.fasta' from 'spacerall.fasta' if not already exists
util.checkCopySpacerFasta(self.fileCls)
self.makeSpacerDB()
self.makeSpacerTempDB()
def collectInfo(self):
"""
Collect information from all the input files
:return:
"""
self.wgsFileList = [f for f in os.listdir(self.useInputFolder) if
os.path.isfile(os.path.join(self.useInputFolder, f))]
self.wgsFileList.sort()
self.numGenomes = len(self.wgsFileList)
def run(self):
"""
Main run method
:return:
"""
# clear DBSC folder first
util.createFolderAndClear(self.fileCls.DBSC_Folder)
for i, wgsFile in enumerate(self.wgsFileList):
wSal = WGSSal(self.fileCls, wgsFile)
wSal.setupIO()
# create folder if not already existing
util.createFolderNotExist(wSal.outFolderPath)
# if wSal.outFolderName not in ('SAL_YC8093AA_AS','BK_SAL1', ):
# continue
self.makeBlastDB(wSal.inputFilePath, wSal.outFolderName)
wSal.runMLST()
wSal.runSISTR()
wSal.runAMRFinderPlus()
wSal.runSPIFinder()
wSal.runCrisprFinder()
self.genomeNameList.append((wSal.outFolderName, wSal.outFolderPath))
self.collectResult(wSal)
if i == 3:
break
# Perform Spacer Profile Alignment
finalMatrix = self.performSpacerProfileAlignment()
if len(finalMatrix) != 0:
self.constructPhylogeneticTreeSpacerProfile(finalMatrix)
self.removeTempSpacerDB()
# Record all results in the excel file
self.recordResult()
# Read from the record
self.readFromRecord()
# Construct pie charts
self.constructPieChartEach('serovar')
# self.constructPieChartEach('identification')
self.constructPieChartEach('call_ST')
# Construct ParSNP tree
self.constructSNPTree()
# Remove all folders inside /DBSC/
util.createFolderAndClear(self.fileCls.DBSC_Folder)
def run_CRISPR(self):
"""
Main run method
:return:
"""
# clear DBSC folder first
util.createFolderAndClear(self.fileCls.DBSC_Folder)
for i, wgsFile in enumerate(self.wgsFileList):
wSal = WGSSal(self.fileCls, wgsFile)
wSal.setupIO()
# Test only
# if wSal.outFolderName not in ('SAL_BA3995AA_AS', ):
# continue
# if wSal.outFolderName not in ('SAL_HC6463AA_AS', ):
# continue
# if wSal.outFolderName not in ('SAL_EA2914AA_AS', ):
# continue
# if wSal.outFolderName not in ('SAL_KA9071AA_AS', ):
# continue
# if wSal.outFolderName not in ('SAL_HC4765AA_AS', 'SAL_HC4766AA_AS', 'SAL_FA1054AA_AS', ):
# continue
# if wSal.outFolderName not in ('SAL_VC9662AA_AS', ):
# continue
# if wSal.outFolderName not in ('SAL_KA9071AA_AS', 'SAL_LA0087AA_AS','SAL_QC3063AA_AS'):
# continue
# if wSal.outFolderName not in ('SAL_BA3995AA_AS', 'SAL_BA
# 804AA_AS', 'SAL_HC6481AA_AS','SAL_IB8216AA_AS'):
# continue
# if wSal.outFolderName not in (
# 'SAL_BA3995AA_AS', 'SAL_BA5804AA_AS', 'SAL_HC6463AA_AS', 'SAL_HC6481AA_AS', 'SAL_IB8216AA_AS',
# 'SAL_EA2914AA_AS','SAL_KA9071AA_AS', 'SAL_LA0087AA_AS','SAL_QC3063AA_AS'):
# continue
# if wSal.outFolderName not in ('SAL_BA3995AA_AS', 'SAL_BA5804AA_AS', 'SAL_EA2914AA_AS'):
# continue
# if wSal.outFolderName not in ('SAL_BA3995AA_AS','SAL_BA5804AA_AS','SAL_HC6463AA_AS','SAL_HC6481AA_AS'):
# continue
# make folder right here
util.createFolderAndClear(wSal.outFolderPath)
# make DB in the "/maindb/DBSC/BK_SAL1_DB"
self.makeBlastDB(wSal.inputFilePath, wSal.outFolderName)
# call runCrisprFinder method
wSal.runCrisprFinder()
self.genomeNameList.append((wSal.outFolderName, wSal.outFolderPath))
self.collectResult_CRISPR(wSal)
# if i == 50:
# break
# # Perform Spacer Profile Alignment
# finalMatrix = self.performSpacerProfileAlignment()
# if len(finalMatrix) != 0:
# self.constructPhylogeneticTreeSpacerProfile(finalMatrix)
self.removeTempSpacerDB()
# Record all results in the excel file
self.recordResult_CRISPR()
# Remove all folders inside /DBSC/
util.createFolderAndClear(self.fileCls.DBSC_Folder)
def aggregateST(self):
"""
We need to aggregate
:return:
"""
main_dir = '/home/nuttachat/Downloads/SalmoStrain'
xcelFilePath = os.path.join(main_dir, 'summary_THAICR.xlsx')
assemFilePath = os.path.join(main_dir, 'Thai_1.txt')
stFilePath = os.path.join(main_dir, 'ThaiST_1.txt')
seroFilePath = os.path.join(main_dir, 'Thai_SERO.txt')
assemList = util.read_file_tab(assemFilePath, headInclude=True)
stList = util.read_file_tab(stFilePath, headInclude=True)
seroList = util.read_file_tab(seroFilePath, headInclude=True)
# self.assemHash -> {Uberstrain: AssemblyAnnotation ins,..}
self.assemHash = {}
# self.assemHashX -> {Assembly Barcode: AssemblyAnnotation ins,..}
self.assemHashX = {}
# self.stHash -> {Uberstrain: STAnnotation ins,..}
self.stHash = {}
# self.seroHash -> {Uberstrain: STAnnotation ins,..}
self.seroHash = {}
for k in assemList:
# k -> ['SAL_AA0066AA','184756',..,'SAL_AA0066AA_AS'] ->
# [Uberstrain=0,Name=1,Serovar=18,..,Assembly Barcode=38]
uberStrain = k[0]
serovar = k[19]
assemBarcode = k[40]
assemAnn = AssemblyAnnotation(uberStrain, assemBarcode, serovar)
self.assemHash[uberStrain] = assemAnn
self.assemHashX[assemBarcode] = assemAnn
for k in stList:
# k -> ['SAL_AA0066AA','184756',..,'SAL_AA0066AA_AS'] ->
# [Uberstrain=0,..,ST=34]
uberStrain = k[0]
stIndex = k[33]
stAnn = STAnnotation(uberStrain, stIndex)
self.stHash[uberStrain] = stAnn
for k in seroList:
# k -> ['SAL_AA0066AA','184756',..,'SAL_AA0066AA_AS'] ->
# [Uberstrain=0,..,Sero='Typhi']
uberStrain = k[0]
serotype = k[32]
seroAnn = SeroAnnotation(uberStrain, serotype)
self.seroHash[uberStrain] = seroAnn
print('assemHash')
print(self.assemHash)
for key in self.assemHash:
print(self.assemHash[key].AssemblyBarcode)
print(self.assemHash[key].Serovar)
break
print('assemHashX')
print(self.assemHashX)
# for key in self.assemHashX:
# print(self.assemHashX[key].AssemblyBarcode)
# print(self.assemHashX[key].Serovar)
print('stHash')
print(self.stHash)
for key in self.stHash:
print(self.stHash[key].STIndex)
break
# Read xcelFilePath
wb = load_workbook(xcelFilePath)
ws = wb['summary']
rows = list(ws.rows)
headerOld = []
dataOld = []
crpLen = [] # [(C1 len,C2 len),..]
for i in range(len(rows)):
b = []
row = rows[i]
if i == 0:
for col in range(len(row)):
value = row[col].value
headerOld.append(value)
else:
for col in range(len(row)):
value = row[col].value
b.append(value)
dataOld.append(b)
# row[5] -> Pos-C1, row[6] -> Pos-C2
posC1 = str(row[5].value) # 3077831-3078162
posC2 = str(row[6].value) # 3077831-3078162
# find length
if posC1 != 'None-None':
# print(posC1.split('-'))
v1 = posC1.split('-')
v1 = list(filter(lambda x: x != '', v1))
startC1 = int(v1[0])
stopC1 = int(v1[1])
lenC1 = abs(startC1 - stopC1) + 1
else:
lenC1 = 'None'
if posC2 != 'None-None':
v2 = posC2.split('-')
v2 = list(filter(lambda x: x != '', v2))
startC2 = int(v2[0])
stopC2 = int(v2[1])
lenC2 = abs(startC2 - stopC2) + 1
else:
lenC2 = 'None'
crpLen.append((lenC1, lenC2))
# print('headerOld')
# print(headerOld)
# print('dataOld')
# print(dataOld[0])
headerNew = headerOld + ['Serovar', 'STIndex', 'Serotype', 'LenC1(bp)', 'LenC2(bp)']
dataNew = []
for i, a in enumerate(dataOld):
# a -> ['a','b','c','d',..]
# barcode -> 'SAL_AA0066AA_AS'
barcode = a[0]
# gg = []
serovar = self.assemHashX[barcode].Serovar
uber = self.assemHashX[barcode].uberStrain
st = self.stHash[uber].STIndex
serotype = self.seroHash[uber].serotype
lenC1 = crpLen[i][0]
lenC2 = crpLen[i][1]
gg = a + [serovar, st, serotype, lenC1, lenC2]
dataNew.append(gg)
print(dataNew[0])
# CREATE NEW WORKBOOK
wb = Workbook()
ws_sum = wb.active
ws_sum.title = 'main'
ws_sum.column_dimensions[get_column_letter(1)].width = 15
ws_sum.column_dimensions[get_column_letter(2)].width = 30
ws_sum.column_dimensions[get_column_letter(3)].width = 25
currentRow = 1
for i, label in enumerate(headerNew):
ws_sum.cell(row=currentRow, column=i + 1, value=label)
# currentRow = 2
for i, data in enumerate(dataNew):
# data -> [a,b,c,d]
for j, a in enumerate(data):
ws_sum.cell(row=i + 2, column=j + 1, value=a)
newFilePath = os.path.join(main_dir, 'summary_THAICR_NEWX.xlsx')
wb.save(newFilePath)
def crisprPrimerAlignment(self):
"""
We need to aggregate
:return:
"""
main_dir = '/home/aonlazio/Python/Project/WGSCP'
inputFolderPath = os.path.join(main_dir, 'input')
outputFolderPath = os.path.join(main_dir, 'output')
resFolderPath = os.path.join(main_dir, 'res')
xceloutFolderPath = os.path.join(resFolderPath, 'xcelout')
xcelFilePath = os.path.join(xceloutFolderPath, 'summary_THAICR_NEWX.xlsx')
primerFilePath = os.path.join(resFolderPath, 'primer.fasta')
# read primer
for seqRecord in SeqIO.parse(primerFilePath, "fasta"):
if seqRecord.id == 'forward_C1':
primerForward_C1 = seqRecord
elif seqRecord.id == 'reverse_C1':
primerReverse_C1 = seqRecord
elif seqRecord.id == 'forward_C2':
primerForward_C2 = seqRecord
elif seqRecord.id == 'reverse_C2':
primerReverse_C2 = seqRecord
# Read xcelFilePath
wb = load_workbook(xcelFilePath)
ws = wb['main']
rows = list(ws.rows)
headerOld = []
dataOld = []
for i in range(len(rows)):
b = []
row = rows[i]
if i == 0:
for col in range(len(row)):
value = row[col].value
headerOld.append(value)
else:
for col in range(len(row)):
value = row[col].value
b.append(value)
dataOld.append(b)
# assemHash -> {AssemblyName: AssemblyAnnotation ins,..}
assemHash = {}
for i, a in enumerate(dataOld):
# a -> ['a','b','c','d',..]
# assemblyName -> 'SAL_AA0066AA_AS'
name = a[0]
spacerC1 = a[1]
spacerC2 = a[2]
nodeC1 = a[9]
nodeC2 = a[10]
assemHash[name] = AssemblyAnnXcel(name)
assemHash[name].spacerC1 = spacerC1
assemHash[name].spacerC2 = spacerC2
assemHash[name].nodeC1 = nodeC1
assemHash[name].nodeC2 = nodeC2
# print(assemHash)
# for a in assemHash:
# print(assemHash[a].AssemblyName)
# print(assemHash[a].spacerC1)
# print(assemHash[a].spacerC2)
# print(assemHash[a].nodeC1)
# print(assemHash[a].nodeC2)
aligner = Align.PairwiseAligner()
# aligner.substitution_matrix = substitution_matrices.load("BLOSUM62")
aligner.mode = 'local'
aligner.match_score = 2
aligner.mismatch_score = -1
aligner.open_gap_score = -3
aligner.extend_gap_score = -0.5
# iterate files over input folder
wgsFileList = [f for f in os.listdir(inputFolderPath) if
os.path.isfile(os.path.join(inputFolderPath, f))]
# get node in FASTA in SeqRecord ins
# print(wgsFileList)
for wgsFile in wgsFileList:
# wgsFile -> SAL_BA4239AA_AS.scaffold.fasta
# print(wgsFile)
assemName = wgsFile.split('.')[0]
wgsFilePath = os.path.join(inputFolderPath, wgsFile)
# print('assemName: ', assemName)
# print('wgsFilePath: ', wgsFilePath)
# print('nodeC1: ', assemHash[assemName].nodeC1)
# print('nodeC2: ', assemHash[assemName].nodeC2)
nodeC1rec = None # SeqRecord ins
nodeC2rec = None # SeqRecord ins
try:
# get SeqRecord from nodeC1
for seqRecord in SeqIO.parse(wgsFilePath, "fasta"):
if seqRecord.id == assemHash[assemName].nodeC1:
nodeC1rec = seqRecord
break
# get SeqRecord from nodeC2
for seqRecord in SeqIO.parse(wgsFilePath, "fasta"):
if seqRecord.id == assemHash[assemName].nodeC2:
nodeC2rec = seqRecord
break
except:
continue
# print('primerForward_C1: ', primerForward_C1.seq)
# print('primerReverse_C1: ', primerReverse_C1.seq)
# print('primerForward_C2: ', primerForward_C2.seq)
# print('primerReverse_C2: ', primerReverse_C2.seq)
# print('nodeC1rec: ', nodeC1rec.seq)
# print('nodeC2rec: ', nodeC2rec.seq)
# print('len(nodeC1rec.seq): ', len(nodeC1rec.seq))
# print('len(nodeC2rec.seq): ', len(nodeC2rec.seq))
try:
nodeC1LEN = len(nodeC1rec.seq)
nodeC2LEN = len(nodeC2rec.seq)
except:
continue
A_CRISPR1_L, A_CRISPR2_L = self.determineCrisprLength(nodeC1rec.seq,
nodeC1rec.seq.reverse_complement(),
primerForward_C1.seq,
primerReverse_C1.seq,
nodeC2rec.seq,
nodeC2rec.seq.reverse_complement(),
primerForward_C2.seq,
primerReverse_C2.seq,
aligner,
assemHash[assemName])
B_CRISPR1_L, B_CRISPR2_L = self.determineCrisprLength(nodeC1rec.seq.reverse_complement(),
nodeC1rec.seq,
primerForward_C1.seq,
primerReverse_C1.seq,
nodeC2rec.seq.reverse_complement(),
nodeC2rec.seq,
primerForward_C2.seq,
primerReverse_C2.seq,
aligner,
assemHash[assemName])
print('xxx')
print('A_CRISPR1_L: ', A_CRISPR1_L)
# print('A_CRISPR2_L: ', A_CRISPR2_L)
# print('B_CRISPR1_L: ', B_CRISPR1_L)
# print('B_CRISPR2_L: ', B_CRISPR2_L)
assemHash[assemName].A_CRISPR1_L = A_CRISPR1_L
assemHash[assemName].A_CRISPR2_L = A_CRISPR2_L
assemHash[assemName].B_CRISPR1_L = B_CRISPR1_L
assemHash[assemName].B_CRISPR2_L = B_CRISPR2_L
# break
headerNew = headerOld + ['LPRIMER_C1_A', 'LPRIMER_C2_A', 'LPRIMER_C1_B', 'LPRIMER_C2_B', ]
dataNew = []
for i, a in enumerate(dataOld):
# a -> ['a','b','c','d',..]
# assemName -> 'SAL_AA0066AA_AS'
assemName = a[0]
A_CRISPR1_L = assemHash[assemName].A_CRISPR1_L
A_CRISPR2_L = assemHash[assemName].A_CRISPR2_L
B_CRISPR1_L = assemHash[assemName].B_CRISPR1_L
B_CRISPR2_L = assemHash[assemName].B_CRISPR2_L
gg = a + [A_CRISPR1_L, A_CRISPR2_L, B_CRISPR1_L, B_CRISPR2_L]
dataNew.append(gg)
# CREATE NEW WORKBOOK
wb = Workbook()
ws_sum = wb.active
ws_sum.title = 'summary'
ws_sum.column_dimensions[get_column_letter(1)].width = 15
ws_sum.column_dimensions[get_column_letter(2)].width = 30
ws_sum.column_dimensions[get_column_letter(3)].width = 25
currentRow = 1
for i, label in enumerate(headerNew):
ws_sum.cell(row=currentRow, column=i + 1, value=label)
# currentRow = 2
for i, data in enumerate(dataNew):
# data -> [a,b,c,d]
for j, a in enumerate(data):
ws_sum.cell(row=i + 2, column=j + 1, value=a)
newFilePath = os.path.join(outputFolderPath, 'summary_PRIMER2.xlsx')
wb.save(newFilePath)
def generateFileFolderFromSerotype(self):
"""
Group all Assembly files using Serotype
(1) For each folder, make FASTA file
- SAL_AB1316AA_AS_NODEC1.fasta : contain only NODE sequence
- SAL_AB1316AA_AS_NODEC2.fasta : contain only NODE sequence
:return:
"""
main_dir = '/home/aonlazio/Python/Project/WGSCP'
inputFolderPath = os.path.join(main_dir, 'input')
outputFolderPath = os.path.join(main_dir, 'output')
resFolderPath = os.path.join(main_dir, 'res')
# xceloutFolderPath = os.path.join(resFolderPath, 'xcelout')
xcelreadFilePath = os.path.join(outputFolderPath, 'summary_PRIMER2.xlsx')
assemoutFolderPath = os.path.join(resFolderPath, 'assemout')
util.createFolderAndClear(assemoutFolderPath)
AssemblyList = []
SerotypeList = []
# Read xcelFilePath
wb = load_workbook(xcelreadFilePath)
ws = wb['summary']
rows = list(ws.rows)
headerOld = []
dataOld = []
for i in range(len(rows)):
b = []
row = rows[i]
if i == 0:
for col in range(len(row)):
value = row[col].value
headerOld.append(value)
else:
for col in range(len(row)):
value = row[col].value
b.append(value)
dataOld.append(b)
# assemHash -> {AssemblyName: AssemblyAnnotation ins,..}
assemHash = {}
for i, a in enumerate(dataOld):
# a -> ['a','b','c','d',..]
# assemblyName -> 'SAL_AA0066AA_AS'
name = a[0]
# spacerC1 = a[1]
# spacerC2 = a[2]
nodeC1 = a[9]
nodeC2 = a[10]
Serovar = a[11]
Serotype = a[13]
assemHash[name] = AssemblyAnnXcel(name)
assemHash[name].nodeC1 = nodeC1
assemHash[name].nodeC2 = nodeC2
assemHash[name].Serotype = Serotype
assemHash[name].Serovar = Serovar
SerotypeList.append(Serotype)
# SerotypeGroupList -> ['Newport','Stanley',..]
SerotypeGroupList = list(set(SerotypeList))
SerotypeGroupList.sort() # sort letters
print('SerotypeGroupList: ', SerotypeGroupList)
# iterate files over input folder
wgsFileList = [f for f in os.listdir(inputFolderPath) if
os.path.isfile(os.path.join(inputFolderPath, f))]
# get node in FASTA in SeqRecord ins
# print(wgsFileList)
for i, wgsFile in enumerate(wgsFileList):
# wgsFile -> SAL_BA4239AA_AS.scaffold.fasta
# print(wgsFile)
# if i == 4:
# break
assemName = wgsFile.split('.')[0]
try:
Serotype = assemHash[assemName].Serotype
except (KeyError,):
print('error assemName: ', assemName)
continue
# create folder for Serotype
# serotypeFolderPath -> folder name 'Stanley'
serotypeFolderPath = os.path.join(assemoutFolderPath, Serotype)
# util.createFolderAndClear(serotypeFolderPath)
try:
os.mkdir(serotypeFolderPath)
except (FileExistsError,):
# folder already exists
pass
wgsFilePath = os.path.join(inputFolderPath, wgsFile)
nodeC1rec = None # SeqRecord ins
nodeC2rec = None # SeqRecord ins
try:
# get SeqRecord from nodeC1
for seqRecord in SeqIO.parse(wgsFilePath, "fasta"):
if seqRecord.id == assemHash[assemName].nodeC1:
nodeC1rec = seqRecord
break
# get SeqRecord from nodeC2
for seqRecord in SeqIO.parse(wgsFilePath, "fasta"):
if seqRecord.id == assemHash[assemName].nodeC2:
nodeC2rec = seqRecord
break
except:
continue
if nodeC1rec is not None and nodeC2rec is not None:
fastaC1FileName = '%s_NODEC1.fasta' % (assemName,)
fastaC2FileName = '%s_NODEC2.fasta' % (assemName,)
fastaC1FilePath = os.path.join(serotypeFolderPath, fastaC1FileName)
fastaC2FilePath = os.path.join(serotypeFolderPath, fastaC2FileName)
with open(fastaC1FilePath, "w") as output_handle:
SeqIO.write([nodeC1rec], output_handle, "fasta")
with open(fastaC2FilePath, "w") as output_handle:
SeqIO.write([nodeC2rec], output_handle, "fasta")
def determineAlignPositions(self, element):
"""
element -> [[5461 5468]
[5469 5487]]
:param element:
:return:
"""
start = None # 5461
stop = None # 5487
for i, a in enumerate(element):
# i -> 0 or 1
# a -> [5461 5468] or [5469 5487]
if i == 0:
start = a[0]
if i == len(element) - 1:
# len(RVC2[0]) -> 2
stop = a[1]
return start, stop
def determineCrisprLength(self,
nodeFWC1seq,
nodeRVC1seq,
primerFWC1seq,
primerRVC1seq,
nodeFWC2seq,
nodeRVC2seq,
primerFWC2seq,
primerRVC2seq,
aligner,
AAX):
"""
determineCrisprLength
:param nodeFWC1seq: node C1 forward sequence
:param nodeRVC1seq: node C1 reverse sequence
:param primerFWC1seq: primer C1 forward sequence
:param primerRVC1seq: primer C1 reverse sequence
:param nodeFWC2seq: node C2 forward sequence
:param nodeRVC2seq: node C2 reverse sequence
:param primerFWC2seq: primer C2 forward sequence
:param primerRVC2seq: primer C2 reverse sequence
:param aligner: aligner
:param AAX: AssemblyAnnXcel ins
:return: C1_L and C2_L
"""
nodeC1LEN = len(nodeFWC1seq)
nodeC2LEN = len(nodeFWC2seq)
alignFWC1s = aligner.align(nodeFWC1seq, primerFWC1seq)
alignRVC1s = aligner.align(nodeRVC1seq, primerRVC1seq)
alignFWC2s = aligner.align(nodeFWC2seq, primerFWC2seq)
alignRVC2s = aligner.align(nodeRVC2seq, primerRVC2seq)
alignFWC1 = alignFWC1s[0]
alignRVC1 = alignRVC1s[0]
alignFWC2 = alignFWC2s[0]
alignRVC2 = alignRVC2s[0]
FWC1 = alignFWC1.aligned
RVC1 = alignRVC1.aligned
FWC2 = alignFWC2.aligned
RVC2 = alignRVC2.aligned
# print(RVC2)
# print(type(FWC1))
# RVC2[0] -> [[82999 83016]
# [83017 83025]]
# RVC2[1] -> [[ 0 17]
# [ 17 25]]
# Extract Positions RVC2
start_FWC1, stop_FWC1 = self.determineAlignPositions(FWC1[0])
start_RVC1, stop_RVC1 = self.determineAlignPositions(RVC1[0])
start_FWC2, stop_FWC2 = self.determineAlignPositions(FWC2[0])
start_RVC2, stop_RVC2 = self.determineAlignPositions(RVC2[0])
# print('start_FWC1: ', start_FWC1)
# print('stop_FWC1: ', stop_FWC1)
# print('start_RVC1: ', start_RVC1)
# print('stop_RVC1: ', stop_RVC1)
# print('start_FWC2: ', start_FWC2)
# print('stop_FWC2: ', stop_FWC2)
# print('start_RVC2: ', start_RVC2)
# print('stop_RVC2: ', stop_RVC2)
# Adjust start_RVC1 and stop_RVC1
start_RVC1_adj = nodeC1LEN - stop_RVC1
stop_RVC1_adj = nodeC1LEN - start_RVC1
# print('start_RVC1_adj: ', start_RVC1_adj)
# print('stop_RVC1_adj: ', stop_RVC1_adj)
start_RVC2_adj = nodeC2LEN - stop_RVC2
stop_RVC2_adj = nodeC2LEN - start_RVC2
# print('start_RVC2_adj: ', start_RVC2_adj)
# print('stop_RVC2_adj: ', stop_RVC2_adj)
CRISPR1_L = stop_RVC1_adj - start_FWC1
CRISPR2_L = stop_RVC2_adj - start_FWC2
# AAX.CRISPR1_L = CRISPR1_L
# AAX.CRISPR2_L = CRISPR2_L
# print('CRISPR1_L: ', AAX.CRISPR1_L)
# print('CRISPR2_L: ', AAX.CRISPR2_L)
return CRISPR1_L, CRISPR2_L
def run_AMRandSPI(self):
"""
Main run method
:return:
"""
# clear DBSC folder first
util.createFolderAndClear(self.fileCls.DBSC_Folder)
for i, wgsFile in enumerate(self.wgsFileList):
wSal = WGSSal(self.fileCls, wgsFile)
wSal.setupIO()
# Test only
# if wSal.outFolderName not in ('SAL_BA3995AA_AS', 'SAL_BA5799AA_AS',):
# continue
# make folder right here
util.createFolderAndClear(wSal.outFolderPath)
# make DB in the "/maindb/DBSC/BK_SAL1_DB"
self.makeBlastDB(wSal.inputFilePath, wSal.outFolderName)
# call runCrisprFinder method
wSal.runAMRFinderPlus()
wSal.runSPIFinder()
self.genomeNameList.append((wSal.outFolderName, wSal.outFolderPath))
self.collectResult_AMRandSPI(wSal)
# Record all results in the excel file
self.recordResult_AMRandSPI()
# Remove all folders inside /DBSC/
util.createFolderAndClear(self.fileCls.DBSC_Folder)
def constructSNPTree(self):
"""
Construct SNP-based tree
:return:
"""
cmdLine = 'parsnp -c -r %s -d %s -o %s' % (self.fileCls.refGenome_LT2_FilePath,
self.useInputFolder,
self.fileCls.treeOutputFolder,
)
os.system(cmdLine)
def collectResult(self, wSal):
"""
Collect all results
:return:
"""
self.resultHash[wSal.outFolderName] = RES_ALL(wSal.outFolderName)
# res -> RES_ALL ins
resAll = self.resultHash[wSal.outFolderName]
# resAll.res_SeqSero2 -> RES_SeqSero2 ins
# resAll.res_SeqSero2 = wSal.seqsero2_res
resAll.res_MLST = wSal.mlst_res
resAll.res_SISTR = wSal.sistr_res
resAll.res_AMRFinder = wSal.amrfinder_res
resAll.res_SPI = wSal.spifinder_res
resAll.res_CRISPR = wSal.crisprfinder_res
# after all results are collected, delete the temp folder
tempDir_SISTR = os.path.join(self.fileCls.outputFolder, '%s_%s' % (wSal.outFolderName, 'tmpSistr',))
util.removeFolderAndContent(tempDir_SISTR)
def collectResult_CRISPR(self, wSal):
"""
Collect all results
:return:
"""
# self.resultHash -> {'BK_SAL1: RES_ALL ins}
self.resultHash[wSal.outFolderName] = RES_ALL(wSal.outFolderName)
# res -> RES_ALL ins
resAll = self.resultHash[wSal.outFolderName]
# print('resAll: ', resAll)
resAll.res_CRISPR = wSal.crisprfinder_res
# print('resAll.res_CRISPR: ', resAll.res_CRISPR)
def collectResult_AMRandSPI(self, wSal):
"""
Collect all results
:return:
"""
# self.resultHash -> {'BK_SAL1: RES_ALL ins}
self.resultHash[wSal.outFolderName] = RES_ALL(wSal.outFolderName)
# res -> RES_ALL ins
resAll = self.resultHash[wSal.outFolderName]
print('resAll: ', resAll)
resAll.res_AMRFinder = wSal.amrfinder_res
resAll.res_SPI = wSal.spifinder_res
print('resAll.res_AMRFinder: ', resAll.res_AMRFinder)
print('resAll.res_SPI: ', resAll.res_SPI)
def makeBlastDB(self, scaffoldFilePath, genomeName):
"""
Need to transform a scaffolds file into database for the search to begin
For instance, 'BK_SAL1' is associated with the file 'BK_SAL1.scaffolds.fasta'
This one file is split into 8 sub-files which are all put into "/maindb/DBSC/BK_SAL1" folder.
:param scaffoldFilePath: e.g. 'in_file/BK_SAL1.scaffolds.fasta'
:param genomeName: e.g. 'BK_SAL1' or 'SAL_IC4008AA_AS'
:return:
"""
srList = []
for seq_record in SeqIO.parse(scaffoldFilePath, "fasta"):
try:
newID = seq_record.id[0:50]
except:
newID = seq_record.id
record = SeqRecord(seq_record.seq, id=newID, description='')
srList.append(record)
# Write into the file
# fastaFilePath = os.path.join(scaffoldFilePath, 'NodeFound.fasta')
with open(scaffoldFilePath, "w") as output_handle:
SeqIO.write(srList, output_handle, "fasta")
# create main db folder e.g. "/maindb/DBSC/BK_SAL1_DB"
genomeName_DB = '%s_DB' % (genomeName,) # 'BK_SAL1_DB'
# dbFolder -> "/maindb/DBSC/BK_SAL1_DB"
dbFolder = os.path.join(self.fileCls.DBSC_Folder, genomeName_DB)
util.createFolderAndClear(dbFolder)
# dbFolder -> "/maindb/DBSC/BK_SAL1_DB/BK_SAL1"
dbFolder_X = os.path.join(dbFolder, genomeName)
cmdLine = 'makeblastdb -in %s -dbtype nucl -parse_seqids -out %s' % (scaffoldFilePath,
dbFolder_X,
)
os.system(cmdLine)
def makeRefLT2_DB(self):
"""
Need to transform a scaffolds file into database for the search to begin
For instance, 'BK_SAL1' is associated with the file 'BK_SAL1.scaffolds.fasta'
This one file is split into 8 sub-files which are all put into "/maindb/DBSC/BK_SAL1" folder.
:param scaffoldFilePath: e.g. 'in_file/BK_SAL1.scaffolds.fasta'
:param genomeName: e.g. 'BK_SAL1' or 'SAL_IC4008AA_AS'
:return:
"""
RefGenome_DB = 'LT2DB'
# dbFolder -> "/maindb/REF/LT2_DB"
dbFolder = os.path.join(self.fileCls.REFDBFolder, RefGenome_DB)
util.createFolderAndClear(dbFolder)
# dbFolder -> "/maindb/REF/LT2_DB/BK_SAL1"
dbFolder_X = os.path.join(dbFolder, 'LT2')
cmdLine = 'makeblastdb -in %s -dbtype nucl -parse_seqids -out %s' % (self.fileCls.refGenome_LT2_FilePath,
dbFolder_X,
)
os.system(cmdLine)
def makeSpacerDB(self):
"""
Transform a 'spacerall.fasta' into the database used in Blast operation
:param genomeName: genome name e.g. 'BK_SAL1'
:return:
"""
# get the list of scaffolds files in "in_file" folder
spacerFile = 'spacerall.fasta' # Spacer file
spacerFilePath = os.path.join(self.fileCls.SPACER_Folder, spacerFile)
# create main db folder -> "/SPACER/SP_DB"
spacerDB_name = 'SP_DB'
# dbFolder -> "/SPACER/SP_DB"
dbFolder = os.path.join(self.fileCls.SPACER_Folder, spacerDB_name)
if os.path.exists(dbFolder) is False:
# DB not exist yet, create new one
util.createFolderAndClear(dbFolder)
# dbFolder -> "/SPACER/SP_DB/SPALL"
dbFolder_X = os.path.join(dbFolder, 'SPALL')
cmdLine = 'makeblastdb -in %s -dbtype nucl -parse_seqids -out %s' % (spacerFilePath,
dbFolder_X,
)
os.system(cmdLine)
def makeSpacerTempDB(self):
"""
Transform a 'spacerall.fasta' into the database used in Blast operation
:param genomeName: genome name e.g. 'BK_SAL1'
:return:
"""
# get the list of scaffolds files in "in_file" folder
spacerFile = 'spacerall.fasta' # Spacer file
spacerFilePath = os.path.join(self.fileCls.SPACER_Folder, spacerFile)
# create main db folder -> "/SPACER/SP_DB"
spacerDB_name = 'SPTEMP_DB'
# dbFolder -> "/SPACER/SPTEMP_DB"
dbFolder = os.path.join(self.fileCls.SPACER_Folder, spacerDB_name)
util.createFolderAndClear(dbFolder)
# dbFolder -> "/SPACER/SP_DB/SPALL"
dbFolder_X = os.path.join(dbFolder, 'SPALL')
cmdLine = 'makeblastdb -in %s -dbtype nucl -parse_seqids -out %s' % (spacerFilePath,
dbFolder_X,
)
os.system(cmdLine)
def performSpacerProfileAlignment(self):
"""
Perform spacer profile alignment among all genomes
:return:
"""
# print(self.genomeNameList)
for genomeName, folderPath in self.genomeNameList:
spProfileFilePath = os.path.join(folderPath, 'spProfile.txt')
try:
spProfile = util.readSpacerProfile(spProfileFilePath)
except (FileNotFoundError,):
continue
self.spProfileList.append((genomeName, spProfile))
# find all combinations in pairs
matrix = []
finalMatrix = []
# comb -> [(a,b),(c,d),..]
combList = list(itertools.combinations(self.spProfileList, 2))
if len(combList) == 0:
return finalMatrix
n = 0
j = 0
k = 0
startNumber = len(self.genomeNameList) # if len = 5, n -> 4
u = []
while True:
# a -> ('BK_SAL1',spProfile)
# b -> ('BK_SAL10',spProfile)
a, b = combList[n]
# align C1 first
pair_C1 = PerformListAlignment(a[1].C1, b[1].C1)
pair_C1.run()
pair_C2 = PerformListAlignment(a[1].C2, b[1].C2)
pair_C2.run()
totalScore = pair_C1.totalScore + pair_C2.totalScore
u.append(totalScore)
n += 1
j += 1
if j == startNumber - 1:
startNumber -= 1
j = 0
u = [0] * (k + 1) + u
k += 1
matrix.append(u)
u = []
if n == len(combList):
matrix.append([0] * (len(self.genomeNameList)))
break
# transform to numpy array
X = np.array(matrix)
# copy to lower triangle
X = np.triu(X)
X = X + X.T - np.diag(np.diag(X))
# get only the lower triangle
for i in range(X.shape[0]):
ab = X[i][0:i + 1]
ab = ab.tolist()
finalMatrix.append(list(ab))
return finalMatrix
def constructPhylogeneticTreeSpacerProfile(self, distanceMatrix):
"""
Construct the tree by UPGMA method
:param distanceMatrix:
:return:
"""
def get_label(leaf):
a = str(leaf.name)
if a.find('Inner') != -1:
# find 'Inner'
return None
return leaf.name
pureList = [u[0] for u in self.genomeNameList]
# print(pureList)
b = DistanceMatrix(names=pureList, matrix=distanceMatrix)
# Creating a DistanceTreeConstructor object
constructor = DistanceTreeConstructor()
# Construct the phlyogenetic tree using UPGMA algorithm
UPGMATree = constructor.upgma(b)
# Draw the phlyogenetic tree
# Phylo.draw(UPGMATree,axes=fig_axes, do_show=False)
plt.clf()
# fig1, ax1 = plt.subplots(figsize=(11, 11))
fig = plt.figure(figsize=(15, 20), dpi=150)
fig_axes = fig.add_subplot(1, 1, 1)
Phylo.draw(UPGMATree, label_func=get_label, axes=fig_axes, do_show=False)
# save tree to PNG file
plt.savefig(self.fileCls.CRISPR_TreeFilePath)
def recordResult(self):
"""
Record result to excel/csv
:return:
"""
print('recordResult')
wb = Workbook()
ws_sum = wb.active
ws_sum.title = self.recordTitle
ws_sum.column_dimensions[get_column_letter(1)].width = 15
ws_sum.column_dimensions[get_column_letter(2)].width = 30
ws_sum.column_dimensions[get_column_letter(3)].width = 25
currentRow = 1
for i, label in enumerate(self.headLabels):
ws_sum.cell(row=currentRow, column=i + 1, value=label)
currentRow = 2
for name in self.resultHash:
resAll = self.resultHash[name] # RES_ALL ins
rc = resAll.res_CRISPR
spC1 = rc.spacerC1
spC2 = rc.spacerC2
num_spC1 = len(spC1.split('|')) if spC1 != '' else 0
num_spC2 = len(spC2.split('|')) if spC2 != '' else 0
print('rc.posEndC1: ', type(rc.posEndC1))
print('rc.posStartC1: ', rc.posStartC1)
print('rc.posEndC2: ', rc.posEndC2)
print('rc.posStartC2: ', rc.posStartC2)
try:
lenC1 = rc.posEndC1 - rc.posStartC1