View Javadoc
1   //******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  //******************************************************************************
38  package ffx.potential.commands;
39  
40  import edu.rit.pj.ParallelTeam;
41  import ffx.potential.bonded.Atom;
42  import ffx.potential.cli.PotentialCommand;
43  import ffx.potential.nonbonded.implicit.ConnollyRegion;
44  import ffx.potential.nonbonded.implicit.GaussVol;
45  import ffx.potential.parameters.ForceField;
46  import ffx.utilities.FFXBinding;
47  import picocli.CommandLine.Command;
48  import picocli.CommandLine.Option;
49  import picocli.CommandLine.Parameters;
50  
51  import static ffx.utilities.Constants.NS2SEC;
52  import static java.lang.String.format;
53  import static org.apache.commons.math3.util.FastMath.pow;
54  
55  /**
56   * Calculate the surface area and volume using the GaussVol (default) or Connolly algorithm.
57   *
58   * Usage:
59   *   ffxc Volume [options] <filename>
60   */
61  @Command(name = "Volume", description = " Calculate the surface area and volume using the GaussVol (default) or Connolly algorithm.")
62  public class Volume extends PotentialCommand {
63  
64    private static final double rminToSigma = 1.0 / pow(2.0, 1.0 / 6.0);
65  
66    /** Use the Connolly algorithm. */
67    @Option(names = {"-c", "--connolly"}, paramLabel = "false",
68        description = "Use the Connolly algorithm to compute solvent excluded volume and solvent accessible surface area.")
69    private boolean connolly = false;
70  
71    /** For Connolly, compute molecular volume and surface area (instead of SEV/SASA). */
72    @Option(names = {"-m", "--molecular"}, paramLabel = "false",
73        description = "For Connolly, compute molecular volume and surface area (instead of SEV/SASA).")
74    private boolean molecular = false;
75  
76    /** For Connolly, compute van der Waals volume and surface area (instead of SEV/SASA). */
77    @Option(names = {"--vdW", "--vanDerWaals"}, paramLabel = "false",
78        description = "For Connolly, compute van der Waals volume and surface area (instead of SEV/SASA)")
79    private boolean vdW = false;
80  
81    /** For Connolly, set the exclude radius (SASA) or probe (molecular surface). Ignored for vdW. */
82    @Option(names = {"-p", "--probe"}, paramLabel = "1.4",
83        description = "For Connolly, set the exclude radius (SASA) or probe radius (molecular surface). Ignored for vdW.")
84    private double probe = 1.4;
85  
86    /** Include Hydrogen in calculation. */
87    @Option(names = {"-y", "--includeHydrogen"}, paramLabel = "false",
88        description = "Include Hydrogen in calculation volume and surface area.")
89    private boolean includeHydrogen = false;
90  
91    /** Use sigma radii instead of Rmin. */
92    @Option(names = {"-s", "--sigma"}, paramLabel = "false",
93        description = "Use sigma radii instead of Rmin.")
94    private boolean sigma = false;
95  
96    /** For GaussVol, add an offset to all atomic radii. */
97    @Option(names = {"-o", "--offset"}, paramLabel = "0.0",
98        description = "Add an offset to all atomic radii for GaussVol volume and surface area.")
99    private double offset = 0.0;
100 
101   /** Print detailed information. */
102   @Option(names = {"-v", "--verbose"}, paramLabel = "false",
103       description = "Print out all components of volume of molecule and offset.")
104   private boolean verbose = false;
105 
106   /** The final argument is an atomic coordinate file in PDB or XYZ format. */
107   @Parameters(arity = "1", paramLabel = "file",
108       description = "The atomic coordinate file in PDB or XYZ format.")
109   private String filename = null;
110 
111   /** JUnit Testing Variables. */
112   public double totalVolume = 0.0;
113   public double totalSurfaceArea = 0.0;
114 
115   public Volume() { super(); }
116   public Volume(FFXBinding binding) { super(binding); }
117   public Volume(String[] args) { super(args); }
118 
119   @Override
120   public Volume run() {
121     // Init the context and bind variables.
122     if (!init()) {
123       return this;
124     }
125 
126     // Load the MolecularAssembly.
127     activeAssembly = getActiveAssembly(filename);
128     if (activeAssembly == null) {
129       logger.info(helpString());
130       return this;
131     }
132 
133     // Set the filename.
134     filename = activeAssembly.getFile().getAbsolutePath();
135 
136     logger.info("\n Calculating volume and surface area for " + filename);
137 
138     Atom[] atoms = activeAssembly.getAtomArray();
139     int nAtoms = atoms.length;
140 
141     if (!connolly) {
142       // Input positions.
143       double[][] positions = new double[nAtoms][3];
144       int index = 0;
145       for (Atom atom : atoms) {
146         positions[index][0] = atom.getX();
147         positions[index][1] = atom.getY();
148         positions[index][2] = atom.getZ();
149         index++;
150       }
151 
152       ForceField forceField = activeAssembly.getForceField();
153       if (includeHydrogen) {
154         forceField.addProperty("GAUSSVOL_HYDROGEN", "true");
155       }
156       if (sigma) {
157         forceField.addProperty("GAUSSVOL_USE_SIGMA", "true");
158       }
159       if (offset > 0.0) {
160         forceField.addProperty("GAUSSVOL_RADII_OFFSET", Double.toString(offset));
161       }
162       if (!forceField.hasProperty("GAUSSVOL_RADII_SCALE")) {
163         forceField.addProperty("GAUSSVOL_RADII_SCALE", Double.toString(1.0));
164       }
165 
166       // Run GaussVol calculation.
167       ParallelTeam parallelTeam = new ParallelTeam();
168       GaussVol gaussVol = new GaussVol(atoms, activeAssembly.getForceField(), parallelTeam);
169       long time = -System.nanoTime();
170       gaussVol.computeVolumeAndSA(positions);
171       time += System.nanoTime();
172 
173       if (verbose) {
174         logger.info(format("\n Maximum depth of overlaps in tree: %d", gaussVol.getMaximumDepth()));
175         logger.info(format(" Total number of overlaps in tree: %d", gaussVol.getTotalNumberOfOverlaps()));
176         double[] radii = gaussVol.getRadii();
177         index = 0;
178         for (Atom atom : atoms) {
179           logger.info(format(" %5d %4s: %6.4f", index, atom.getName(), radii[index]));
180           index++;
181         }
182       }
183 
184       logger.info("\n GaussVol Surface Area and Volume\n");
185       if (sigma) {
186         logger.info(format("  Radii:                  Sigma"));
187       } else {
188         logger.info(format("  Radii:                   Rmin"));
189       }
190       logger.info(format("  Radii offset:        %8.4f (Ang)", offset));
191       logger.info(format("  Include hydrogen:    %8b", includeHydrogen));
192       logger.info(format("  Volume:            %10.4f (Ang^3)", gaussVol.getVolume()));
193       logger.info(format("  Surface Area:      %10.4f (Ang^2)", gaussVol.getSurfaceArea()));
194       logger.info(format("  Time:              %10.4f (sec)", time * NS2SEC));
195 
196       // JUnit testing variables.
197       totalVolume = gaussVol.getVolume();
198       totalSurfaceArea = gaussVol.getSurfaceArea();
199     } else {
200       // For Connolly molecular volume & surface area, use the chosen probe and set exclude to 0.0.
201       double exclude = 0.0;
202 
203       if (vdW) {
204         // For Connolly vdW, both exclude & probe are zero.
205         exclude = 0.0;
206         probe = 0.0;
207       } else if (!molecular) {
208         // For Connolly SEV/SASA, set exclude to the chosen probe, and zero the probe.
209         exclude = probe;
210         probe = 0.0;
211       }
212 
213       double[] radii = new double[nAtoms];
214       int index = 0;
215       for (Atom atom : atoms) {
216         radii[index] = atom.getVDWType().radius / 2.0;
217         if (sigma) {
218           radii[index] *= rminToSigma;
219         }
220         boolean hydrogen = atom.isHydrogen();
221         if (!includeHydrogen && hydrogen) {
222           radii[index] = 0.0;
223         }
224         index++;
225       }
226 
227       // Note that the VolumeRegion code is currently limited to a single thread.
228       int nThreads = 1;
229       ConnollyRegion connollyRegion = new ConnollyRegion(atoms, radii, nThreads);
230       // For solvent excluded volume.
231       connollyRegion.setExclude(exclude);
232       // For molecular surface.
233       connollyRegion.setProbe(probe);
234       long time = -System.nanoTime();
235       connollyRegion.runVolume();
236       time += System.nanoTime();
237 
238       double zero = 0.0;
239       if (vdW || (probe == zero && exclude == zero)) {
240         logger.info("\n Connolly van der Waals Surface Area and Volume\n");
241       } else if (!molecular) {
242         logger.info("\n Connolly Solvent Accessible Surface Area and Solvent Excluded Volume\n");
243         logger.info(format("  Exclude radius:      %8.4f (Ang)", exclude));
244       } else {
245         logger.info("\n Connolly Molecular Surface Area and Volume");
246         logger.info(format("  Probe radius:       %8.4f (Ang)", probe));
247       }
248       if (sigma) {
249         logger.info(format("  Radii:                  Sigma"));
250       } else {
251         logger.info(format("  Radii:                   Rmin"));
252       }
253       logger.info(format("  Include hydrogen:    %8b", includeHydrogen));
254       logger.info(format("  Volume:            %10.4f (Ang^3)", connollyRegion.getVolume()));
255       logger.info(format("  Surface Area:      %10.4f (Ang^2)", connollyRegion.getSurfaceArea()));
256       logger.info(format("  Time:              %10.4f (sec)", time * NS2SEC));
257 
258       // JUnit testing variables.
259       totalVolume = connollyRegion.getVolume();
260       totalSurfaceArea = connollyRegion.getSurfaceArea();
261     }
262 
263     return this;
264   }
265 }