-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquid_prep.py
59 lines (40 loc) · 1.82 KB
/
liquid_prep.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
import networkx as nx
import os
from simtk.openmm import app
def nmols_in_file(box_file):
"""return how many molecules are in the pdb file."""
pdb = app.PDBFile(box_file)
n_molecules = pdb.topology.getNumResidues()
return n_molecules
def create_box(pdb_name, nmol=500):
resn = 'MOL' # Enter new molecule description code to rename UNK
with open(pdb_name, 'rt') as f_in, open(f'{resn}.pdb', 'wt') as f_out:
for line in f_in:
f_out.write(line.replace('UNK', resn))
x = y = z = 4 # Initial liquid box dimensions
n_molecules = 0
while n_molecules < nmol:
os.system(f'gmx insert-molecules -ci {resn}.pdb -box {x} {y} {z} -nmol {nmol} -o box.pdb')
n_molecules = nmols_in_file('box.pdb')
x += 0.2
y += 0.2
z += 0.2
def generate_connections(single_pdb, all_pdb, n_molecules):
os.system(f'head -n -2 {all_pdb} > temp.pdb ; mv temp.pdb new.pdb')
with open(single_pdb, 'r') as s_pdb, open('new.pdb', 'a+') as new_pdb:
lines = s_pdb.readlines()
topology = nx.Graph()
for line in lines:
if 'CONECT' in line:
topology.add_node(int(line.split()[1]))
for i in range(2, len(line.split())):
if int(line.split()[i]) != 0:
topology.add_node(int(line.split()[i]))
topology.add_edge(int(line.split()[1]), int(line.split()[i]))
n_atoms = len(list(topology.nodes))
for mol in range(n_molecules):
for node in topology.nodes:
bonded = sorted(list(nx.neighbors(topology, node)))
if len(bonded) > 1:
new_pdb.write(f'CONECT{node + (mol * n_atoms):5}{"".join(f"{x + (mol * n_atoms):5}" for x in bonded)}\n')
new_pdb.write('END\n')