This repository was archived by the owner on Oct 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmolshape.pl
More file actions
303 lines (262 loc) · 11 KB
/
molshape.pl
File metadata and controls
303 lines (262 loc) · 11 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
#!/usr/bin/perl
# ***
use Math::Trig;
# *** Print help
# ***
if ($#ARGV < 2) {
print("\nMOLSHAPE <G03-Cube-File>\n <Atom number for start slice>\n <Atom number for end slice>\n <Axis definition (xyz|xzy|zyx|zxy), default = xyz>\n <Electron density cut-off, default = 0.002>\n\n");
exit;
}
$axis = lc($ARGV[3]);
if ($axis ne 'xyz' && $axis ne 'xzy' &&
$axis ne 'yxz' && $axis ne 'yzx' &&
$axis ne 'zxy' && $axis ne 'zyx' &&
$axis ne 'xy' && $axis ne 'xz' &&
$axis ne 'yx' && $axis ne 'yz' &&
$axis ne 'zx' && $axis ne 'zy') {
$axis = 'xyz'; # Standard axis system
}
$border = $ARGV[4];
if ($border eq '') {
$border = 0.002; # Standard cut-off value
}
$bohr2angstrom = 0.5291772; # Conversion factor Bohr to Angstrom
$aaxis = substr($axis,0,1); # a-axis
$baxis = substr($axis,1,1); # b-axis
$caxis = substr($axis,2,1); # c-axis
$threedim = 1; # auxiliary variable whether MOLSHAPE is in 3D mode (=1) or 2D mode (=0)
if ($axis eq 'xy') { $caxis = 'z'; $threedim = 0; }
if ($axis eq 'xz') { $caxis = 'y'; $threedim = 0; }
if ($axis eq 'yx') { $caxis = 'z'; $threedim = 0; }
if ($axis eq 'yz') { $caxis = 'x'; $threedim = 0; }
if ($axis eq 'zx') { $caxis = 'y'; $threedim = 0; }
if ($axis eq 'zy') { $caxis = 'x'; $threedim = 0; }
# *** Print title
# ***
$VERSION = 0.4;
$TITLE = sprintf("MOLSHAPE (Version %01.1f)",$VERSION);
print ("\n"."-" x length($TITLE)."\n");
print ($TITLE."\n");
print ("-" x length($TITLE)."\n\n");
# *** Open file
# ***
open(INPUTFILE, $ARGV[0]) or die("\nERROR: Cannot open file '$ARGV[0]'\n\n");
@inputlines = <INPUTFILE>;
close(INPUTFILE);
# *** Read header
# ***
foreach (0..5) { push(@header, $inputlines[$_]); }
# *** Read number of atoms and coordinate origin
# ***
$header[2]=~/\ *(\d*)\ *(-?\d*.\d*)\ *(-?\d*.\d*)\ *(-?\d*.\d*)/;
$atoms = $1;
if (substr($axis,0,2) eq 'xy') { $aorigin = $2; $borigin = $3; $corigin = $4; }
if (substr($axis,0,2) eq 'xz') { $aorigin = $2; $borigin = $4; $corigin = $3; }
if (substr($axis,0,2) eq 'yx') { $aorigin = $3; $borigin = $2; $corigin = $4; }
if (substr($axis,0,2) eq 'yz') { $aorigin = $3; $borigin = $4; $corigin = $2; }
if (substr($axis,0,2) eq 'zx') { $aorigin = $4; $borigin = $2; $corigin = $3; }
if (substr($axis,0,2) eq 'zy') { $aorigin = $4; $borigin = $3; $corigin = $2; }
# *** Read number of voxels and voxel lengths
# ***
$header[3]=~/\ *(\d*)\ *(\d*.\d*)\ */;
$aavoxels = $1; # auxiliary variable for cube field
if ($aaxis eq 'x') { $avoxels = $1; $avoxellength = $2; }
if ($baxis eq 'x') { $bvoxels = $1; $bvoxellength = $2; }
if ($caxis eq 'x') { $cvoxels = $1; $cvoxellength = $2; }
$header[4]=~/\ *(\d*)\ *\d*.\d*\ *(\d*.\d*)\ */;
$bbvoxels = $1; # auxiliary variable for cube field
if ($aaxis eq 'y') { $avoxels = $1; $avoxellength = $2; }
if ($baxis eq 'y') { $bvoxels = $1; $bvoxellength = $2; }
if ($caxis eq 'y') { $cvoxels = $1; $cvoxellength = $2; }
$header[5]=~/\ *(\d*)\ *\d*.\d*\ *\d*.\d*\ *(\d*.\d*)/;
$ccvoxels = $1; # auxiliary variable for cube field
if ($aaxis eq 'z') { $avoxels = $1; $avoxellength = $2; }
if ($baxis eq 'z') { $bvoxels = $1; $bvoxellength = $2; }
if ($caxis eq 'z') { $cvoxels = $1; $cvoxellength = $2; }
# *** Print out the read data
# ***
if ($threedim) {
print ("Axis definition : $aaxis (main axis), $baxis (secondary axis), $caxis (secondary axis)\n");
}
else {
print ("Axis definition : $aaxis (main axis), $baxis (secondary axis)\n");
}
print ("Electron density cut-off: $border electrons/Bohr^3\n");
print ("Number of atoms : $atoms\n\n");
print ("Number/length $aaxis-voxels : $avoxels/$avoxellength Bohr\n");
print ("Number/length $baxis-voxels : $bvoxels/$bvoxellength Bohr\n");
print ("Number/length $caxis-voxels : $cvoxels/$cvoxellength Bohr\n");
$voxelarea = $bvoxellength*$cvoxellength*$bohr2angstrom**2;
printf ("Voxel area ($baxis,$caxis) : %.5f Bohr^2 = %.5f A^2\n\n",$voxelarea/($bohr2angstrom**2),$voxelarea);
# *** Start of the cube definition
# ***
$cubestart=$atoms+2+1+3; # *** $atoms atom coordinates + TWO comment lines + ONE origin definition + THREE voxel definitions
# *** Read cube definition
# ***
foreach $l ($cubestart..$#inputlines) # iterate through cube definition
{
foreach (split(/\s+/,substr($inputlines[$l],2))) { # TWO leading spaces removed, then split by whitespace
push(@cube, $_);
}
}
if ($#cube-($avoxels*$bvoxels*$cvoxels-1)!=0) {
die("ERROR: The read cube definition does not match the cube dimensions!\n");
}
else {
print ("OK: The read cube definition matches the cube dimensions!\n\n");
}
# *** Convert cube definition into cube array (a,b,c)
# ***
foreach $ia (1..$aavoxels) {
foreach $ib (1..$bbvoxels) {
foreach $ic (1..$ccvoxels) {
# DENSITY = $cubearray[$xvox][$yvox][$zvox][0];
# FLAG = $cubearray[$xvox][$yvox][$zvox][1];
if (substr($axis,0,2) eq 'xy') { $cubearray[$ia][$ib][$ic]=[shift(@cube),0]; }
if (substr($axis,0,2) eq 'xz') { $cubearray[$ia][$ic][$ib]=[shift(@cube),0]; }
if (substr($axis,0,2) eq 'yx') { $cubearray[$ib][$ia][$ic]=[shift(@cube),0]; }
if (substr($axis,0,2) eq 'yz') { $cubearray[$ib][$ic][$ia]=[shift(@cube),0]; }
if (substr($axis,0,2) eq 'zx') { $cubearray[$ic][$ia][$ib]=[shift(@cube),0]; }
if (substr($axis,0,2) eq 'zy') { $cubearray[$ic][$ib][$ia]=[shift(@cube),0]; }
}
}
}
# *** Create empty MolShape array (a,b,c)
# ***
foreach $ia (1..$aavoxels) {
foreach $ib (1..$bbvoxels) {
foreach $ic (1..$ccvoxels) {
$molshapearray[$ia][$ib][$ic]=0;
}
}
}
# *** Assumed cavity/volume center (b,c)
# ***
$bvoxelstart = ($bvoxels-($bvoxels%2))/2;
$cvoxelstart = ($cvoxels-($cvoxels%2))/2;
print ("Cube-Zentrum ($baxis,$caxis) : ($bvoxelstart,$cvoxelstart)\n");
# *** Determine a-slices
# ***
$inputlines[$ARGV[1]+2+1+3-1] =~ /\ *\d*\ *\d*.\d*\ *(-?\d*.\d*)\ *(-?\d*.\d*)\ *(-?\d*.\d*)/;
if ($aaxis eq 'x') { $astartcoord = $1; }
if ($aaxis eq 'y') { $astartcoord = $2; }
if ($aaxis eq 'z') { $astartcoord = $3; }
$astartcoordapprox = $aorigin;
$astartslice = 0;
while ($astartcoordapprox<=$astartcoord) { # search start coordinate
$astartcoordapprox += $avoxellength;
$astartslice++;
}
$inputlines[$ARGV[2]+2+1+3-1]=~/\ *\d*\ *\d*.\d*\ *(-?\d*.\d*)\ *(-?\d*.\d*)\ *(-?\d*.\d*)/;
if ($aaxis eq 'x') { $aendcoord = $1; }
if ($aaxis eq 'y') { $aendcoord = $2; }
if ($aaxis eq 'z') { $aendcoord = $3; }
$aendcoordapprox = $aorigin;
$aendslice = 0;
while ($aendcoordapprox<=$aendcoord) { # search end coordinate
$aendcoordapprox += $avoxellength;
$aendslice++;
}
if ($aendslice<$astartslice) { # swap start and end if end coordinates < start coordinates
($aendslice,$astartslice) = ($astartslice,$aendslice);
($aendcoord,$astartcoord) = ($astartcoord,$aendcoord);
($ARGV[1],$ARGV[2]) = ($ARGV[2],$ARGV[1])
}
print ("first $aaxis-voxel : $astartslice (atom number $ARGV[1])\n");
print ("last $aaxis-voxel : $aendslice (atom number $ARGV[2])\n\n");
# *** Volume or cavity mode?
# ***
$volume = undef; # auxiliary variable for volume (=1) or cavity mode (=0)
$avoxelmiddle = int(($aendslice+$astartslice)/2);
if ($cubearray[$avoxelmiddle][$bvoxelstart][$cvoxelstart][0] > $border) {
$volume = 1; # volume mode
print ("MolShape mode : Volume mode\n\n");
}
else {
$volume = 0; # cavity mode
print ("MolShape mode : Cavity mode\n\n");
}
# *** Create profile
# ***
$cavevolume = 0;
$output = $ARGV[0];
if ($volume) { # volume mode
$outputinfo = "_volume_".$axis."_".$border;
}
else { # cavity mode
$outputinfo = "_cavity_".$axis."_".$border;
}
$output =~ s/\.cube|\.cub/$outputinfo.csv/;
open (OUTPUTFILE, ">$output") or die("ERROR: Cannot create file '$output'!\n\n");
print OUTPUTFILE ("slice, coord_rel, coord_abs, slicearea, integration, equiv_radius\n");
foreach ($astartslice..$aendslice) {
$voxelcount=0;
if ($volume) {
BoundaryFillVolume($bvoxelstart,$cvoxelstart,$_); # volume mode
}
else {
BoundaryFillCavity($bvoxelstart,$cvoxelstart,$_); # cavity mode
}
$caveslicearea = $voxelcount*$voxelarea; # area of the current slice (in A^2)
$cavevolume += $caveslicearea*$avoxellength*$bohr2angstrom; # molecule/cavity volume (in A^3)
$equivradius = sqrt($caveslicearea/pi); # equivalent radius of the current slice (in A)
printf OUTPUTFILE ("%u, %.4f, %.4f, %.2f, %.2f, %.3f\n", $_, ($_-$astartslice)*$avoxellength*$bohr2angstrom, ($aorigin+$_*$avoxellength)*$bohr2angstrom, $caveslicearea, $cavevolume, $equivradius);
}
close(OUTPUTFILE);
print ("OK: The file '$output' was created successfully.\n");
# *** Write new cube file
# ***
$output = $ARGV[0];
$output =~ s/\.cube|\.cub/$outputinfo.cube/;
open(OUTPUTFILE, ">$output") or die("ERROR: Cannot create file '$output'!\n\n");
foreach (0..$cubestart-1) {
print OUTPUTFILE $inputlines[$_];
}
foreach $ia (1..$aavoxels) {
foreach $ib (1..$bbvoxels) {
foreach $ic (1..$ccvoxels) {
if (substr($axis,0,2) eq 'xy') { printf OUTPUTFILE (" %.5E",$molshapearray[$ia][$ib][$ic]); }
if (substr($axis,0,2) eq 'xz') { printf OUTPUTFILE (" %.5E",$molshapearray[$ia][$ic][$ib]); }
if (substr($axis,0,2) eq 'yx') { printf OUTPUTFILE (" %.5E",$molshapearray[$ib][$ia][$ic]); }
if (substr($axis,0,2) eq 'yz') { printf OUTPUTFILE (" %.5E",$molshapearray[$ib][$ic][$ia]); }
if (substr($axis,0,2) eq 'zx') { printf OUTPUTFILE (" %.5E",$molshapearray[$ic][$ia][$ib]); }
if (substr($axis,0,2) eq 'zy') { printf OUTPUTFILE (" %.5E",$molshapearray[$ic][$ib][$ia]); }
if ($ic%6 == 0) { print OUTPUTFILE ("\n"); }
}
print OUTPUTFILE ("\n");
}
}
close(OUTPUTFILE);
print ("OK: The file '$output' was created successfully.\n\n");
# *** Define BoundaryFill functions
# ***
sub BoundaryFillVolume { # (y,z,x)
local $bvox = shift(@_);
local $cvox = shift(@_);
local $avox = shift(@_);
if ($cubearray[$avox][$bvox][$cvox][0]>$border and $cubearray[$avox][$bvox][$cvox][1]==0 and $bvox>=1 and $bvox<=$bvoxels and $cvox>=1 and $cvox<=$cvoxels) {
$cubearray[$avox][$bvox][$cvox][1]=1; # voxel is flagged
$voxelcount++; # "volume voxels" + 1
$molshapearray[$avox][$bvox][$cvox]=1; # electron density of the voxel > $border
BoundaryFillVolume($bvox+1,$cvox,$avox);
BoundaryFillVolume($bvox-1,$cvox,$avox);
if ($threedim) { BoundaryFillVolume($bvox,$cvox+1,$avox) };
if ($threedim) { BoundaryFillVolume($bvox,$cvox-1,$avox) };
}
return;
}
sub BoundaryFillCavity { # (y,z,x)
local $bvox = shift(@_);
local $cvox = shift(@_);
local $avox = shift(@_);
if ($cubearray[$avox][$bvox][$cvox][0]<$border and $cubearray[$avox][$bvox][$cvox][1]==0 and $bvox>=1 and $bvox<=$bvoxels and $cvox>=1 and $cvox<=$cvoxels) {
$cubearray[$avox][$bvox][$cvox][1]=1; # voxel is flagged
$voxelcount++; # "cavity voxels" + 1
$molshapearray[$avox][$bvox][$cvox]=1; # electron density of the voxel < $border
BoundaryFillCavity($bvox+1,$cvox,$avox);
BoundaryFillCavity($bvox-1,$cvox,$avox);
if ($threedim) { BoundaryFillCavity($bvox,$cvox+1,$avox) };
if ($threedim) { BoundaryFillCavity($bvox,$cvox-1,$avox) };
}
return;
}