-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmultitargeting.py
More file actions
1458 lines (1250 loc) · 64 KB
/
multitargeting.py
File metadata and controls
1458 lines (1250 loc) · 64 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
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
from PyQt5 import QtWidgets, Qt, QtGui, QtCore, uic
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
import GlobalSettings
import matplotlib
from Algorithms import SeqTranslate
from CSPRparser import CSPRparser
from matplotlib.ticker import MaxNLocator
import os
import sqlite3
import gzip
from collections import Counter
import statistics
import traceback
import math
import numpy as np
from matplotlib.widgets import Slider
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
#global logger
logger = GlobalSettings.logger
class Multitargeting(QtWidgets.QMainWindow):
def __init__(self):
try:
self.count = 0
super(Multitargeting, self).__init__()
uic.loadUi(GlobalSettings.appdir + 'mt.ui', self)
self.setWindowIcon(Qt.QIcon(GlobalSettings.appdir + "cas9image.ico"))
self.multitargeting_statistics = Multitargeting_Statistics()
self.sq = SeqTranslate() # SeqTranslate object used in class
self.line_bool = False # Used to check if VBoxLayout already has canvas in it
self.bar_bool = False # Used to check if VBoxLayout already has canvas in it
self.seed_bar_bool = False # Used to check if VBoxLayout already has canvas in it
# GroupBox Styling
groupbox_style = """
QGroupBox:title{subcontrol-origin: margin;
left: 10px;
padding: 0 5px 0 15px;}
QGroupBox#groupBox{border: 2px solid rgb(111,181,110);
border-radius: 9px;
font: bold;
margin-top: 10px;}"""
self.groupBox.setStyleSheet(groupbox_style)
self.groupBox_2.setStyleSheet(groupbox_style.replace("groupBox","groupBox_2"))
self.groupBox_3.setStyleSheet(groupbox_style.replace("groupBox","groupBox_3"))
#layout for table
self.table.setColumnCount(8)
self.table.setShowGrid(False)
# for keeping track of where we are in the sorting clicking for each column
self.switcher_table = [1, 1, 1, 1, 1, 1, 1, 1]
self.table.setHorizontalHeaderLabels(
["Seed", "Total Repeats", "Avg. Repeats/Scaffold", "Consensus Sequence", "% Consensus",
"Score", "PAM", "Strand"])
self.table.horizontalHeader().setSectionsClickable(True)
self.table.horizontalHeader().sectionClicked.connect(self.table_sorting)
self.table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.table.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
self.table.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.table.resizeColumnsToContents()
# Initializes layouts for the graphs
self.global_line = QtWidgets.QVBoxLayout()
self.global_bar = QtWidgets.QVBoxLayout()
self.seed_bar = QtWidgets.QVBoxLayout()
self.global_line.setContentsMargins(0,0,0,0)
self.global_bar.setContentsMargins(0,0,0,0)
self.seed_bar.setContentsMargins(0,0,0,0)
self.chrom_viewer_layout = QtWidgets.QVBoxLayout()
self.chrom_viewer_layout.setContentsMargins(0,0,0,0)
self.data = ""
self.shortHand = ""
self.chromo_length = list()
# Listeners for changing the seed sequence or the .cspr file
self.Analyze_Button.clicked.connect(self.make_graphs)
self.statistics_overview.clicked.connect(self.show_statistics)
self.export_button.clicked.connect(self.export_tool)
self.selectAll.stateChanged.connect(self.select_all)
self.selectAll.setEnabled(False)
# go back to main button
self.back_button.clicked.connect(self.go_back)
# Statistics storage variables
self.max_repeats = 1
self.average = 0
self.median = 0
self.mode = 0
self.average_unique = 0
self.average_rep = 0
self.bar_coords = []
self.seed_id_seq_pair = {}
# parser object
self.parser = CSPRparser("")
self.ready_chromo_min_max = True
self.ready_chromo_make_graph = True
self.info_path = os.getcwd()
##################################
self.scene = QtWidgets.QGraphicsScene()
self.scene2 = QtWidgets.QGraphicsScene()
self.graphicsView_2.setScene(self.scene2)
self.scrollArea.viewport().installEventFilter(self)
self.graphicsView_2.viewport().installEventFilter(self)
self.loading_window = loading_window()
#sql query settings
self.row_limit = 1000
self.sql_query_settings.clicked.connect(self.update_sql_query_settings)
self.sql_settings = sql_query_settings()
self.sql_settings.row_count.textChanged.connect(self.sql_row_count_value_changed)
#scale UI
self.first_show = True
self.scaleUI()
except Exception as e:
logger.critical("Error initializing Multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def select_all(self):
try:
if self.selectAll.isChecked():
self.table.selectAll()
else:
self.table.clearSelection()
except Exception as e:
logger.critical("Error in selectAll() in multitargeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def scaleUI(self):
try:
self.repaint()
QtWidgets.QApplication.processEvents()
screen = self.screen()
dpi = screen.physicalDotsPerInch()
self.dpi = dpi
width = screen.geometry().width()
height = screen.geometry().height()
# font scaling
fontSize = 12
self.fontSize = fontSize
self.centralWidget().setStyleSheet("font: " + str(fontSize) + "pt 'Arial';" )
self.menuBar().setStyleSheet("font: " + str(fontSize) + "pt 'Arial';" )
#CASPER header scaling
fontSize = 30
self.title.setStyleSheet("font: bold " + str(fontSize) + "pt 'Arial';")
self.adjustSize()
currentWidth = self.size().width()
currentHeight = self.size().height()
#make sure chromosome viewer doesnt get too small
self.groupBox_2.setMinimumHeight(int(0.3 * height))
# window scaling
scaledWidth = int((width * 1400) / 1920)
scaledHeight = int((height * 900) / 1080)
if scaledHeight < currentHeight:
scaledHeight = currentHeight
if scaledWidth < currentWidth:
scaledWidth = currentWidth
#set min width of table
self.table.setMinimumWidth(int(0.5 * scaledWidth))
screen = QtWidgets.QApplication.desktop().screenNumber(QtWidgets.QApplication.desktop().cursor().pos())
centerPoint = QtWidgets.QApplication.desktop().screenGeometry(screen).center()
x = centerPoint.x()
y = centerPoint.y()
x = x - (math.ceil(scaledWidth / 2))
y = y - (math.ceil(scaledHeight / 2))
self.setGeometry(x, y, scaledWidth, scaledHeight)
self.repaint()
QtWidgets.QApplication.processEvents()
except Exception as e:
logger.critical("Error in scaleUI() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def centerUI(self):
self.repaint()
QtWidgets.QApplication.processEvents()
width = self.width()
height = self.height()
# scale/center window
screen = QtWidgets.QApplication.desktop().screenNumber(QtWidgets.QApplication.desktop().cursor().pos())
centerPoint = QtWidgets.QApplication.desktop().screenGeometry(screen).center()
x = centerPoint.x()
y = centerPoint.y()
x = x - (math.ceil(width / 2))
y = y - (math.ceil(height / 2))
self.setGeometry(x, y, width, height)
self.Analyze_Button.resize(200, 200)
self.repaint()
QtWidgets.QApplication.processEvents()
def export_tool(self):
try:
select_items = self.table.selectedItems()
if len(select_items) <= 0:
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Nothing Selected")
msgBox.setText("No targets were highlighted. Please highlight the targets you want to be exported to a CSV File!")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return
GlobalSettings.mainWindow.export_tool_window.launch(select_items,"mt")
except Exception as e:
logger.critical("Error in export_tool() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def show_statistics(self):
try:
if (self.line_bool and self.bar_bool):
self.multitargeting_statistics.centerUI()
self.multitargeting_statistics.show()
self.multitargeting_statistics.activateWindow()
else:
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("No analysis run.")
msgBox.setText('Multitargeting Analysis must be performed before viewing statistics.\n\nSelect an organism and endonuclease and click "Analyze" then try again.')
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return True
except Exception as e:
logger.critical("Error in show_statistics() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
#event handler to show details of targets in chromosome viewer while hovering over canvases
def chromosome_event_handler(self, event):
try:
#get current moust location
x = event.xdata
y = event.y
#get event data relative to the canvas (chromosome) the viewer is hovering at
curr_chromosome = self.canvas_chromosome_map[event.canvas]
chromosome_seed_data = self.event_data[curr_chromosome]
#get targets within small range of the mouse location
local_targets = []
for entry in chromosome_seed_data:
try:
if x >= entry[0] - 0.001 and x <= entry[0] + 0.001:
local_targets.append(entry)
except:
pass
#make sure targets are found before overwriting the viewers details
if local_targets != []:
#prep the viewer to show the target details
self.scene2 = QtWidgets.QGraphicsScene()
self.graphicsView_2.setScene(self.scene2)
output = str()
for target in local_targets:
output += f"Location: {target[1]} | Seq: {target[2]} | PAM: {target[3]} | SCR: {target[4]} | DIRA: {target[5]}\n"
text = self.scene2.addText(output)
font = QtGui.QFont()
font.setPointSize(self.fontSize-2)
text.setFont(font)
except Exception as e:
logger.critical("Error in event_data() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def launch(self):
try:
self.get_data()
except Exception as e:
logger.critical("Error in launch() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
#button trigger for sql settings
def update_sql_query_settings(self):
try:
self.sql_settings.centerUI()
self.sql_settings.show()
self.sql_settings.activateWindow()
except Exception as e:
logger.critical("Error in update_sql_query_settings() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
#trigger for if sql line edit value has changed
def sql_row_count_value_changed(self):
try:
self.row_limit = int(self.sql_settings.row_count.text())
except Exception as e:
logger.critical("Error in sql_row_count_value_changed() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
pass
def get_data(self):
try:
#disconnect index changed signal on endo dropdown if there is one
try:
self.organism_drop.currentIndexChanged.disconnect()
except:
pass
try:
self.endo_drop.currentIndexChanged.disconnect()
except:
pass
self.organism_drop.clear()
self.endo_drop.clear()
onlyfiles = [f for f in os.listdir(GlobalSettings.CSPR_DB) if os.path.isfile(os.path.join(GlobalSettings.CSPR_DB, f))]
self.organisms_to_files = {}
self.organisms_to_endos = {}
# shortName = {}
self.endo_drop.clear()
for file in onlyfiles:
if file.find('.cspr') != -1:
newname = file[0:-4]
endo = newname[newname.rfind("_") + 1:-1]
hold = open(file, 'r')
buf = (hold.readline())
hold.close()
buf = str(buf)
buf = buf.strip()
species = buf.replace("GENOME: ", "")
if species in self.organisms_to_files:
self.organisms_to_files[species][endo] = [file, file.replace(".cspr", "_repeats.db")]
else:
self.organisms_to_files[species] = {}
self.organisms_to_files[species][endo] = [file, file.replace(".cspr", "_repeats.db")]
if species in self.organisms_to_endos:
self.organisms_to_endos[species].append(endo)
else:
self.organisms_to_endos[species] = [endo]
if self.organism_drop.findText(species) == -1:
self.organism_drop.addItem(species)
#fill in endos dropdown based on current organism
endos = self.organisms_to_endos[str(self.organism_drop.currentText())]
self.endo_drop.addItems(endos)
self.organism_drop.currentIndexChanged.connect(self.update_endos)
#update file names for current org/endo combo
self.cspr_file = self.organisms_to_files[str(self.organism_drop.currentText())][endos[0]][0]
self.db_file = self.organisms_to_files[str(self.organism_drop.currentText())][endos[0]][1]
except Exception as e:
logger.critical("Error in get_data() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def update_endos(self):
try:
#try to disconnect index changed signal on endo dropdown if there is one
try:
self.endo_drop.currentIndexChanged.disconnect()
except:
pass
#clear endo dropdown and fill in with endos relative to the current organism
self.endo_drop.clear()
endos = self.organisms_to_endos[str(self.organism_drop.currentText())]
self.endo_drop.addItems(endos)
except Exception as e:
logger.critical("Error in update_endos() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def make_graphs(self):
try:
self.cspr_file = self.organisms_to_files[str(self.organism_drop.currentText())][str(self.endo_drop.currentText())][0]
self.db_file = self.organisms_to_files[str(self.organism_drop.currentText())][str(self.endo_drop.currentText())][1]
self.loading_window.loading_bar.setValue(0)
self.loading_window.centerUI()
self.loading_window.show()
QtCore.QCoreApplication.processEvents()
self.chromo_length.clear()
self.loading_window.loading_bar.setValue(10)
self.plot_repeats_vs_seeds()
self.loading_window.loading_bar.setValue(30)
self.bar_seeds_vs_repeats()
self.loading_window.loading_bar.setValue(50)
self.fill_table()
self.table.selectRow(0) # Set the index to first row by default so
# all graphs are generated
self.selectAll.setEnabled(True) # Enable select all checkbox only
# after analysis has been performed
self.loading_window.loading_bar.setValue(100)
self.multitargeting_statistics.avg_rep.setText(str(round(float(self.average),1)))
self.multitargeting_statistics.med_rep.setText(str(round(float(self.median),1)))
self.multitargeting_statistics.mode_rep.setText(str(round(float(self.mode),1)))
self.multitargeting_statistics.nbr_seq.setText(str(round(float(self.repeat_count),1)))
self.loading_window.hide()
self.repaint()
QtWidgets.QApplication.processEvents()
except Exception as e:
logger.critical("Error in make_graphs() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
#function to fill table in UI
def fill_table(self):
try:
#disable row triggers
try:
self.table.itemSelectionChanged.disconnect()
except:
pass
#empty table
self.table.setRowCount(0)
#query db file for data
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
row_cnt = 0
if self.row_limit == -1:
sql_query = "SELECT * FROM repeats ORDER BY count DESC;"
else:
sql_query = "SELECT * FROM repeats ORDER BY count DESC LIMIT 0, " + str(self.row_limit) + ";"
for repeat in c.execute(sql_query):
#expand table by 1 row
self.table.setRowCount(row_cnt + 1)
#extract repeat info
seed = repeat[0]
chroms = repeat[1].split(",")
locs = repeat[2].split(",")
threes = repeat[3].split(",")
fives = repeat[4].split(",")
pams = repeat[5].split(",")
scores = repeat[6].split(",")
count = repeat[7]
#push seed
table_seed = QtWidgets.QTableWidgetItem()
table_seed.setData(QtCore.Qt.EditRole, seed)
table_seed.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 0, table_seed)
if len(threes) < len(fives):
for i in range(len(fives) - len(threes)):
threes.append('')
elif len(fives) < len(threes):
for i in range(len(threes) - len(fives)):
fives.append('')
majority_index = 0
three_prime, five_prime, both_prime = False, False, False
if threes[0] == '':
majority = max(set(fives), key=fives.count)
majority_index = fives.index(majority)
five_prime = True
elif fives[0] == '':
majority = max(set(threes), key=threes.count)
majority_index = threes.index(majority)
three_prime = True
else:
# account for both 3 and 5 present
threes_and_fives = []
for i in range(len(threes)):
threes_and_fives.append(threes[i] + fives[i])
majority = max(set(threes_and_fives), key=threes_and_fives.count)
majority_index = threes_and_fives.index(majority)
both_prime = True
# push total count
table_count = QtWidgets.QTableWidgetItem()
table_count.setData(QtCore.Qt.EditRole, count)
table_count.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 1, table_count)
# push avg repeat
location_repeat_counts = Counter(chroms)
avg_rep_per_scaff = sum(location_repeat_counts.values()) / len(location_repeat_counts.values())
avg_rep = QtWidgets.QTableWidgetItem()
avg_rep_per_scaff = float("%.2f" % avg_rep_per_scaff)
avg_rep.setData(QtCore.Qt.EditRole, avg_rep_per_scaff)
avg_rep.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 2, avg_rep)
# push seq
seq = QtWidgets.QTableWidgetItem()
seq.setData(QtCore.Qt.EditRole, fives[majority_index] + seed + threes[majority_index])
seq.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 3, seq)
# push percent consensus
perc_cons = QtWidgets.QTableWidgetItem()
percent_consensus = 0
if five_prime == True:
percent_consensus = (fives.count(fives[majority_index]) / len(fives)) * 100
elif three_prime == True:
percent_consensus = (threes.count(threes[majority_index]) / len(threes)) * 100
elif both_prime:
percent_consensus = (threes_and_fives.count(threes_and_fives[majority_index]) / len(threes_and_fives)) * 100
percent_consensus = float("%.2f" % percent_consensus)
perc_cons.setData(QtCore.Qt.EditRole, percent_consensus)
perc_cons.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 4, perc_cons)
# push score
score = QtWidgets.QTableWidgetItem()
score.setData(QtCore.Qt.EditRole, scores[majority_index])
score.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 5, score)
# push PAM
pam = QtWidgets.QTableWidgetItem()
pam.setData(QtCore.Qt.EditRole, pams[majority_index])
pam.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 6, pam)
# push strand
strand_val = ""
if int(locs[majority_index]) < 0:
strand_val = "-"
else:
strand_val = "+"
strand = QtWidgets.QTableWidgetItem()
strand.setData(QtCore.Qt.EditRole, strand_val)
strand.setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.table.setItem(row_cnt, 7, strand)
#increment row count
row_cnt += 1
#close db connection
c.close()
conn.close()
#resize columns in table to match contents
self.table.resizeColumnsToContents()
#reconnect row trigger
self.table.itemSelectionChanged.connect(self.row_selection_trigger)
except Exception as e:
logger.critical("Error in fill_table() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
#function for triggering graph updates when user selects row in table
def row_selection_trigger(self):
try:
item = self.table.currentItem()
if item.isSelected() == True:
row_num = item.row()
seed = self.table.item(row_num,0).text()
self.fill_Chromo_Text(seed)
self.chro_bar_create(seed)
except Exception as e:
logger.critical("Error in row_selection_trigger() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
# sorting to table
def table_sorting(self, logicalIndex):
try:
self.switcher_table[logicalIndex] *= -1
if self.switcher_table[logicalIndex] == -1:
self.table.sortItems(logicalIndex, QtCore.Qt.DescendingOrder)
else:
self.table.sortItems(logicalIndex, QtCore.Qt.AscendingOrder)
except Exception as e:
logger.critical("Error in table_sorting() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
#fill in chromo bar visualization
def fill_Chromo_Text(self, seed):
try:
#global dictionary to map canvases to chromosomes
self.canvas_chromosome_map = {}
# get kstats
kstats = []
with open(self.cspr_file, "r") as f:
for line in f:
buf = str(line)
if buf.find("KARYSTATS") != -1:
buf = buf.replace("KARYSTATS: ", "")
kstats = buf.split(',')
kstats = kstats[:-1]
break
#get chromosomes/locations of repeats for current seed
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
data = c.execute("SELECT chromosome, location, pam, score, five, three FROM repeats WHERE seed = ? ", (seed,)).fetchone()
c.close()
#make sure there is data on current seed
if data != None:
# build out dictionary (seed_data) mapping chromosome numbers to list of repeat locations
# normalize the location values based on kstat value of chromosome
seed_data = {}
self.event_data = {}
data = list(data)
chromo = data[0].split(',')
pos = data[1].split(',')
pam = data[2].split(',')
score = data[3].split(',')
five = data[4].split(',')
three = data[5].split(',')
#get x-cordinates for graphs and event data
for i in range(len(chromo)):
curr_chromo = int(chromo[i])
if int(pos[i]) >= 0:
dir = "+"
else:
dir = "-"
normalized_location = abs(float(pos[i]) / float(kstats[curr_chromo - 1]))
if int(chromo[i]) in seed_data.keys():
seed_data[int(chromo[i])].append(normalized_location)
self.event_data[int(chromo[i])].append([normalized_location, pos[i], five[i] + seed + three[i], pam[i], score[i], dir])
else:
seed_data[int(chromo[i])] = [normalized_location]
self.event_data[int(chromo[i])] = [[normalized_location, pos[i], five[i] + seed + three[i] ,pam[i], score[i], dir]]
# graph the locations for each chromosome
# Clear out old widgets in layout
for i in reversed(range(self.chrom_viewer_layout.count())):
self.chrom_viewer_layout.itemAt(i).widget().setParent(None)
top_widget = QtWidgets.QWidget()
top_layout = QtWidgets.QVBoxLayout()
chromo_keys = sorted(list(seed_data.keys()))
screen = self.screen()
height = screen.geometry().height()
groupbox_height = int((height * 100) / 1080)
for i in range(len(chromo_keys)):
curr_chromo = chromo_keys[i]
group_box = QtWidgets.QGroupBox()
group_box.setTitle(f"Chromosome {curr_chromo}")
group_box.setMinimumHeight(groupbox_height)
group_box.setMaximumHeight(groupbox_height)
layout = QtWidgets.QVBoxLayout(group_box)
canvas = MplCanvas()
canvas.axes.eventplot(seed_data[curr_chromo])
canvas.mpl_connect("motion_notify_event", self.chromosome_event_handler)
# surrounding border
canvas.axes.hlines(1.5, -0.01, 1.01, colors="Black", linewidth=1.5)
canvas.axes.hlines(0.5, -0.01, 1.01, colors="Black", linewidth=1.5)
canvas.axes.vlines(-0.01, 0.5, 1.5, colors="Black", linewidth=1.5)
canvas.axes.vlines(1.01, 0.5, 1.5, colors="Black", linewidth=1.5)
canvas.axes.set_ylim(0.45, 1.55)
canvas.axes.set_xlim(-0.05, 1.05)
canvas.axes.axis('off')
canvas.draw()
self.canvas_chromosome_map[canvas] = curr_chromo
layout.addWidget(canvas)
top_layout.addWidget(group_box)
top_widget.setLayout(top_layout)
self.scrollArea.setWidget(top_widget)
return True
else:
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Seed Error")
msgBox.setText("No such seed exists in the repeats section of this organism.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return False
except Exception as e:
logger.critical("Error in fill_Chromo_text() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
# creates bar graph num of repeats vs. chromosome
def chro_bar_create(self, seed):
try:
###Clear out old widgets in layout
for i in reversed(range(self.seed_bar.count())):
self.seed_bar.itemAt(i).widget().setParent(None)
self.seed_canvas = MplCanvas(self, width=5, height=3, dpi=self.dpi) ###Initialize new Canvas
self.seed_bar.addWidget(self.seed_canvas) ### Add canvas to global line layout
self.repeats_vs_chromo.setLayout(self.seed_bar) ### Add global line layout to repeats vs. seeds line plot widget
y = []
x_labels = []
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
data = c.execute("SELECT chromosome FROM repeats WHERE seed = ? ", (seed,)).fetchone()
c.close()
data = data[0].split(',')
for i in range(len(data)):
data[i] = int(data[i])
bar_data = Counter(data)
for chromo in sorted(bar_data):
x_labels.append(chromo)
y.append(bar_data[chromo])
x = list(range(0, len(x_labels)))
#the following statements are plottings / formatting for the graph
self.seed_canvas.axes.bar(x, y, align='center')
self.seed_canvas.axes.yaxis.set_major_locator(MaxNLocator(integer=True))
self.seed_canvas.axes.set_ylim(0, max(y) + 1)
self.seed_canvas.axes.set_xticks(x)
self.seed_canvas.axes.set_xticklabels(x_labels)
if len(x_labels) > 10:
tick_spacing = round(len(x_labels)/10)
for i, t in enumerate(self.seed_canvas.axes.get_xticklabels()):
if (i % tick_spacing) != 0:
t.set_visible(False)
self.seed_canvas.axes.set_xlabel('Chromosome', fontsize = 10)
self.seed_canvas.axes.set_ylabel('Number of Repeats', fontsize=10)
self.line_canvas.axes.set_title('Repeats per ID Number',fontsize=10)
self.line_canvas.axes.tick_params(axis='both', which='major', labelsize=8)
self.line_canvas.draw()
except Exception as e:
logger.critical("Error in chro_bar_create() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
def bar_seeds_vs_repeats(self):
try:
###Clear out old widgets in layout
for i in reversed(range(self.global_bar.count())):
self.global_bar.itemAt(i).widget().setParent(None)
self.bar_canvas = MplCanvas(self, width=5, height=3, dpi=self.dpi) ###Initialize new Canvas
self.global_bar.addWidget(self.bar_canvas) ### Add canvas to global line layout
self.seeds_vs_repeats_bar.setLayout(self.global_bar) ### Add global line layout to repeats vs. seeds line plot widget
""" Get the data """
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
x_labels = []
y = []
for obj in c.execute("select count, COUNT(count) as cnt from repeats group by count order by cnt DESC;"):
x_labels.append(obj[0])
y.append(obj[1])
x_labels = sorted(x_labels)
# the following are plotting / formatting for the graph
self.bar_canvas.axes.scatter(x_labels, y, s=10)
self.bar_canvas.axes.set_xlim(x_labels[0]-0.5, x_labels[-1] + 0.5)
self.bar_canvas.axes.set_yscale('log')
self.bar_canvas.axes.set_xlabel('Number of Repeats', fontsize=10)
self.bar_canvas.axes.set_ylabel('Number of Sequences', fontsize=10)
self.bar_canvas.axes.set_title('Number of Sequences per Number of Repeats',fontsize=10)
self.bar_canvas.axes.tick_params(axis='both', which='major', labelsize=8)
self.bar_canvas.draw()
self.bar_bool = True
except Exception as e:
logger.critical("Error in bar_seeds_vs_repeats() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Fatal Error")
msgBox.setText("Fatal Error:\n"+str(e)+ "\n\nFor more information on this error, look at CASPER.log in the application folder.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Close)
msgBox.exec()
exit(-1)
# # plots the repeats per ID number graph as line graph
def plot_repeats_vs_seeds(self):
try:
###Clear out old widgets in layout
for i in reversed(range(self.global_line.count())):
self.global_line.itemAt(i).widget().setParent(None)
self.line_canvas = MplCanvas(self, width=5, height=3, dpi=self.dpi) ###Initialize new Canvas
self.global_line.addWidget(self.line_canvas) ### Add canvas to global line layout
self.repeats_vs_seeds_line.setLayout(self.global_line) ### Add global line layout to repeats vs. seeds line plot widget
""" Fetch all the data """
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
y1 = []
for obj in c.execute("SELECT count from repeats;"):
y1.append(obj[0])
c.close()
#get stats
self.average = statistics.mean(y1)
self.mode = statistics.mode(y1)
self.median = statistics.median(y1)
self.repeat_count = len(y1)
# clear axes
self.line_canvas.axes.clear()
#Plotting / formatting
self.line_canvas.axes.plot(y1)
self.line_canvas.axes.set_xlabel('Seed ID Number',fontsize=10)
self.line_canvas.axes.set_ylabel('Number of Repeats',fontsize=10)
self.line_canvas.axes.set_title('Number of Repeats per Seed ID Number',fontsize=10)
self.line_canvas.axes.tick_params(axis='both', which='major', labelsize=8)
# always redraw at the end
self.line_bool = True
self.line_canvas.draw()
except Exception as e:
logger.critical("Error in plot_repeats_vs_seeds() in multi-targeting.")
logger.critical(e)
logger.critical(traceback.format_exc())
msgBox = QtWidgets.QMessageBox()