-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwater_deletor.pl
More file actions
322 lines (271 loc) · 6.66 KB
/
water_deletor.pl
File metadata and controls
322 lines (271 loc) · 6.66 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
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
#!/usr/bin/perl
use strict;
# Written by Justin Lemkul, Ph.D.
# Contact: jalemkul@vt.edu
# 6/7/2017
#
# License: GPL-3.0 or later, at user's option
# This script deletes out waters between certain z-coordinate values
# Z-values are specified by a chosen atom on the command line
# The decision on how to split "top" of bilayer from "bottom" - define "middle" atom,
# is taken from the command line. If z(reference) > z(middle) -> top, and vice versa
# Determine average z-coordinate value for top and bottom, if OW of water falls within
# that range, delete it and its corresponding HW's
# Collect stuff from command line arguments into a hash
unless (@ARGV)
{
die "Example usage: perl water_deletor.pl -in sol.gro -out system_sol_fix.gro -ref O32 -middle C316 -nwater 3 -v [yes/no]\n";
}
print "\nStarting water deletion process...\n";
my %args = @ARGV;
my $input;
my $output;
my $ref_atom;
my $middle_atom;
my $nwater;
my $verbose;
if (exists($args{"-in"}))
{
$input = $args{"-in"};
}
else
{
die "No input specified!\n";
}
# collect all the input lines into an array
open(IN, $input);
my @in = <IN>;
close(IN);
# grab some stuff from the input (non-atom lines)
my $header = shift(@in);
my $orig_natoms = shift(@in);
chomp($orig_natoms);
my $box = pop(@in);
if (exists($args{"-out"}))
{
$output = $args{"-out"};
}
else
{
print "Using default output filename of \"bilayer_fix.gro\"\n";
$output = "bilayer_fix.gro";
}
if (exists($args{"-ref"}))
{
$ref_atom = $args{"-ref"};
}
else
{
die "No reference atom specified!\n";
}
if (exists($args{"-middle"}))
{
$middle_atom = $args{"-middle"};
}
else
{
die "No middle atom specified!\n";
}
# define number of water atoms for splicing
if (exists($args{"-nwater"}))
{
$nwater = $args{"-nwater"};
}
else
{
print "Option -nwater not set, assuming 3 atoms in the water model.\n";
$nwater = 3;
}
# define verbosity
if (exists($args{"-v"}) && (($args{"-v"} eq "yes") || ($args{"-v"} eq "Yes") || ($args{"-v"} eq "y") || ($args{"-v"} eq "Y")))
{
$verbose = 1;
}
else
{
$verbose = 0; # default to non-verbose mode
}
# process input file to strip out all lines corresponding to reference or middle atoms
my @ref_atoms;
my @middle_atoms;
# also count how many water molecules we start with
my $nwater_start = 0;
foreach $_ (@in)
{
my @data = split(" ", $_);
my $atom = $data[1];
if ($atom =~ /$ref_atom/)
{
push(@ref_atoms, $_);
}
elsif ($atom =~ /$middle_atom/)
{
push(@middle_atoms, $_);
}
elsif ($atom =~ /OW/)
{
$nwater_start++;
}
}
# determine the middle of the bilayer from the middle atoms
# middle of bilayer is defined as the average z-coordinate of the middle atoms
my $total_z;
my $nmiddle = scalar(@middle_atoms);
foreach $_ (@middle_atoms)
{
my @data = split(" ", $_);
my $z = 0;
# last field on line if no velocities, otherwise n-3
if (scalar(@data) > 6)
{
# there are velocities
$z = $data[scalar(@data)-4];
}
else
{
$z = $data[scalar(@data)-1];
}
# debug
#print "$z\n";
$total_z += $z;
}
my $middle_z = $total_z / $nmiddle;
print "Defining the middle of the bilayer as z = $middle_z.\n";
# now split the reference atoms into top and bottom
my @top_ref;
my @bottom_ref;
foreach $_ (@ref_atoms)
{
my @data = split(" ", $_);
my $z = 0;
# last field on line if no velocities, otherwise n-3
if (scalar(@data) > 6)
{
# there are velocities
$z = $data[scalar(@data)-4];
}
else
{
$z = $data[scalar(@data)-1];
}
if ($z > $middle_z)
{
push(@top_ref, $_);
}
elsif ($z < $middle_z)
{
push(@bottom_ref, $_);
}
else
{
print "Weird z-coordinate found!\n";
print "$_\n";
}
}
# determine average z-coordinate values for the top and bottom reference atoms
my $top_z;
my $ntop = scalar(@top_ref);
# debug
print "ntop = $ntop\n";
foreach $_ (@top_ref)
{
my @data = split(" ", $_);
my $z = 0;
# last field on line if no velocities, otherwise n-3
if (scalar(@data) > 6)
{
# there are velocities
$z = $data[scalar(@data)-4];
}
else
{
$z = $data[scalar(@data)-1];
}
# debug
print "$z\n";
$top_z += $z;
}
my $top_z_avg = $top_z / $ntop;
my $bottom_z;
my $nbottom = scalar(@bottom_ref);
foreach $_ (@bottom_ref)
{
my @data = split(" ", $_);
my $z = 0;
# last field on line if no velocities, otherwise n-3
if (scalar(@data) > 6)
{
# there are velocities
$z = $data[scalar(@data)-4];
}
else
{
$z = $data[scalar(@data)-1];
}
$bottom_z += $z;
}
my $bottom_z_avg = $bottom_z / $nbottom;
print "Boundaries for eliminating water are $bottom_z_avg < z < $top_z_avg.\n";
# now actually delete the waters
# go thru the input file and find any OW that has a z-coordinate within the bounds
# defined above.
my @clean_atoms;
my $size = scalar(@in);
my $ndel = 0; # keep track of how many waters we have deleted
for (my $i=0; $i<$size; $i++)
{
my @line = split(" ", $in[$i]);
# account for the fact that at high atom numbers, the string is joined, i.e. OW10000
my $atom = substr($line[1], 0, 2);
# get last entry - in the above case, OW10000, there is no distinct atom number
my $z = $line[(scalar(@line)-1)];
unless ($atom eq "OW")
{
push(@clean_atoms, $in[$i]);
}
else
{
if (($z > $bottom_z_avg) && ($z < $top_z_avg))
{
# assemble the residue numbering for printing/bookkeeping
if ($verbose == 1)
{
my @res = split('', $line[0]);
pop(@res); # get rid of "SOL"
pop(@res);
pop(@res);
my $resnr = join('', @res);
print "Deleting residue $resnr\n";
}
# debug
# my @delete = splice(@in, $i, 3);
# print "@delete";
splice(@in, $i, $nwater);
$i--;
$size -= $nwater;
$ndel++;
}
else
{
push(@clean_atoms, $in[$i]);
}
}
}
# Report what happened
print "$ndel water molecules have been deleted.\n";
my $watnew = $nwater_start - $ndel;
print "$watnew water molecules remain. Update your topology!\n";
# print the cleaned output
my $natoms_clean = scalar(@clean_atoms);
open(OUT, ">$output");
print OUT $header;
print OUT "$natoms_clean\n";
foreach $_ (@clean_atoms)
{
print OUT "$_";
}
print OUT $box;
close(OUT);
print "\nOutput file $output written. You may want to renumber the file with,\n";
print "for example, genconf in the GROMACS package.\n\n";
exit;