-
Notifications
You must be signed in to change notification settings - Fork 3
/
NewEndonuclease.py
406 lines (345 loc) · 17.6 KB
/
NewEndonuclease.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
import sys, os
from PyQt5 import QtWidgets, uic, QtGui, QtCore, Qt
import GlobalSettings
from PyQt5.QtGui import QIntValidator
import traceback
import math
#global logger
logger = GlobalSettings.logger
class NewEndonuclease(QtWidgets.QMainWindow):
def __init__(self):
try:
super(NewEndonuclease, self).__init__()
uic.loadUi(GlobalSettings.appdir + 'newendonuclease.ui', self)
self.setWindowIcon(Qt.QIcon(GlobalSettings.appdir + "cas9image.ico"))
self.setWindowTitle('New Endonuclease')
self.error = False
pamFlag = False
self.onList = []
self.offList = []
self.onList, self.offList = self.get_on_off_data() ### Call function to fill on- and off- data name lists
for name in self.onList: ### Add on-target names to drop-down
self.comboBox.addItem(str(name))
for name in self.offList: ### Add off-target names to drop-down
self.comboBox_2.addItem(str(name))
self.submit_button.clicked.connect(self.submit)
self.cancel_button.clicked.connect(self.cancel)
### Set up validators for input fields:
reg_ex1 = QtCore.QRegExp("[^/\\\\_]+") # No slashes or underscores
reg_ex2 = QtCore.QRegExp("[^/\\\\_\\s]+") # No slashes, underscores, or spaces
reg_ex3 = QtCore.QRegExp("[acdefghiklmnpqrstvwyACDEFGHIKLMNPQRSTVWY\S]+") # Only approved PAM characters and no spaces
input_validator1 = QtGui.QRegExpValidator(reg_ex1, self)
input_validator2 = QtGui.QRegExpValidator(reg_ex2, self)
input_validator3 = QtGui.QRegExpValidator(reg_ex3, self)
self.organism_name.setValidator(input_validator1)
self.abbreviation.setValidator(input_validator2)
self.pam_sequence.setValidator(input_validator3)
self.seed_length.setValidator(QIntValidator(0,30,self.seed_length))
self.five_length.setValidator(QIntValidator(0,20,self.five_length))
self.three_length.setValidator(QIntValidator(0,20,self.three_length))
groupbox_style = """
QGroupBox:title{subcontrol-origin: margin;
left: 10px;
padding: 0 5px 0 5px;}
QGroupBox#groupBox{border: 2px solid rgb(111,181,110);
border-radius: 9px;
font: bold 14pt 'Arial';
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"))
#scale UI
self.scaleUI()
except Exception as e:
logger.critical("Error initializing NewEndonuclease class.")
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)
#scale UI based on current screen
def scaleUI(self):
try:
self.repaint()
QtWidgets.QApplication.processEvents()
screen = self.screen()
dpi = screen.physicalDotsPerInch()
width = screen.geometry().width()
height = screen.geometry().height()
# font scaling
fontSize = 12
self.fontSize = fontSize
self.centralWidget().setStyleSheet("font: " + str(fontSize) + "pt 'Arial';")
#title scaling
fontSize = 20
self.title.setStyleSheet("font: bold " + str(fontSize) + "pt 'Arial';")
self.adjustSize()
currentWidth = self.size().width()
currentHeight = self.size().height()
# window scaling
scaledWidth = int((width * 480) / 1920)
scaledHeight = int((height * 615) / 1080)
if scaledHeight < currentHeight:
scaledHeight = currentHeight
if scaledWidth < currentWidth:
scaledWidth = currentWidth
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 new endo.")
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)
#center UI on current screen
def centerUI(self):
try:
self.repaint()
QtWidgets.QApplication.processEvents()
#center window on current screen
width = self.width()
height = self.height()
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.repaint()
QtWidgets.QApplication.processEvents()
except Exception as e:
logger.critical("Error in centerUI() in new endo.")
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)
#helper function for writing new endo information to CASPERinfo - used by submit()
def writeNewEndonuclease(self, newEndonucleaseStr):
try:
with open(GlobalSettings.appdir + 'CASPERinfo', 'r') as f, open(GlobalSettings.appdir + "new_file", 'w+') as f1:
for line in f:
f1.write(line)
if 'ENDONUCLEASES' in line:
f1.write(newEndonucleaseStr + '\n') # Move f1.write(line) above, to write above instead
os.remove(GlobalSettings.appdir + "CASPERinfo")
os.rename(GlobalSettings.appdir + "new_file",
GlobalSettings.appdir + "CASPERinfo") # Rename the new file
except Exception as e:
logger.critical("Error in writeNewEndonuclease() in New Endonuclease.")
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)
#submit new endo to CASPERinfo file
def submit(self):
try:
# This is executed when the button is pressed
name = str(self.organism_name.text())
abbr = str(self.abbreviation.text())
crisprtype = str(self.crispr_type.text())
seed_len = str(self.seed_length.text())
five_len = str(self.five_length.text())
three_len = str(self.three_length.text())
pam = str(self.pam_sequence.text()).upper()
### Check for multiple PAMs and format if present
if len(pam.split(','))>0:
pam = [x.strip() for x in pam.split(',')]
pam = ",".join(pam)
### Check for PAM directionality
if self.five_pam.isChecked():
pam_dir = str(5)
else:
pam_dir = str(3)
on_scoring = str(self.comboBox.currentText())
off_scoring = str(self.comboBox_2.currentText())
length = len(seed_len) + len(five_len) + len(three_len)
argument_list = [abbr, pam, five_len, seed_len, three_len, pam_dir, name, crisprtype, on_scoring, off_scoring]
validPAM = ('A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y')
self.error = False;
### Error checking for PAM alphabet
for letter in pam:
if (letter not in validPAM):
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Invalid PAM")
msgBox.setText("Invalid characters in PAM Sequence.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return True
### Error checking for filling out all fields
for arg in argument_list:
if ';' in arg:
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Invalid Semicolon")
msgBox.setText("Invalid character used: ';'")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return True
elif arg == "":
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Empty Field")
msgBox.setText("Please fill in all fields.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return True
else:
pass
### Check for duplicate endo abbreviations
for key in GlobalSettings.mainWindow.organisms_to_endos:
endo = GlobalSettings.mainWindow.organisms_to_endos[key]
if abbr in endo:
msgBox = QtWidgets.QMessageBox()
msgBox.setStyleSheet("font: " + str(self.fontSize) + "pt 'Arial'")
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
msgBox.setWindowTitle("Duplicate endo name.")
msgBox.setText("The given abbreviation already exists. Please choose a unique identifier.")
msgBox.addButton(QtWidgets.QMessageBox.StandardButton.Ok)
msgBox.exec()
return True
else:
pass
myString = ""
for i, arg in enumerate(argument_list):
if i == len(argument_list)-1: ### Last argument in list
myString += str(arg)
else:
myString += str(arg) + ";"
self.writeNewEndonuclease(myString)
### Refresh endonuclease dropdown in New Genome
GlobalSettings.mainWindow.newGenome.fillEndo()
self.clear_all()
self.close()
except Exception as e:
logger.critical("Error in submit() in New Endonuclease.")
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)
#cancel and close window
def cancel(self):
try:
self.clear_all()
self.close()
except Exception as e:
logger.critical("Error in cancel() in New Endonuclease.")
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)
# This function clears all of the line edits
def clear_all(self):
try:
self.organism_name.clear()
self.abbreviation.clear()
self.crispr_type.clear()
self.seed_length.clear()
self.five_length.clear()
self.three_length.clear()
self.pam_sequence.clear()
except Exception as e:
logger.critical("Error in clear_all() in New Endonuclease.")
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)
# This function parses CASPERinfo to return the names (in lists) of all on-target and off-target scoring data
def get_on_off_data(self):
try:
filename = GlobalSettings.appdir + "CASPERinfo"
retList_on = []
retList_off = []
with open(filename, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
line = str(line)
if "ON-TARGET DATA" in line:
index = i
while "-----" not in line:
if "DATA:" in line:
retList_on.append(line.split("DATA:")[-1].strip()) ### Append name of scoring data to on-target name list
line = lines[index+1]
index += 1
else:
line = lines[index+1]
index += 1
continue
elif "OFF-TARGET MATRICES" in line:
index = i
while "-----" not in line:
if "MATRIX:" in line:
retList_off.append(line.split("MATRIX:")[-1].strip()) ### Append name of scoring data to off-target name list
line = lines[index+1]
index += 1
else:
line = lines[index+1]
index += 1
continue
else:
continue
return retList_on, retList_off
except Exception as e:
logger.critical("Error in get_on_off_data() in New Endonuclease.")
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)