forked from anabiman/msr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSRWriter.py
104 lines (74 loc) · 3.12 KB
/
MSRWriter.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
import sys
import numpy as np
import MDAnalysis as md
import os
from numpy.linalg import norm, solve
import proto_md.subsystems.spacewarping_subsystem as SS
""" This is a simple script that uses MSR + protoMD to guide a trajectory of micro (all-atom) states
to new configurations consistent with the imposed coarse-grained (CG) and fine-grained (FG) constraints """
def readCoords(fname, natoms):
try:
with open(fname) as f:
lines = (line for line in f if not line.strip()[:3].isalpha())
r = np.loadtxt(lines)
pos = np.reshape(r, (3, natoms)).T
return pos
except:
print "Unexpected error:", sys.exc_info()[0]
raise
if __name__ == '__main__':
# read user input
_, gro, traj, tpr, tol = sys.argv
tol = np.float(tol)
U = md.Universe(gro, traj)
Top = md.Universe(tpr, gro)
args = {'kmax':2}
n, ss = SS.SpaceWarpingSubsystemFactory(U, ['protein'], **args)
s = ss[0]
s.universe_changed(U)
s.equilibrated()
bonds = np.array(Top.bonds.to_indices())
angles = np.array(Top.angles.to_indices())
angles = np.array([angles[:,0], angles[:,2]]).T
indices = np.concatenate((bonds, angles), axis=0)
indices.sort()
ncons = len(indices)
leq2 = norm(Top.atoms.positions[indices[:,0]] - Top.atoms.positions[indices[:,1]], axis=1)**2.0
np.savetxt('Indices.dat', indices, fmt='%d')
np.savetxt('LengthEq.dat', leq2)
natoms = U.atoms.n_atoms
nframes = U.trajectory.n_frames
fname = 'coords.dat'
cgFname = 'CG.dat'
basis = 'basis.dat'
cFname = 'coordsInput.dat'
invOpFname = 'invOP.dat'
W = md.Writer('MSR.dcd', n_atoms=natoms)
Utw = s.basis.T * s.atoms.masses
Ub = solve(np.dot(Utw, s.basis), Utw)
np.savetxt(basis, Ub.T)
invOP = np.dot(Ub, Ub.T)
np.savetxt(invOpFname, invOP)
for ts in U.trajectory:
with open(cFname, 'w') as fp:
np.savetxt(fp, s.atoms.positions)
CG = s.ComputeCG(s.atoms.positions)
nCG = len(CG)
np.savetxt(cgFname, CG)
print 'mpirun -n 1 MSR.a -nc {} -ns {} -c {} -i Indices.dat -l LengthEq.dat -cg {} -ncg {} -ref {} -refTrans {} -o {} -tol {} -PC_type jacobi'.format(natoms, \
ncons, cFname, cgFname, nCG, basis, invOpFname, fname, tol)
os.system('mpirun -n 1 MSR.a -nc {} -ns {} -c {} -i Indices.dat -l LengthEq.dat -cg {} -ncg {} -ref {} -refTrans {} -o {} -tol {} -PC_type jacobi'.format(natoms, \
ncons, cFname, cgFname, nCG, basis, invOpFname, fname, tol))
pos = readCoords(fname, natoms)
U.atoms.set_positions(pos)
W.write(ts)
# clean up
os.system('rm {}'.format(cgFname))
os.system('rm {}'.format(cFname))
os.system('rm {}'.format(fname))
W.close()
# input clean up
os.system('rm {}'.format(invOpFname))
os.system('rm {}'.format(basis))
os.system('rm Indices.dat')
os.system('rm LengthEq.dat')