-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_REST.py
More file actions
207 lines (149 loc) · 5.69 KB
/
preprocess_REST.py
File metadata and controls
207 lines (149 loc) · 5.69 KB
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
import os
import sys
import shutil
import re
import numpy as np
import argparse
def get_fields(line):
fields = line.split()
new_fields = []
for i in range(len(fields)):
if fields[i] != '':
new_fields.append(fields[i]);
return new_fields
def set_underscore(line):
fields = get_fields(line)
if len(fields) == 0:
return line
if fields[0] == ';':
return line
if len(fields) != 11:
return line
new_line = '';
for i in range(len(fields)):
if i == 1:
new_line = new_line + '\t' + fields[i] + '_ '
else:
new_line = new_line + '\t' + fields[i]
new_line = new_line+'\n'
return new_line
def update_pairtypes(line,scaling_factor):
fields = get_fields(line)
new_line = line
if len(fields) >= 1:
if line[0] != ';' and line[0] != '[' and len(line) > 1:
new_line = new_line + '\t' +fields[0]+'_\t' + fields[1]+'_'
for i in range(2,len(fields)-1):
new_line = new_line + '\t' + fields[i]
last_num = float(fields[-1])
new_line = new_line + '\t' + str(scaling_factor*last_num) + '\t ; scaled \n'
return new_line
def update_cmaptypes(line,scaling_factor):
fields = get_fields(line)
if len(fields) == 0:
return line
if fields[0] == ';':
return line;
new_line = ''
if (len(fields) == 6 or len(fields) == 10) and fields[0][0] != ';':
for i in range(len(fields)-1):
num = float(fields[i])
new_line = new_line + str(scaling_factor*num) + '\t'
num = float(fields[-1][:-1])
new_line = new_line + str(scaling_factor*num) +'\ \n'
else:
new_line = line
return new_line;
def get_temperature_list(nReplicas, minT, maxT):
T_list = np.zeros(int(nReplicas))
# Set temperatures
for iTemp in range(int(nReplicas)):
T_list[iTemp]=minT*np.exp(float(iTemp)*np.log(maxT/minT)/(nReplicas-1.0))
return T_list;
def main(args):
# Set parameters
minT = float(args.min_temperature)
maxT = float(args.max_temperature)
nReplicas = float(args.number_of_replicas)
system_name = args.system_label
standard_folder_name = args.example_directory
if standard_folder_name[-1] != '/':
standard_folder_name += '/'
mdp_file = args.mdp_file
top_file = args.topology
ndx_file = args.index
heating_regions = args.heat_regions
T_list = get_temperature_list(nReplicas, minT, maxT)
# Create processed.top
os.system('gmx grompp -f '+ standard_folder_name + mdp_file +' -c '+ standard_folder_name + system_name +'.gro -pp '+ standard_folder_name +'processed.top -n '+ standard_folder_name + ndx_file +' -p '+ standard_folder_name + top_file)
os.system('rm topol.tpr')
os.system('rm mdout.mdp')
# Create an empty plumed.dat file
os.system('touch '+standard_folder_name+'plumed.dat')
# Process each replica
for iTemp in range(int(nReplicas)):
scaling_factor = T_list[0]/T_list[iTemp]
print('lambda = ' + str(scaling_factor))
# Copy entire standard folder to new folder
current_folder = system_name+str(iTemp)+'/'
if not os.path.exists(current_folder):
shutil.copytree(standard_folder_name,current_folder)
# Write updated .top file to processed2.top
fID1 = open(current_folder+'processed.top','r')
fID2 = open(current_folder+'processed2.top','w')
cmaptypes=False
pairtypes=False
moleculetype=False
atoms=False
in_heating_region = False
do_underscore = False
for line in fID1:
find_bracket = re.search('\[',line)
if find_bracket is not None:
cmaptypes=False
pairtypes=False
atoms=False
moleculetype=False
do_underscore=False
find_atoms = re.search('\[ atoms \]',line)
find_cmaptypes = re.search('\[ cmaptypes \]',line)
find_pairtypes = re.search('\[ pairtypes \]',line)
find_moleculetype = re.search('\[ moleculetype \]',line)
if find_cmaptypes is not None:
cmaptypes=True
if find_pairtypes is not None:
pairtypes=True
if find_moleculetype is not None:
moleculetype=True
in_heating_region = False
if find_atoms is not None:
atoms=True
if in_heating_region:
do_underscore=True
if moleculetype:
for iRegion in range(len(heating_regions)):
find_heat_reg = re.search(heating_regions[iRegion],line)
if find_heat_reg is not None:
in_heating_region = True
new_line = line
if do_underscore:
new_line = set_underscore(line)
if cmaptypes:
new_line = update_cmaptypes(line, scaling_factor)
fID2.write(new_line)
fID1.close()
fID2.close()
# Perform partial tempering with plumed to get new_topol.top
os.system('plumed partial_tempering '+ str(scaling_factor) +' < ' + current_folder + 'processed2.top > ' + current_folder + 'new_topol.top' )
parser = argparse.ArgumentParser(epilog='Create REST folders and perform scaling or selected region. Annie Westerlund 2018.');
parser.add_argument('-minT','--min_temperature',help='Minimum temperature',type=float);
parser.add_argument('-maxT','--max_temperature',help='Maximum temperature',type=float);
parser.add_argument('-n_rep','--number_of_replicas',help='Number of replicas to use',type=float);
parser.add_argument('-label','--system_label',help='Name of the system.',type=str);
parser.add_argument('-ex_dir','--example_directory',help='Name of the standard folder (will be copied).',type=str, default='standard/');
parser.add_argument('-f','--mdp_file',help='Name of the production .mdp file.',type=str, default='step7_production.mdp');
parser.add_argument('-heat_reg','--heat_regions',help='Name(s) of the moleculetypes to heat.',type=str, nargs='+', default='step7_production.mdp');
parser.add_argument('-p','--topology',help='Name of topology file, default topol.top',default='topol.top')
parser.add_argument('-n','--index',help='Name of index file, default index.ndx',default='index.ndx')
args = parser.parse_args();
main(args);