-
Notifications
You must be signed in to change notification settings - Fork 1
/
threadsTest.py
247 lines (195 loc) · 8.67 KB
/
threadsTest.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
"""Testing ccmap C extension
Usage:
threadTest.py (cmap|lcmap) <threadNum> <dataSize> --lib <pathToPyLib> (--inp <tranformationFile> [--gen <seed>] | --rec <filepathFirstPdbFile> [--lig <filepathFirstPdbFile>] ) [ --encode --atomic ] [ --dist <ccDist> ] [--out <fOut> --pref <PDBfolder>]
threadTest.py (zmap|lzmap) <threadNum> <dataSize> --lib <pathToPyLib> --inp <tranformationFile> [--encode --atomic] [--dist <ccDist>] [--out <fOut> --pref <PDBfolder>]
Options:
-h --help
<threadNum> Number of threads (integer)
<dataSize> Number of structure to process (integer)
--rec <filepathFirstPdbFile> Path to "receptor" PDB.
--lig <filepathSecondPdbFile> Path to "ligand" PDB, optional.
--inp <transformationFile> Path to JSON transformation specs
--lib <pathToPyLib>, folder containing the ccmap.so
--encode use integer encoding, default = False. Optional
--atomic compute atomic contact map, default = False. Optional
--dist <ccDist> atomic contact threshold distance, in A.
--pref <PDBfolder> prefix for PDB file reading.
--gen <seed> A transformation number (from 1 to 50000), used to generate one valid dimer. cmap/lcmap only
--out <fOut> path to json output file, default = \"threadsTest.json\"
"""
import sys, threading, json, time, copy, os
from docopt import docopt
def cThread(*args, **kwargs):
tArgs, tNum, results = args
dual = len(tArgs) == 2
results[tNum] = []
print(f"cThread {tNum}: Starting as dual:{dual}")
print( f"cThread {tNum}: Shapes of ligand/receptor arrays {len(tArgs[0])}/{len(tArgs[1])}" )
tStart = time.time()
if dual:
for rec,lig in zip(tArgs[0], tArgs[1]):
results[tNum].append( ccmap.cmap(rec, lig, **kwargs) )
else :
for mol in tArgs[0]:
results[tNum].append( ccmap.cmap(mol, **kwargs) )
print(f"cThread {tNum}: Finished in { time.time() - tStart }")
return
def lcThread(*args, **kwargs):
tStart = time.time()
tArgs, tNum, results = args
assert len(tArgs) == 2
results[tNum] = []
print(f"Starting lcThread {tNum}")
print( f"Shapes of ligand/receptor arrays {len(tArgs[0])}/{len(tArgs[1])}" )
tStart = time.time()
results[tNum] = ccmap.lcmap(*tArgs, **kwargs)
print(f"End of lcThread {tNum} in { time.time() - tStart }")
return
def zThread(*args, **kwargs):
tStart = time.time()
tArgs, tNum, results = args
pdbRec, pdbLib, eulerList, translationList = tArgs
print(f"Starting zThread {tNum}, for {len(eulerList)} calls")
results[tNum] = []
for e,t in zip(eulerList, translationList):
results[tNum].append( ccmap.zmap(pdbRec, pdbLib, e, t, **kwargs) )
print(f"End of zThread {tNum} in { time.time() - tStart }")
return
def lzThread(*args, **kwargs):
tStart = time.time()
tArgs, tNum, results = args
print(f"Starting lzThread {tNum}")
results[tNum] = ccmap.lzmap(*tArgs, **kwargs)
print(f"End of lzThread {tNum} in { time.time() - tStart }")
return
"""Yield successive interval boundaries spanning [0, iLen]"""
def splitInterval(iLen, nElem):
assert(nElem <= iLen)
nWidth = int(iLen/nElem)
for i in range(nElem):
top = (i+1) * nWidth
if i == (nElem - 1): #and iLen%nElem != 0:
top += iLen%nElem
yield (i * nWidth, top)
def splitList(myList, nChunck):
for x,y in splitInterval(len(myList), nChunck):
yield myList[x:y]
def setPDBFiles(pArgs):
if ( pArgs['--inp'] ):
print(f"Reading PDB names from transformationFile: {pArgs['--inp']}")
d = {}
with open(pArgs['--inp'], 'r') as fp:
d = json.load(fp)
iFile = f"{ pArgs['--pref'] + '/' if pArgs['--pref'] else './' }{d['receptorFile']}"
assert( os.path.isfile(iFile) )
jFile = f"{ pArgs['--pref'] + '/' if pArgs['--pref'] else './' }{d['ligandFile']}"
assert( os.path.isfile(jFile) )
if (pArgs['cmap'] or pArgs['lcmap']):
if pArgs['--gen']:
return iFile, jFile
return iFile, None
return iFile, jFile
assert( os.path.isfile(pArgs['--rec']) )
if pArgs['--lig']:
assert( os.path.isfile(pArgs['--lig']) )
return pArgs['--rec'], pArgs['--lig']
return pArgs['--rec'], None
def generateDimer(pdbDictREC, pdbDictLIG, vectors, number):
a = copy.deepcopy(pdbDictREC)
b = copy.deepcopy(pdbDictLIG)
print(f"Generating conformation number {number}")
_ = ccmap.zmap( a, b, vectors['euler'][number], vectors['translation'][number], \
offsetRec=vectors['recOffset'], offsetLig=vectors['ligOffset'], \
apply=True)
return a, b
if __name__ == "__main__":
ARGS = docopt(__doc__, version="1.0.0")
sys.path.append(ARGS["--lib"])
import ccmap
import pyproteinsExt.structure.coordinates as PDB
#print(ARGS)
threadNum = int(ARGS['<threadNum>'])
dataSize = int(ARGS['<dataSize>'])
pdbREC, pdbLIG = setPDBFiles(ARGS)
parser = PDB.Parser()
pdbDictREC = parser.load(file=pdbREC).atomDictorize
pdbDictLIG = parser.load(file=pdbLIG).atomDictorize if pdbLIG else None
vectors = None
if ARGS["--inp"]:
with open(ARGS["--inp"], 'r') as fp:
vectors = json.load(fp)
eulers = [tuple(_) for _ in vectors['euler']]
translations = [tuple(_) for _ in vectors['translation']]
ligOffset = vectors['ligOffset']
recOffset = vectors['recOffset']
print(f"Loaded {len(eulers)} potential transformations to apply to \"{vectors['ligandFile']}\"")
if ARGS['--gen']: # We must transform receptors coordinate do generate a valid complex. cmap/lcmap only
generateDimer( pdbDictREC, pdbDictLIG, vectors, int(ARGS['--gen']) )
# Set worker function
wThread = None
if ARGS['cmap']:
wThread = cThread
elif ARGS['lcmap']:
wThread = lcThread
elif ARGS['zmap']:
wThread = zThread
else:
wThread = lzThread
# Setting default thread named arguments
threadKwargs = {\
"encode" : ARGS["--encode"],\
"atomic" : ARGS["--atomic"] \
}
if ARGS['--dist']:
threadKwargs["d"] = float(ARGS['--dist'])
# Setting default thread positional arguments
threadArgs = None
### Slicing & Shaping threads-types specific inputs ###
## We fill list-structures args with repetitions of the same PDBs
if ARGS['cmap'] or ARGS['lcmap']:
wThread = cThread if ARGS['cmap'] else lcThread
threadArgs = []
# WRONG SHAPES [ [[] or ], .. NTHREADS]
for x,y in splitInterval(dataSize, threadNum):
threadArgs.append(( [], [] )) if pdbDictLIG else threadArgs.append(( [] ))
for i in range(y - x):
threadArgs[-1][0].append(pdbDictREC)
if pdbDictLIG:
threadArgs[-1][1].append(pdbDictLIG)
## We only slice euler/translations data
elif ARGS['zmap'] or ARGS['lzmap']:
if dataSize > len(vectors['euler']):
print(f"dataSize {dataSize} exceeds avaible transformations {len(vectors['euler'])}, resizing it to maximum")
dataSize = len(vectors['euler'])
wThread = zThread if ARGS['zmap'] else lzThread
threadKwargs["offsetRec"] = vectors['recOffset']
threadKwargs["offsetLig"] = vectors['ligOffset']
threadArgs = []
for x,y in splitInterval(dataSize, threadNum):
print(x,y)
print(vectors['translation'][x:y])
threadArgs.append( ( pdbDictREC, pdbDictLIG, \
vectors['euler'][x:y],\
vectors['translation'][x:y]\
) )
mStart = time.time()
output = [ None for i in range(threadNum) ]
threadPool = [ threading.Thread(args = tuple( [ threadArgs[i], i, output ] )\
, kwargs = threadKwargs \
, target=wThread) \
for i in range(threadNum)]
for th in threadPool:
th.start()
for th in threadPool:
th.join()
print(f"{threadNum} threads finished in { time.time() - mStart }")
if not ARGS['--encode']:
for i,d in enumerate(output):
if isinstance(d, list):
_ = [ json.loads(_str) for _str in d ]
output[i] = _
else:
output[i] = json.loads(d)
fOut = ARGS['--out'] if ARGS['--out'] else "threadsTest.json"
with open(fOut, 'w') as fp:
json.dump({ "threadData" : output }, fp)