-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRETrest.py
executable file
·513 lines (445 loc) · 16.9 KB
/
FRETrest.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import LabelLib as ll
import numpy as np
import mdtraj as md
import argparse
import os
import json
import re
import sys
def main():
parser = argparse.ArgumentParser(description='Create a FRET restraint file for AMBER and update pseudo atom positions.')
parser.add_argument('-t','--top', required=True, type=str,
help='Input topology file path')
parser.add_argument('-r','--restin', required=True, type=str,
help='Input restart file path')
parser.add_argument('-j','--json', required=True, type=str,
help='FRET-restraint file path in .fps.json format')
parser.add_argument('--restout', required=True, type=str,
help='Output restart file path with updated pseudoatom positions')
parser.add_argument('--fout', required=True, type=str,
help='Output restraints file path (AMBER NMR restraints format, DISANG)')
parser.add_argument('--force', required=True, type=float,
help='Maximum force applied between a pair of pseudoatoms in piconewtos')
# Optional arguments
parser.add_argument('--chi2', type=str,
help='Name of the chi2[r] to consider. If this option is provided, Labelling Positions, not used by the given chi2[r] will be skipped.')
parser.add_argument('--resoffset', type=int, default=0,
help='Residue numbering offset between the .fps.json and topology.')
parser.add_argument('--nofcap', action='store_true',
help='By default forces are scaled down such, that total FRET force applied to any pseudoatom does not exceed the limit specified with --force. This option disables scaling.')
args = parser.parse_args()
top=args.top
inRestartPath=args.restin
jsonPath=args.json
outRestartPath=args.restout
outDisang=args.fout
maxForce=args.force
chi2Name=args.chi2
resSeqOffset=args.resoffset
noCapForces=args.nofcap
for path in [top,inRestartPath,jsonPath]:
if not os.path.isfile(path):
parser.error("file {} does not exist".format(path))
with open(jsonPath, encoding="utf-8") as jsonFile:
jdata = json.load(jsonFile)
selDistList=selectedDistances(jdata,chi2Name)
if selDistList is None:
parser.error("evaluator "+chi2Name+" is not found in "+jsonPath)
selLPs=selectedLPs(jdata,selDistList)
print('#loading trajectory')
frame=md.load(inRestartPath,top=top)[0]
duIds=frame.topology.select('name DU')
lpNames=sorted(selLPs.keys())
if len(duIds) != len(lpNames):
print('ERROR! Number of pseudoatoms in topology ({}) does not match to the number of labelling positions ({}).'.format(len(duIds),len(lpNames)))
return
#frame.image_molecules(inplace=True)
#update pseudoatom positions
print('#update pseudoatom positions')
avs={}
for ilp,lpName in enumerate(lpNames):
av, attachAtId=getAV(frame,selLPs[lpName],resSeqOffset)
if av is None:
print('ERROR! Could not calculate av for labelling position '+lpName)
return
avs[lpName]=av
duId=duIds[ilp]
if np.max(av.grid)>0.0:
mp=avMP(av)*0.1
frame.xyz[0,duId,:]=mp
else:
print('WARNING! Calculation resulted in an empty AV for position {}. Old position will be used.'.format(lpName))
avs[lpName]=point2Av(frame.xyz[0,duId,:]*10.0)
linker_length=float(selLPs[lpName]['linker_length'])
dist=frame.xyz[0,duId,:]-frame.xyz[0,attachAtId,:]
dist=np.sqrt(np.sum(np.square(dist)))*10.0
if dist>linker_length:
print('WARNING! Distance between the pseudoatom {} and the attachment atom ({:.2f} Angstrom) is larger than the linker length ({:.2f} Angstrom).'
.format(lpName,dist,linker_length))
print(lpName+' done!')
restraints=[]
#FRET restraints
print('\n#FRET restraints')
print('#\tname\t\ttRda\t\t\tRda\tdRda\ttRmp\tRmp')
for idist,dist in enumerate(selDistList):
lp1name=jdata["Distances"][dist]["position1_name"]
lp2name=jdata["Distances"][dist]["position2_name"]
lp1Index=lpNames.index(lp1name)
lp2Index=lpNames.index(lp2name)
duId1=duIds[lp1Index]
duId2=duIds[lp2Index]
rdaTarget=float(jdata["Distances"][dist]["distance"])
rtype=jdata["Distances"][dist]["distance_type"]
R0=jdata["Distances"][dist]["Forster_radius"]
errNeg=float(jdata["Distances"][dist]["error_neg"])
errPos=float(jdata["Distances"][dist]["error_pos"])
av1,av2=avs[lp1name],avs[lp2name]
rmpTarget=RmpFromRda(av1,av2,rdaTarget,rtype,R0=R0)
rdaCurrent=Rda(av1,av2,rtype=rtype,R0=R0)
print('#{}\t{:<15}\t{:.1f} [+{:.1f}, -{:.1f}]\t{:.1f}\t{:.1f}\t{:.1f}\t{:.1f}'
.format(idist,dist,rdaTarget,errNeg,errPos,rdaCurrent,rdaCurrent-rdaTarget,rmpTarget,Rmp(av1,av2)))
rest=AmberRestraint()
rest.iat1=duId1+1
rest.iat2=duId2+1
rest.r2=rmpTarget
rest.r3=rest.r2
rest.r1=rest.r2-errNeg
rest.r4=rest.r3+errPos
rest.rk2=maxForce/(2.0*69.4786*(rest.r2-rest.r1)) #69.4786 pN = 1 kcal/mol Angstrom
rest.rk3=maxForce/(2.0*69.4786*(rest.r4-rest.r3))
rest.comment='{} ({}) <--> {} ({}) '.format(lp1name,rest.iat1,lp2name,rest.iat2)
restraints.append(rest)
#scale force constants down
if not noCapForces:
restraints, maxFuc=cappedRestraints(restraints, maxForce, frame.xyz[0,:,:])
Ftot=sumForces(restraints, frame.xyz[0,:,:])
maxFcheck=np.sqrt(np.sum(np.square([f for f in Ftot.values()]),axis=1)).max()
print('Maximum force was scaled down from {:.1f} pN to {:.1f} pN.'.format(maxFuc,maxFcheck))
#anchor restraints
print('\n#Anchor restraints')
for ilp,lpName in enumerate(lpNames):
sys.stdout.write('Position '+lpName+'. ')
sys.stdout.flush()
vmdSel=selLPs[lpName]['anchor_atoms']
if len(vmdSel)==0:
print("ERROR! Empty anchor atoms selection mask for position "+lpName)
return
anchorSel=selVmd2Mdtraj(vmdSel, resSeqOffset)
selExprStr=frame.topology.select_expression(anchorSel)
selExprStr=selExprStr.replace('[atom.index for atom in topology.atoms if ','')[:-1]
print('Selection: '+selExprStr+'')
ancIds=frame.topology.select(anchorSel)
if len(ancIds)==0:
print("ERROR! No anchor atoms selected for position "+lpName+":")
print(anchorSel)
return
duId=duIds[ilp]
for ancId in ancIds:
ancXYZ=frame.xyz[0,ancId,:]
mp=frame.xyz[0,duId,:]
dist=np.sqrt(np.sum(np.square(ancXYZ-mp)))*10.0
rest=AmberRestraint()
rest.iat1=duId+1
rest.iat2=ancId+1
rest.r2=dist
rest.r3=rest.r2
rest.r1=rest.r2-1.0
rest.r4=rest.r3+1.0
rest.rk2=2.0*maxForce/(2.0*69.4786*(rest.r2-rest.r1)) #69.4786 pN = 1 kcal/mol Angstrom
rest.rk3=rest.rk2
resid2=frame.topology.atom(ancId).residue.resSeq
atname2=frame.topology.atom(ancId).name
rest.comment='{} ({}) <--> {}@{} ({})'.format(lpName,rest.iat1,resid2,atname2,rest.iat2)
restraints.append(rest)
print('Done! Atoms selected: {}\n'.format(len(ancIds)))
with open(outDisang, "w") as text_file:
for rest in restraints:
text_file.write(rest.formString())
saveRestart(outRestartPath,frame,inRestartPath)
class AmberRestraint:
iat1=-100
iat2=-100
r1=0.0
r2=0.0
r3=0.0
r4=0.0
rk2 = 0.0
rk3 = 0.0
comment= ''
def formString(self):
return '#{}\n&rst iat = {}, {}, r1 = {:.3f}, r2 = {:.3f}, r3 = {:.3f}, r4 = {:.3f}, rk2 = {:.5f}, rk3 = {:.5f},\n/\n'.format(
self.comment, self.iat1, self.iat2, self.r1, self.r2, self.r3, self.r4, self.rk2, self.rk3)
def force_pN(self,r):
#69.4786 pN = 1 kcal/mol Angstrom
pNconv=69.4786
if r<self.r1:
return pNconv*2.0*self.rk2*(self.r2-self.r1) #>=0, push apart
elif r<self.r2:
return pNconv*2.0*self.rk2*(self.r2-r)
elif r<self.r3:
return pNconv*0.0
elif r<self.r4:
return pNconv*2.0*self.rk3*(self.r3-r)
else:
return pNconv*2.0*self.rk3*(self.r3-self.r4) #<=0, pull closer
def sumForces(restraints, xyz):
Ftot={} #{iat:[Fx,Fy,Fz]}
for rest in restraints:
mp1 = xyz[rest.iat1-1]*10.0
mp2 = xyz[rest.iat2-1]*10.0
r = np.sqrt(np.sum(np.square(mp2-mp1)))
for iat in [rest.iat1, rest.iat2]:
if not iat in Ftot:
Ftot[iat] = np.zeros(3)
#push apart if force>0
dMpNorm = (mp1-mp2)/r
#print('{} {} {}'.format(rest.iat1,rest.iat2,rest.force_pN(r)))
Ftot[rest.iat1] += dMpNorm * rest.force_pN(r)
Ftot[rest.iat2] -= Ftot[rest.iat1]
return Ftot
def cappedRestraints(restraints, maxF, xyz):
Ftot=sumForces(restraints, xyz)
maxFtot=np.sqrt(np.sum(np.square([f for f in Ftot.values()]),axis=1)).max()
if maxF>=maxFtot:
return restraints, maxFtot
scale=maxF/maxFtot
for i in range(len(restraints)):
restraints[i].rk2*=scale
restraints[i].rk3*=scale
return restraints, maxFtot
def readVelocities(inRestartPath):
finRest = open(inRestartPath,'r')
inlines=finRest.readlines()
n_at=int(inlines[1].split()[0])
if len(inlines) < (3+n_at):
#No velocities in the restart file
return None, None, None
vel=np.zeros([n_at,3])
iLineVel=2+int(n_at/2.0+0.5)
vel=np.empty([n_at,3])
for i in range(0,n_at):
if i%2==0:
vel[i] = [float(x) for x in inlines[iLineVel].split()[:3]]
else:
vel[i] = [float(x) for x in inlines[iLineVel].split()[3:]]
iLineVel+=1
cell_length=np.array([[float(x) for x in inlines[-1].split()[:3]]])
cell_angles=np.array([[float(x) for x in inlines[-1].split()[3:]]])
return vel, cell_length, cell_angles
def saveRestart(outPath,frame,inRestartPath):
xyz=frame.xyz[0,:,:]*10.0
cell_lengths=frame.unitcell_lengths*10.0
cell_angles=frame.unitcell_angles
time=frame.time[0]
#read velocities
vel, cell_length_rest, cell_angles_rest=readVelocities(inRestartPath)
if vel is None:
print('Information: No velocities in the restart file.')
frame.save_amberrst7(outPath)
return
if len(vel) != frame.n_atoms:
print('ERROR! Number of atoms in the frame ({}) and restart file ({}) do not match.'.format(frame.n_atoms,len(vel)))
return
if np.absolute(cell_length_rest-cell_lengths).max()>0.0001:
print('ERROR! Cell length in the frame ({}) and restart file ({}) do not match.'.format(cell_lengths,cell_length_rest))
return
if np.absolute(cell_angles-cell_angles_rest).max()>0.0001:
print('ERROR! Cell angles in the frame ({}) and restart file ({}) do not match.'.format(cell_angles,cell_angles_rest))
return
out=open(outPath, 'w')
out.write('Amber restart file written by FRETrest\n')
out.write('%5d%15.7e\n' % (frame.n_atoms, time))
fmt = '%12.7f%12.7f%12.7f'
#coordinates
for i in range(frame.n_atoms):
acor = xyz[i, :]
out.write(fmt % (acor[0], acor[1], acor[2]))
if i % 2 == 1: out.write('\n')
if frame.n_atoms % 2 == 1: out.write('\n')
#velocities
for i in range(frame.n_atoms):
avel = vel[i, :]
out.write(fmt % (avel[0], avel[1], avel[2]))
if i % 2 == 1: out.write('\n')
if frame.n_atoms % 2 == 1: out.write('\n')
if cell_lengths is not None:
out.write(fmt % (cell_length_rest[0,0], cell_length_rest[0,1],
cell_length_rest[0,2]))
out.write(fmt % (cell_angles_rest[0,0], cell_angles_rest[0,1],
cell_angles_rest[0,2]) + '\n')
out.flush()
def av2points(grid):
g = np.array(grid.grid).reshape(grid.shape,order='F')
idx=np.argwhere(g > 0.0)
idx_rav=np.ravel_multi_index(idx.T,g.shape)
vals=np.take(g,idx_rav)
points=idx*grid.discStep+grid.originXYZ
points=np.column_stack([points,vals])
return points
class AV:
grid=[]
originXYZ=np.zeros(3)
discStep=0.0
shape=[0,0,0]
def point2Av(xyz):
av=AV()
av.shape=[1,1,1]
av.originXYZ=xyz
av.discStep=0.01
av.grid=[1.0]
return av
def Rmp(av1,av2, transVec=None):
mp1=avMP(av1)
if transVec is None:
transVec=np.zeros(3)
mp2=avMP(av2)+transVec
return np.sqrt(np.sum(np.square(mp2-mp1)))
def Rda2Efficiency(rda,R0):
return 1.0/(np.power(rda/R0,6)+1.0)
def Efficiency2Rda(E,R0):
return R0*np.power((1.0/E)-1.0,1.0/6.0)
def Rda(av1,av2, rtype, transVec=None, R0=None):
if rtype=='Rmp':
return Rmp(av1,av2, transVec)
p1=av2points(av1)
p2=av2points(av2)
if transVec is not None:
p2+=np.append(transVec,np.zeros(1))
rand1=np.random.randint(0,len(p1),20000)
rand2=np.random.randint(0,len(p2),20000)
rdas=np.sqrt(np.sum(np.square(p2[rand2,:3]-p1[rand1,:3]),axis=1))
weights=p2[rand2,3]*p1[rand1,3]
if rtype=='RDAMean':
return np.average(rdas,weights=weights)
elif rtype=='RDAMeanE':
aveE=np.average(Rda2Efficiency(rdas,R0),weights=weights)
return Efficiency2Rda(aveE,R0)
else:
print('ERROR! Unknown distance type: "{}"'.format(rtype))
return None
def RmpFromRda(av1, av2, rda, rtype, R0=None, tolerance=0.2, maxIter=10):
mp1=avMP(av1)
mp2=avMP(av2)
drNorm=(mp2-mp1)/np.sqrt(np.sum(np.square(mp2-mp1)))
shift=np.zeros(3)
curRda=Rda(av1,av2,rtype,R0=R0)
dev=rda-curRda
for it in range(maxIter):
shift+=drNorm*dev
curRda=Rda(av1,av2,rtype,shift,R0=R0)
dev=rda-curRda
if abs(dev)<tolerance:
break
if abs(dev)>tolerance:
print('WARNING! RmpFromRda() could not converge! Achieved deviation: {:.4f}, tolerance: {:.4f}'.format(abs(dev),tolerance))
return np.sqrt(np.sum(np.square(mp2+shift-mp1)))
def selectedLPs(jdata,selDistList):
selLPs={}
for dist in selDistList:
lp1name=jdata["Distances"][dist]["position1_name"]
lp2name=jdata["Distances"][dist]["position2_name"]
selLPs[lp1name]=jdata["Positions"][lp1name]
selLPs[lp2name]=jdata["Positions"][lp2name]
return selLPs
def selectedDistances(jdata,chi2Name):
selDistList=[]
try:
chi2Name=unicode(chi2Name, "utf-8")
except:
pass
if chi2Name is not None:
if chi2Name in jdata[u'χ²']:
selDistList=list(jdata[u'χ²'][chi2Name]['distances'])
elif chi2Name in jdata[u'χᵣ²']:
selDistList=list(jdata[u'χᵣ²'][chi2Name]['distances'])
else:
return None #error
else:
selDistList=list(jdata['Distances'].keys())
return sorted(selDistList)
def chain2index(chain):
return str(ord(chain[0])-ord('A'))
def attachmentString(lp, resSeqOffset=0):
resi=int(lp['residue_seq_number'])+resSeqOffset
resn=lp['residue_name']
at=lp['atom_name']
s=''
chain=lp["chain_identifier"]
if len(chain)>0:
chain=chain2index(chain)
s+='chainid {} and '.format(chain)
s+='resSeq {} and resname {} and name {}'.format(resi,resn,at)
return s
def selVmd2Mdtraj(sel, resSeqOffset=0):
sel=sel.replace('resid', 'resSeq')
p=re.compile('chain ([A-Z])')
sel=p.sub(lambda m: 'chain '+chain2index(m.group(1)),sel)
p=re.compile('resSeq ([0-9]+)')
sel=p.sub(lambda m: 'resSeq {}'.format(int(m.group(1))+resSeqOffset),sel)
p=re.compile('(resSeq [0-9]+ to )([0-9]+)')
sel=p.sub(lambda m: '{}{}'.format(m.group(1),int(m.group(2))+resSeqOffset),sel)
return sel
def avMP(grid):
g = np.array(grid.grid).reshape(grid.shape,order='F')
idx=np.argwhere(g > 0.0)
idx_rav=np.ravel_multi_index(idx.T,g.shape)
vals=np.take(g,idx_rav)
points=idx*grid.discStep+grid.originXYZ
mp=np.average(points,axis=0,weights=vals)
return mp
def getAV(fr,lp, resSeqOffset=0):
avType=lp['simulation_type']
if avType != 'AV1':
print('ERROR! Simulation type is not supported: '+avType)
return None, None
linker_length=float(lp['linker_length'])
linker_width=float(lp['linker_width'])
dye_radius=float(lp['radius1'])
disc_step=float(lp['simulation_grid_resolution'])
attachSel=attachmentString(lp,resSeqOffset)
try:
iAttach=fr.topology.select(attachSel)[0]
except IndexError:
print('ERROR! Could not find the specified attachment atom in the topology:\n'+attachSel)
return None, None
xyzAttach=fr.xyz[0][iAttach]*10.0
radii=np.empty(fr.n_atoms)
for iat in range(fr.n_atoms):
radii[iat]=fr.topology.atom(iat).element.radius*10.0
xyzr=np.vstack([fr.xyz[0].T*10.0,radii])
stripSel=selVmd2Mdtraj(lp["strip_mask"], resSeqOffset)
stripAtIds=fr.topology.select(stripSel)
limDist=float(lp['allowed_sphere_radius'])
dist=np.sqrt(np.sum(np.square(fr.xyz[0]*10.0-xyzAttach),axis=1))
nearbyAtoms=np.flatnonzero(dist < limDist)
stripAtIds=np.append(stripAtIds,nearbyAtoms)
xyzr=np.delete(xyzr,stripAtIds,1)
av1 = ll.dyeDensityAV1(xyzr, xyzAttach, linker_length,linker_width,dye_radius, disc_step)
return av1, iAttach
def savePqr(fileName, grid):
with open(fileName, "w") as out:
area = grid.shape[0] * grid.shape[1]
nx, ny, nz = grid.shape
ox, oy, oz = grid.originXYZ
dx = grid.discStep
g = np.array(grid.grid).reshape((nx, ny, nz),order='F')
iat = 0
for iz in range(nz):
for iy in range(ny):
for ix in range(nx):
val = g[ix, iy, iz]
if val <= 0.0:
continue
iat += 1
resi = int(iat / 10)
x = ix * dx + ox
y = iy * dx + oy
z = iz * dx + oz
sz = 'ATOM{0: 7} AV AV{1: 6}{2:12.1f}{3:8.1f}{4:8.1f}{5:8.2f}{6:7.3f}\n'
sz = sz.format(iat, resi, x, y, z, val, dx * 0.5)
out.write(sz)
if __name__ == "__main__":
main()