forked from lmorency/NRGsuite
-
Notifications
You must be signed in to change notification settings - Fork 3
/
General.py
executable file
·331 lines (243 loc) · 8.94 KB
/
General.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
'''
NRGsuite: PyMOL molecular tools interface
Copyright (C) 2011 Gaudreault, F., Morency, LP. & Najmanovich, R.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
if sys.version_info[0] < 3:
from Tkinter import *
else:
from tkinter import *
import re
import time
import os
import hashlib
BLOCKSIZE = 65536
''' ==================================================================================
FUNCTION Get_Date: Return the actual date (mm/dd/yyyy - hh:mm:ss).
================================================================================== '''
def Get_Date():
timeNow = time.localtime()
Date = ''
month = int(timeNow.tm_mon)
day = int(timeNow.tm_mday)
hour = int(timeNow.tm_hour)
minute = int(timeNow.tm_min)
second = int(timeNow.tm_sec)
if month < 10:
Date += '0'
Date += str(month) + '/'
if day < 10:
Date += '0'
Date += str(day) + '/' + str(timeNow.tm_year) + ' - '
if hour < 10:
Date += '0'
Date += str(hour) + ':'
if minute < 10:
Date += '0'
Date += str(minute) + ':'
if second < 10:
Date += '0'
Date += str(second)
return Date
''' ==========================================================
validate_Float: validate the entry field (float value)
==========================================================='''
def validate_Float(sVal, Min, Max, nDec):
try:
fVal = float(sVal)
except:
return 1
if len(str(fVal).split('.')[1]) > nDec:
return 2
elif fVal < Min or fVal > Max:
return 3
return 0
''' ==========================================================
validate_Integer: validate the entry field (int value)
==========================================================='''
def validate_Integer(sVal, Min, Max):
try:
iVal = int(sVal)
except:
return 1
if iVal < Min or iVal > Max:
return 3
return 0
''' ==========================================================
validate_String: validate the entry field (str value)
==========================================================='''
def validate_String(sVal, bExt, bPath, bSpaces, bPyMOL):
# Does sVal is a path (bPath)
# If so, only analyze the basefilename
if bPath:
sVal = os.path.split(sVal)[1]
# Does extension match
if bExt and bExt != os.path.splitext(sVal)[1]:
return 1
# Are spaces allowed (bSpaces)
if bSpaces and not re.match('^[A-Za-z0-9_\-\. ]*$', sVal):
return 1
elif not bSpaces and not re.match('^[A-Za-z0-9_\-\.]*$', sVal):
return 1
# If the name is a pymol object, the name must begin with alphanumeric
if bPyMOL and not re.match('[A-Za-z0-9]', sVal):
return 1
return 0
''' ==========================================================
setState: set states for all child widget
==========================================================='''
def setState(widget, state='disabled'):
try:
widget.configure(state=state)
except:
pass
for child in widget.winfo_children():
setState(child, state=state)
''' ==========================================================
backState: back states from list for all child widget
==========================================================='''
def backState(widget, li):
try:
s = widget.cget('state')
state = li.pop(0)
widget.configure(state=state)
except:
pass
for child in widget.winfo_children():
backState(child, li)
''' ==========================================================
saveState: save states list for all child widget
==========================================================='''
def saveState(widget, li):
try:
state = widget.cget('state')
li.append(state)
except:
pass
for child in widget.winfo_children():
saveState(child, li)
return li
''' ==========================================================
CenterWindow: Centers the window in the middle of the monitor
==========================================================='''
def CenterWindow(top, w, h):
# Get screen width and height
ws = top.winfo_screenwidth()
hs = top.winfo_screenheight()
# Calculate position x,y
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
top.geometry('%dx%d+%d+%d' % (w,h,x,y))
#=======================================================================
''' Get center of geometry of molecule '''
#=======================================================================
def get_CenterGeometry(CG,PDBFile):
del CG[:]
CG.append(0.0)
CG.append(0.0)
CG.append(0.0)
tot = 0
try:
# Read the PDB file
file = open(PDBFile, 'r')
PDBLines = file.readlines()
file.close()
# Read every ATOM/HETATM line
for line in PDBLines:
if line.startswith('ATOM ') or \
line.startswith('HETATM'):
#ATOM 3 C ALA A 13 24.276 5.552 9.942 1.00 43.61 C
CG[0] += float(line[30:38])
CG[1] += float(line[38:46])
CG[2] += float(line[46:54])
tot += 1
except:
return -1
CG[0] /= float(tot)
CG[1] /= float(tot)
CG[2] /= float(tot)
return tot
#=======================================================================
''' Store Residue List from PDB file '''
#=======================================================================
def store_Residues(listResidues, PDBFile, HETATM):
del listResidues[:]
max = 0
try:
# Read the PDB file
file = open(PDBFile, 'r')
PDBLines = file.readlines()
file.close()
# Get the possibles Residues
for line in PDBLines:
if line.startswith('ATOM ') or \
line.startswith('HETATM'):
index = int(line[6:11].strip())
resn = line[17:20].strip()
chain = line[21:22]
resi = line[22:27].strip()
if index > max:
max = index
if chain == ' ':
chain = '-'
residue = resn + resi + chain
if line.startswith('HETATM') and not HETATM:
continue
if listResidues.count(residue) == 0:
listResidues.append(residue)
except:
return -1
return max
#=======================================================================
''' Returns the Signature of a file contents '''
#=======================================================================
def hashfile(file):
hasher = hashlib.md5()
afile = open(file, 'r')
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
afile.close()
return hasher.digest()
#=======================================================================
''' Returns an updated md5 hashlib '''
#=======================================================================
def hashfile_update(file, hasher):
afile = open(file, 'r')
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf.encode('utf-8'))
buf = afile.read(BLOCKSIZE)
afile.close()
return hasher
''' ==================================================================================
FUNCTION repeat: repeats a character N number of times
================================================================================== '''
def repeat(string, length):
L = len(string)
return string * (length // L) + string[:length % L]
''' ==================================================================================
FUNCTION hex_to_rgb and rgb_to_hex: functions used in coloring
================================================================================== '''
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def one_to_rgb(one):
rgb = list(one)
for i in range(0,3):
rgb[i] = int(rgb[i]*255)
return rgb